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

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

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

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

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_portfolio_layout',
			'default'           => 'layout-three',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_portfolio_active',
			'choices'           => signify_sections_layout_options(),
			'label'             => esc_html__( 'Select Portfolio Layout', 'signify-pro' ),
			'section'           => 'signify_portfolio',
			'type'              => 'select',
		)
	);

	$type = signify_section_type_options();

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

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_portfolio_type',
			'default'           => 'category',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_portfolio_active',
			'choices'           => $type,
			'description'       => sprintf( esc_html__( 'For Custom Post Type Content, install %1$sEssential Content Types%2$s Plugin with Portfolio 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_portfolio',
			'type'              => 'select',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_portfolio_fluid_layout',
			'default'			=> 0,
			'sanitize_callback' => 'signify_sanitize_checkbox',
			'active_callback'   => 'signify_is_portfolio_active',
			'label'             => esc_html__( 'Fluid Layout', 'signify-pro' ),
			'section'           => 'signify_portfolio',
			'type'    			=> 'checkbox',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_portfolio_post_thumbnail_style',
			'default'           => 'flexible',
			'active_callback'   => 'signify_is_custom_portfolio_inactive',
			'sanitize_callback' => 'signify_sanitize_select',
			'choices'           => array(
				'flexible' 		=> esc_html__( 'Flexible', 'signify-pro' ),
				'square' 		=> esc_html__( 'Square', 'signify-pro' ),
			),
			'label'             => esc_html__( 'Post Thumbnail Style', 'signify-pro' ),
			'section'           => 'signify_portfolio',
			'type'              => 'select',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_portfolio_category_nav_alignment',
			'default'           => 'nav-left',
			'active_callback'   => 'signify_is_category_portfolio_active',
			'sanitize_callback' => 'signify_sanitize_select',
			'choices'           => array(
				'nav-left' 		=> esc_html__( 'Left Align', 'signify-pro' ),
				'nav-center' 	=> esc_html__( 'Center Align', 'signify-pro' ),
				'nav-right'		=> esc_html__( 'Right Align', 'signify-pro' ),
			),
			'label'             => esc_html__( 'Category Nav Alignment', 'signify-pro' ),
			'section'           => 'signify_portfolio',
			'type'              => 'select',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_portfolio_cpt_note',
			'sanitize_callback' => 'sanitize_text_field',
			'custom_control'    => 'Signify_Note_Control',
			'active_callback'   => 'signify_is_jetpack_portfolio_active',
			'label'             => sprintf( esc_html__( 'For CPT heading and sub-heading, go %1$shere%2$s', 'signify-pro' ),
				 '<a href="javascript:wp.customize.control( \'jetpack_portfolio_title\' ).focus();">',
				 '</a>'
			),
			'section'           => 'signify_portfolio',
			'type'              => 'description',
		)
	);

	signify_section_header_options( 'signify_portfolio', 'portfolio', 'signify_is_portfolio_active' );
	
	signify_register_option( $wp_customize, array(
			'name'              => 'signify_portfolio_number',
			'default'           => 6,
			'sanitize_callback' => 'signify_sanitize_number_range',
			'active_callback'   => 'signify_is_portfolio_active',
			'label'             => esc_html__( 'Number of items to show', 'signify-pro' ),
			'section'           => 'signify_portfolio',
			'type'              => 'number',
			'input_attrs'       => array(
				'style'             => 'width: 100px;',
				'min'               => 0,
			),
		)
	);

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

	$number = get_theme_mod( 'signify_portfolio_number', 6 );

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

		//for CPT
		signify_register_option( $wp_customize, array(
				'name'              => 'signify_portfolio_cpt_' . $i,
				'sanitize_callback' => 'signify_sanitize_post',
				'active_callback'   => 'signify_is_jetpack_portfolio_active',
				'label'             => esc_html__( 'Portfolio', 'signify-pro' ) . ' ' . $i ,
				'section'           => 'signify_portfolio',
				'type'              => 'select',
				'choices'           => signify_generate_post_array( 'jetpack-portfolio' ),
			)
		);

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

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_portfolio_note_' . $i,
				'sanitize_callback' => 'sanitize_text_field',
				'custom_control'    => 'Signify_Note_Control',
				'active_callback'   => 'signify_is_image_portfolio_active',
				'label'             => esc_html__( 'Portfolio #', 'signify-pro' ) .  $i,
				'section'           => 'signify_portfolio',
				'type'              => 'description',
			)
		);

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

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

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

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_portfolio_target_' . $i,
				'sanitize_callback' => 'signify_sanitize_checkbox',
				'active_callback'   => 'signify_is_image_portfolio_active',
				'label'             => esc_html__( 'Open in New Window/Tab', 'signify-pro' ),
				'section'           => 'signify_portfolio',
				'type'    			=> 'checkbox',
			)
		);
	} // End for().

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_portfolio_meta_show',
			'default'           => 'show-meta',
			'active_callback'   => 'signify_is_post_category_post_cpt_portfolio_active',
			'sanitize_callback' => 'signify_sanitize_select',
			'choices'           => signify_meta_show(),
			'label'             => esc_html__( 'Display Meta', 'signify-pro' ),
			'section'           => 'signify_portfolio',
			'type'              => 'select',
		)
	);

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

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_portfolio_link',
			'sanitize_callback' => 'esc_url_raw',
			'active_callback'   => 'signify_is_portfolio_active',
			'label'             => esc_html__( 'Button Link', 'signify-pro' ),
			'section'           => 'signify_portfolio',
		)
	);

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

/**
 * Active Callback Functions
 */
if ( ! function_exists( 'signify_is_portfolio_active' ) ) :
	/**
	* Return true if portfolio is active
	*
	* @since 1.0.0
	*/
	function signify_is_portfolio_active( $control ) {
		$enable = $control->manager->get_setting( 'signify_portfolio_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_portfolio_active' ) ) :
	/**
	* Return true if page content is active
	*
	* @since 1.0.0
	*/
	function signify_is_post_portfolio_active( $control ) {
		$type = $control->manager->get_setting( 'signify_portfolio_type' )->value();

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

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

		return ( signify_is_portfolio_active( $control ) && 'jetpack-portfolio' === $type );
	}
endif;

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

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

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

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

if ( ! function_exists( 'signify_is_post_category_post_cpt_portfolio_active' ) ) :
	/**
	* Return true if page and image is not active
	*
	* @since 1.0.0
	*/
	function signify_is_post_category_post_cpt_portfolio_active( $control ) {
		$type = $control->manager->get_setting( 'signify_portfolio_type' )->value();

		return ( signify_is_portfolio_active( $control ) && 'page' !== $type && 'custom' !== $type );
	}
endif;

if ( ! function_exists( 'signify_is_custom_portfolio_inactive' ) ) :
	/**
	* Return true if page and image is not active
	*
	* @since 1.0.0
	*/
	function signify_is_custom_portfolio_inactive( $control ) {
		$type = $control->manager->get_setting( 'signify_portfolio_type' )->value();

		return ( signify_is_portfolio_active( $control ) && 'custom' !== $type );
	}
endif;

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

		return ( signify_is_portfolio_active( $control ) && 'jetpack-portfolio' !== $type );
	}
endif;

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

		return ( signify_is_portfolio_active( $control ) && 'custom' === $type );
	}
endif;
