<?php
/**
 * Traveling Theme Customizer
 *
 * @package traveling
 */

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

    $food_travel_blog_options = food_travel_blog_theme_options();

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

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

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

    $wp_customize->add_setting('food_travel_blog_theme_options[facebook]', [
        'type' => 'option',
        'default' => $food_travel_blog_options['facebook'],
        'sanitize_callback' => 'food_travel_blog_sanitize_url',
    ]);
    $wp_customize->add_control('food_travel_blog_theme_options[facebook]', [
        'label' => esc_html__('Facebook Link', 'food-travel-blog'),
        'type' => 'url',
        'section' => 'header_section',
        'settings' => 'food_travel_blog_theme_options[facebook]',
    ]);

    $wp_customize->add_setting('food_travel_blog_theme_options[pinterest]', [
        'type' => 'option',
        'default' => $food_travel_blog_options['pinterest'],
        'sanitize_callback' => 'food_travel_blog_sanitize_url',
    ]);
    $wp_customize->add_control('food_travel_blog_theme_options[pinterest]', [
        'label' => esc_html__('pinterest Link', 'food-travel-blog'),
        'type' => 'url',
        'section' => 'header_section',
        'settings' => 'food_travel_blog_theme_options[pinterest]',
    ]);

    $wp_customize->add_setting('food_travel_blog_theme_options[twitter]', [
        'type' => 'option',
        'default' => $food_travel_blog_options['twitter'],
        'sanitize_callback' => 'food_travel_blog_sanitize_url',
    ]);
    $wp_customize->add_control('food_travel_blog_theme_options[twitter]', [
        'label' => esc_html__('Twitter Link', 'food-travel-blog'),
        'type' => 'url',
        'section' => 'header_section',
        'settings' => 'food_travel_blog_theme_options[twitter]',
    ]);



    $wp_customize->add_section('main_banner_section', [
        'title' => esc_html__('Banner Section', 'food-travel-blog'),
        'panel' => 'theme_options',
        'capability' => 'edit_theme_options',
    ]);
    $wp_customize->add_setting(
        'food_travel_blog_theme_options[main_banner_category]',
        [
            'type' => 'option',
            'sanitize_callback' => 'food_travel_blog_sanitize_select',
            'default' => $food_travel_blog_options['main_banner_category'],
        ]
    );

    $wp_customize->add_control(
        'food_travel_blog_theme_options[main_banner_category]',
        [
            'section' => 'main_banner_section',
            'type' => 'select',
            'choices' => food_travel_blog_get_categories_select(),
            'label' => esc_html__('Select Category to show Posts', 'food-travel-blog'),
            'description' => esc_html__(
                'Max 4 Posts will be shown from the selected Category in Free Version',
                'food-travel-blog'
            ),
            'settings' => 'food_travel_blog_theme_options[main_banner_category]',
            'priority' => 1,
        ]
    );


    //radio box sanitization function
    function food_travel_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('grid_layout_section', [
        'title' => esc_html__('Grid Blog Section', 'food-travel-blog'),
        'panel' => 'theme_options',
        'capability' => 'edit_theme_options',
    ]);

    $wp_customize->add_setting('food_travel_blog_theme_options[grid_layout_section_title]',
    array(
        'type' => 'option',
        'sanitize_callback' => 'sanitize_text_field',
    )
);
$wp_customize->add_control('grid_layout_section_title',
    array(
        'label' => esc_html__('Section Title', 'food-travel-blog'),
        'type' => 'text',
        'section' => 'grid_layout_section',
        'settings' => 'food_travel_blog_theme_options[grid_layout_section_title]',
    )
);

