<?php
/**
* Related Posts Section options
*
* @package Theme Palace
* @subpackage Power News
* @since Power News 1.0.0
*/

// Add Related Posts section
$wp_customize->add_section( 'power_news_related_posts_section', array(
	'title'             => esc_html__( 'Related Posts','power-news' ),
	'description'       => esc_html__( 'Related Posts Section options.', 'power-news' ),
	'panel'             => 'power_news_front_page_panel',
) );

// Related Posts content enable control and setting
$wp_customize->add_setting( 'power_news_theme_options[related_posts_section_enable]', array(
	'default'			=> 	$options['related_posts_section_enable'],
	'sanitize_callback' => 'power_news_sanitize_switch_control',
	) );

$wp_customize->add_control( new Power_News_Switch_Control( $wp_customize, 'power_news_theme_options[related_posts_section_enable]', array(
	'label'             => esc_html__( 'Related Posts Section Enable', 'power-news' ),
	'section'           => 'power_news_related_posts_section',
	'on_off_label' 		=> power_news_switch_options(),
	) ) );

// Abort if selective refresh is not available.
if ( isset( $wp_customize->selective_refresh ) ) {
    $wp_customize->selective_refresh->add_partial( 'power_news_theme_options[related_posts_section_enable]', array(
		'selector'      => '.related-posts-section .tooltiptext',
		'settings'      => 'power_news_theme_options[related_posts_section_enable]',
    ) );
}

// related posts title setting and control
$wp_customize->add_setting( 'power_news_theme_options[related_posts_title]', array(
	'sanitize_callback' => 'sanitize_text_field',
	'default'			=> $options['related_posts_title'],
	'transport'			=> 'postMessage',
	) );

$wp_customize->add_control( 'power_news_theme_options[related_posts_title]', array(
	'label'           	=> esc_html__( 'Title', 'power-news' ),
	'section'        	=> 'power_news_related_posts_section',
	'active_callback' 	=> 'power_news_is_related_posts_section_enable',
	'type'				=> 'text',
	) );

// Abort if selective refresh is not available.
if ( isset( $wp_customize->selective_refresh ) ) {
	$wp_customize->selective_refresh->add_partial( 'power_news_theme_options[related_posts_title]', array(
		'selector'            => '#power_news_related_posts_section .section-header .section-title',
		'settings'            => 'power_news_theme_options[related_posts_title]',
		'container_inclusive' => false,
		'fallback_refresh'    => true,
		'render_callback'     => 'power_news_related_posts_title_partial',
		) );
}

// Related Posts number control and setting
$wp_customize->add_setting( 'power_news_theme_options[related_posts_count]', array(
	'default'          	=> $options['related_posts_count'],
	'sanitize_callback' => 'power_news_sanitize_number_range',
	'validate_callback' => 'power_news_validate_related_posts_count',
	) );

$wp_customize->add_control( 'power_news_theme_options[related_posts_count]', array(
	'label'             => esc_html__( 'Number of Posts', 'power-news' ),
	'description'       => esc_html__( 'Note: Min 3 & Max 10. Please input the valid number and save. Then refresh the page to see the change.', 'power-news' ),
	'section'           => 'power_news_related_posts_section',
	'active_callback'   => 'power_news_is_related_posts_section_enable',
	'type'				=> 'number',
	'input_attrs'		=> array(
		'min'	=> 3,
		'max'	=> 10,
		'style' => 'width: 100px;'
		),
	) );

// Related Posts content type control and setting
$wp_customize->add_setting( 'power_news_theme_options[related_posts_content_type]', array(
	'default'          	=> $options['related_posts_content_type'],
	'sanitize_callback' => 'power_news_sanitize_select',
	) );

$wp_customize->add_control( 'power_news_theme_options[related_posts_content_type]', array(
	'label'             => esc_html__( 'Content Type', 'power-news' ),
	'section'           => 'power_news_related_posts_section',
	'type'				=> 'select',
	'active_callback' 	=> 'power_news_is_related_posts_section_enable',
	'choices'			=> array( 
		'post' 		=> esc_html__( 'Post', 'power-news' ),
		'category' 	=> esc_html__( 'Category', 'power-news' ),
		),
	) );

for ( $i = 1; $i <= 4; $i++ ) :
// related posts posts drop down chooser control and setting
$wp_customize->add_setting( 'power_news_theme_options[related_posts_content_post_' . $i . ']', array(
	'sanitize_callback' => 'power_news_sanitize_page',
	) );

$wp_customize->add_control( new Power_News_Dropdown_Chooser( $wp_customize, 'power_news_theme_options[related_posts_content_post_' . $i . ']', array(
	'label'             => sprintf( esc_html__( 'Select Post %d', 'power-news' ), $i ),
	'section'           => 'power_news_related_posts_section',
	'choices'			=> power_news_post_choices(),
	'active_callback'	=> 'power_news_is_related_posts_section_content_post_enable',
	) ) );
endfor;

// Add dropdown category setting and control.
$wp_customize->add_setting(  'power_news_theme_options[related_posts_content_category]', array(
	'sanitize_callback' => 'power_news_sanitize_single_category',
	) ) ;

$wp_customize->add_control( new Power_News_Dropdown_Taxonomies_Control( $wp_customize,'power_news_theme_options[related_posts_content_category]', array(
	'label'             => esc_html__( 'Select Category', 'power-news' ),
	'description'      	=> esc_html__( 'Note: Latest selected no of posts will be shown from selected category', 'power-news' ),
	'section'           => 'power_news_related_posts_section',
	'type'              => 'dropdown-taxonomies',
	'active_callback'	=> 'power_news_is_related_posts_section_content_category_enable'
	) ) );