<?php
/**
 * Nice Blog Theme Customizer
 *
 * @package Nice Blog
 */

// 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 nice_blog_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' => 'nice_blog_customize_partial_blogname',
			)
		);
		$wp_customize->selective_refresh->add_partial(
			'blogdescription',
			array(
				'selector'        => '.site-description',
				'render_callback' => 'nice_blog_customize_partial_blogdescription',
			)
		);
	}

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

	$wp_customize->add_control(
		'nice_blog_header_text_display',
		array(
			'section' => 'title_tagline',
			'type'    => 'checkbox',
			'label'   => esc_html__( 'Display Site Title and Tagline', 'nice-blog' ),
		)
	);

	// Archive Page title.
	$wp_customize->add_setting(
		'nice_blog_archive_page_title',
		array(
			'default'           => __( 'Latest Posts', 'nice-blog' ),
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'nice_blog_archive_page_title',
		array(
			'label'           => esc_html__( 'Archive Posts Title', 'nice-blog' ),
			'section'         => 'static_front_page',
			'active_callback' => 'nice_blog_is_latest_posts',
		)
	);

	// Abort if selective refresh is not available.
	if ( isset( $wp_customize->selective_refresh ) ) {
		$wp_customize->selective_refresh->add_partial(
			'nice_blog_archive_page_title',
			array(
				'selector'            => '.home main.site-main h3.section-title',
				'settings'            => 'nice_blog_archive_page_title',
				'container_inclusive' => false,
				'fallback_refresh'    => true,
			)
		);
	}

	// Archive Page sub title.
	$wp_customize->add_setting(
		'nice_blog_archive_page_subtitle',
		array(
			'default'           => '',
			'sanitize_callback' => 'sanitize_text_field',
		)
	);

	$wp_customize->add_control(
		'nice_blog_archive_page_subtitle',
		array(
			'label'           => esc_html__( 'Archive Posts Sub Title', 'nice-blog' ),
			'section'         => 'static_front_page',
			'active_callback' => 'nice_blog_is_latest_posts',
		)
	);

	// Homepage Settings - Enable Homepage Content.
	$wp_customize->add_setting(
		'nice_blog_enable_frontpage_content',
		array(
			'default'           => false,
			'sanitize_callback' => 'nice_blog_sanitize_checkbox',
		)
	);

	$wp_customize->add_control(
		'nice_blog_enable_frontpage_content',
		array(
			'label'           => esc_html__( 'Enable Homepage Content', 'nice-blog' ),
			'description'     => esc_html__( 'Check to enable content on static homepage.', 'nice-blog' ),
			'section'         => 'static_front_page',
			'type'            => 'checkbox',
			'active_callback' => 'nice_blog_is_static_homepage_enabled',
		)
	);

	// Color Customizer Setting.
	require get_template_directory() . '/inc/customizer/color.php';

	// 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', 'nice_blog_customize_register' );

// Check if static home page is enabled.
function nice_blog_is_static_homepage_enabled( $control ) {
	return ( 'page' === $control->manager->get_setting( 'show_on_front' )->value() );
}


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

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

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

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

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