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

/**
 * Define Theme Version
 */
define( 'NEWS25_THEME_VERSION', '3.9' );

// Root path/URI.
define( 'NEWS25_PARENT_DIR', get_template_directory() );
define( 'NEWS25_PARENT_URI', get_template_directory_uri() );

// Root path/URI.
define( 'NEWS25_PARENT_INC_DIR', NEWS25_PARENT_DIR . '/inc');
define( 'NEWS25_PARENT_INC_URI', NEWS25_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' );
	// add_theme_support( 'post-formats', array( 'gallery', 'quote', 'video', 'aside', 'image', 'link', 'audio', 'status', 'chat' ) );

	/*
	 * 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( 'news-25' );
		
	// This theme uses wp_nav_menu() in one location.
	register_nav_menus( array(
		'primary_menu' => esc_html__( 'Primary Menu', 'news-25' ),
		'footer_menu' => esc_html__( 'Footer Menu', 'news-25' ),
	) );

	/*
	 * 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');
	
	// remove_theme_support( 'widgets-block-editor' );
	
	/*
	 * WooCommerce Plugin Support
	 */
	add_theme_support( 'woocommerce' );
	
	/*
	 * 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', news25_google_font() ) );
	
	//Set up the WordPress core custom background feature.
	add_theme_support( 'custom-background', apply_filters( 'news25_custom_background_args', array(
		'default-color' => 'ffffff',
		'default-image' => '',
	) ) );
}
endif;
add_action( 'after_setup_theme', 'news25_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 news25_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'news25_content_width', 1170 );
}
add_action( 'after_setup_theme', 'news25_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';


/**
 * Called Breadcrumb
 */
require_once get_template_directory() . '/inc/breadcrumb/breadcrumb.php';

/**
 * Sidebar.
 */
require_once get_template_directory() . '/inc/sidebar/sidebar.php';

/**
 * Custom template tags for this theme.
 */
require_once get_template_directory() . '/inc/template-tags.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/news25-customizer.php';

/**
 * Load Jetpack compatibility file.
 */
require_once get_template_directory() . '/inc/jetpack.php';


/**
 * Customizer additions.
 */
 require get_template_directory() . '/inc/customizer-repeater/functions.php';
 
 
/* Data Migration From Lite Chilren to Lite Parent */

function news25_child_theme_changes() {
    $current_theme = wp_get_theme();
    
    // Ensure we are switching from News 25 parent
    if ($current_theme->get_template() !== 'news-25') {
        return;
    }

    $parent_theme_mods = get_option('theme_mods_news-25');
    if (!is_array($parent_theme_mods)) {
        return;
    }

    // Get current (new) theme slug
    $child_theme_slug = get_stylesheet(); // e.g., 'news25-child'

    $child_theme_mods = get_option("theme_mods_{$child_theme_slug}");
    if (!is_array($child_theme_mods)) {
        return;
    }

    // Apply all mods from child theme to active theme
    foreach ($child_theme_mods as $mod_key => $mod_value) {
		
	 // Handle if value is a JSON string
            if ( is_string( $mod_value ) ) {
                $decoded = json_decode( $mod_value, true );

                if ( json_last_error() === JSON_ERROR_NONE && is_array( $decoded ) ) {
                    $cleaned_array = [];

                    // If it's a list of arrays
                    foreach ( $decoded as $item ) {
                        if ( is_array( $item ) ) {
                            $cleaned_item = array_filter(
                                $item,
                                function( $value ) {
                                    return $value !== 'undefined';
                                }
                            );
                            $cleaned_array[] = $cleaned_item;
                        }
                    }

                    // Re-encode and assign back
                    $mod_value = json_encode( $cleaned_array );
                }
            }
        set_theme_mod($mod_key, $mod_value);
    }
}
add_action('after_switch_theme', 'news25_child_theme_changes');