<?php
/**
 * Gallery Options
 *
 * @package Signify
 */

/**
 * Add gallery options to theme options
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function signify_gallery_options( $wp_customize ) {
	$wp_customize->add_section( 'signify_gallery_options', array(
			'title' => esc_html__( 'Gallery', 'signify-pro' ),
			'panel' => 'signify_theme_options',
		)
	);

	signify_register_option( $wp_customize, array(
            'name'              => 'signify_gallery_note',
            'sanitize_callback' => 'sanitize_text_field',
            'custom_control'    => 'Signify_Note_Control',
            'active_callback'   => 'signify_is_catch_gallery_inactive',
            'label'             => sprintf( esc_html__( 'For Gallery, install %1$sCatch Gallery%2$s Plugin', 'signify-pro' ),
                '<a target="_blank" href="https://wordpress.org/plugins/catch-gallery/">',
                '</a>'
            ),
            'section'           => 'signify_gallery_options',
            'type'              => 'description',
            'priority'          => 1,
        )
    );


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

	$types = signify_section_type_options();

	// Unset image as gallery does not support it.
	unset( $types['custom'] );

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_gallery_type',
			'default'           => 'page',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_gallery_active',
			'choices'           => $types,
			'label'             => esc_html__( 'Type', 'signify-pro' ),
			'section'           => 'signify_gallery_options',
			'type'              => 'select',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_gallery',
			'default'           => '0',
			'sanitize_callback' => 'signify_sanitize_post',
			'active_callback'   => 'signify_is_gallery_page_content_active',
			'label'             => esc_html__( 'Page', 'signify-pro' ),
			'section'           => 'signify_gallery_options',
			'type'              => 'dropdown-pages',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_gallery_post',
			'default'           => 0,
			'sanitize_callback' => 'signify_sanitize_post',
			'active_callback'   => 'signify_is_gallery_post_content_active',
			'label'             => esc_html__( 'Post', 'signify-pro' ),
			'section'           => 'signify_gallery_options',
			'choices'           => signify_generate_post_array(),
			'type'              => 'select',
		)
	);

	// create an empty array.
	$cats = array();

	$cats['0'] = esc_html__( '-- Select --', 'signify-pro' );

	// we loop over the categories and set the names and
	// labels we need.
	foreach ( get_categories() as $categories => $category ) {
		$cats[ $category->term_id ] = $category->name;
	}

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_gallery_category',
			'default'           => '0',
			'sanitize_callback' => 'absint',
			'active_callback'   => 'signify_is_gallery_category_content_active',
			'label'             => esc_html__( 'Category', 'signify-pro' ),
			'type'              => 'select',
			'choices'           => $cats,
			'section'           => 'signify_gallery_options',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_gallery_sub_title',
			'sanitize_callback' => 'wp_kses_post',
			'active_callback'   => 'signify_is_gallery_post_page_category_content_active',
			'label'             => esc_html__( 'Gallery Description', 'signify-pro' ),
			'section'           => 'signify_gallery_options',
			'type'              => 'textarea',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_display_gallery_title',
			'sanitize_callback' => 'signify_sanitize_checkbox',
			'active_callback'   => 'signify_is_gallery_post_page_category_content_active',
			'label'             => esc_html__( 'Display title', 'signify-pro' ),
			'section'           => 'signify_gallery_options',
			'type'    			=> 'checkbox',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_gallery_text',
			'sanitize_callback' => 'sanitize_text_field',
			'active_callback'   => 'signify_is_gallery_active',
			'label'             => esc_html__( 'Button Text', 'signify-pro' ),
			'section'           => 'signify_gallery_options',
			'type'              => 'text',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_gallery_link',
			'sanitize_callback' => 'esc_url_raw',
			'active_callback'   => 'signify_is_gallery_active',
			'label'             => esc_html__( 'Button Link', 'signify-pro' ),
			'section'           => 'signify_gallery_options',
		)
	);

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

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

		return signify_check_section( $enable );
	}
endif;

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

		return ( signify_is_gallery_active( $control ) && 'post' === $type );
	}
endif;

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

		return ( signify_is_gallery_active( $control ) && 'page' === $type );
	}
endif;

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

		return ( signify_is_gallery_active( $control ) && 'category' === $type );
	}
endif;

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

		return ( signify_is_gallery_active( $control ) && ( 'page' === $type || 'post' === $type || 'category' === $type ) );
	}
endif;

if ( ! function_exists( 'signify_is_catch_gallery_inactive' ) ) :
    /**
    *
    * @since 1.0.0
    */
    function signify_is_catch_gallery_inactive( $control ) {
        return ! ( defined( 'CATCH_GALLERY_VERSION' ) || defined( 'CATCH_GALLERY_PRO_VERSION' ) );
    }
endif;

if ( ! function_exists( 'signify_is_catch_gallery_active' ) ) :
    /**
    *
    * @since 1.0.0
    */
    function signify_is_catch_gallery_active( $control ) {
        return  ( defined( 'CATCH_GALLERY_VERSION' ) || defined( 'CATCH_GALLERY_PRO_VERSION' ) );
    }
endif;
