<?php
/**
 * Featured Content options
 *
 * @package Signify
 */

/**
 * Add featured content options to theme options
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function signify_featured_content_options( $wp_customize ) {
	// Add note to ECT Featured Content Section
    signify_register_option( $wp_customize, array(
			'name'              => 'signify_featured_content_jetpack_note',
			'sanitize_callback' => 'sanitize_text_field',
			'custom_control'    => 'Signify_Note_Control',
			'label'             => sprintf( esc_html__( 'For all Featured Content Options for Signify Theme, go %1$shere%2$s', 'signify-pro' ),
			'<a href="javascript:wp.customize.section( \'signify_featured_content\' ).focus();">',
			'</a>'
			),
			'section'           => 'featured_content',
			'type'              => 'description',
			'priority'          => 1,
        )
    );

    $wp_customize->add_section( 'signify_featured_content', array(
			'title' => esc_html__( 'Featured Content', 'signify-pro' ),
			'panel' => 'signify_theme_options',
		)
	);

	// Add color scheme setting and control.
	signify_register_option( $wp_customize, array(
			'name'              => 'signify_featured_content_option',
			'default'           => 'disabled',
			'sanitize_callback' => 'signify_sanitize_select',
			'choices'           => signify_section_visibility_options(),
			'label'             => esc_html__( 'Enable on', 'signify-pro' ),
			'section'           => 'signify_featured_content',
			'type'              => 'select',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_featured_content_layout',
			'default'           => 'layout-three',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_featured_content_active',
			'choices'           => signify_sections_layout_options(),
			'label'             => esc_html__( 'Select Layout', 'signify-pro' ),
			'section'           => 'signify_featured_content',
			'type'              => 'select',
		)
	);

	signify_section_header_options( 'signify_featured_content', 'featured_content', 'signify_is_featured_page_post_category_image_content_active' );

	$type = signify_section_type_options();

	$type['featured-content'] = esc_html__( 'Custom Post Type', 'signify-pro' );

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_featured_content_type',
			'default'           => 'page',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_featured_content_active',
			'description'       => sprintf( esc_html__( 'For Custom Post Type Content, install %1$sEssential Content Types%2$s Plugin with Featured Content Type Enabled', 'signify-pro' ),
                '<a target="_blank" href="https://wordpress.org/plugins/essential-content-types/">',
                '</a>'
            ),
            'choices'           => $type,
			'label'             => esc_html__( 'Type', 'signify-pro' ),
			'section'           => 'signify_featured_content',
			'type'              => 'select',
		)
	);

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_featured_content_cpt_note',
            'sanitize_callback' => 'sanitize_text_field',
            'custom_control'    => 'Signify_Note_Control',
            'active_callback'   => 'signify_is_featured_cpt_content_active',
            'label'             => sprintf( esc_html__( 'For CPT heading and sub-heading, go %1$shere%2$s', 'signify-pro' ),
                 '<a href="javascript:wp.customize.control( \'featured_content_title\' ).focus();">',
                 '</a>'
            ),
            'section'           => 'signify_featured_content',
            'type'              => 'description',
        )
    );

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_featured_content_number',
			'default'           => 3,
			'sanitize_callback' => 'signify_sanitize_number_range',
			'active_callback'   => 'signify_is_featured_content_active',
			'description'       => esc_html__( 'Save and refresh the page if No. of Featured Content is changed (Max no of Featured Content is 20)', 'signify-pro' ),
			'input_attrs'       => array(
				'style' => 'width: 100px;',
				'min'   => 0,
			),
			'label'             => esc_html__( 'No of Featured Content', 'signify-pro' ),
			'section'           => 'signify_featured_content',
			'type'              => 'number',
			'transport'         => 'postMessage',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_featured_content_show',
			'default'           => 'excerpt',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_featured_post_category_cpt_content_active',
			'choices'           => signify_content_show(),
			'label'             => esc_html__( 'Display Content', 'signify-pro' ),
			'section'           => 'signify_featured_content',
			'type'              => 'select',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_featured_content_select_category',
			'sanitize_callback' => 'signify_sanitize_category_list',
			'custom_control'    => 'Signify_Multi_Cat',
			'active_callback'   => 'signify_is_featured_category_content_active',
			'label'             => esc_html__( 'Select Categories', 'signify-pro' ),
			'name'              => 'signify_featured_content_select_category',
			'section'           => 'signify_featured_content',
			'type'              => 'dropdown-categories',
		)
	);

	$number = get_theme_mod( 'signify_featured_content_number', 3 );

	//loop for featured post content
	for ( $i = 1; $i <= $number ; $i++ ) {
		signify_register_option( $wp_customize, array(
				'name'              => 'signify_featured_content_post_' . $i,
				'sanitize_callback' => 'signify_sanitize_post',
				'default'           => 0,
				'active_callback'   => 'signify_is_featured_post_content_active',
				'input_attrs'       => array(
					'style'             => 'width: 40px;'
				),
				'label'             => esc_html__( 'Post', 'signify-pro' ) . ' ' . $i ,
				'section'           => 'signify_featured_content',
				'choices'           => signify_generate_post_array(),
				'type'              => 'select'
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_featured_content_page_' . $i,
				'sanitize_callback' => 'signify_sanitize_post',
				'active_callback'   => 'signify_is_featured_page_content_active',
				'label'             => esc_html__( 'Featured Page', 'signify-pro' ) . ' ' . $i ,
				'section'           => 'signify_featured_content',
				'type'              => 'dropdown-pages',
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_featured_content_cpt_' . $i,
				'sanitize_callback' => 'signify_sanitize_post',
				'active_callback'   => 'signify_is_featured_cpt_content_active',
				'label'             => esc_html__( 'Featured Content', 'signify-pro' ) . ' ' . $i ,
				'section'           => 'signify_featured_content',
				'type'              => 'select',
                'choices'           => signify_generate_post_array( 'featured-content' ),
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_featured_content_note_' . $i,
				'sanitize_callback' => 'sanitize_text_field',
				'custom_control'    => 'Signify_Note_Control',
				'active_callback'   => 'signify_is_featured_image_content_active',
				'label'             => esc_html__( 'Featured Content #', 'signify-pro' ) .  $i,
				'section'           => 'signify_featured_content',
				'type'              => 'description',
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_featured_content_image_' . $i,
				'sanitize_callback' => 'signify_sanitize_image',
				'custom_control'    => 'WP_Customize_Image_Control',
				'active_callback'   => 'signify_is_featured_image_content_active',
				'label'             => esc_html__( 'Image', 'signify-pro' ),
				'section'           => 'signify_featured_content',
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_featured_content_link_' . $i,
				'sanitize_callback' => 'esc_url_raw',
				'active_callback'   => 'signify_is_featured_image_content_active',
				'label'             => esc_html__( 'Link', 'signify-pro' ),
				'section'           => 'signify_featured_content',
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_featured_content_target_' . $i,
				'sanitize_callback' => 'signify_sanitize_checkbox',
				'active_callback'   => 'signify_is_featured_image_content_active',
				'label'             => esc_html__( 'Open Link in New Window/Tab', 'signify-pro' ),
				'section'           => 'signify_featured_content',
				'type'    			=> 'checkbox',
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_featured_content_title_' . $i,
				'sanitize_callback' => 'wp_kses_post',
				'active_callback'   => 'signify_is_featured_image_content_active',
				'label'             => esc_html__( 'Title', 'signify-pro' ),
				'section'           => 'signify_featured_content',
				'type'              => 'text',
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_featured_content_content_' . $i,
				'sanitize_callback' => 'wp_kses_post',
				'active_callback'   => 'signify_is_featured_image_content_active',
				'label'             => esc_html__( 'Content', 'signify-pro' ),
				'section'           => 'signify_featured_content',
				'type'              => 'textarea',
			)
		);
	} // End for().

	signify_register_option( $wp_customize, array(
            'name'              => 'signify_featured_content_text',
            'sanitize_callback' => 'sanitize_text_field',
            'active_callback'   => 'signify_is_featured_content_active',
            'label'             => esc_html__( 'Button Text', 'signify-pro' ),
            'section'           => 'signify_featured_content',
            'type'              => 'text',
        )
    );

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_featured_content_link',
            'sanitize_callback' => 'esc_url_raw',
            'active_callback'   => 'signify_is_featured_content_active',
            'label'             => esc_html__( 'Button Link', 'signify-pro' ),
            'section'           => 'signify_featured_content',
        )
    );

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_featured_content_target',
            'sanitize_callback' => 'signify_sanitize_checkbox',
            'active_callback'   => 'signify_is_featured_content_active',
            'label'             => esc_html__( 'Open Link in New Window/Tab', 'signify-pro' ),
            'section'           => 'signify_featured_content',
            'type'    			=> 'checkbox',
        )
    );
}
add_action( 'customize_register', 'signify_featured_content_options', 10 );

/** Active Callback Functions **/
if ( ! function_exists( 'signify_is_featured_content_active' ) ) :
	/**
	* Return true if featured content is active
	*
	* @since 1.0.0
	*/
	function signify_is_featured_content_active( $control ) {
		$enable = $control->manager->get_setting( 'signify_featured_content_option' )->value();

		return signify_check_section( $enable );
	}