$wp_customize->add_setting('food_travel_blog_theme_options[three_no_of_posts]',
array(
    'type' => 'option',
    'sanitize_callback' => 'sanitize_text_field',
)
);
$wp_customize->add_control('three_no_of_posts',
array(
    'label' => esc_html__('No of Posts to show (Default 6)', 'food-travel-blog'),
    'type' => 'text',
    'section' => 'grid_layout_section',
    'settings' => 'food_travel_blog_theme_options[three_no_of_posts]',
)
);
    $wp_customize->add_setting('food_travel_blog_theme_options[show_grid_layout_blogs]', [
        'type' => 'option',
        'default' => true,
        'default' => $food_travel_blog_options['show_grid_layout_blogs'],
        'sanitize_callback' => 'food_travel_blog_sanitize_checkbox',
    ]);

    $wp_customize->add_control('food_travel_blog_theme_options[show_grid_layout_blogs]', [
        'label' => esc_html__('Three Grid Blog Section', 'food-travel-blog'),
        'type' => 'Checkbox',
        'priority' => 1,
        'section' => 'grid_layout_section',
    ]);

    $wp_customize->add_setting(
        'food_travel_blog_theme_options[grid_layout_blog_category]',
        [
            'type' => 'option',
            'sanitize_callback' => 'food_travel_blog_sanitize_select',
            'default' => $food_travel_blog_options['grid_layout_blog_category'],
        ]
    );

    $wp_customize->add_control(
        'food_travel_blog_theme_options[grid_layout_blog_category]',
        [
            'section' => 'grid_layout_section',
            'type' => 'select',
            'choices' => food_travel_blog_get_categories_select(),
            'label' => esc_html__('Select Category to show Posts', 'food-travel-blog'),
            'settings' => 'food_travel_blog_theme_options[grid_layout_blog_category]',
            'priority' => 1,
        ]
    );

    $wp_customize->add_setting( 'food_travel_blog_theme_options[grid_layout]', array(
		'capability' => 'edit_theme_options',
		'default' => '3',
		'sanitize_callback' => 'food_travel_blog_sanitize_radio',
		'type' => 'option',
  ) );

  $wp_customize->add_control( 'food_travel_blog_theme_options[grid_layout]', array(
		'type' => 'radio',
		'section' => 'grid_layout_section',
		'label' => esc_attr( __('Choose Column', 'food-travel-blog') ),
		'choices' => array(
		  '3' => esc_attr( __('3 Column', 'food-travel-blog') ),
		  '4' => esc_attr( __('4 Column', 'food-travel-blog') ),
		),
	  ) );


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







    $wp_customize->add_section('green_blog_carousel_section', [
        'title' => esc_html__('Middle Blog Carousel Section', 'food-travel-blog'),
        'panel' => 'theme_options',
        'capability' => 'edit_theme_options',
    ]);
    $wp_customize->add_setting('food_travel_blog_theme_options[green_blog_section_title]',
    array(
        'type' => 'option',
        'sanitize_callback' => 'sanitize_text_field',
    )
);
$wp_customize->add_control('green_blog_section_title',
    array(
        'label' => esc_html__('Section Title', 'food-travel-blog'),
        'type' => 'text',
        'section' => 'green_blog_carousel_section',
        'settings' => 'food_travel_blog_theme_options[green_blog_section_title]',
    )
);
    $wp_customize->add_setting('food_travel_blog_theme_options[show_green_blog_carousel]', [
        'type' => 'option',
        'default' => true,
        'default' => $food_travel_blog_options['show_green_blog_carousel'],
        'sanitize_callback' => 'food_travel_blog_sanitize_checkbox',
    ]);

    $wp_customize->add_control('food_travel_blog_theme_options[show_green_blog_carousel]', [
        'label' => esc_html__('Middle Blog Section', 'food-travel-blog'),
        'type' => 'Checkbox',
        'priority' => 1,
        'section' => 'green_blog_carousel_section',
    ]);
    $wp_customize->add_setting(
        'food_travel_blog_theme_options[green_blog_carousel]',
        [
            'type' => 'option',
            'sanitize_callback' => 'food_travel_blog_sanitize_select',
            'default' => $food_travel_blog_options['green_blog_carousel'],
        ]
    );

    $wp_customize->add_control(
        'food_travel_blog_theme_options[green_blog_carousel]',
        [
            'section' => 'green_blog_carousel_section',
            'type' => 'select',
            'choices' => food_travel_blog_get_categories_select(),
            'label' => esc_html__('Select Category to show Posts', 'food-travel-blog'),
            'description' => esc_html__(
                'Max 4 Posts will be shown from the chosen category in Free Version and Carousel only available in Premium Version',
                'food-travel-blog'
            ),
            'settings' => 'food_travel_blog_theme_options[green_blog_carousel]',
            'priority' => 1,
        ]
    );




    $wp_customize->add_section('two_column_blog', [
        'title' => esc_html__('Two Column Blog Section', 'food-travel-blog'),
        'panel' => 'theme_options',
        'capability' => 'edit_theme_options',
    ]);
    $wp_customize->add_setting('food_travel_blog_theme_options[two_column_section_title]',
    array(
        'type' => 'option',
        'sanitize_callback' => 'sanitize_text_field',
    )
);
$wp_customize->add_control('two_column_section_title',
    array(
        'label' => esc_html__('Section Title', 'food-travel-blog'),
        'type' => 'text',
        'section' => 'two_column_blog',
        'settings' => 'food_travel_blog_theme_options[two_column_section_title]',
    )
);


