/*
Theme Name:     Foxiz child
Template:      	foxiz
Theme URI:      https://foxiz.themeruby.com
Author:         Theme-Ruby
Author URI:     https://themeforest.net/user/theme-ruby/portfolio/
Description:    Make your modifications to Foxiz in this child theme.
Version:        1.0
Tags:           custom-background, custom-menu, featured-images, theme-options, custom-colors, translation-ready, threaded-comments, translation-ready
License:        Themeforest Licence
License URI:    https://themeforest.net/licenses/standard
*/

/* Your CSS code goes here
-------------------------------------- */


function cptui_register_my_taxes() {

	/**
	 * Taxonomy: Locais.
	 */

	$labels = [
		"name" => esc_html__( "Locais", "custom-post-type-ui" ),
		"singular_name" => esc_html__( "Local", "custom-post-type-ui" ),
	];

	
	$args = [
		"label" => esc_html__( "Locais", "custom-post-type-ui" ),
		"labels" => $labels,
		"public" => true,
		"publicly_queryable" => true,
		"hierarchical" => true,
		"show_ui" => true,
		"show_in_menu" => true,
		"show_in_nav_menus" => true,
		"query_var" => true,
		"rewrite" => [ 'slug' => 'local', 'with_front' => true,  'hierarchical' => true, ],
		"show_admin_column" => false,
		"show_in_rest" => true,
		"show_tagcloud" => true,
		"rest_base" => "local",
		"rest_controller_class" => "WP_REST_Terms_Controller",
		"rest_namespace" => "wp/v2",
		"show_in_quick_edit" => true,
		"sort" => false,
		"show_in_graphql" => false,
		"default_term" => ['name' => 'Geral'],
	];
	register_taxonomy( "local", [ "post" ], $args );
}
add_action( 'init', 'cptui_register_my_taxes' );

function cptui_register_my_taxes_local() {

	/**
	 * Taxonomy: Locais.
	 */

	$labels = [
		"name" => esc_html__( "Locais", "custom-post-type-ui" ),
		"singular_name" => esc_html__( "Local", "custom-post-type-ui" ),
	];

	
	$args = [
		"label" => esc_html__( "Locais", "custom-post-type-ui" ),
		"labels" => $labels,
		"public" => true,
		"publicly_queryable" => true,
		"hierarchical" => true,
		"show_ui" => true,
		"show_in_menu" => true,
		"show_in_nav_menus" => true,
		"query_var" => true,
		"rewrite" => [ 'slug' => 'local', 'with_front' => true,  'hierarchical' => true, ],
		"show_admin_column" => false,
		"show_in_rest" => true,
		"show_tagcloud" => true,
		"rest_base" => "local",
		"rest_controller_class" => "WP_REST_Terms_Controller",
		"rest_namespace" => "wp/v2",
		"show_in_quick_edit" => true,
		"sort" => false,
		"show_in_graphql" => false,
		"default_term" => ['name' => 'Geral'],
	];
	register_taxonomy( "local", [ "post" ], $args );
}
add_action( 'init', 'cptui_register_my_taxes_local' );

function custom_post_permalink($permalink, $post) {
    if (strpos($permalink, '%local%') === false) {
        return $permalink;
    }

    $terms = get_the_terms($post, 'local');

    if (!empty($terms) && is_array($terms)) {
        usort($terms, function ($a, $b) {
            return count(get_ancestors($a->term_id, 'local')) < count(get_ancestors($b->term_id, 'bairro'));
        });

        $bairro = array_pop($terms);
        $permalink = str_replace('%local%', $bairro->slug, $permalink);
    } else {
        $permalink = str_replace('%local%', 'geral', $permalink); // Defina o valor padrão aqui.
    }

    return $permalink;
}
add_filter('post_link', 'custom_post_permalink', 10, 2);
add_filter('post_type_link', 'custom_post_permalink', 10, 2);

/* retira a barra admin pra todo mundo menos admin */
add_action('after_setup_theme', 'remove_admin_bar');
function remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
  show_admin_bar(false);
}
}
