<?php
/**
 * Adding support for WooCommerce Plugin
 */

if ( ! class_exists( 'WooCommerce' ) ) {
    // Bail early if WooCommerce Plugin is not installed
    return;
}

if ( ! function_exists( 'gridalicious_woocommerce_setup' ) ) :
    /**
     * Sets up support for various WooCommerce features.
     */
    function gridalicious_woocommerce_setup() {
        add_theme_support( 'woocommerce' );

        $options = gridalicious_get_theme_options();

        if ( ! $options['disable_product_gallery_zoom'] ) {
            add_theme_support('wc-product-gallery-zoom');
        }

        if ( ! $options['disable_product_gallery_lightbox'] ) {
            add_theme_support('wc-product-gallery-lightbox');
        }

        if ( ! $options['disable_product_gallery_slider'] ) {
            add_theme_support('wc-product-gallery-slider');
        }
    }
endif; //gridalicious_woocommerce_setup
add_action( 'after_setup_theme', 'gridalicious_woocommerce_setup' );

/**
 * Adding support for WooCommerce Plugin
 *
 * uses remove_action to remove the WooCommerce Wrapper and add_action to add Catch Kathmandu Wrapper
 *
 */
remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );

if ( ! function_exists( 'gridalicious_woocommerce_start' ) ) :
function gridalicious_woocommerce_start() {
	echo '<main role="main" class="site-main woocommerce" id="main"><div class="woocommerce-wrap">';
}
endif; //gridalicious_woocommerce_start
add_action( 'woocommerce_before_main_content', 'gridalicious_woocommerce_start', 15 );

if ( ! function_exists( 'gridalicious_woocommerce_end' ) ) :
function gridalicious_woocommerce_end() {
	echo '</div><!-- .woocommerce-wrap --></main><!-- #main -->';
}
endif; //gridalicious_woocommerce_end
add_action( 'woocommerce_after_main_content', 'gridalicious_woocommerce_end', 15 );


function gridalicious_woocommerce_options( $wp_customize ) {
    $defaults = gridalicious_get_default_theme_options();

    $wp_customize->add_setting( 'gridalicious_theme_options[woocommerce_layout]', array(
		'capability'        => 'edit_theme_options',
		'default'           => $defaults['woocommerce_layout'],
		'sanitize_callback' => 'gridalicious_sanitize_select',
    ) );

    $wp_customize->add_control( 'gridalicious_theme_options[woocommerce_layout]', array(
		'choices'  => gridalicious_layouts(),
		'label'    => esc_html__( 'Woocommerce Layout', 'gridalicious-pro' ),
		'priority' => 2,
		'section'  => 'gridalicious_layout',
		'settings' => 'gridalicious_theme_options[woocommerce_layout]',
		'type'     => 'select',
    ) );

    // WooCommerce Options
    $wp_customize->add_section( 'gridalicious_woocommerce_options', array(
        'title'       => esc_html__( 'WooCommerce Options', 'gridalicious-pro' ),
        'panel'       => 'gridalicious_theme_options',
        'priority'    => 260,
        'description' => esc_html__( 'Since these options are added via theme support, you will need to save and refresh the customizer to view the full effect.', 'gridalicious-pro' ),
    ) );

    $wp_customize->add_setting( 'gridalicious_theme_options[disable_product_gallery_zoom]', array(
        'default'           => $defaults['disable_product_gallery_zoom'],
        'sanitize_callback' => 'gridalicious_sanitize_checkbox',
    ) );

    $wp_customize->add_control( 'gridalicious_theme_options[disable_product_gallery_zoom]', array(
        'label'     => esc_html__( 'Check to disable Product Gallery Zoom', 'gridalicious-pro' ),
        'section'   => 'gridalicious_woocommerce_options',
        'type'      => 'checkbox',
    ) );

    $wp_customize->add_setting( 'gridalicious_theme_options[disable_product_gallery_lightbox]', array(
        'default'           => $defaults['disable_product_gallery_lightbox'],
        'sanitize_callback' => 'gridalicious_sanitize_checkbox',
    ) );

    $wp_customize->add_control( 'gridalicious_theme_options[disable_product_gallery_lightbox]', array(
        'label'     => esc_html__( 'Check to disable Product Gallery Lightbox', 'gridalicious-pro' ),
        'section'   => 'gridalicious_woocommerce_options',
        'type'      => 'checkbox',
    ) );

    $wp_customize->add_setting( 'gridalicious_theme_options[disable_product_gallery_slider]', array(
        'default'           => $defaults['disable_product_gallery_slider'],
        'sanitize_callback' => 'gridalicious_sanitize_checkbox',
    ) );

    $wp_customize->add_control( 'gridalicious_theme_options[disable_product_gallery_slider]', array(
        'label'     => esc_html__( 'Check to disable Product Gallery Slider', 'gridalicious-pro' ),
        'section'   => 'gridalicious_woocommerce_options',
        'type'      => 'checkbox',
    ) );
}
add_action( 'customize_register', 'gridalicious_woocommerce_options' );