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

/**
 * Add featured content options to theme options
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function personaltrainer_featured_content_options( $wp_customize ) {
    $wp_customize->add_section( 'personaltrainer_featured_content', array(
			'title' => esc_html__( 'Featured Content', 'personal-trainer' ),
			'panel' => 'personaltrainer_theme_options',
		)
	);

	personaltrainer_register_option( $wp_customize, array(
            'name'              => 'personaltrainer_featured_content_note_1',
            'sanitize_callback' => 'sanitize_text_field',
            'custom_control'    => 'Personaltrainer_Note_Control',
            'active_callback'   => 'personaltrainer_is_ect_featured_content_inactive',
            'label'             => sprintf( esc_html__( 'For Featured Content, install %1$sEssential Content Types%2$s Plugin with Featured Content Type Enabled', 'personal-trainer' ),
                '<a target="_blank" href="https://wordpress.org/plugins/essential-content-types/">',
                '</a>'
            ),
            'section'           => 'personaltrainer_featured_content',
            'type'              => 'description',
            'priority'          => 1,
        )
    );

	// Add color scheme setting and control.
	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_featured_content_option',
			'default'           => 'disabled',
			'sanitize_callback' => 'personaltrainer_sanitize_select',
			'active_callback'   => 'personaltrainer_is_ect_featured_content_active',
			'choices'           => personaltrainer_section_visibility_options(),
			'label'             => esc_html__( 'Enable on', 'personal-trainer' ),
			'section'           => 'personaltrainer_featured_content',
			'type'              => 'select',
		)
	);

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_featured_content_position',
			'sanitize_callback' => 'personaltrainer_sanitize_checkbox',
			'active_callback'   => 'personaltrainer_is_move_above_footer_enabled',
			'label'             => esc_html__( 'Check to Move above footer', 'personal-trainer' ),
			'section'           => 'personaltrainer_featured_content',
			'type'              => 'checkbox',
		)
	);

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_featured_content_archive_title',
			'default'           => esc_html__( 'Featured', 'personal-trainer' ),
			'sanitize_callback' => 'wp_kses_post',
			'active_callback'   => 'personaltrainer_is_featured_content_active',
			'label'             => esc_html__( 'Headline', 'personal-trainer' ),
			'section'           => 'personaltrainer_featured_content',
			'type'              => 'text',
		)
	);

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_featured_content_sub_title',
			'sanitize_callback' => 'wp_kses_post',
			'active_callback'   => 'personaltrainer_is_sub_headline_enabled',
			'label'             => esc_html__( 'Sub headline', 'personal-trainer' ),
			'section'           => 'personaltrainer_featured_content',
			'type'              => 'textarea',
		)
	);

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_featured_content_number',
			'default'           => 3,
			'sanitize_callback' => 'personaltrainer_sanitize_number_range',
			'active_callback'   => 'personaltrainer_is_featured_content_active',
			'description'       => esc_html__( 'Save and refresh the page if this option is changed', 'personal-trainer' ),
			'input_attrs'       => array(
				'style' => 'width: 100px;',
				'min'   => 0,
			),
			'label'             => esc_html__( 'No of items', 'personal-trainer' ),
			'section'           => 'personaltrainer_featured_content',
			'type'              => 'number',
			'transport'         => 'postMessage',
		)
	);

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_featured_content_show',
			'default'           => 'excerpt',
			'sanitize_callback' => 'personaltrainer_sanitize_select',
			'active_callback'   => 'personaltrainer_is_featured_content_show_enabled',
			'choices'           => personaltrainer_content_show(),
			'label'             => esc_html__( 'Display Content', 'personal-trainer' ),
			'section'           => 'personaltrainer_featured_content',
			'type'              => 'select',
		)
	);

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

	//loop for featured post content
	for ( $i = 1; $i <= $number ; $i++ ) {
		personaltrainer_register_option( $wp_customize, array(
				'name'              => 'personaltrainer_featured_content_cpt_' . $i,
				'sanitize_callback' => 'personaltrainer_sanitize_post',
				'active_callback'   => 'personaltrainer_is_featured_content_active',
				'label'             => esc_html__( 'Featured Content', 'personal-trainer' ) . ' ' . $i ,
				'section'           => 'personaltrainer_featured_content',
				'type'              => 'select',
                'choices'           => personaltrainer_generate_post_array( 'featured-content' ),
			)
		);
	} // End for().
}
add_action( 'customize_register', 'personaltrainer_featured_content_options', 10 );

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

		//return true only if previewed page on customizer matches the type of content option selected
		return ( personaltrainer_check_section( $enable ) );
	}
endif;

if ( ! function_exists( 'personaltrainer_is_ect_featured_content_active' ) ) :
    /**
    * Return true if featured_content is active
    *
    * @since Personal Trainer 0.1
    */
    function personaltrainer_is_ect_featured_content_active( $control ) {
        return ( class_exists( 'Essential_Content_Featured_Content' ) || class_exists( 'Essential_Content_Pro_Featured_Content' ) );
    }
endif;

if ( ! function_exists( 'personaltrainer_is_ect_featured_content_inactive' ) ) :
    /**
    * Return true if featured_content is active
    *
    * @since Personal Trainer 0.1
    */
    function personaltrainer_is_ect_featured_content_inactive( $control ) {
        return ! ( class_exists( 'Essential_Content_Featured_Content' ) || class_exists( 'Essential_Content_Pro_Featured_Content' ) );
    }
endif;

if( ! function_exists( 'personaltrainer_is_move_above_footer_enabled' ) ) :
	/**
	* @since Personal Trainer 1.2
	*/
	function personaltrainer_is_move_above_footer_enabled( $control ) {
		$enable = $control->manager->get_setting( 'personaltrainer_featured_content_position' )->value();
		return ( ($enable != '') && (personaltrainer_is_featured_content_active($control)) );
	}
endif;

if( ! function_exists( 'personaltrainer_is_sub_headline_enabled' ) ) :
	/**
	* @since Personal Trainer 1.2
	*/
	function personaltrainer_is_sub_headline_enabled( $control ) {
		$enable = $control->manager->get_setting( 'personaltrainer_featured_content_sub_title' )->value();
		return ( ($enable != '') && (personaltrainer_is_featured_content_active($control)) );
	}
endif;

if( ! function_exists( 'personaltrainer_is_featured_content_show_enabled' ) ) :
	/**
	* @since Personal Trainer 1.2
	*/
	function personaltrainer_is_featured_content_show_enabled( $control ) {
		$enable = $control->manager->get_setting( 'personaltrainer_featured_content_show' )->value();
		return ( ($enable != $control->manager->get_setting( 'personaltrainer_featured_content_show' )->default) && (personaltrainer_is_featured_content_active($control)) );
	}
endif;
