<?php
/**
 * Theme Customizer
 *
 * @package Blorigan
 */

/**
 * Add Customizer options.
 *
 * @since 1.0.0
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function blorigan_customize_register( $wp_customize ) {
	// Load custom controls.
	require_once trailingslashit( get_template_directory() ) . 'includes/customizer/control.php';

	// Load custom controls and sections.
	$wp_customize->register_control_type( 'Blorigan_Heading_Control' );
	$wp_customize->register_control_type( 'Blorigan_Message_Control' );
	$wp_customize->register_control_type( 'Blorigan_Dropdown_Taxonomies_Control' );
	$wp_customize->register_control_type( 'Blorigan_Dropdown_Sidebars_Control' );
	$wp_customize->register_control_type( 'Blorigan_Radio_Image_Control' );
	$wp_customize->register_section_type( 'Blorigan_Upsell_Section' );

	// Upsell section.
	$wp_customize->add_section(
		new Blorigan_Upsell_Section( $wp_customize, 'custom_theme_upsell',
			array(
				'title'    => esc_html__( 'Blorigan Pro', 'blorigan' ),
				'pro_text' => esc_html__( 'Buy Pro', 'blorigan' ),
				'pro_url'  => 'https://axlethemes.com/wordpress-themes/blorigan-pro/',
				'priority' => 1,
			)
		)
	);

	// Load helpers.
	require_once trailingslashit( get_template_directory() ) . 'includes/helpers.php';

	// Load customize sanitize.
	require_once trailingslashit( get_template_directory() ) . 'includes/customizer/sanitize.php';

	// Load customize callback.
	require_once trailingslashit( get_template_directory() ) . 'includes/customizer/callback.php';

	// Load customize option.
	require_once trailingslashit( get_template_directory() ) . 'includes/customizer/option.php';

	// Load slider customize option.
	require_once trailingslashit( get_template_directory() ) . 'includes/customizer/slider.php';

	// Modify default customizer options.
	$wp_customize->get_control( 'background_color' )->description = esc_html__( 'Note: Background Color is applicable only if no image is set as Background Image.', 'blorigan' );
}

add_action( 'customize_register', 'blorigan_customize_register' );

/**
 * Register Customizer partials.
 *
 * @since 1.0.0
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function blorigan_customizer_partials( WP_Customize_Manager $wp_customize ) {
	// Bail 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;
	}

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

	// Register partial for blogname.
	$wp_customize->selective_refresh->add_partial( 'blogname', array(
		'selector'            => '.site-title a',
		'container_inclusive' => false,
		'render_callback'     => 'blorigan_customize_partial_blogname',
	) );

	// Register partial for blogdescription.
	$wp_customize->selective_refresh->add_partial( 'blogdescription', array(
		'selector'            => '.site-description',
		'container_inclusive' => false,
		'render_callback'     => 'blorigan_customize_partial_blogdescription',
	) );
}

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

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

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

/**
 * Register customizer controls scripts.
 *
 * @since 1.0.0
 */
function blorigan_customize_controls_register_scripts() {
	$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';

	wp_enqueue_style( 'blorigan-customize-controls', get_template_directory_uri() . '/css/customize-controls' . $min . '.css', array(), '2.0.1' );
	wp_enqueue_script( 'blorigan-customize-controls', get_template_directory_uri() . '/js/customize-controls' . $min . '.js', array( 'jquery', 'customize-controls' ), '2.0.1', true );
}

add_action( 'customize_controls_enqueue_scripts', 'blorigan_customize_controls_register_scripts', 0 );
