<?php
/**
 * News Articles Theme Customizer
 *
 * @package News Articles
 */

// upgrade to pro.
require get_template_directory() . '/inc/customizer/upgrade-to-pro/class-customize.php';

/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function news_articles_customize_register( $wp_customize ) {

	// Custom Controls.
	require get_template_directory() . '/inc/customizer/custom-controller.php';

	$wp_customize->get_setting( 'blogname' )->transport        = 'postMessage';
	$wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage';

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

	// Enable site title text shadow.
	$wp_customize->add_setting(
		'news_articles_enable_site_title_text_shadow',
		array(
			'default'           => true,
			'sanitize_callback' => 'news_articles_sanitize_checkbox',
		)
	);
	$wp_customize->add_control(
		new News_Articles_Toggle_Checkbox_Custom_control(
			$wp_customize,
			'news_articles_enable_site_title_text_shadow',
			array(
				'label'    => esc_html__( 'Enable Site Title Text Shadow.', 'news-articles' ),
				'settings' => 'news_articles_enable_site_title_text_shadow',
				'section'  => 'title_tagline',
				'type'     => 'checkbox',
				'priority' => 9,
			)
		)
	);

	// Header text display setting.
	$wp_customize->add_setting(
		'news_articles_header_text_display',
		array(
			'default'           => true,
			'sanitize_callback' => 'news_articles_sanitize_checkbox',
		)
	);

	$wp_customize->add_control(
		'news_articles_header_text_display',
		array(
			'section' => 'title_tagline',
			'type'    => 'checkbox',
			'label'   => esc_html__( 'Display Site Title and Tagline', 'news-articles' ),
		)
	);

	// frontpage customizer section.
	require get_template_directory() . '/inc/customizer/frontpage-customizer/customizer-sections.php';

	// theme options customizer section.
	require get_template_directory() . '/inc/customizer/theme-options/theme-options-sections.php';

}
add_action( 'customize_register', 'news_articles_customize_register' );

// Sanitize callback.
require get_template_directory() . '/inc/customizer/sanitize-callback.php';

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

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

/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 */
function news_articles_customize_preview_js() {
	wp_enqueue_script( 'news-articles-customizer', get_template_directory_uri() . '/assets/js/customizer.min.js', array( 'customize-preview' ), NEWS_ARTICLES_VERSION, true );
}
add_action( 'customize_preview_init', 'news_articles_customize_preview_js' );

/**
 * Binds JS handlers for Customizer controls.
 */
function news_articles_customize_control_js() {
	wp_enqueue_style( 'news-articles-customize-style', get_template_directory_uri() . '/assets/css/customize-controls.min.css', array(), NEWS_ARTICLES_VERSION );
	wp_enqueue_script( 'news-articles-customize-control', get_template_directory_uri() . '/assets/js/customize-control.min.js', array( 'jquery', 'customize-controls' ), NEWS_ARTICLES_VERSION, true );
	$localized_data = array(
		'refresh_msg' => esc_html__( 'Refresh the page after Save and Publish.', 'news-articles' ),
		'reset_msg'   => esc_html__( 'Warning!!! This will reset all the settings. Refresh the page after Save and Publish to reset all.', 'news-articles' ),
	);
	wp_localize_script( 'news-articles-customize-control', 'localized_data', $localized_data );
}
add_action( 'customize_controls_enqueue_scripts', 'news_articles_customize_control_js' );
