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

$wp_customize->add_section(
	'corporaze_blog_section',
	array(
		'panel'    => 'corporaze_front_page_options',
		'title'    => esc_html__( 'Blog Section', 'corporaze' ),
		'priority' => 80,
	)
);

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

$wp_customize->add_control(
	new Corporaze_Toggle_Switch_Custom_Control(
		$wp_customize,
		'corporaze_enable_blog_section',
		array(
			'label'    => esc_html__( 'Enable Blog Section', 'corporaze' ),
			'section'  => 'corporaze_blog_section',
			'settings' => 'corporaze_enable_blog_section',
		)
	)
);

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

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

$wp_customize->add_control(
	'corporaze_blog_title',
	array(
		'label'           => esc_html__( 'Section Title', 'corporaze' ),
		'section'         => 'corporaze_blog_section',
		'settings'        => 'corporaze_blog_title',
		'type'            => 'text',
		'active_callback' => 'corporaze_is_blog_section_enabled',
	)
);

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

$wp_customize->add_control(
	'corporaze_blog_text',
	array(
		'label'           => esc_html__( 'Section Text', 'corporaze' ),
		'section'         => 'corporaze_blog_section',
		'settings'        => 'corporaze_blog_text',
		'type'            => 'text',
		'active_callback' => 'corporaze_is_blog_section_enabled',
	)
);

// Blog Section - Number of Posts.
$wp_customize->add_setting(
	'corporaze_blog_count',
	array(
		'default'           => 3,
		'sanitize_callback' => 'corporaze_sanitize_number_range',
		'validate_callback' => 'corporaze_validate_blog_count',
	)
);

$wp_customize->add_control(
	'corporaze_blog_count',
	array(
		'label'           => esc_html__( 'Number of Posts to Show', 'corporaze' ),
		'description'     => esc_html__( 'Min 1 & Max 6. Please input the valid number and save. Then refresh the page to see the change.', 'corporaze' ),
		'section'         => 'corporaze_blog_section',
		'settings'        => 'corporaze_blog_count',
		'type'            => 'number',
		'input_attrs'     => array(
			'min' => 1,
			'max' => 6,
		),
		'active_callback' => 'corporaze_is_blog_section_enabled',
	)
);

// Blog Section - Description Length.
$wp_customize->add_setting(
	'corporaze_blog_excerpt_count',
	array(
		'default'           => 30,
		'sanitize_callback' => 'corporaze_sanitize_number_range',
		'validate_callback' => 'corporaze_validate_excerpt_length',
	)
);

$wp_customize->add_control(
	'corporaze_blog_excerpt_count',
	array(
		'label'           => esc_html__( 'Description Length ( Words )', 'corporaze' ),
		'description'     => esc_html__( 'Min 1 & Max 100. Please input the valid number and save. Then refresh the page to see the change.', 'corporaze' ),
		'section'         => 'corporaze_blog_section',
		'settings'        => 'corporaze_blog_excerpt_count',
		'type'            => 'number',
		'input_attrs'     => array(
			'min' => 1,
			'max' => 100,
		),
		'active_callback' => 'corporaze_is_blog_section_enabled',
	)
);

// Blog Section - Posts Button.
$wp_customize->add_setting(
	'corporaze_blog_posts_button',
	array(
		'default'           => __( 'Read More', 'corporaze' ),
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	'corporaze_blog_posts_button',
	array(
		'label'           => esc_html__( 'Posts Button', 'corporaze' ),
		'section'         => 'corporaze_blog_section',
		'settings'        => 'corporaze_blog_posts_button',
		'type'            => 'text',
		'active_callback' => 'corporaze_is_blog_section_enabled',
	)
);

// Blog Section - Content Type.
$wp_customize->add_setting(
	'corporaze_blog_content_type',
	array(
		'default'           => 'post',
		'sanitize_callback' => 'corporaze_sanitize_select',
	)
);

$wp_customize->add_control(
	'corporaze_blog_content_type',
	array(
		'label'           => esc_html__( 'Select Content Type', 'corporaze' ),
		'section'         => 'corporaze_blog_section',
		'settings'        => 'corporaze_blog_content_type',
		'type'            => 'select',
		'active_callback' => 'corporaze_is_blog_section_enabled',
		'choices'         => array(
			'page' => esc_html__( 'Page', 'corporaze' ),
			'post' => esc_html__( 'Post', 'corporaze' ),
		),
	)
);

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

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

	$wp_customize->add_control(
		'corporaze_blog_content_post_' . $i,
		array(
			'label'           => sprintf( esc_html__( 'Select Post %d', 'corporaze' ), $i ),
			'section'         => 'corporaze_blog_section',
			'settings'        => 'corporaze_blog_content_post_' . $i,
			'active_callback' => 'corporaze_is_blog_section_and_content_type_post_enabled',
			'type'            => 'select',
			'choices'         => corporaze_get_post_choices(),
		)
	);

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

	$wp_customize->add_control(
		'corporaze_blog_content_page_' . $i,
		array(
			'label'           => sprintf( esc_html__( 'Select Page %d', 'corporaze' ), $i ),
			'section'         => 'corporaze_blog_section',
			'settings'        => 'corporaze_blog_content_page_' . $i,
			'active_callback' => 'corporaze_is_blog_section_and_content_type_page_enabled',
			'type'            => 'select',
			'choices'         => corporaze_get_page_choices(),
		)
	);

}

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

$wp_customize->add_control(
	'corporaze_blog_button_label',
	array(
		'label'           => esc_html__( 'Button Label', 'corporaze' ),
		'section'         => 'corporaze_blog_section',
		'settings'        => 'corporaze_blog_button_label',
		'type'            => 'text',
		'active_callback' => 'corporaze_is_blog_section_enabled',
	)
);

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

$wp_customize->add_control(
	'corporaze_blog_button_link',
	array(
		'label'           => esc_html__( 'Button Link', 'corporaze' ),
		'section'         => 'corporaze_blog_section',
		'settings'        => 'corporaze_blog_button_link',
		'type'            => 'url',
		'active_callback' => 'corporaze_is_blog_section_enabled',
	)
);
