<?php
/**
 * Theme Customizer.
 *
 * @package Trade_Line
 */

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

	// Load custom controls.
	require get_template_directory() . '/inc/customizer/control.php';

	// Load customize helpers.
	require get_template_directory() . '/inc/helper/options.php';

	// Load customize sanitize.
	require get_template_directory() . '/inc/customizer/sanitize.php';

	// Load customize callback.
	require get_template_directory() . '/inc/customizer/callback.php';

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

	// Load customize option.
	require get_template_directory() . '/inc/customizer/option.php';

	// Load font family option.
	require get_template_directory() . '/inc/customizer/font.php';

	// Load slider customize option.
	require get_template_directory() . '/inc/customizer/slider.php';

	// Load featured content customize option.
	require get_template_directory() . '/inc/customizer/featured-content.php';

	// Load color option.
	require get_template_directory() . '/inc/customizer/color.php';

	// Load client option.
	require get_template_directory() . '/inc/customizer/clients.php';

	// Load reset option.
	require get_template_directory() . '/inc/customizer/reset.php';

	// Modify default customizer options.
	$wp_customize->get_control( 'background_color' )->description = __( 'Note: Background Color is applicable only if Boxed Layout used and no image is set as Background Image.', 'trade-line-pro' );
	$wp_customize->get_control( 'background_color' )->section = 'section_color_main_background';

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

/**
 * Load styles for Customizer.
 *
 * @since 1.0.0
 */
function trade_line_load_customizer_styles() {

	global $pagenow;

	if ( 'customize.php' === $pagenow ) {
		$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
		wp_enqueue_style( 'trade-line-customizer-style', get_template_directory_uri() . '/css/customizer' . $min . '.css', false, '2.0.0' );
	}

}

add_action( 'admin_enqueue_scripts', 'trade_line_load_customizer_styles' );

/**
 * Customizer partials.
 *
 * @since 2.2.0
 */
function trade_line_customizer_partials( WP_Customize_Manager $wp_customize ) {

	// Abort if selective refresh is not available.
	if ( ! isset( $wp_customize->selective_refresh ) ) {

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

	}

	// Load customizer partials callback.
	include_once get_template_directory() . '/inc/customizer/partials.php';

	// Partial blogname.
	$wp_customize->selective_refresh->add_partial(
		'blogname', array(
		'selector'            => '.site-title a',
		'container_inclusive' => false,
		'render_callback'     => 'trade_line_customize_partial_blogname',
		 )
	);

	// Partial blogdescription.
	$wp_customize->selective_refresh->add_partial(
		'blogdescription', array(
		'selector'            => '.site-description',
		'container_inclusive' => false,
		'render_callback'     => 'trade_line_customize_partial_blogdescription',
		 )
	);

}

add_action( 'customize_register', 'trade_line_customizer_partials', 99 );

/**
 * Hide Custom CSS.
 *
 * @since 2.2.0
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function trade_line_hide_custom_css( $wp_customize ) {

	// Bail if not WP 4.7.
	if ( ! function_exists( 'wp_get_custom_css_post' ) ) {
		return;
	}

	$wp_customize->remove_control( 'theme_options[custom_css]' );

}

add_action( 'customize_register', 'trade_line_hide_custom_css', 99 );
