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

/**
 * Add testimonial options to theme options
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function signify_testimonial_options( $wp_customize ) {
    // Add note to Jetpack Testimonial Section
    signify_register_option( $wp_customize, array(
            'name'              => 'signify_jetpack_testimonial_cpt_note',
            'sanitize_callback' => 'sanitize_text_field',
            'custom_control'    => 'Signify_Note_Control',
            'label'             => sprintf( esc_html__( 'For Testimonial Options for Catch Revolution Theme, go %1$shere%2$s', 'signify-pro' ),
                '<a href="javascript:wp.customize.section( \'signify_testimonials\' ).focus();">',
                 '</a>'
            ),
           'section'            => 'jetpack_testimonials',
            'type'              => 'description',
            'priority'          => 1,
        )
    );

    $wp_customize->add_section( 'signify_testimonials', array(
            'panel'    => 'signify_theme_options',
            'title'    => esc_html__( 'Testimonials', 'signify-pro' ),
        )
    );

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_testimonial_option',
            'default'           => 'disabled',
            'sanitize_callback' => 'signify_sanitize_select',
            'choices'           => signify_section_visibility_options(),
            'label'             => esc_html__( 'Enable on', 'signify-pro' ),
            'section'           => 'signify_testimonials',
            'type'              => 'select',
            'priority'          => 1,
        )
    );

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_testimonial_bg_image',
            'sanitize_callback' => 'signify_sanitize_image',
            'active_callback'   => 'signify_is_muic_color_scheme_active',
            'custom_control'    => 'WP_Customize_Image_Control',
            'label'             => esc_html__( 'Background Image', 'signify-pro' ),
            'section'           => 'signify_testimonials',
        )
    );

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_testimonial_transition_in',
            'default'           => 'default',
            'sanitize_callback' => 'signify_sanitize_select',
            'active_callback'   => 'signify_is_testimonial_active',
            'choices'           => signify_transition_effects(),
            'label'             => esc_html__( 'Transition In', 'signify-pro' ),
            'section'           => 'signify_testimonials',
            'type'              => 'select',
        )
    );

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_testimonial_transition_out',
            'default'           => 'default',
            'sanitize_callback' => 'signify_sanitize_select',
            'active_callback'   => 'signify_is_testimonial_active',
            'choices'           => signify_transition_effects(),
            'label'             => esc_html__( 'Transition Out', 'signify-pro' ),
            'section'           => 'signify_testimonials',
            'type'              => 'select',
        )
    );

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_testimonial_dots',
            'default'           => 1,
            'sanitize_callback' => 'signify_sanitize_checkbox',
            'active_callback'   => 'signify_is_testimonial_active',
            'label'             => esc_html__( 'Display nav dots', 'signify-pro' ),
            'section'           => 'signify_testimonials',
            'type'              => 'checkbox',
        )
    );

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_testimonial_autoplay',
            'default'           => 0,
            'sanitize_callback' => 'signify_sanitize_checkbox',
            'active_callback'   => 'signify_is_testimonial_active',
            'label'             => esc_html__( 'Autoplay', 'signify-pro' ),
            'section'           => 'signify_testimonials',
            'type'              => 'checkbox',
        )
    );

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_testimonial_loop',
            'default'           => 1,
            'sanitize_callback' => 'signify_sanitize_checkbox',
            'active_callback'   => 'signify_is_testimonial_active',
            'label'             => esc_html__( 'Loop (Last to first)', 'signify-pro' ),
            'section'           => 'signify_testimonials',
            'type'              => 'checkbox',
        )
    );

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_testimonial_transition_timeout',
            'default'           => 4,
            'sanitize_callback' => 'signify_sanitize_number_range',
            'active_callback'   => 'signify_is_testimonial_active',
            'input_attrs'       => array(
                'style'       => 'width: 100px;',
                'min'         => 0,
            ),
            'label'             => esc_html__( 'Transition timeout', 'signify-pro' ),
            'section'           => 'signify_testimonials',
            'type'              => 'number',
        )
    );

    $type = signify_section_type_options();

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

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

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_testimonial_cpt_note',
            'sanitize_callback' => 'sanitize_text_field',
            'custom_control'    => 'Signify_Note_Control',
            'active_callback'   => 'signify_is_jetpack_testimonial_active',
            /* translators: 1: <a>/link tag start, 2: </a>/link tag close. */
            'label'             => sprintf( esc_html__( 'For CPT heading and sub-heading, go %1$shere%2$s', 'signify-pro' ),
                '<a href="javascript:wp.customize.section( \'jetpack_testimonials\' ).focus();">',
                '</a>'
            ),
            'section'           => 'signify_testimonials',
            'type'              => 'description',
        )
    );

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_testimonial_sub_title',
            'sanitize_callback' => 'wp_kses_post',
            'active_callback'   => 'signify_is_testimonial_active',
            'label'             => esc_html__( 'Section Tagline', 'signify-pro' ),
            'section'           => 'signify_testimonials',
            'type'              => 'text',
        )
    );

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_testimonial_title',
            'sanitize_callback' => 'wp_kses_post',
            'active_callback'   => 'signify_is_cpt_testimonial_inactive',
            'label'             => esc_html__( 'Section Title', 'signify-pro' ),
            'section'           => 'signify_testimonials',
            'type'              => 'text',
        )
    );

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_testimonial_description',
            'sanitize_callback' => 'wp_kses_post',
            'active_callback'   => 'signify_is_cpt_testimonial_inactive',
            'label'             => esc_html__( 'Section Description', 'signify-pro' ),
            'section'           => 'signify_testimonials',
            'type'              => 'textarea',
        )
    );

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_testimonial_number',
            'default'           => '5',
            'sanitize_callback' => 'signify_sanitize_number_range',
            'active_callback'   => 'signify_is_testimonial_active',
            'label'             => esc_html__( 'Number of items', 'signify-pro' ),
            'section'           => 'signify_testimonials',
            'type'              => 'number',
            'input_attrs'       => array(
                'style'             => 'width: 100px;',
                'min'               => 0,
            ),
        )
    );

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_testimonial_enable_title',
            'default'           => 1,
            'sanitize_callback' => 'signify_sanitize_checkbox',
            'active_callback'   => 'signify_is_testimonial_active',
            'label'             => esc_html__( 'Testimonial Title', 'signify-pro' ),
            'section'           => 'signify_testimonials',
            'type'              => 'checkbox',
        )
    );

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

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

    $number = get_theme_mod( 'signify_testimonial_number', 5 );

    for ( $i = 1; $i <= $number ; $i++ ) {
        //for featured post content
        signify_register_option( $wp_customize, array(
                'name'              => 'signify_testimonial_post_' . $i,
                'sanitize_callback' => 'signify_sanitize_post',
                'active_callback'   => 'signify_is_post_testimonial_active',
                'input_attrs'       => array(
                'style'             => 'width: 100px;'
                ),
                'label'             => esc_html__( 'Post', 'signify-pro' ) . ' ' . $i ,
                'section'           => 'signify_testimonials',
                'choices'           => signify_generate_post_array(),
                'type'              => 'select',
            )
        );

        //for CPT
        signify_register_option( $wp_customize, array(
                'name'              => 'signify_testimonial_cpt_' . $i,
                'sanitize_callback' => 'signify_sanitize_post',
                'active_callback'   => 'signify_is_jetpack_testimonial_active',
                'label'             => esc_html__( 'Testimonial', 'signify-pro' ) . ' ' . $i ,
                'section'           => 'signify_testimonials',
                'type'              => 'select',
                'choices'           => signify_generate_post_array( 'jetpack-testimonial' ),
            )
        );

        signify_register_option( $wp_customize, array(
                'name'              => 'signify_testimonial_page_' . $i,
                'sanitize_callback' => 'signify_sanitize_post',
                'active_callback'   => 'signify_is_page_testimonial_active',
                'label'             => esc_html__( 'Page', 'signify-pro' ) . ' ' . $i ,
                'section'           => 'signify_testimonials',
                'type'              => 'dropdown-pages',
            )
        );

        signify_register_option( $wp_customize, array(
                'name'              => 'signify_testimonial_note_' . $i,
                'sanitize_callback' => 'sanitize_text_field',
                'custom_control'    => 'Signify_Note_Control',
                'active_callback'   => 'signify_is_image_testimonial_active',
                'label'             => esc_html__( 'Testimonial #', 'signify-pro' ) .  $i,
                'section'           => 'signify_testimonials',
                'type'              => 'description',
            )
        );

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

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

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

        signify_register_option( $wp_customize, array(
                'name'              => 'signify_testimonial_content_' . $i,
                'sanitize_callback' => 'sanitize_text_field',
                'active_callback'   => 'signify_is_image_testimonial_active',
                'label'             => esc_html__( 'Testimonial Text', 'signify-pro' ),
                'section'           => 'signify_testimonials',
                'type'              => 'textarea',
            )
        );

        signify_register_option( $wp_customize, array(
                'name'              => 'signify_testimonial_title_' . $i,
                'sanitize_callback' => 'sanitize_text_field',
                'active_callback'   => 'signify_is_image_testimonial_active',
                'label'             => esc_html__( 'Name', 'signify-pro' ),
                'section'           => 'signify_testimonials',
                'type'              => 'text',
            )
        );

        signify_register_option( $wp_customize, array(
                'name'              => 'signify_testimonial_position_' . $i,
                'sanitize_callback' => 'sanitize_text_field',
                'active_callback'   => 'signify_is_cpt_testimonial_inactive',
                'label'             => esc_html__( 'Position', 'signify-pro' ),
                'section'           => 'signify_testimonials',
                'type'              => 'text',
            )
        );
    } // End for().
}
add_action( 'customize_register', 'signify_testimonial_options' );

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

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

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

        //return true only if previwed page on customizer matches the type of option selected
        return ( signify_is_testimonial_active( $control ) && 'post' === $type );
    }
