<?php
/**
 * United Blog Theme Customizer.
 *
 * @package United Blog
 */
/**
 * Core functions.
 *
 */
if (!function_exists('united_blog_get_option')):
    /**
     * Get theme option.
     *
     * @param string $key Option key.
     * @return mixed Option value.
     * @since 1.0.0
     *
     */
    function united_blog_get_option($key)
    {
        if (empty($key)) {
            return;
        }
        $value = '';
        $default = united_blog_get_default_theme_options();
        $default_value = null;
        if (is_array($default) && isset($default[$key])) {
            $default_value = $default[$key];
        }
        if (null !== $default_value) {
            $value = get_theme_mod($key, $default_value);
        } else {
            $value = get_theme_mod($key);
        }
        return $value;
    }
endif;
//customizer default
require get_template_directory() . '/inc/customizer/default.php';
/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function united_blog_customize_register($wp_customize)
{
    // Load custom controls.
    require get_template_directory() . '/inc/customizer/customizer-function.php';
    /**
     * Site Title & Description.
     * */
    $wp_customize->get_setting('blogname')->transport = 'postMessage';
    $wp_customize->get_setting('blogdescription')->transport = 'postMessage';
    $wp_customize->selective_refresh->add_partial(
        'blogname',
        array(
            'selector' => '.site-title a',
            'render_callback' => 'united_blog_partial_blogname',
        )
    );
    $wp_customize->selective_refresh->add_partial(
        'blogdescription',
        array(
            'selector' => '.site-description',
            'render_callback' => 'united_blog_partial_blogdescription',
        )
    );
    $wp_customize->selective_refresh->add_partial(
        'custom_logo',
        array(
            'selector' => '.header-titles [class*=site-]:not(.site-description)',
            'render_callback' => 'united_blog_partial_site_logo',
            'container_inclusive' => true,
        )
    );
    $wp_customize->selective_refresh->add_partial(
        'retina_logo',
        array(
            'selector' => '.header-titles [class*=site-]:not(.site-description)',
            'render_callback' => 'united_blog_partial_site_logo',
        )
    );
    /**
     * Site Identity
     */
    /* 2X Header Logo ---------------- */
    $wp_customize->add_setting(
        'retina_logo',
        array(
            'capability' => 'edit_theme_options',
            'sanitize_callback' => 'united_blog_sanitize_checkbox',
            'transport' => 'postMessage',
        )
    );
    $wp_customize->add_control(
        'retina_logo',
        array(
            'type' => 'checkbox',
            'section' => 'title_tagline',
            'priority' => 10,
            'label' => __('Retina logo', 'united-blog'),
            'description' => __('Scales the logo to half its uploaded size, making it sharp on high-res screens.', 'united-blog'),
        )
    );
    if (isset($wp_customize->selective_refresh)) {
        $wp_customize->selective_refresh->add_partial('blogname', array(
            'selector' => '.site-title a',
            'render_callback' => 'united_blog_customize_partial_blogname',
        ));
        $wp_customize->selective_refresh->add_partial('blogdescription', array(
            'selector' => '.site-description',
            'render_callback' => 'united_blog_customize_partial_blogdescription',
        ));
    }
    // Move Site Identity section inside our basic panel.
    $site_identity = $wp_customize->get_section('title_tagline');
    $site_identity->panel = 'theme_general_panel';
    // Move background image section inside our basic panel.
    $site_identity = $wp_customize->get_section('background_image');
    $site_identity->panel = 'theme_general_panel';
    // Get the default Colors section and change its title
    $wp_customize->get_section('colors')->title = __('Theme Colors', 'united-blog');
    /*theme option panel details*/
    require get_template_directory() . '/inc/customizer/theme-option.php';
    // Register custom section types.
    $wp_customize->register_section_type('united_blog_Customize_Section_Upsell');
    // Register sections.
    $wp_customize->add_section(new united_blog_Customize_Section_Upsell(
            $wp_customize,
            'theme_upsell',
            array(
                'title' => esc_html__('United Blog Plus', 'united-blog'),
                'pro_text' => esc_html__('Switch to Premium', 'united-blog'),
                'pro_url' => 'https://unitedtheme.com/themes/united-blog-plus/',
                'priority' => 1,
            )
        )
    );
}

add_action('customize_register', 'united_blog_customize_register');
/**
 * Customizer control scripts and styles.
 *
 * @since 1.0.0
 */
function united_blog_customizer_control_scripts()
{
    $suffix = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min';
    $file_name = is_rtl() ? 'customizer-rtl' . $suffix . '.css' : 'customizer' . $suffix . '.css';
    wp_enqueue_style(
        'customizer-style',
        get_template_directory_uri() . '/assets/css/' . $file_name,
        array(),
        _UNITED_VERSION
    );

    wp_enqueue_script( 'united-blog-customizer-controls', get_template_directory_uri() . '/assets/js/customizer-admin.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-sortable', 'customize-controls' ), '', true );
}

add_action('customize_controls_enqueue_scripts', 'united_blog_customizer_control_scripts', 0);
/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 *
 * @since 1.0.0
 */
function united_blog_customize_preview_js()
{
    wp_enqueue_script('united_blog_customizer', get_template_directory_uri() . '/assets/js/customizer.js', array('customize-preview'), '20130508', true);
}

add_action('customize_preview_init', 'united_blog_customize_preview_js');
function united_blog_upsell_js()
{
    wp_enqueue_script('united_blog_customize_controls', get_template_directory_uri() . '/assets/js/upsell.js', array('customize-controls'));
}

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