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

$wp_customize->add_section(
	'social_news_recent_posts_section',
	array(
		'title' => esc_html__( 'Recent Posts Section', 'social-news' ),
		'panel' => 'social_news_frontpage_panel',
	)
);

// Recent Posts section enable settings.
$wp_customize->add_setting(
	'social_news_recent_posts_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_recent_posts_section_enable',
		array(
			'label'    => esc_html__( 'Enable Recent Posts Section', 'social-news' ),
			'type'     => 'checkbox',
			'settings' => 'social_news_recent_posts_section_enable',
			'section'  => 'social_news_recent_posts_section',
		)
	)
);

// Recent Posts title settings.
$wp_customize->add_setting(
	'social_news_recent_posts_title',
	array(
		'default'           => __( 'Recent Articles', 'social-news' ),
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	'social_news_recent_posts_title',
	array(
		'label'           => esc_html__( 'Title', 'social-news' ),
		'section'         => 'social_news_recent_posts_section',
		'active_callback' => 'social_news_if_recent_posts_enabled',
	)
);

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

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

$wp_customize->add_control(
	'social_news_recent_posts_view_all_button_label',
	array(
		'label'           => esc_html__( 'View All Button Label', 'social-news' ),
		'section'         => 'social_news_recent_posts_section',
		'settings'        => 'social_news_recent_posts_view_all_button_label',
		'type'            => 'text',
		'active_callback' => 'social_news_if_recent_posts_enabled',
	)
);

// Abort if selective refresh is not available.
if ( isset( $wp_customize->selective_refresh ) ) {
	$wp_customize->selective_refresh->add_partial(
		'social_news_recent_posts_view_all_button_label',
		array(
			'selector'            => '.recentpost .adore-view-all',
			'settings'            => 'social_news_recent_posts_view_all_button_label',
			'container_inclusive' => false,
			'fallback_refresh'    => true,
			'render_callback'     => 'social_news_recent_posts_view_all_btn_partial',
		)
	);
}

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

$wp_customize->add_control(
	'social_news_recent_posts_view_all_button_url',
	array(
		'label'           => esc_html__( 'View All Button Link', 'social-news' ),
		'section'         => 'social_news_recent_posts_section',
		'settings'        => 'social_news_recent_posts_view_all_button_url',
		'type'            => 'url',
		'active_callback' => 'social_news_if_recent_posts_enabled',
	)
);

// recent_posts content type settings.
$wp_customize->add_setting(
	'social_news_recent_posts_content_type',
	array(
		'default'           => 'post',
		'sanitize_callback' => 'social_news_sanitize_select',
	)
);

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

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

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

}

// recent_posts category setting.
$wp_customize->add_setting(
	'social_news_recent_posts_category',
	array(
		'sanitize_callback' => 'social_news_sanitize_select',
	)
);

$wp_customize->add_control(
	'social_news_recent_posts_category',
	array(
		'label'           => esc_html__( 'Category', 'social-news' ),
		'section'         => 'social_news_recent_posts_section',
		'type'            => 'select',
		'choices'         => social_news_get_post_cat_choices(),
		'active_callback' => 'social_news_recent_posts_section_content_type_category_enabled',
	)
);

/*========================Active Callback==============================*/
function social_news_if_recent_posts_enabled( $control ) {
	return $control->manager->get_setting( 'social_news_recent_posts_section_enable' )->value();
}
function social_news_recent_posts_section_content_type_post_enabled( $control ) {
	$content_type = $control->manager->get_setting( 'social_news_recent_posts_content_type' )->value();
	return social_news_if_recent_posts_enabled( $control ) && ( 'post' === $content_type );
}
function social_news_recent_posts_section_content_type_category_enabled( $control ) {
	$content_type = $control->manager->get_setting( 'social_news_recent_posts_content_type' )->value();
	return social_news_if_recent_posts_enabled( $control ) && ( 'category' === $content_type );
}

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

if ( ! function_exists( 'social_news_recent_posts_view_all_btn_partial' ) ) :
	// View All Btn.
	function social_news_recent_posts_view_all_btn_partial() {
		return esc_html( get_theme_mod( 'social_news_recent_posts_view_all_button_label' ) );
	}
endif;
