<?php
/**
 * Add Testimonial Settings in Customizer
 *
 * @package Personal_Trainer
*/

/**
 * Add testimonial options to theme options
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function personaltrainer_testimonial_options( $wp_customize ) {
    // Add note to Jetpack Testimonial Section
    personaltrainer_register_option( $wp_customize, array(
            'name'              => 'personaltrainer_jetpack_testimonial_cpt_note',
            'sanitize_callback' => 'sanitize_text_field',
            'custom_control'    => 'Personaltrainer_Note_Control',
            'label'             => sprintf( esc_html__( 'For Testimonial Options for Personal Trainer Theme, go %1$shere%2$s', 'personal-trainer' ),
                '<a href="javascript:wp.customize.section( \'personaltrainer_testimonials\' ).focus();">',
                 '</a>'
            ),
           'section'            => 'jetpack_testimonials',
            'type'              => 'description',
            'priority'          => 1,
        )
    );

    $wp_customize->add_section( 'personaltrainer_testimonials', array(
            'panel'    => 'personaltrainer_theme_options',
            'title'    => esc_html__( 'Testimonials', 'personal-trainer' ),
        )
    );

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

    personaltrainer_register_option( $wp_customize, array(
            'name'              => 'personaltrainer_testimonial_option',
            'default'           => 'disabled',
            'sanitize_callback' => 'personaltrainer_sanitize_select',
            'active_callback'   => 'personaltrainer_is_ect_testimonials_active',
            'choices'           => personaltrainer_section_visibility_options(),
            'label'             => esc_html__( 'Enable on', 'personal-trainer' ),
            'section'           => 'personaltrainer_testimonials',
            'type'              => 'select',
            'priority'          => 1,
        )
    );

    personaltrainer_register_option( $wp_customize, array(
            'name'              => 'personaltrainer_testimonial_position',
            'sanitize_callback' => 'personaltrainer_sanitize_checkbox',
            'active_callback'   => 'personaltrainer_is_testimonial_move_above_footer_enabled',
            'label'             => esc_html__( 'Check to Move below Content', 'personal-trainer' ),
            'section'           => 'personaltrainer_testimonials',
            'type'              => 'checkbox',
        )
    );

    personaltrainer_register_option( $wp_customize, array(
            'name'              => 'personaltrainer_testimonial_headline',
            'default'           => esc_html__( 'Testimonials', 'personal-trainer' ),
            'sanitize_callback' => 'wp_kses_post',
            'label'             => esc_html__( 'Headline', 'personal-trainer' ),
            'active_callback'   => 'personaltrainer_is_testimonial_active',
            'section'           => 'personaltrainer_testimonials',
            'type'              => 'text',
        )
    );

    personaltrainer_register_option( $wp_customize, array(
            'name'              => 'personaltrainer_testimonial_subheadline',
            'sanitize_callback' => 'wp_kses_post',
            'label'             => esc_html__( 'Sub headline', 'personal-trainer' ),
            'active_callback'   => 'personaltrainer_is_testimonial_active',
            'section'           => 'personaltrainer_testimonials',
            'type'              => 'text',
        )
    );

    personaltrainer_register_option( $wp_customize, array(
            'name'              => 'personaltrainer_testimonial_number',
            'default'           => '3',
            'sanitize_callback' => 'personaltrainer_sanitize_number_range',
            'active_callback'   => 'personaltrainer_is_testimonial_active',
            'label'             => esc_html__( 'No of items', 'personal-trainer' ),
            'section'           => 'personaltrainer_testimonials',
            'type'              => 'number',
            'input_attrs'       => array(
                'style'             => 'width: 100px;',
                'min'               => 0,
            ),
        )
    );

if( 1 == get_theme_mod( 'personaltrainer_testimonial_enable_title' ) )
    {
        personaltrainer_register_option( $wp_customize, array(
            'name'              => 'personaltrainer_testimonial_enable_title',
            'default'           => 1,
            'sanitize_callback' => 'personaltrainer_sanitize_checkbox',
            'active_callback'   => 'personaltrainer_is_testimonial_active',
            'label'             => esc_html__( 'Check to Enable Title', 'personal-trainer' ),
            'section'           => 'personaltrainer_testimonials',
            'type'              => 'checkbox',
        )
     );
    }
   $number = get_theme_mod( 'personaltrainer_testimonial_number', 3 );

    for ( $i = 1; $i <= $number ; $i++ ) {
        //for CPT
        personaltrainer_register_option( $wp_customize, array(
                'name'              => 'personaltrainer_testimonial_cpt_' . $i,
                'sanitize_callback' => 'personaltrainer_sanitize_post',
                'active_callback'   => 'personaltrainer_is_testimonial_active',
                'label'             => esc_html__( 'Testimonial', 'personal-trainer' ) . ' ' . $i ,
                'section'           => 'personaltrainer_testimonials',
                'type'              => 'select',
                'choices'           => personaltrainer_generate_post_array( 'jetpack-testimonial' ),
            )
        );
    } // End for().
}
add_action( 'customize_register', 'personaltrainer_testimonial_options' );

/**
 * Active Callback Functions
 */
if ( ! function_exists( 'personaltrainer_is_testimonial_active' ) ) :
    /**
    * Return true if testimonial is active
    *
    * @since Personal Trainer 0.1
    */
    function personaltrainer_is_testimonial_active( $control ) {
        $enable = $control->manager->get_setting( 'personaltrainer_testimonial_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_testimonials_active' ) ) :
    /**
    * Return true if service is active
    *
    * @since Personal Trainer 0.1
    */
    function personaltrainer_is_ect_testimonials_active( $control ) {
        return ( class_exists( 'Essential_Content_Jetpack_Testimonial' ) || class_exists( 'Essential_Content_Pro_Jetpack_Testimonial' ) );
    }
endif;

if ( ! function_exists( 'personaltrainer_is_ect_testimonials_inactive' ) ) :
    /**
    * Return true if service is active
    *
    * @since Personal Trainer 0.1
    */
    function personaltrainer_is_ect_testimonials_inactive( $control ) {
        return ! ( class_exists( 'Essential_Content_Jetpack_Testimonial' ) || class_exists( 'Essential_Content_Pro_Jetpack_Testimonial' ) );
    }
endif;

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