<?php
if ( ! function_exists( 'pet_bazaar_setup' ) ) :
function pet_bazaar_setup() {

/**
 * Define Theme Version
 */
define('PET_BAZAAR_THEME_VERSION', '12.1');

// Root path/URI.
define( 'PET_BAZAAR_PARENT_DIR', get_template_directory() );
define( 'PET_BAZAAR_PARENT_URI', get_template_directory_uri() );

// Root path/URI.
define( 'PET_BAZAAR_PARENT_INC_DIR', PET_BAZAAR_PARENT_DIR . '/inc');
define( 'PET_BAZAAR_PARENT_INC_URI', PET_BAZAAR_PARENT_URI . '/inc');

	// Add default posts and comments RSS feed links to head.
	add_theme_support( 'automatic-feed-links' );

	/*
	 * Let WordPress manage the document title.
	 */
	add_theme_support( 'title-tag' );
	
	add_theme_support( 'custom-header' );
	
	/*
	 * Enable support for Post Thumbnails on posts and pages.
	 */
	add_theme_support( 'post-thumbnails' );
	
	//Add selective refresh for sidebar widget
	add_theme_support( 'customize-selective-refresh-widgets' );
	
	/*
	 * Make theme available for translation.
	 */
	load_theme_textdomain( 'pet-bazaar' );
		
	// This theme uses wp_nav_menu() in one location.
	register_nav_menus( array(
		'primary_menu' => esc_html__( 'Primary Menu', 'pet-bazaar' ),
		'footer_menu' => esc_html__( 'Footer Menu', 'pet-bazaar' ),
	) );

	/*
		* Switch default core markup for search form, comment form, and comments
	 * to output valid HTML5.
	 */
	add_theme_support( 'html5', array(
		'search-form',
		'comment-form',
		'comment-list',
		'gallery',
		'caption',
	) );
	
	
	add_theme_support('custom-logo');
	
	/*
	 * WooCommerce Plugin Support
	 */
	add_theme_support( 'woocommerce' );
	
	// Gutenberg wide images.
	add_theme_support( 'align-wide' );
	
	/*
	 * This theme styles the visual editor to resemble the theme style,
	 * specifically font, colors, icons, and column width.
	 */
	add_editor_style( array( 'assets/css/editor-style.css', pet_bazaar_google_font() ) );
	
	//Set up the WordPress core custom background feature.
	add_theme_support( 'custom-background', apply_filters( 'pet_bazaar_custom_background_args', array(
		'default-color' => 'ffffff',
		'default-image' => '',
	) ) );
}
endif;
add_action( 'after_setup_theme', 'pet_bazaar_setup' );

/**
 * Set the content width in pixels, based on the theme's design and stylesheet.
 *
 * Priority 0 to make it available to lower priority callbacks.
 *
 * @global int $content_width
 */
function pet_bazaar_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'pet_bazaar_content_width', 1170 );
}
add_action( 'after_setup_theme', 'pet_bazaar_content_width', 0 );


/**
 * All Styles & Scripts.
 */
require_once get_template_directory() . '/inc/enqueue.php';

/**
 * Nav Walker fo Bootstrap Dropdown Menu.
 */

require_once get_template_directory() . '/inc/class-wp-bootstrap-navwalker.php';

/**
 * Implement the Custom Header feature.
 */
require_once get_template_directory() . '/inc/custom-header.php';


/**
 * Custom template tags for this theme.
 */
require_once get_template_directory() . '/inc/template-tags.php';

/**
 * Dynamic Style
 */
require_once get_template_directory() . '/inc/dynamic_style.php';

/**
 * Custom functions that act independently of the theme templates.
 */
 require_once get_template_directory() . '/inc/extras.php';
require_once get_template_directory() . '/inc/getting-start.php';

/**
 * Customizer additions.
 */
 require_once get_template_directory() . '/inc/pet-bazaar-customizer.php';
 require get_template_directory() . '/inc/customizer/customizer-repeater/functions.php';
 
/**
 * Frontpage
 */
 require_once get_template_directory() . '/template-parts/sections/section-blog.php'; 
 
 /* Get Product parent category Start */
 function pet_bazaar_has_Children($cat_id)
{
    $children = get_terms(
        'product_cat',
        array( 'parent' => $cat_id, 'hide_empty' => false )
    );
    if ($children){
        return true;
    }
    return false;
}
/* Get Product parent category END */