endif;

if ( ! function_exists( 'signify_is_jetpack_testimonial_active' ) ) :
    /**
    * Return true if jetpack testimonial is active
    *
    * @since 1.0.0
    */
    function signify_is_jetpack_testimonial_active( $control ) {
        $type = $control->manager->get_setting( 'signify_testimonial_type' )->value();

        //return true only if previwed page on customizer matches the type of option selected
        return ( signify_is_testimonial_active( $control ) && 'jetpack-testimonial' === $type );
    }
endif;

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

        //return true only if previwed page on customizer matches the type of option selected
        return ( signify_is_testimonial_active( $control ) && 'page' === $type );
    }
endif;

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

        //return true only if previwed page on customizer matches the type of option selected
        return ( signify_is_testimonial_active( $control ) && 'category' === $type );
    }
endif;

if ( ! function_exists( 'signify_is_post_page_category_cpt_testimonial_active' ) ) :
    /**
    * Return true if custom option is not active
    *
    * @since 1.0.0
    */
    function signify_is_post_page_category_cpt_testimonial_active( $control ) {
        $type = $control->manager->get_setting( 'signify_testimonial_type' )->value();

        //return true only if previwed page on customizer matches the type of option selected
        return ( signify_is_testimonial_active( $control ) && 'custom' !== $type );
    }
endif;

if ( ! function_exists( 'signify_is_cpt_testimonial_inactive' ) ) :
    /**
    * Return true if cpt option is not active
    *
    * @since 1.0.0
    */
    function signify_is_cpt_testimonial_inactive( $control ) {
        $type = $control->manager->get_setting( 'signify_testimonial_type' )->value();

        //return true only if previwed page on customizer matches the type of option selected
        return ( signify_is_testimonial_active( $control ) && 'jetpack-testimonial' !== $type );
    }
endif;

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

        //return true only if previwed page on customizer matches the type of option selected
        return ( signify_is_testimonial_active( $control ) && 'custom' === $type );
    }
endif;

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

        //return true only if previwed page on customizer matches the type of option selected
        return ( signify_is_testimonial_active( $control ) && 'tune' === $type );
    }
endif;
