<?php
/**
 * Feminine Blog Theme Customizer
 *
 * @package feminine_blog
 */

/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function feminine_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';

    $feminine_blog_options = feminine_blog_theme_options();

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

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

    /* Header Section */
    $wp_customize->add_section('header_section', [
        'title' => esc_html__('Header Section', 'feminine-blog'),
        'panel' => 'theme_options',
        'capability' => 'edit_theme_options',
    ]);

    $wp_customize->add_setting('feminine_blog_theme_options[facebook]', [
        'type' => 'option',
        'default' => $feminine_blog_options['facebook'],
        'sanitize_callback' => 'feminine_blog_sanitize_url',
    ]);
    $wp_customize->add_control('feminine_blog_theme_options[facebook]', [
        'label' => esc_html__('Facebook Link', 'feminine-blog'),
        'type' => 'url',
        'section' => 'header_section',
        'settings' => 'feminine_blog_theme_options[facebook]',
    ]);
    $wp_customize->add_setting('feminine_blog_theme_options[pinterest]', [
        'type' => 'option',
        'default' => $feminine_blog_options['pinterest'],
        'sanitize_callback' => 'esc_url_raw',
    ]);
    $wp_customize->add_control('feminine_blog_theme_options[pinterest]', [
        'label' => esc_html__('pinterest Link', 'feminine-blog'),
        'type' => 'url',
        'section' => 'header_section',
        'settings' => 'feminine_blog_theme_options[pinterest]',
    ]);

    $wp_customize->add_setting('feminine_blog_theme_options[twitter]', [
        'type' => 'option',
        'default' => $feminine_blog_options['twitter'],
        'sanitize_callback' => 'feminine_blog_sanitize_url',
    ]);
   
    $wp_customize->add_control('feminine_blog_theme_options[twitter]', [
        'label' => esc_html__('Twitter Link', 'feminine-blog'),
        'type' => 'url',
        'section' => 'header_section',
        'settings' => 'feminine_blog_theme_options[twitter]',
    ]);
    $wp_customize->add_setting('feminine_blog_theme_options[youtube]', [
        'type' => 'option',
        'default' => $feminine_blog_options['youtube'],
        'sanitize_callback' => 'esc_url_raw',
    ]);
    $wp_customize->add_control('feminine_blog_theme_options[youtube]', [
        'label' => esc_html__('Youtube Link', 'feminine-blog'),
        'type' => 'url',
        'section' => 'header_section',
        'settings' => 'feminine_blog_theme_options[youtube]',
    ]);
    $wp_customize->add_setting('feminine_blog_theme_options[instagram]', [
        'type' => 'option',
        'default' => $feminine_blog_options['instagram'],
        'sanitize_callback' => 'esc_url_raw',
    ]);
    $wp_customize->add_control('feminine_blog_theme_options[instagram]', [
        'label' => esc_html__('Instagram Link', 'feminine-blog'),
        'type' => 'url',
        'section' => 'header_section',
        'settings' => 'feminine_blog_theme_options[instagram]',
    ]);
    
   

    $wp_customize->add_setting('feminine_blog_theme_options[header_search]', [
        'type' => 'option',
        'default' => true,
        'default' => $feminine_blog_options['header_search'],
        'sanitize_callback' => 'feminine_blog_sanitize_checkbox',
    ]);
    $wp_customize->add_control(
        'feminine_blog_theme_options[header_search]',
        [
            'label' => esc_html__('Show Search', 'feminine-blog'),
            'type' => 'Checkbox',
            'priority' => 1,
            'section' => 'header_section',
        ]
    );



    //radio box sanitization function
    function feminine_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__('Blog Carousel Section', 'feminine-blog'),
        'panel' => 'theme_options',
        'capability' => 'edit_theme_options',
    ]);
    $wp_customize->add_setting(
        'feminine_blog_theme_options[blog_carousel_category]',
        [
            'type' => 'option',
            'sanitize_callback' => 'feminine_blog_sanitize_select',
            'default' => $feminine_blog_options['blog_carousel_category'],
        ]
    );

    $wp_customize->add_control(
        'feminine_blog_theme_options[blog_carousel_category]',
        [
            'section' => 'below_banner_section',
            'type' => 'select',
            'choices' => feminine_blog_get_categories_select(),
            'label' => esc_html__('Select Category to show Posts', 'feminine-blog'),
            'description' => esc_html__(
                'Max 6 Posts will be shown from the selected Category in Free Version',
                'feminine-blog'
            ),
            'settings' => 'feminine_blog_theme_options[blog_carousel_category]',
            'priority' => 1,
        ]
    );

    function feminine_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', 'feminine-blog'),
        'panel' => 'theme_options',
        'capability' => 'edit_theme_options',
    ]);

    $wp_customize->add_setting(
        'feminine_blog_theme_options[sidebar_blog_category]',
        [
            'default' => $feminine_blog_options['sidebar_blog_category'],
            'type' => 'option',
            'sanitize_callback' => 'feminine_blog_sanitize_select',
            'capability' => 'edit_theme_options',
        ]
    );

    $wp_customize->add_control(
        new feminine_blog_Dropdown_Customize_Control(
            $wp_customize,
            'feminine_blog_theme_options[sidebar_blog_category]',
            [
                'label' => esc_html__('Select Blog Posts Category', 'feminine-blog'),
                'section' => 'blog_sidebar_section',
                'choices' => feminine_blog_get_categories_select(),
                'settings' => 'feminine_blog_theme_options[sidebar_blog_category]',
            ]
        )
    );
    $wp_customize->add_setting('feminine_blog_theme_options[big_post_id]', [
        'type' => 'option',
        'sanitize_callback' => 'sanitize_text_field',
    ]);
    $wp_customize->add_control('big_post_id', [
        'label' => esc_html__('Enter Post Id for Big Post', 'feminine-blog'),
        'type' => 'text',
        'section' => 'blog_sidebar_section',
        'settings' => 'feminine_blog_theme_options[big_post_id]',
    ]);



    /* Blog Section */

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

    $wp_customize->add_setting('feminine_blog_theme_options[show_prefooter]', [
        'type' => 'option',
        'default' => true,
        'default' => $feminine_blog_options['show_prefooter'],
        'sanitize_callback' => 'feminine_blog_sanitize_checkbox',
    ]);

    $wp_customize->add_control('feminine_blog_theme_options[show_prefooter]', [
        'label' => esc_html__('Show Prefooter Section', 'feminine-blog'),
        'type' => 'Checkbox',
        'priority' => 1,
        'section' => 'prefooter_section',
    ]);

//copyright 
$wp_customize->add_section(
    'copyright_section',
    array(
        'title' => esc_html__( 'Copyright Section','feminine-blog' ),
        'panel'=>'theme_options',
        'capability'=>'edit_theme_options',
    )
);

$wp_customize->add_setting('feminine_blog_theme_options[copyright_text]', [
    'type' => 'option',
    'sanitize_callback' => 'sanitize_text_field',
]);
$wp_customize->add_control('copyright_text', [
    'label' => esc_html__('Enter Copyright Text', 'feminine-blog'),
    'type' => 'text',
    'section' => 'copyright_section',
    'settings' => 'feminine_blog_theme_options[copyright_text]',
]);

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

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

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

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