$wp_customize->add_setting(
    'food_travel_blog_theme_options[two_column_blogs_category]',
    [
        'type' => 'option',
        'sanitize_callback' => 'food_travel_blog_sanitize_select',
        'default' => $food_travel_blog_options['two_column_blogs_category'],
    ]
);

$wp_customize->add_control(
    'food_travel_blog_theme_options[two_column_blogs_category]',
    [
        'section' => 'two_column_blog',
        'type' => 'select',
        'choices' => food_travel_blog_get_categories_select(),
        'label' => esc_html__('Select Category to show Posts', 'food-travel-blog'),

        'settings' => 'food_travel_blog_theme_options[two_column_blogs_category]',
        'priority' => 1,
    ]
);
$wp_customize->add_setting('food_travel_blog_theme_options[two_no_of_posts]',
array(
    'type' => 'option',
    'sanitize_callback' => 'sanitize_text_field',
)
);
$wp_customize->add_control('two_no_of_posts',
array(
    'label' => esc_html__('No of Posts to show (Default 6)', 'food-travel-blog'),
    'type' => 'text',
    'section' => 'two_column_blog',
    'settings' => 'food_travel_blog_theme_options[two_no_of_posts]',
)
);





    $wp_customize->add_section('prefooter_section', [
        'title' => esc_html__('Prefooter Section', 'food-travel-blog'),

        'panel' => 'theme_options',
        'capability' => 'edit_theme_options',
    ]);

    $wp_customize->add_setting('food_travel_blog_theme_options[show_prefooter]', [
        'type' => 'option',
        'default' => true,
        'default' => $food_travel_blog_options['show_prefooter'],
        'sanitize_callback' => 'food_travel_blog_sanitize_checkbox',
    ]);

    $wp_customize->add_control('food_travel_blog_theme_options[show_prefooter]', [
        'label' => esc_html__('Show Prefooter Section', 'food-travel-blog'),
        'description' => esc_html__(
            'Copyright text can be changed in Premium Version only',
            'food-travel-blog'
        ),
        'type' => 'Checkbox',
        'priority' => 1,
        'section' => 'prefooter_section',
    ]);


    $wp_customize->add_section('single_page_section', [
        'title' => esc_html__('Article Page Option', 'food-travel-blog'),
        'panel' => 'theme_options',
        'capability' => 'edit_theme_options',
    ]);

    $wp_customize->add_setting('food_travel_blog_theme_options[show_sidebar]', [
        'type' => 'option',
        'default' => true,
        'default' => $food_travel_blog_options['show_sidebar'],
        'sanitize_callback' => 'food_travel_blog_sanitize_checkbox',
    ]);

    $wp_customize->add_control('food_travel_blog_theme_options[show_sidebar]', [
        'label' => esc_html__('Show Sidebar in Single Article Page', 'food-travel-blog'),
        'type' => 'Checkbox',
        'priority' => 1,
        'section' => 'single_page_section',
    ]);
}
add_action('customize_register', 'food_travel_blog_customize_register');

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

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

/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 */
function food_travel_blog_customize_preview_js() {
	wp_enqueue_script( 'food-travel-blog-customizer', get_template_directory_uri() . '/js/customizer.js', array( 'customize-preview' ), _S_VERSION, true );
}
add_action( 'customize_preview_init', 'food_travel_blog_customize_preview_js' );