endif;

if ( ! function_exists( 'signify_is_featured_post_content_active' ) ) :
	/**
	* Return true if page content is active
	*
	* @since 1.0.0
	*/
	function signify_is_featured_post_content_active( $control ) {
		$type = $control->manager->get_setting( 'signify_featured_content_type' )->value();

		return ( signify_is_featured_content_active( $control ) && 'post' === $type );
	}
endif;

if ( ! function_exists( 'signify_is_featured_page_content_active' ) ) :
	/**
	* Return true if page content is active
	*
	* @since 1.0.0
	*/
	function signify_is_featured_page_content_active( $control ) {
		$type = $control->manager->get_setting( 'signify_featured_content_type' )->value();

		return ( signify_is_featured_content_active( $control ) && 'page' === $type );
	}
endif;

if ( ! function_exists( 'signify_is_featured_cpt_content_active' ) ) :
	/**
	* Return true if page content is active
	*
	* @since 1.0.0
	*/
	function signify_is_featured_cpt_content_active( $control ) {
		$type = $control->manager->get_setting( 'signify_featured_content_type' )->value();

		return ( signify_is_featured_content_active( $control ) && 'featured-content' === $type );
	}
endif;

if ( ! function_exists( 'signify_is_featured_category_content_active' ) ) :
	/**
	* Return true if page content is active
	*
	* @since 1.0.0
	*/
	function signify_is_featured_category_content_active( $control ) {
		$type = $control->manager->get_setting( 'signify_featured_content_type' )->value();

		return ( signify_is_featured_content_active( $control ) && ( 'category' === $type ) );
	}
