<?php
/**
 * Grid Section
 *
 * @package Routine News
 */

$wp_customize->add_section(
	'routine_news_grid_section',
	array(
		'panel' => 'routine_news_front_page_options',
		'title' => esc_html__( 'Posts Grid Section', 'routine-news' ),
	)
);

// Grid Section - Enable Section.
$wp_customize->add_setting(
	'routine_news_enable_grid_section',
	array(
		'default'           => false,
		'sanitize_callback' => 'routine_news_sanitize_switch',
	)
);

$wp_customize->add_control(
	new Routine_News_Toggle_Switch_Custom_Control(
		$wp_customize,
		'routine_news_enable_grid_section',
		array(
			'label'    => esc_html__( 'Enable Grid Section', 'routine-news' ),
			'section'  => 'routine_news_grid_section',
			'settings' => 'routine_news_enable_grid_section',
		)
	)
);

if ( isset( $wp_customize->selective_refresh ) ) {
	$wp_customize->selective_refresh->add_partial(
		'routine_news_enable_grid_section',
		array(
			'selector' => '#routine_news_grid_section .section-link',
			'settings' => 'routine_news_enable_grid_section',
		)
	);
}

// Grid Section - Section Title.
$wp_customize->add_setting(
	'routine_news_grid_title',
	array(
		'default'           => __( 'Grid Posts', 'routine-news' ),
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	'routine_news_grid_title',
	array(
		'label'           => esc_html__( 'Section Title', 'routine-news' ),
		'section'         => 'routine_news_grid_section',
		'settings'        => 'routine_news_grid_title',
		'type'            => 'text',
		'active_callback' => 'routine_news_is_grid_section_enabled',
	)
);

// Grid Section - Grid Content Type.
$wp_customize->add_setting(
	'routine_news_grid_content_type',
	array(
		'default'           => 'post',
		'sanitize_callback' => 'routine_news_sanitize_select',
	)
);

$wp_customize->add_control(
	'routine_news_grid_content_type',
	array(
		'label'           => esc_html__( 'Select Content Type', 'routine-news' ),
		'section'         => 'routine_news_grid_section',
		'settings'        => 'routine_news_grid_content_type',
		'type'            => 'select',
		'active_callback' => 'routine_news_is_grid_section_enabled',
		'choices'         => array(
			'post'     => esc_html__( 'Post', 'routine-news' ),
			'category' => esc_html__( 'Category', 'routine-news' ),
		),
	)
);

// Grid Section - Select grid Category.
$wp_customize->add_setting(
	'routine_news_grid_content_category',
	array(
		'sanitize_callback' => 'routine_news_sanitize_select',
	)
);

$wp_customize->add_control(
	'routine_news_grid_content_category',
	array(
		'label'           => esc_html__( 'Select Category', 'routine-news' ),
		'section'         => 'routine_news_grid_section',
		'settings'        => 'routine_news_grid_content_category',
		'active_callback' => 'routine_news_is_grid_section_and_content_type_category_enabled',
		'type'            => 'select',
		'choices'         => routine_news_get_post_cat_choices(),
	)
);

for ( $i = 1; $i <= 5; $i++ ) {

	// Grid Section - Select grid Post.
	$wp_customize->add_setting(
		'routine_news_grid_content_post_' . $i,
		array(
			'sanitize_callback' => 'absint',
		)
	);

	$wp_customize->add_control(
		'routine_news_grid_content_post_' . $i,
		array(
			'label'           => sprintf( esc_html__( 'Select Post %d', 'routine-news' ), $i ),
			'section'         => 'routine_news_grid_section',
			'settings'        => 'routine_news_grid_content_post_' . $i,
			'active_callback' => 'routine_news_is_grid_section_and_content_type_post_enabled',
			'type'            => 'select',
			'choices'         => routine_news_get_post_choices(),
		)
	);

}

// Grid Section - Button Label.
$wp_customize->add_setting(
	'routine_news_grid_button_label',
	array(
		'default'           => __( 'View All', 'routine-news' ),
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	'routine_news_grid_button_label',
	array(
		'label'           => esc_html__( 'Button Label', 'routine-news' ),
		'section'         => 'routine_news_grid_section',
		'settings'        => 'routine_news_grid_button_label',
		'type'            => 'text',
		'active_callback' => 'routine_news_is_grid_section_enabled',
	)
);

// Grid Section - Button Link.
$wp_customize->add_setting(
	'routine_news_grid_button_link',
	array(
		'default'           => '#',
		'sanitize_callback' => 'esc_url_raw',
	)
);

$wp_customize->add_control(
	'routine_news_grid_button_link',
	array(
		'label'           => esc_html__( 'Button Link', 'routine-news' ),
		'section'         => 'routine_news_grid_section',
		'settings'        => 'routine_news_grid_button_link',
		'type'            => 'url',
		'active_callback' => 'routine_news_is_grid_section_enabled',
	)
);
