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

/**
 * Define Theme Version
 */
define( 'ELECTROMIX_THEME_VERSION', '2.2' );

// Root path/URI.
define( 'ELECTROMIX_PARENT_DIR', get_template_directory() );
define( 'ELECTROMIX_PARENT_URI', get_template_directory_uri() );

// Root path/URI.
define( 'ELECTROMIX_PARENT_INC_DIR', ELECTROMIX_PARENT_DIR . '/inc');
define( 'ELECTROMIX_PARENT_INC_URI', ELECTROMIX_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( 'electromix' );
		
	// This theme uses wp_nav_menu() in one location.
	register_nav_menus( array(
		'primary_menu' => esc_html__( 'Primary Menu', 'electromix' ),
		'footer_menu' => esc_html__( 'Footer Menu', 'electromix' ),
	) );
	
	/*
		* Switch default core markup for search form, comment form, and comments
	 * to output valid HTML5.
	 */
	add_theme_support( 'post-thumbnails' );
	add_theme_support( 'title-tag' );
	add_theme_support( 'automatic-feed-links' );
	add_theme_support( 'custom-background' );
	add_theme_support( 'custom-logo' );
	add_theme_support( 'align-wide' );
	add_theme_support( 'add_editor_style()' );
	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' ) );
	
	//Set up the WordPress core custom background feature.
	add_theme_support( 'custom-background', apply_filters( 'electromix_custom_background_args', array(
		'default-color' => 'ffffff',
		'default-image' => '',
	) ) );
}
endif;
add_action( 'after_setup_theme', 'electromix_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 electromix_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'electromix_content_width', 1170 );
}
add_action( 'after_setup_theme', 'electromix_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';

/**
 * Customizer additions.
 */
 require_once get_template_directory() . '/inc/electromix-customizer.php';
 require get_template_directory() . '/inc/customizer/customizer-repeater/functions.php';
 
if ( is_admin() ) {
	require_once get_template_directory() . '/inc/admin/class-electromix-plugin-utilities.php';
	require_once get_template_directory() . '/inc/admin/class-electromix-admin.php';
}
  

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

    if (wp_get_theme()->name == 'Electromix') {
        $parent_theme_mods = get_option('theme_mods_electromix');
        if (!is_array($parent_theme_mods)) {
            $parent_theme_mods = [];
        }

        $parent_theme_mods = electromix_sanitize_theme_mod_values($parent_theme_mods);

        $themes = wp_get_themes();

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

            if (!empty($template) && $template == 'electromix') {
                $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 = electromix_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 electromix_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', 'electromix_child_theme_changes');

/* End Data Migration */

// Blog More classes
function electromix_add_more_link_classes( $content ) {
	if ( strpos( $content, 'class="more-link"' ) !== false ) {
		$content = str_replace(
			'class="more-link"',
			'class="more-link btn btn-primary"',
			$content
		);
	}
	return $content;
}
add_filter( 'the_content', 'electromix_add_more_link_classes' );