<?php
/**
 * Frontpage Customizer Settings
 *
 * @package Early News
 *
 * Banner Section
 */

$wp_customize->add_section(
	'early_news_banner_section',
	array(
		'title' => esc_html__( 'Banner Section', 'early-news' ),
		'panel' => 'early_news_frontpage_panel',
	)
);

// Banner section enable settings.
$wp_customize->add_setting(
	'early_news_banner_section_enable',
	array(
		'default'           => false,
		'sanitize_callback' => 'early_news_sanitize_checkbox',
	)
);
$wp_customize->add_control(
	new Early_News_Toggle_Checkbox_Custom_control(
		$wp_customize,
		'early_news_banner_section_enable',
		array(
			'label'    => esc_html__( 'Enable Banner Section', 'early-news' ),
			'type'     => 'checkbox',
			'settings' => 'early_news_banner_section_enable',
			'section'  => 'early_news_banner_section',
		)
	)
);

// Banner Editor Choice Sub Heading.
$wp_customize->add_setting(
	'early_news_banner_editor_choice_sub_heading',
	array(
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	new Early_News_Section_Sub_Heading_Control(
		$wp_customize,
		'early_news_banner_editor_choice_sub_heading',
		array(
			'label'           => esc_html__( 'Banner Editor Choice Section', 'early-news' ),
			'settings'        => 'early_news_banner_editor_choice_sub_heading',
			'section'         => 'early_news_banner_section',
			'active_callback' => 'early_news_if_banner_enabled',
		)
	)
);

// Banner Editor Choice title settings.
$wp_customize->add_setting(
	'early_news_banner_editor_choice_title',
	array(
		'default'           => __( 'Editor Choice', 'early-news' ),
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	'early_news_banner_editor_choice_title',
	array(
		'label'           => esc_html__( 'Section Title', 'early-news' ),
		'section'         => 'early_news_banner_section',
		'active_callback' => 'early_news_if_banner_enabled',
	)
);

// Abort if selective refresh is not available.
if ( isset( $wp_customize->selective_refresh ) ) {
	$wp_customize->selective_refresh->add_partial(
		'early_news_banner_editor_choice_title',
		array(
			'selector'            => '.recentpost h3.section-title',
			'settings'            => 'early_news_banner_editor_choice_title',
			'container_inclusive' => false,
			'fallback_refresh'    => true,
			'render_callback'     => 'early_news_banner_editor_choice_title_text_partial',
		)
	);
}

// View All button label setting.
$wp_customize->add_setting(
	'early_news_banner_editor_choice_view_all_button_label',
	array(
		'default'           => __( 'View All', 'early-news' ),
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	'early_news_banner_editor_choice_view_all_button_label',
	array(
		'label'           => esc_html__( 'View All Button Label', 'early-news' ),
		'section'         => 'early_news_banner_section',
		'settings'        => 'early_news_banner_editor_choice_view_all_button_label',
		'type'            => 'text',
		'active_callback' => 'early_news_if_banner_enabled',
	)
);

// View All button URL setting.
$wp_customize->add_setting(
	'early_news_banner_editor_choice_view_all_button_url',
	array(
		'default'           => '#',
		'sanitize_callback' => 'esc_url_raw',
	)
);

$wp_customize->add_control(
	'early_news_banner_editor_choice_view_all_button_url',
	array(
		'label'           => esc_html__( 'View All Button Link', 'early-news' ),
		'section'         => 'early_news_banner_section',
		'settings'        => 'early_news_banner_editor_choice_view_all_button_url',
		'type'            => 'url',
		'active_callback' => 'early_news_if_banner_enabled',
	)
);

// banner content type settings.
$wp_customize->add_setting(
	'early_news_banner_editor_choice_content_type',
	array(
		'default'           => 'post',
		'sanitize_callback' => 'early_news_sanitize_select',
	)
);

$wp_customize->add_control(
	'early_news_banner_editor_choice_content_type',
	array(
		'label'           => esc_html__( 'Content type:', 'early-news' ),
		'description'     => esc_html__( 'Choose where you want to render the content from.', 'early-news' ),
		'section'         => 'early_news_banner_section',
		'type'            => 'select',
		'active_callback' => 'early_news_if_banner_enabled',
		'choices'         => array(
			'post'     => esc_html__( 'Post', 'early-news' ),
			'category' => esc_html__( 'Category', 'early-news' ),
		),
	)
);

for ( $i = 1; $i <= 5; $i++ ) {
	// banner post setting.
	$wp_customize->add_setting(
		'early_news_banner_editor_choice_post_' . $i,
		array(
			'sanitize_callback' => 'early_news_sanitize_dropdown_pages',
		)
	);

	$wp_customize->add_control(
		'early_news_banner_editor_choice_post_' . $i,
		array(
			'label'           => sprintf( esc_html__( 'Post %d', 'early-news' ), $i ),
			'section'         => 'early_news_banner_section',
			'type'            => 'select',
			'choices'         => early_news_get_post_choices(),
			'active_callback' => 'early_news_banner_editor_choice_content_type_post_enabled',
		)
	);

}

// banner category setting.
$wp_customize->add_setting(
	'early_news_banner_editor_choice_category',
	array(
		'sanitize_callback' => 'early_news_sanitize_select',
	)
);

$wp_customize->add_control(
	'early_news_banner_editor_choice_category',
	array(
		'label'           => esc_html__( 'Category', 'early-news' ),
		'section'         => 'early_news_banner_section',
		'type'            => 'select',
		'choices'         => early_news_get_post_cat_choices(),
		'active_callback' => 'early_news_banner_editor_choice_content_type_category_enabled',
	)
);

// Banner Posts Sub Heading.
$wp_customize->add_setting(
	'early_news_banner_posts_sub_heading',
	array(
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	new Early_News_Section_Sub_Heading_Control(
		$wp_customize,
		'early_news_banner_posts_sub_heading',
		array(
			'label'           => esc_html__( 'Banner Posts Section', 'early-news' ),
			'settings'        => 'early_news_banner_posts_sub_heading',
			'section'         => 'early_news_banner_section',
			'active_callback' => 'early_news_if_banner_enabled',
		)
	)
);

// banner content type settings.
$wp_customize->add_setting(
	'early_news_banner_posts_content_type',
	array(
		'default'           => 'post',
		'sanitize_callback' => 'early_news_sanitize_select',
	)
);

$wp_customize->add_control(
	'early_news_banner_posts_content_type',
	array(
		'label'           => esc_html__( 'Content type:', 'early-news' ),
		'description'     => esc_html__( 'Choose where you want to render the content from.', 'early-news' ),
		'section'         => 'early_news_banner_section',
		'type'            => 'select',
		'active_callback' => 'early_news_if_banner_enabled',
		'choices'         => array(
			'post'     => esc_html__( 'Post', 'early-news' ),
			'category' => esc_html__( 'Category', 'early-news' ),
		),
	)
);

for ( $i = 1; $i <= 3; $i++ ) {
	// banner post setting.
	$wp_customize->add_setting(
		'early_news_banner_posts_post_' . $i,
		array(
			'sanitize_callback' => 'early_news_sanitize_dropdown_pages',
		)
	);

	$wp_customize->add_control(
		'early_news_banner_posts_post_' . $i,
		array(
			'label'           => sprintf( esc_html__( 'Post %d', 'early-news' ), $i ),
			'section'         => 'early_news_banner_section',
			'type'            => 'select',
			'choices'         => early_news_get_post_choices(),
			'active_callback' => 'early_news_banner_posts_content_type_post_enabled',
		)
	);

}

// banner category setting.
$wp_customize->add_setting(
	'early_news_banner_posts_category',
	array(
		'sanitize_callback' => 'early_news_sanitize_select',
	)
);

$wp_customize->add_control(
	'early_news_banner_posts_category',
	array(
		'label'           => esc_html__( 'Category', 'early-news' ),
		'section'         => 'early_news_banner_section',
		'type'            => 'select',
		'choices'         => early_news_get_post_cat_choices(),
		'active_callback' => 'early_news_banner_posts_content_type_category_enabled',
	)
);

/*========================Active Callback==============================*/
function early_news_if_banner_enabled( $control ) {
	return $control->manager->get_setting( 'early_news_banner_section_enable' )->value();
}
//Banner Editor Choice
function early_news_banner_editor_choice_content_type_post_enabled( $control ) {
	$content_type = $control->manager->get_setting( 'early_news_banner_editor_choice_content_type' )->value();
	return early_news_if_banner_enabled( $control ) && ( 'post' === $content_type );
}
function early_news_banner_editor_choice_content_type_category_enabled( $control ) {
	$content_type = $control->manager->get_setting( 'early_news_banner_editor_choice_content_type' )->value();
	return early_news_if_banner_enabled( $control ) && ( 'category' === $content_type );
}
//Banner Posts
function early_news_banner_posts_content_type_post_enabled( $control ) {
	$content_type = $control->manager->get_setting( 'early_news_banner_posts_content_type' )->value();
	return early_news_if_banner_enabled( $control ) && ( 'post' === $content_type );
}
function early_news_banner_posts_content_type_category_enabled( $control ) {
	$content_type = $control->manager->get_setting( 'early_news_banner_posts_content_type' )->value();
	return early_news_if_banner_enabled( $control ) && ( 'category' === $content_type );
}
