<?php
/**
 * NewsBox Plus Theme Customizer
 *
 * @package NewsBox Plus
 */

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

	//select sanitization function
	function newsbox_plus_sanitize_select( $input, $setting ){
		$input = sanitize_key($input);
		$choices = $setting->manager->get_control( $setting->id )->choices;
		return ( array_key_exists( $input, $choices ) ? $input : $setting->default );                
		  
	}

	$wp_customize->add_panel( 'newsbox_plus_settings', array(
		 'priority'       => 50,
		  'title'          => __('NewsBox Plus Theme settings', 'newsbox-plus'),
		  'description'    => __('All NewsBox Plus theme settings', 'newsbox-plus'),
		  ) );
    $wp_customize->add_section('newsbox_plus_header', array(
		'title' => __('NewsBox Plus Header Settings', 'newsbox-plus'),
		'capability'     => 'edit_theme_options',
		'description'     => __('NewsBox Plus theme header settings', 'newsbox-plus'),
		'panel'    => 'newsbox_plus_settings',
	
	));
	$wp_customize->add_setting('newsbox_plus_main_menu_position', array(
        'default'        => 'bottom',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'newsbox_plus_sanitize_select',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('newsbox_plus_main_menu_position', array(
        'label'      => __('Main Menu Position', 'newsbox-plus'),
        'description'=> __('You can set the menu top of the page or under logo. ', 'newsbox-plus'),
        'section'    => 'newsbox_plus_header',
        'settings'   => 'newsbox_plus_main_menu_position',
        'type'       => 'select',
        'choices'    => array(
            'top' => __('Top', 'newsbox-plus'),
            'bottom' => __('Bottom', 'newsbox-plus'),
        ),
    ));
	$wp_customize->add_setting('newsbox_plus_latest_news', array(
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'default'       =>  '1',
        'sanitize_callback' => 'absint',
         'transport' => 'refresh',
    ));
    $wp_customize->add_control('newsbox_plus_latest_news', array(
        'label'      => __('Show Latest News Ticker?', 'newsbox-plus'),
        'section'    => 'newsbox_plus_header',
        'settings'   => 'newsbox_plus_latest_news',
        'type'       => 'checkbox',
    ));
	//NewsBox PLus blog settings
    $wp_customize->add_section('newsbox_plus_blog', array(
		'title' => __('NewsBox Plus Blog Settings', 'newsbox-plus'),
		'capability'     => 'edit_theme_options',
		'description'     => __('NewsBox Plus theme blog settings', 'newsbox-plus'),
		'panel'    => 'newsbox_plus_settings',
	
	));
	$wp_customize->add_setting('newsbox_plus_blog_container', array(
        'default'        => 'container',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'newsbox_plus_sanitize_select',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('newsbox_plus_blog_container', array(
        'label'      => __('Container type', 'newsbox-plus'),
        'description'=> __('You can set standard container or full width container. ', 'newsbox-plus'),
        'section'    => 'newsbox_plus_blog',
        'settings'   => 'newsbox_plus_blog_container',
        'type'       => 'select',
        'choices'    => array(
            'container' => __('Standard Container', 'newsbox-plus'),
            'container-fluid' => __('Full width Container', 'newsbox-plus'),
        ),
    ));
    $wp_customize->add_setting('newsbox_plus_blog_layout', array(
        'default'        => 'rightside',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'newsbox_plus_sanitize_select',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('newsbox_plus_blog_layout', array(
        'label'      => __('Select Blog Layout', 'newsbox-plus'),
        'description'=> __('Right and Left sidebar only show when sidebar widget is available. ', 'newsbox-plus'),
        'section'    => 'newsbox_plus_blog',
        'settings'   => 'newsbox_plus_blog_layout',
        'type'       => 'select',
        'choices'    => array(
            'rightside' => __('Right Sidebar', 'newsbox-plus'),
            'leftside' => __('Left Sidebar', 'newsbox-plus'),
            'fullwidth' => __('No Sidebar', 'newsbox-plus'),
        ),
    ));
	$wp_customize->add_setting('newsbox_plus_blog_style', array(
        'default'        => 'grid',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'newsbox_plus_sanitize_select',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('newsbox_plus_blog_style', array(
        'label'      => __('Select Blog Style', 'newsbox-plus'),
        'section'    => 'newsbox_plus_blog',
        'settings'   => 'newsbox_plus_blog_style',
        'type'       => 'select',
        'choices'    => array(
            'grid' => __('Grid Style', 'newsbox-plus'),
            'list' => __('List Style', 'newsbox-plus'),
            'classic' => __('Classic Style', 'newsbox-plus'),
        ),
    ));
		//NewsBox Plus Plus page settings
		$wp_customize->add_section('newsbox_plus_page', array(
			'title' => __('NewsBox Plus Page Settings', 'newsbox-plus'),
			'capability'     => 'edit_theme_options',
			'description'     => __('NewsBox Plus theme blog settings', 'newsbox-plus'),
			'panel'    => 'newsbox_plus_settings',
		
		));
		$wp_customize->add_setting('newsbox_plus_page_container', array(
			'default'        => 'container',
			'capability'     => 'edit_theme_options',
			'type'           => 'theme_mod',
			'sanitize_callback' => 'newsbox_plus_sanitize_select',
			'transport' => 'refresh',
		));
		$wp_customize->add_control('newsbox_plus_page_container', array(
			'label'      => __('Page Container type', 'newsbox-plus'),
			'description'=> __('You can set standard container or full width container for page. ', 'newsbox-plus'),
			'section'    => 'newsbox_plus_page',
			'settings'   => 'newsbox_plus_page_container',
			'type'       => 'select',
			'choices'    => array(
				'container' => __('Standard Container', 'newsbox-plus'),
				'container-fluid' => __('Full width Container', 'newsbox-plus'),
			),
		));	
		$wp_customize->add_setting('newsbox_plus_page_header', array(
			'default'        => 'show',
			'capability'     => 'edit_theme_options',
			'type'           => 'theme_mod',
			'sanitize_callback' => 'newsbox_plus_sanitize_select',
			'transport' => 'refresh',
		));
		$wp_customize->add_control('newsbox_plus_page_header', array(
			'label'      => __('Show Page header', 'newsbox-plus'),
			'section'    => 'newsbox_plus_page',
			'settings'   => 'newsbox_plus_page_header',
			'type'       => 'select',
			'choices'    => array(
				'show' => __('Show all pages', 'newsbox-plus'),
				'hide-home' => __('Hide Only Front Page', 'newsbox-plus'),
				'hide' => __('Hide All Pages', 'newsbox-plus'),
			),
		));	




	if ( isset( $wp_customize->selective_refresh ) ) {
		$wp_customize->selective_refresh->add_partial(
			'blogname',
			array(
				'selector'        => '.site-title a',
				'render_callback' => 'newsbox_plus_customize_partial_blogname',
			)
		);
		$wp_customize->selective_refresh->add_partial(
			'blogdescription',
			array(
				'selector'        => '.site-description',
				'render_callback' => 'newsbox_plus_customize_partial_blogdescription',
			)
		);
	}
}
add_action( 'customize_register', 'newsbox_plus_customize_register' );

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

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

/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 */
function newsbox_plus_customize_preview_js() {
	wp_enqueue_script( 'newsbox-plus-customizer', get_template_directory_uri() . '/assets/js/customizer.js', array( 'customize-preview' ), NEWSBOX_PLUS_VERSION, true );
}
add_action( 'customize_preview_init', 'newsbox_plus_customize_preview_js' );