/* Data Migration From Lite Chilren to Lite Parent */
function pet_bazaar_child_theme_changes() {
    $changed_theme_slug = '';
    $diff = [];

    if (wp_get_theme()->name == 'Pet Bazaar') {
        $parent_theme_mods = get_option('theme_mods_pet-bazaar');
        if (!is_array($parent_theme_mods)) {
            $parent_theme_mods = [];
        }

        $parent_theme_mods = sanitize_theme_mod_values($parent_theme_mods);

        $themes = wp_get_themes();

        foreach ($themes as $theme) {
            $template = $theme->get('Template');

            if (!empty($template) && $template == 'pet-bazaar') {
                $child_slug = strtolower($theme->get('Name'));
                $hasSpace = preg_match('/\s/', $child_slug);

                if ($hasSpace) {
                    $child_theme_slug = str_replace(' ', '-', $child_slug);
                } else {
                    $child_theme_slug = $child_slug;
                }

                $child_theme_mods = get_option('theme_mods_' . $child_theme_slug);

                if (!is_array($child_theme_mods)) {
                    $child_theme_mods = [];
                }

                $child_theme_mods = sanitize_theme_mod_values($child_theme_mods);

                // Compare individual theme mods
                $diff = array_diff_assoc($child_theme_mods, $parent_theme_mods);

                if (!empty($diff)) {
                    $changed_theme_slug = $child_theme_slug;
                    break;
                }
            }
        }

        if (!empty($changed_theme_slug)) {
            // Apply changes for the changed child theme
            $child_mods = get_option('theme_mods_' . $changed_theme_slug);

            if (is_array($child_mods)) {
                foreach ($child_mods as $child_mod_k => $child_mod_v) {
                    if (is_array($child_mod_v) || is_object($child_mod_v)) {
                        continue; 
                    }

                   set_theme_mod($child_mod_k, $child_mod_v);
                }
            }
        }
    }
}

function sanitize_theme_mod_values($theme_mods) {
    $sanitized_mods = [];

    foreach ($theme_mods as $key => $value) {
        if (is_array($value) || is_object($value)) {
            $sanitized_mods[$key] = '';
        } else {
            $sanitized_mods[$key] = $value;
        }
    }

    return $sanitized_mods;
}

add_action('after_switch_theme', 'pet_bazaar_child_theme_changes');

/* End Data Migration */

/**
 * Pet_Bazaar Product Category
 */
 add_action('product_cat_add_form_fields', 'pet_bazaar_product_taxonomy_add_new_meta_field', 10, 1);
add_action('product_cat_edit_form_fields', 'pet_bazaar_product_taxonomy_edit_meta_field', 10, 1);
//Product Cat Create page
function pet_bazaar_product_taxonomy_add_new_meta_field() {
    ?>   
    <div class="form-field">
        <label for="pet_bazaar_product_cat_icon"><?php _e('Icon', 'pet-bazaar'); ?></label>
        <input type="text" name="pet_bazaar_product_cat_icon" id="pet_bazaar_product_cat_icon">
        <p class="description"><?php _e('Enter a meta title, <= 60 character', 'pet-bazaar'); ?></p>
    </div>
    <?php
}
//Product Cat Edit page
function pet_bazaar_product_taxonomy_edit_meta_field($term) {
    //getting term ID
    $term_id = $term->term_id;
    // retrieve the existing value(s) for this meta field.
    $pet_bazaar_product_cat_icon = get_term_meta($term_id, 'pet_bazaar_product_cat_icon', true);
    ?>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="pet_bazaar_product_cat_icon"><?php _e('Icon', 'pet-bazaar'); ?></label></th>
        <td>
            <input type="text" name="pet_bazaar_product_cat_icon" id="pet_bazaar_product_cat_icon" value="<?php echo esc_attr($pet_bazaar_product_cat_icon) ? esc_attr($pet_bazaar_product_cat_icon) : ''; ?>">
        </td>
    </tr>
    <?php
}


add_action('edited_product_cat', 'pet_bazaar_save_taxonomy_product_meta', 10, 1);
add_action('create_product_cat', 'pet_bazaar_save_taxonomy_product_meta', 10, 1);
// Save extra taxonomy fields callback function.
function pet_bazaar_save_taxonomy_product_meta($term_id) {
    $pet_bazaar_product_cat_icon = filter_input(INPUT_POST, 'pet_bazaar_product_cat_icon');
    update_term_meta($term_id, 'pet_bazaar_product_cat_icon', $pet_bazaar_product_cat_icon);
}