endif;

if ( ! function_exists( 'signify_is_featured_page_post_category_image_content_active' ) ) :
	/**
	* Return true if page/post/category/image content is active
	*
	* @since 1.0.0
	*/
	function signify_is_featured_page_post_category_image_content_active( $control ) {
		$type = $control->manager->get_setting( 'signify_featured_content_type' )->value();

		return ( signify_is_featured_content_active( $control ) && ( 'custom' === $type || 'category' === $type || 'page' === $type || 'post' === $type ) );
	}
endif;

if ( ! function_exists( 'signify_is_featured_post_category_cpt_content_active' ) ) :
	/**
	* Return true if page/post/category/image content is active
	*
	* @since 1.0.0
	*/
	function signify_is_featured_post_category_cpt_content_active( $control ) {
		$type = $control->manager->get_setting( 'signify_featured_content_type' )->value();

		return ( signify_is_featured_content_active( $control ) && ( 'category' === $type || 'post' === $type || 'featured-content' === $type ) );
	}
endif;

if ( ! function_exists( 'signify_is_featured_image_content_active' ) ) :
	/**
	* Return true if page content is active
	*
	* @since 1.0.0
	*/
	function signify_is_featured_image_content_active( $control ) {
		$type = $control->manager->get_setting( 'signify_featured_content_type' )->value();

		return ( signify_is_featured_content_active( $control ) && 'custom' === $type );
	}
endif;
