<?php
/**
 * Blog Section
 *
 * @package Construktly
 */

$wp_customize->add_section(
	'construktly_blog_section',
	array(
		'panel' => 'construktly_front_page_options',
		'title' => esc_html__( 'Blog Section', 'construktly' ),
	)
);

// Blog Section - Enable Section.
$wp_customize->add_setting(
	'construktly_enable_blog_section',
	array(
		'default'           => false,
		'sanitize_callback' => 'construktly_sanitize_switch',
	)
);

$wp_customize->add_control(
	new Construktly_Toggle_Switch_Custom_Control(
		$wp_customize,
		'construktly_enable_blog_section',
		array(
			'label'    => esc_html__( 'Enable Blog Section', 'construktly' ),
			'section'  => 'construktly_blog_section',
			'settings' => 'construktly_enable_blog_section',
		)
	)
);

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

// Blog Section - Section Title.
$wp_customize->add_setting(
	'construktly_blog_title',
	array(
		'default'           => __( 'Recent News', 'construktly' ),
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	'construktly_blog_title',
	array(
		'label'           => esc_html__( 'Section Title', 'construktly' ),
		'section'         => 'construktly_blog_section',
		'settings'        => 'construktly_blog_title',
		'type'            => 'text',
		'active_callback' => 'construktly_is_blog_section_enabled',
	)
);

// Blog Section - Section Text.
$wp_customize->add_setting(
	'construktly_blog_text',
	array(
		'default'           => '',
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	'construktly_blog_text',
	array(
		'label'           => esc_html__( 'Section Text', 'construktly' ),
		'section'         => 'construktly_blog_section',
		'settings'        => 'construktly_blog_text',
		'type'            => 'text',
		'active_callback' => 'construktly_is_blog_section_enabled',
	)
);

// Blog Section - Number of Posts.
$wp_customize->add_setting(
	'construktly_blog_count',
	array(
		'default'           => 5,
		'sanitize_callback' => 'construktly_sanitize_number_range',
	)
);

$wp_customize->add_control(
	'construktly_blog_count',
	array(
		'label'           => esc_html__( 'Number of Posts to Show', 'construktly' ),
		'section'         => 'construktly_blog_section',
		'settings'        => 'construktly_blog_count',
		'type'            => 'number',
		'input_attrs'     => array(
			'min' => 1,
			'max' => 20,
		),
		'active_callback' => 'construktly_is_blog_section_enabled',
	)
);

// Blog Section - Content Type.
$wp_customize->add_setting(
	'construktly_blog_content_type',
	array(
		'default'           => 'page',
		'sanitize_callback' => 'construktly_sanitize_select',
	)
);

$wp_customize->add_control(
	'construktly_blog_content_type',
	array(
		'label'           => esc_html__( 'Select Content Type', 'construktly' ),
		'section'         => 'construktly_blog_section',
		'settings'        => 'construktly_blog_content_type',
		'type'            => 'select',
		'active_callback' => 'construktly_is_blog_section_enabled',
		'choices'         => array(
			'page'     => esc_html__( 'Page', 'construktly' ),
			'post'     => esc_html__( 'Post', 'construktly' ),
			'category' => esc_html__( 'Category', 'construktly' ),
		),
	)
);

// List out selected number of fields.
$blog_count = get_theme_mod( 'construktly_blog_count', 5 );

for ( $i = 1; $i <= $blog_count; $i++ ) {
	// Blog Section - Select Post.
	$wp_customize->add_setting(
		'construktly_blog_content_post_' . $i,
		array(
			'sanitize_callback' => 'absint',
		)
	);

	$wp_customize->add_control(
		'construktly_blog_content_post_' . $i,
		array(
			/* translators: %d: count. */
			'label'           => sprintf( esc_html__( 'Select Post %d', 'construktly' ), $i ),
			'section'         => 'construktly_blog_section',
			'settings'        => 'construktly_blog_content_post_' . $i,
			'active_callback' => 'construktly_is_blog_section_and_content_type_post_enabled',
			'type'            => 'select',
			'choices'         => construktly_get_post_choices(),
		)
	);

	// Blog Section - Select Page.
	$wp_customize->add_setting(
		'construktly_blog_content_page_' . $i,
		array(
			'sanitize_callback' => 'absint',
		)
	);

	$wp_customize->add_control(
		'construktly_blog_content_page_' . $i,
		array(
			/* translators: %d: count. */
			'label'           => sprintf( esc_html__( 'Select Page %d', 'construktly' ), $i ),
			'section'         => 'construktly_blog_section',
			'settings'        => 'construktly_blog_content_page_' . $i,
			'active_callback' => 'construktly_is_blog_section_and_content_type_page_enabled',
			'type'            => 'select',
			'choices'         => construktly_get_page_choices(),
		)
	);

}

// Blog Section - Select Category.
$wp_customize->add_setting(
	'construktly_blog_content_category',
	array(
		'sanitize_callback' => 'construktly_sanitize_select',
	)
);

$wp_customize->add_control(
	'construktly_blog_content_category',
	array(
		'label'           => esc_html__( 'Select Category', 'construktly' ),
		'section'         => 'construktly_blog_section',
		'settings'        => 'construktly_blog_content_category',
		'active_callback' => 'construktly_is_blog_section_and_content_type_category_enabled',
		'type'            => 'select',
		'choices'         => construktly_get_post_cat_choices(),
	)
);

// Blog Section - Button Label.
$wp_customize->add_setting(
	'construktly_blog_button_label',
	array(
		'default'           => __( 'View All', 'construktly' ),
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	'construktly_blog_button_label',
	array(
		'label'           => esc_html__( 'Button Label', 'construktly' ),
		'section'         => 'construktly_blog_section',
		'settings'        => 'construktly_blog_button_label',
		'type'            => 'text',
		'active_callback' => 'construktly_is_blog_section_enabled',
	)
);

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

$wp_customize->add_control(
	'construktly_blog_button_link',
	array(
		'label'           => esc_html__( 'Button Link', 'construktly' ),
		'section'         => 'construktly_blog_section',
		'settings'        => 'construktly_blog_button_link',
		'type'            => 'url',
		'active_callback' => 'construktly_is_blog_section_enabled',
	)
);
