<?php
/**
 * Messina Blog Theme Customizer
 *
 * @package messina_blog
 */

/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function messina_blog_customize_register($wp_customize)
{
    $wp_customize->get_setting('blogname')->transport = 'postMessage';
    $wp_customize->get_setting('blogdescription')->transport = 'postMessage';
    $wp_customize->get_setting('header_textcolor')->transport = 'postMessage';

    $messina_blog_options = messina_blog_theme_options();

    if (isset($wp_customize->selective_refresh)) {
        $wp_customize->selective_refresh->add_partial('blogname', [
            'selector' => '.site-title a',
            'render_callback' => 'messina_blog_customize_partial_blogname',
        ]);
        $wp_customize->selective_refresh->add_partial('blogdescription', [
            'selector' => '.site-description',
            'render_callback' => 'messina_blog_customize_partial_blogdescription',
        ]);
    }

    $wp_customize->add_panel('theme_options', [
        'title' => esc_html__('Theme Options', 'messina-blog'),
        'priority' => 2,
    ]);

    /* Header Section */
    $wp_customize->add_section('header_section', [
        'title' => esc_html__('Header Section', 'messina-blog'),
        'panel' => 'theme_options',
        'description' => esc_html__(
            'More Social Icons are only available in premium version'
        ),
        'capability' => 'edit_theme_options',
    ]);

    $wp_customize->add_setting('messina_blog_theme_options[facebook]', [
        'type' => 'option',
        'default' => $messina_blog_options['facebook'],
        'sanitize_callback' => 'messina_blog_sanitize_url',
    ]);
    $wp_customize->add_control('messina_blog_theme_options[facebook]', [
        'label' => esc_html__('Facebook Link', 'messina-blog'),
        'type' => 'url',
        'section' => 'header_section',
        'settings' => 'messina_blog_theme_options[facebook]',
    ]);

    $wp_customize->add_setting('messina_blog_theme_options[twitter]', [
        'type' => 'option',
        'default' => $messina_blog_options['twitter'],
        'sanitize_callback' => 'messina_blog_sanitize_url',
    ]);
    $wp_customize->add_control('messina_blog_theme_options[twitter]', [
        'label' => esc_html__('Twitter Link', 'messina-blog'),
        'type' => 'url',
        'section' => 'header_section',
        'settings' => 'messina_blog_theme_options[twitter]',
    ]);


    $wp_customize->add_setting('messina_blog_theme_options[header_search]', [
        'type' => 'option',
        'default' => true,
        'default' => $messina_blog_options['header_search'],
        'sanitize_callback' => 'messina_blog_sanitize_checkbox',
    ]);
    $wp_customize->add_control(
        'messina_blog_theme_options[header_search]',
        [
            'label' => esc_html__('Show Search', 'messina-blog'),
            'type' => 'Checkbox',
            'priority' => 1,
            'section' => 'header_section',
        ]
    );



    //radio box sanitization function
    function messina_blog_sanitize_radio( $input, $setting ){

        //input must be a slug: lowercase alphanumeric characters, dashes and underscores are allowed only
        $input = sanitize_key($input);

        //get the list of possible radio box options
        $choices = $setting->manager->get_control( $setting->id )->choices;

        //return input if valid or return default option
        return ( array_key_exists( $input, $choices ) ? $input : $setting->default );

    }
    $wp_customize->add_section('below_banner_section', [
        'title' => esc_html__('Banner Carousel Section', 'messina-blog'),
        'panel' => 'theme_options',
        'capability' => 'edit_theme_options',
    ]);
    $wp_customize->add_setting(
        'messina_blog_theme_options[blog_carousel_category]',
        [
            'type' => 'option',
            'sanitize_callback' => 'messina_blog_sanitize_select',
            'default' => $messina_blog_options['blog_carousel_category'],
        ]
    );

    $wp_customize->add_control(
        'messina_blog_theme_options[blog_carousel_category]',
        [
            'section' => 'below_banner_section',
            'type' => 'select',
            'choices' => messina_blog_get_categories_select(),
            'label' => esc_html__('Select Category to show Posts', 'messina-blog'),
            'description' => esc_html__(
                'Max 3 Posts will be shown from the selected Category and no carousel in Free Version ',
                'messina-blog'
            ),
            'settings' => 'messina_blog_theme_options[blog_carousel_category]',
            'priority' => 1,
        ]
    );

    function messina_blog_sanitize_checkbox($input)
    {
        if (true === $input) {
            return 1;
        } else {
            return 0;
        }
    }



    /* Blog Section */

    $wp_customize->add_section('blog_sidebar_section', [
        'title' => esc_html__('Homepage Sidebar Section', 'messina-blog'),
        'panel' => 'theme_options',
        'capability' => 'edit_theme_options',
    ]);

    
    $wp_customize->add_setting(
        'messina_blog_theme_options[sidebar_blog_category]',
        [
            'type' => 'option',
            'sanitize_callback' => 'messina_blog_sanitize_select',
            'default' => $messina_blog_options['sidebar_blog_category'],
        ]
    );

    $wp_customize->add_control(
        'messina_blog_theme_options[sidebar_blog_category]',
        [
            'section' => 'blog_sidebar_section',
            'type' => 'select',
            'choices' => messina_blog_get_categories_select(),
            'label' => esc_html__('Select Category to show Posts', 'messina-blog'),
            'settings' => 'messina_blog_theme_options[sidebar_blog_category]',
            'priority' => 1,
        ]
    );

    /* Blog Section */

    $wp_customize->add_section('prefooter_section', [
        'title' => esc_html__('Prefooter Section', 'messina-blog'),
        'panel' => 'theme_options',
        'capability' => 'edit_theme_options',
    ]);

    $wp_customize->add_setting('messina_blog_theme_options[show_prefooter]', [
        'type' => 'option',
        'default' => true,
        'default' => $messina_blog_options['show_prefooter'],
        'sanitize_callback' => 'messina_blog_sanitize_checkbox',
    ]);

    $wp_customize->add_control('messina_blog_theme_options[show_prefooter]', [
        'label' => esc_html__('Show Prefooter Section', 'messina-blog'),
        'type' => 'Checkbox',
        'priority' => 1,
        'description' => esc_html__(
            'Copyright text can be changed in only premium version'
        ),
        'section' => 'prefooter_section',
    ]);
}
add_action('customize_register', 'messina_blog_customize_register');

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

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

/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 */
function messina_blog_customize_preview_js()
{
    wp_enqueue_script(
        'messina-blog-customizer',
        get_template_directory_uri() . '/js/customizer.js',
        ['customize-preview'],
        '20151215',
        true
    );
}
add_action('customize_preview_init', 'messina_blog_customize_preview_js');
