<?php
/**
 * Adore Themes Customizer
 *
 * @package Social News
 *
 * Banner Section
 */

$wp_customize->add_section(
	'social_news_banner_section',
	array(
		'title' => esc_html__( 'Banner Section', 'social-news' ),
		'panel' => 'social_news_frontpage_panel',
	)
);

// Banner enable setting.
$wp_customize->add_setting(
	'social_news_banner_section_enable',
	array(
		'default'           => false,
		'sanitize_callback' => 'social_news_sanitize_checkbox',
	)
);

$wp_customize->add_control(
	new Social_News_Toggle_Checkbox_Custom_control(
		$wp_customize,
		'social_news_banner_section_enable',
		array(
			'label'    => esc_html__( 'Enable Banner Section', 'social-news' ),
			'type'     => 'checkbox',
			'settings' => 'social_news_banner_section_enable',
			'section'  => 'social_news_banner_section',
		)
	)
);

// Main News Sub Heading.
$wp_customize->add_setting(
	'social_news_banner_main_news_sub_heading',
	array(
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	new Social_News_Sub_Section_Heading_Custom_Control(
		$wp_customize,
		'social_news_banner_main_news_sub_heading',
		array(
			'label'           => esc_html__( 'Main News Section', 'social-news' ),
			'settings'        => 'social_news_banner_main_news_sub_heading',
			'section'         => 'social_news_banner_section',
			'active_callback' => 'social_news_if_banner_enabled',
		)
	)
);

// Main News title settings.
$wp_customize->add_setting(
	'social_news_banner_main_news_title',
	array(
		'default'           => __( 'Main News', 'social-news' ),
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	'social_news_banner_main_news_title',
	array(
		'label'           => esc_html__( 'Title', 'social-news' ),
		'section'         => 'social_news_banner_section',
		'active_callback' => 'social_news_if_banner_enabled',
	)
);

// Abort if selective refresh is not available.
if ( isset( $wp_customize->selective_refresh ) ) {
	$wp_customize->selective_refresh->add_partial(
		'social_news_banner_main_news_title',
		array(
			'selector'            => '.main-news-outer h3.widget-title',
			'settings'            => 'social_news_banner_main_news_title',
			'container_inclusive' => false,
			'fallback_refresh'    => true,
		)
	);
}

// banner main news content type settings.
$wp_customize->add_setting(
	'social_news_banner_main_news_content_type',
	array(
		'default'           => 'post',
		'sanitize_callback' => 'social_news_sanitize_select',
	)
);

$wp_customize->add_control(
	'social_news_banner_main_news_content_type',
	array(
		'label'           => esc_html__( 'Main News Content type:', 'social-news' ),
		'description'     => esc_html__( 'Choose where you want to render the content from.', 'social-news' ),
		'section'         => 'social_news_banner_section',
		'settings'        => 'social_news_banner_main_news_content_type',
		'type'            => 'select',
		'active_callback' => 'social_news_if_banner_enabled',
		'choices'         => array(
			'post'     => esc_html__( 'Post', 'social-news' ),
			'category' => esc_html__( 'Category', 'social-news' ),
		),
	)
);

for ( $i = 1; $i <= 3; $i++ ) {
	// Main News post setting.
	$wp_customize->add_setting(
		'social_news_banner_main_news_post_' . $i,
		array(
			'sanitize_callback' => 'social_news_sanitize_dropdown_pages',
		)
	);

	$wp_customize->add_control(
		'social_news_banner_main_news_post_' . $i,
		array(
			'label'           => sprintf( esc_html__( 'Post %d', 'social-news' ), $i ),
			'section'         => 'social_news_banner_section',
			'type'            => 'select',
			'choices'         => social_news_get_post_choices(),
			'active_callback' => 'social_news_banner_main_news_content_type_post_enabled',
		)
	);

}

// Main News category setting.
$wp_customize->add_setting(
	'social_news_banner_main_news_category',
	array(
		'sanitize_callback' => 'social_news_sanitize_select',
	)
);

$wp_customize->add_control(
	'social_news_banner_main_news_category',
	array(
		'label'           => esc_html__( 'Category', 'social-news' ),
		'section'         => 'social_news_banner_section',
		'type'            => 'select',
		'choices'         => social_news_get_post_cat_choices(),
		'active_callback' => 'social_news_banner_main_news_content_type_category_enabled',
	)
);

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

$wp_customize->add_control(
	new Social_News_Sub_Section_Heading_Custom_Control(
		$wp_customize,
		'social_news_banner_featured_posts_sub_heading',
		array(
			'label'           => esc_html__( 'Featured Posts Section', 'social-news' ),
			'settings'        => 'social_news_banner_featured_posts_sub_heading',
			'section'         => 'social_news_banner_section',
			'active_callback' => 'social_news_if_banner_enabled',
		)
	)
);

// Featured posts title settings.
$wp_customize->add_setting(
	'social_news_banner_featured_posts_title',
	array(
		'default'           => __( 'Featured News', 'social-news' ),
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	'social_news_banner_featured_posts_title',
	array(
		'label'           => esc_html__( 'Title', 'social-news' ),
		'section'         => 'social_news_banner_section',
		'active_callback' => 'social_news_if_banner_enabled',
	)
);

// Abort if selective refresh is not available.
if ( isset( $wp_customize->selective_refresh ) ) {
	$wp_customize->selective_refresh->add_partial(
		'social_news_banner_featured_posts_title',
		array(
			'selector'            => '.featured-posts-outer h3.widget-title',
			'settings'            => 'social_news_banner_featured_posts_title',
			'container_inclusive' => false,
			'fallback_refresh'    => true,
		)
	);
}

// banner featured posts content type settings.
$wp_customize->add_setting(
	'social_news_banner_featured_posts_content_type',
	array(
		'default'           => 'post',
		'sanitize_callback' => 'social_news_sanitize_select',
	)
);

$wp_customize->add_control(
	'social_news_banner_featured_posts_content_type',
	array(
		'label'           => esc_html__( 'Featured Posts Content type:', 'social-news' ),
		'description'     => esc_html__( 'Choose where you want to render the content from.', 'social-news' ),
		'section'         => 'social_news_banner_section',
		'settings'        => 'social_news_banner_featured_posts_content_type',
		'type'            => 'select',
		'active_callback' => 'social_news_if_banner_enabled',
		'choices'         => array(
			'post'     => esc_html__( 'Post', 'social-news' ),
			'category' => esc_html__( 'Category', 'social-news' ),
		),
	)
);

for ( $i = 1; $i <= 3; $i++ ) {
	// Featured Posts post setting.
	$wp_customize->add_setting(
		'social_news_banner_featured_posts_post_' . $i,
		array(
			'sanitize_callback' => 'social_news_sanitize_dropdown_pages',
		)
	);

	$wp_customize->add_control(
		'social_news_banner_featured_posts_post_' . $i,
		array(
			'label'           => sprintf( esc_html__( 'Post %d', 'social-news' ), $i ),
			'section'         => 'social_news_banner_section',
			'type'            => 'select',
			'choices'         => social_news_get_post_choices(),
			'active_callback' => 'social_news_banner_featured_posts_content_type_post_enabled',
		)
	);

}

// Featured Posts category setting.
$wp_customize->add_setting(
	'social_news_banner_featured_posts_category',
	array(
		'sanitize_callback' => 'social_news_sanitize_select',
	)
);

$wp_customize->add_control(
	'social_news_banner_featured_posts_category',
	array(
		'label'           => esc_html__( 'Category', 'social-news' ),
		'section'         => 'social_news_banner_section',
		'type'            => 'select',
		'choices'         => social_news_get_post_cat_choices(),
		'active_callback' => 'social_news_banner_featured_posts_content_type_category_enabled',
	)
);

/*========================Active Callback==============================*/
function social_news_if_banner_enabled( $control ) {
	return $control->manager->get_setting( 'social_news_banner_section_enable' )->value();
}
// Banner Main News.
function social_news_banner_main_news_content_type_post_enabled( $control ) {
	$content_type = $control->manager->get_setting( 'social_news_banner_main_news_content_type' )->value();
	return social_news_if_banner_enabled( $control ) && ( 'post' === $content_type );
}
function social_news_banner_main_news_content_type_category_enabled( $control ) {
	$content_type = $control->manager->get_setting( 'social_news_banner_main_news_content_type' )->value();
	return social_news_if_banner_enabled( $control ) && ( 'category' === $content_type );
}

// Banner Featured Posts.
function social_news_banner_featured_posts_content_type_post_enabled( $control ) {
	$content_type = $control->manager->get_setting( 'social_news_banner_featured_posts_content_type' )->value();
	return social_news_if_banner_enabled( $control ) && ( 'post' === $content_type );
}
function social_news_banner_featured_posts_content_type_category_enabled( $control ) {
	$content_type = $control->manager->get_setting( 'social_news_banner_featured_posts_content_type' )->value();
	return social_news_if_banner_enabled( $control ) && ( 'category' === $content_type );
}
