<?php
/**
 * Adding support for WooCommerce Products Showcase Option
 */

/**
 * Add WooCommerce Product Showcase Options to customizer
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function signify_woo_products_showcase( $wp_customize ) {
   $wp_customize->add_section( 'signify_woo_products_showcase', array(
        'title' => esc_html__( 'WooCommerce Products Showcase', 'signify-pro' ),
        'panel' => 'signify_theme_options',
    ) );

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

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

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_woo_products_showcase_headline',
            'default'           => esc_html__( 'Exclusive Merch', 'signify-pro' ),
            'sanitize_callback' => 'wp_kses_post',
            'label'             => esc_html__( 'Headline', 'signify-pro' ),
            'active_callback'   => 'signify_is_woo_products_showcase_active',
            'section'           => 'signify_woo_products_showcase',
            'type'              => 'text',
        )
    );

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_woo_products_showcase_subheadline',
            'sanitize_callback' => 'wp_kses_post',
            'label'             => esc_html__( 'Sub headline', 'signify-pro' ),
            'active_callback'   => 'signify_is_woo_products_showcase_active',
            'section'           => 'signify_woo_products_showcase',
            'type'              => 'text',
        )
    );

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_woo_products_showcase_number',
            'default'           => 8,
            'sanitize_callback' => 'signify_sanitize_products_number_range',
            'active_callback'   => 'signify_is_woo_products_showcase_active',
            'description'       => esc_html__( 'Save and refresh the page if No. of Products is changed. Set -1 to display all', 'signify-pro' ),
            'input_attrs'       => array(
                'style' => 'width: 70px;',
                'min'   => -1,
            ),
            'label'             => esc_html__( 'No of Products', 'signify-pro' ),
            'section'           => 'signify_woo_products_showcase',
            'type'              => 'number',
        )
    );

    signify_register_option( $wp_customize, array(
            'name'               => 'signify_woo_products_showcase_columns',
            'default'            => 4,
            'sanitize_callback'  => 'signify_sanitize_number_range',
            'active_callback'    => 'signify_is_woo_products_showcase_active',
            'description'        => esc_html__( 'Theme supports up to 4 columns', 'signify-pro' ),
            'label'              => esc_html__( 'No of Columns', 'signify-pro' ),
            'section'            => 'signify_woo_products_showcase',
            'type'               => 'number',
            'input_attrs'       => array(
                'style' => 'width: 50px;',
                'min'   => 1,
                'max'   => 4,
            ),
        )
    );

    signify_register_option( $wp_customize, array(
            'name'               => 'signify_woo_products_showcase_paginate',
            'default'            => 'false',
            'sanitize_callback'  => 'signify_sanitize_select',
            'active_callback'    => 'signify_is_woo_products_showcase_active',
            'label'              => esc_html__( 'Paginate', 'signify-pro' ),
            'section'            => 'signify_woo_products_showcase',
            'type'               => 'radio',
            'choices'            => array(
                'false' => esc_html__( 'No', 'signify-pro' ),
                'true' => esc_html__( 'Yes', 'signify-pro' ),
            ),
        )
    );

    signify_register_option( $wp_customize, array(
            'name'               => 'signify_woo_products_showcase_orderby',
            'default'            => 'title',
            'sanitize_callback'  => 'signify_sanitize_select',
            'active_callback'    => 'signify_is_woo_products_showcase_active',
            'label'              => esc_html__( 'Order By', 'signify-pro' ),
            'section'            => 'signify_woo_products_showcase',
            'type'               => 'select',
            'choices'            => array(
                'date'       => esc_html__( 'Date - The date the product was published', 'signify-pro' ),
                'id'         => esc_html__( 'ID - The post ID of the product', 'signify-pro' ),
                'menu_order' => esc_html__( 'Menu Order - The Menu Order, if set (lower numbers display first)', 'signify-pro' ),
                'popularity' => esc_html__( 'Popularity - The number of purchases', 'signify-pro' ),
                'rand'       => esc_html__( 'Random', 'signify-pro' ),
                'rating'     => esc_html__( 'Rating - The average product rating', 'signify-pro' ),
                'title'      => esc_html__( 'Title - The product title', 'signify-pro' ),
            ),
        )
    );

    signify_register_option( $wp_customize, array(
            'name'               => 'signify_woo_products_showcase_products_filter',
            'default'            => 'none',
            'sanitize_callback'  => 'signify_sanitize_select',
            'active_callback'    => 'signify_is_woo_products_showcase_active',
            'label'              => esc_html__( 'Products Filter', 'signify-pro' ),
            'section'            => 'signify_woo_products_showcase',
            'type'               => 'radio',
            'choices'            => array(
                'none'         => esc_html__( 'None', 'signify-pro' ),
                'on_sale'      => esc_html__( 'Retrieve on sale products', 'signify-pro' ),
                'best_selling' => esc_html__( 'Retrieve best selling products', 'signify-pro' ),
                'top_rated'    => esc_html__( 'Retrieve top rated products', 'signify-pro' ),
            ),
        )
    );

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_woo_products_showcase_featured',
            'sanitize_callback' => 'signify_sanitize_checkbox',
            'active_callback'   => 'signify_is_woo_products_showcase_active',
            'label'             => esc_html__( 'Show only Products that are marked as Featured Products', 'signify-pro' ),
            'section'           => 'signify_woo_products_showcase',
            'type'              => 'checkbox',
        )
    );

    signify_register_option( $wp_customize, array(
            'name'               => 'signify_woo_products_showcase_order',
            'default'            => 'ASC',
            'sanitize_callback'  => 'signify_sanitize_select',
            'active_callback'    => 'signify_is_woo_products_showcase_active',
            'label'              => esc_html__( 'Order', 'signify-pro' ),
            'section'            => 'signify_woo_products_showcase',
            'type'               => 'radio',
            'choices'            => array(
                'ASC'  => esc_html__( 'Ascending', 'signify-pro' ),
                'DESC' => esc_html__( 'Descending', 'signify-pro' ),
            ),
        )
    );

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_woo_products_showcase_skus',
            'description'       => esc_html__( 'Comma separated list of product SKUs', 'signify-pro' ),
            'sanitize_callback' => 'sanitize_text_field',
            'active_callback'   => 'signify_is_woo_products_showcase_active',
            'label'             => esc_html__( 'SKUs', 'signify-pro' ),
            'section'           => 'signify_woo_products_showcase',
            'type'              => 'text',
        )
    );

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_woo_products_showcase_category',
            'description'       => esc_html__( 'Comma separated list of category slugs', 'signify-pro' ),
            'sanitize_callback' => 'sanitize_text_field',
            'active_callback'   => 'signify_is_woo_products_showcase_active',
            'label'             => esc_html__( 'Category', 'signify-pro' ),
            'section'           => 'signify_woo_products_showcase',
            'type'              => 'textarea',
        )
    );

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_woo_products_showcase_text',
            'default'           => esc_html__( 'Go to Shop Page', 'signify-pro' ),
            'sanitize_callback' => 'sanitize_text_field',
            'active_callback'   => 'signify_is_woo_products_showcase_active',
            'label'             => esc_html__( 'Button Text', 'signify-pro' ),
            'section'           => 'signify_woo_products_showcase',
            'type'              => 'text',
        )
    );
    $shop_page_url = get_permalink( wc_get_page_id( 'shop' ) );
    signify_register_option( $wp_customize, array(
            'name'              => 'signify_woo_products_showcase_link',
            'default'           =>  esc_url( $shop_page_url ),
            'sanitize_callback' => 'esc_url_raw',
            'active_callback'   => 'signify_is_woo_products_showcase_active',
            'label'             => esc_html__( 'Button Link', 'signify-pro' ),
            'section'           => 'signify_woo_products_showcase',
        )
    );

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

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

        return signify_check_section( $enable );
    }
endif;
