<?php
/**
 * Latest Posts options
 *
 * @package Theme Palace
 * @subpackage Charity Give
 * @since Charity Give 1.0.0
 */

// Add Latest Posts
$wp_customize->add_section( 'charity_give_latest_posts_section', array(
	'title'             => esc_html__( 'Latest Posts','charity-give' ),
	'description'       => esc_html__( 'Latest Posts options.', 'charity-give' ),
	'panel'             => 'charity_give_front_page_panel',
	) );

// Hero content enable control and setting
$wp_customize->add_setting( 'charity_give_theme_options[latest_posts_section_enable]', array(
	'default'			=> 	$options['latest_posts_section_enable'],
	'sanitize_callback' => 'charity_give_sanitize_switch_control',
	) );

$wp_customize->add_control( new Charity_Give_Switch_Control( $wp_customize, 'charity_give_theme_options[latest_posts_section_enable]', array(
	'label'             => esc_html__( 'Latest Posts Enable', 'charity-give' ),
	'section'           => 'charity_give_latest_posts_section',
	'on_off_label' 		=> charity_give_switch_options(),
	) ) );

if ( isset( $wp_customize->selective_refresh ) ) {
    $wp_customize->selective_refresh->add_partial( 'charity_give_theme_options[latest_posts_section_enable]', array(
		'selector'            => '#charity_give_latest_posts_section .tooltiptext',
		'settings'            => 'charity_give_theme_options[latest_posts_section_enable]',
    ) );
}

// latest_post title setting and control
$wp_customize->add_setting( 'charity_give_theme_options[latest_posts_title]',
    array(
        'sanitize_callback' => 'sanitize_text_field',
        'default'           => $options['latest_posts_title'],
        'transport'         => 'postMessage',
    )
);

$wp_customize->add_control( 'charity_give_theme_options[latest_posts_title]',
    array(
        'label'             => esc_html__( 'Section Title', 'charity-give' ),
        'section'           => 'charity_give_latest_posts_section',
        'active_callback'   => 'charity_give_is_latest_posts_section_enable',
        'type'              => 'text',
    )
);

// Abort if selective refresh is not available.
if ( isset( $wp_customize->selective_refresh ) ) {
    $wp_customize->selective_refresh->add_partial( 'charity_give_theme_options[latest_posts_title]',
        array(
            'selector'            => '#charity_give_latest_posts_section div.section-header h2',
            'settings'            => 'charity_give_theme_options[latest_posts_title]',
            'container_inclusive' => false,
            'fallback_refresh'    => true,
            'render_callback'     => 'charity_give_latest_posts_title_partial',
        )
    );
}

// latest_post title setting and control
$wp_customize->add_setting( 'charity_give_theme_options[latest_posts_sub_title]',
    array(
        'sanitize_callback' => 'sanitize_text_field',
        'default'           => $options['latest_posts_sub_title'],
        'transport'         => 'postMessage',
    )
);

$wp_customize->add_control( 'charity_give_theme_options[latest_posts_sub_title]',
    array(
        'label'             => esc_html__( 'Section Sub Title', 'charity-give' ),
        'section'           => 'charity_give_latest_posts_section',
        'active_callback'   => 'charity_give_is_latest_posts_section_enable',
        'type'              => 'text',
    )
);

// Abort if selective refresh is not available.
if ( isset( $wp_customize->selective_refresh ) ) {
    $wp_customize->selective_refresh->add_partial( 'charity_give_theme_options[latest_posts_sub_title]',
        array(
            'selector'            => '#charity_give_latest_posts_section .section-subtitle',
            'settings'            => 'charity_give_theme_options[latest_posts_sub_title]',
            'container_inclusive' => false,
            'fallback_refresh'    => true,
            'render_callback'     => 'charity_give_latest_posts_sub_title_partial',
        )
    );
}

$wp_customize->add_setting( 'charity_give_theme_options[latest_posts_excerpt_length]', array(
	'sanitize_callback' => 'charity_give_sanitize_number_range',
	'validate_callback' => 'charity_give_validate_long_excerpt',
	'default'			=> $options['latest_posts_excerpt_length'],
	) );

$wp_customize->add_control( 'charity_give_theme_options[latest_posts_excerpt_length]', array(
	'label'       		=> esc_html__( 'Excerpt Length', 'charity-give' ),
	'description' 		=> esc_html__( 'Total description words to be displayed in lastest posts.', 'charity-give' ),
	'section'     		=> 'charity_give_latest_posts_section',
	'active_callback' 	=> 'charity_give_is_latest_posts_section_enable',
	'type'        		=> 'number',
	'input_attrs' 		=> array(
		'style'       => 'width: 80px;',
		'max'         => 100,
		'min'         => 5,
		),
	) );

// weekly_special title setting and control
$wp_customize->add_setting( 'charity_give_theme_options[latest_posts_btn]', array(
	'sanitize_callback' => 'sanitize_text_field',
	'default'			=> $options['latest_posts_btn'],
	'transport'			=> 'postMessage',
	) );

$wp_customize->add_control( 'charity_give_theme_options[latest_posts_btn]', array(
	'label'           	=> esc_html__( 'Button Label', 'charity-give' ),
	'section'        	=> 'charity_give_latest_posts_section',
	'active_callback' 	=> 'charity_give_is_latest_posts_section_enable',
	'type'				=> 'text',
	) );

// Abort if selective refresh is not available.
if ( isset( $wp_customize->selective_refresh ) ) {
	$wp_customize->selective_refresh->add_partial( 'charity_give_theme_options[latest_posts_btn]', array(
		'selector'            => '#charity_give_latest_posts_section .read-more .btn',
		'settings'            => 'charity_give_theme_options[latest_posts_btn]',
		'container_inclusive' => false,
		'fallback_refresh'    => true,
		'render_callback'     => 'charity_give_latest_posts_btn_partial',
		) );
}

for ( $i = 1; $i <= 3; $i++ ) :

// hero posts drop down chooser control and setting
$wp_customize->add_setting( 'charity_give_theme_options[latest_posts_content_post_' . $i . ']', array(
	'sanitize_callback' => 'charity_give_sanitize_page',
	) );

$wp_customize->add_control( new Charity_Give_Dropdown_Chooser( $wp_customize, 'charity_give_theme_options[latest_posts_content_post_' . $i . ']', array(
	'label'             => sprintf( esc_html__( 'Select Post %d', 'charity-give' ), $i ),
	'section'           => 'charity_give_latest_posts_section',
	'choices'			=> charity_give_post_choices(),
	'active_callback'	=> 'charity_give_is_latest_posts_section_enable',
	) ) );
endfor;