<?php
/**
 * Adore Themes Customizer
 *
 * @package Hot News
 *
 * Breaking News Section
 */

$wp_customize->add_section(
	'hot_news_breaking_news_section',
	array(
		'title'     => esc_html__( 'Breaking News Section', 'hot-news' ),
		'panel'    => 'hot_news_frontpage_panel',
		'priority' => 10
	)
);

// Breaking News section enable settings.
$wp_customize->add_setting(
	'hot_news_breaking_news_section_enable',
	array(
		'default'           => false,
		'sanitize_callback' => 'hot_news_sanitize_checkbox',
	)
);

$wp_customize->add_control(
	new Hot_News_Toggle_Checkbox_Custom_control(
		$wp_customize,
		'hot_news_breaking_news_section_enable',
		array(
			'label'    => esc_html__( 'Enable Breaking News Section', 'hot-news' ),
			'type'     => 'checkbox',
			'settings' => 'hot_news_breaking_news_section_enable',
			'section'  => 'hot_news_breaking_news_section',
		)
	)
);

// Breaking News title settings.
$wp_customize->add_setting(
	'hot_news_breaking_news_title',
	array(
		'default'           => __( 'Breaking News', 'hot-news' ),
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	'hot_news_breaking_news_title',
	array(
		'label'           => esc_html__( 'Title', 'hot-news' ),
		'section'         => 'hot_news_breaking_news_section',
		'active_callback' => 'hot_news_if_breaking_news_enabled',
	)
);

// Abort if selective refresh is not available.
if ( isset( $wp_customize->selective_refresh ) ) {
	$wp_customize->selective_refresh->add_partial(
		'hot_news_breaking_news_title',
		array(
			'selector'            => '.news-ticker-section .theme-wrapper',
			'settings'            => 'hot_news_breaking_news_title',
			'container_inclusive' => false,
			'fallback_refresh'    => true,
			'render_callback'     => 'hot_news_breaking_news_title_text_partial',
		)
	);
}

// Breaking News content type settings.
$wp_customize->add_setting(
	'hot_news_breaking_news_content_type',
	array(
		'default'           => 'post',
		'sanitize_callback' => 'hot_news_sanitize_select',
	)
);

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

for ( $i = 1; $i <= 5; $i++ ) {
	// Breaking News post setting.
	$wp_customize->add_setting(
		'hot_news_breaking_news_post_' . $i,
		array(
			'sanitize_callback' => 'hot_news_sanitize_dropdown_pages',
		)
	);

	$wp_customize->add_control(
		'hot_news_breaking_news_post_' . $i,
		array(
			'label'           => sprintf( esc_html__( 'Post %d', 'hot-news' ), $i ),
			'section'         => 'hot_news_breaking_news_section',
			'type'            => 'select',
			'choices'         => hot_news_get_post_choices(),
			'active_callback' => 'hot_news_breaking_news_section_content_type_post_enabled',
		)
	);

}

// Breaking News category setting.
$wp_customize->add_setting(
	'hot_news_breaking_news_category',
	array(
		'sanitize_callback' => 'hot_news_sanitize_select',
	)
);

$wp_customize->add_control(
	'hot_news_breaking_news_category',
	array(
		'label'           => esc_html__( 'Category', 'hot-news' ),
		'section'         => 'hot_news_breaking_news_section',
		'type'            => 'select',
		'choices'         => hot_news_get_post_cat_choices(),
		'active_callback' => 'hot_news_breaking_news_section_content_type_category_enabled',
	)
);

/*========================Active Callback==============================*/
function hot_news_if_breaking_news_enabled( $control ) {
	return $control->manager->get_setting( 'hot_news_breaking_news_section_enable' )->value();
}
function hot_news_breaking_news_section_content_type_post_enabled( $control ) {
	$content_type = $control->manager->get_setting( 'hot_news_breaking_news_content_type' )->value();
	return hot_news_if_breaking_news_enabled( $control ) && ( 'post' === $content_type );
}
function hot_news_breaking_news_section_content_type_category_enabled( $control ) {
	$content_type = $control->manager->get_setting( 'hot_news_breaking_news_content_type' )->value();
	return hot_news_if_breaking_news_enabled( $control ) && ( 'category' === $content_type );
}

/*========================Partial Refresh==============================*/
if ( ! function_exists( 'hot_news_breaking_news_title_text_partial' ) ) :
	// Title.
	function hot_news_breaking_news_title_text_partial() {
		return esc_html( get_theme_mod( 'hot_news_breaking_news_title' ) );
	}
endif;
