<?php
/**
 * Code Manas Theme Customizer
 *
 * @package Code_Manas
 */

/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function code_manas_customize_register( $wp_customize ) {
	$wp_customize->get_setting( 'blogname' )->transport         = 'postMessage';
	$wp_customize->get_setting( 'blogdescription' )->transport  = 'postMessage';
	$wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';

	if ( isset( $wp_customize->selective_refresh ) ) {
		$wp_customize->selective_refresh->add_partial( 'blogname', array(
			'selector'        => '.site-title a',
			'render_callback' => 'code_manas_customize_partial_blogname',
		) );
		$wp_customize->selective_refresh->add_partial( 'blogdescription', array(
			'selector'        => '.site-description',
			'render_callback' => 'code_manas_customize_partial_blogdescription',
		) );
	}

	if ( ! class_exists( 'Kirki' ) ) {
		return;
	}
	
	// Move default sections
	$wp_customize->get_section( 'title_tagline' )->panel = 'code_manas_header';
	$wp_customize->get_section( 'colors' )->panel = 'code_manas_advance_options';
	$wp_customize->get_section( 'custom_css' )->panel = 'code_manas_advance_options';
	$wp_customize->get_section( 'background_image' )->panel = 'code_manas_advance_options';

	// Change priority of default sections
	$wp_customize->get_section( 'static_front_page' )->priority = 20;

	// Customize default behavior of Header Image
	$wp_customize->get_control( 'header_image' )->section = 'code_manas_featured_section';
	$wp_customize->get_control( 'header_image' )->priority = 100;
	$wp_customize->get_control( 'header_image' )->active_callback = function() {
		// Get all default values
		$defaults = cm_kirki_fields()->get_theme_mods_defaults();
		return ( 'image' === get_theme_mod( 'featured_bg', $defaults['featured_bg'] ) && get_theme_mod( 'featured_enable', 1 ) ) ;
	};
}
add_action( 'customize_register', 'code_manas_customize_register' );

/**
 * Render the site title for the selective refresh partial.
 *
 * @return void
 */
function code_manas_customize_partial_blogname() {
	bloginfo( 'name' );
}

/**
 * Render the site tagline for the selective refresh partial.
 *
 * @return void
 */
function code_manas_customize_partial_blogdescription() {
	bloginfo( 'description' );
}

/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 */
function code_manas_customize_preview_js() {
	$ver        = wp_get_theme()->get( 'Version' );

	wp_enqueue_script( 'code-manas-customizer', get_theme_file_uri( '/assets/js/customizer.js' ), array( 'customize-preview' ), $ver, true );
}
add_action( 'customize_preview_init', 'code_manas_customize_preview_js' );