<?php
/**
 * The main template for implementing Theme/Customzer Options
 *
 * @package Catch Themes
 * @subpackage Gridalicious
 * @since Gridalicious 1.0
 */

if ( ! defined( 'GRIDALICIOUS_THEME_VERSION' ) ) {
	header( 'Status: 403 Forbidden' );
	header( 'HTTP/1.1 403 Forbidden' );
	exit();
}


/**
 * Implements Gridalicious theme options into Theme Customizer.
 *
 * @param $wp_customize Theme Customizer object
 * @return void
 *
 * @since Gridalicious 1.0
 */
function gridalicious_customize_register( $wp_customize ) {
	$wp_customize->get_setting( 'blogname' )->transport			= 'postMessage';

	/**
	  * Set priority of blogname (Site Title) to 1.
	  *  Strangly, if more than two options is added, Site title is moved below Tagline. This rectifies this issue.
	  */
	$wp_customize->get_control( 'blogname' )->priority			= 1;

	$wp_customize->get_setting( 'blogdescription' )->transport	= 'postMessage';

	$options  = gridalicious_get_theme_options();

	$defaults = gridalicious_get_default_theme_options();

	//Custom Controls
	require trailingslashit( get_template_directory() ) . 'inc/customizer-includes/gridalicious-customizer-custom-controls.php';

	//@remove Remove this block when WordPress 4.8 is released
	if ( ! function_exists( 'has_custom_logo' ) ) {
		// Custom Logo (added to Site Title and Tagline section in Theme Customizer)
		$wp_customize->add_setting( 'gridalicious_theme_options[logo]', array(
			'capability'		=> 'edit_theme_options',
			'default'			=> $defaults['logo'],
			'sanitize_callback'	=> 'gridalicious_sanitize_image'
		) );

		$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'logo', array(
			'label'		=> __( 'Logo', 'gridalicious-pro' ),
			'priority'	=> 100,
			'section'   => 'title_tagline',
	        'settings'  => 'gridalicious_theme_options[logo]',
	    ) ) );

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

		$wp_customize->add_control( 'gridalicious_theme_options[logo_disable]', array(
			'label'    => __( 'Check to disable logo', 'gridalicious-pro' ),
			'priority' => 101,
			'section'  => 'title_tagline',
			'settings' => 'gridalicious_theme_options[logo_disable]',
			'type'     => 'checkbox',
		) );

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

		$wp_customize->add_control( 'gridalicious_logo_alt_text', array(
			'label'    	=> __( 'Logo Alt Text', 'gridalicious-pro' ),
			'priority'	=> 102,
			'section' 	=> 'title_tagline',
			'settings' 	=> 'gridalicious_theme_options[logo_alt_text]',
			'type'     	=> 'text',
		) );
	}

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

	$wp_customize->add_control( 'gridalicious_theme_options[move_title_tagline]', array(
		'label'    => __( 'Check to move Site Title and Tagline before logo', 'gridalicious-pro' ),
		'priority' => function_exists( 'has_custom_logo' ) ? 10 : 103,
		'section'  => 'title_tagline',
		'settings' => 'gridalicious_theme_options[move_title_tagline]',
		'type'     => 'checkbox',
	) );
	// Custom Logo End

	// Header Options (added to Header section in Theme Customizer)
	require trailingslashit( get_template_directory() ) . 'inc/customizer-includes/gridalicious-customizer-header-options.php';


   	//Additional Menu Options
   	$wp_customize->add_section( 'gridalicious_menu_options', array(
		'description'	=> __( 'Extra Menu Options specific to this theme', 'gridalicious-pro' ),
		'priority' 		=> 105,
		'title'    		=> __( 'Menu Options', 'gridalicious-pro' ),
	) );

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

	$wp_customize->add_control( 'gridalicious_theme_options[primary_menu_disable]', array(
		'label'    => __( 'Check to disable Primary Menu', 'gridalicious-pro' ),
		'section'  => 'gridalicious_menu_options',
		'settings' => 'gridalicious_theme_options[primary_menu_disable]',
		'type'     => 'checkbox',
	) );

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

	$wp_customize->add_control( 'gridalicious_theme_options[primary_search_disable]', array(
		'label'    => __( 'Check to disable search box in Primary Menu', 'gridalicious-pro' ),
		'section'  => 'gridalicious_menu_options',
		'settings' => 'gridalicious_theme_options[primary_search_disable]',
		'type'     => 'checkbox',
	) );

	//Theme Options
	require trailingslashit( get_template_directory() ) . 'inc/customizer-includes/gridalicious-customizer-theme-options.php';

	// Color Options
	// Moved from Default Color Control to Catbase Color Options
   	$wp_customize->get_control( 'background_color' )->section			= 'gridalicious_color_scheme';
	$wp_customize->get_control( 'header_textcolor' )->section			= 'gridalicious_header_color_options';
	$wp_customize->get_control( 'header_textcolor' )->input_attr	=	array('display'=>'block');

   	require trailingslashit( get_template_directory() ) . 'inc/customizer-includes/gridalicious-customizer-color-options.php';

	//Featured Content Setting
	require trailingslashit( get_template_directory() ) . 'inc/customizer-includes/gridalicious-customizer-featured-content-setting.php';

	//Featured Grid Content
	require trailingslashit( get_template_directory() ) . 'inc/customizer-includes/gridalicious-customizer-featured-grid-content.php';

	//Social Links
	require trailingslashit( get_template_directory() ) . 'inc/customizer-includes/gridalicious-customizer-social-icons.php';

	// Reset all settings to default
	$wp_customize->add_section( 'gridalicious_reset_all_settings', array(
		'description'	=> __( 'Caution: Reset all settings to default. Refresh the page after save to view full effects.', 'gridalicious-pro' ),
		'priority' 		=> 700,
		'title'    		=> __( 'Reset all settings', 'gridalicious-pro' ),
	) );

	$wp_customize->add_setting( 'gridalicious_theme_options[reset_all_settings]', array(
		'capability'		=> 'edit_theme_options',
		'default'			=> $defaults['reset_all_settings'],
		'sanitize_callback' => 'gridalicious_sanitize_checkbox',
		'transport'			=> 'postMessage',
	) );

	$wp_customize->add_control( 'gridalicious_theme_options[reset_all_settings]', array(
		'label'    => __( 'Check to reset all settings to default', 'gridalicious-pro' ),
		'section'  => 'gridalicious_reset_all_settings',
		'settings' => 'gridalicious_theme_options[reset_all_settings]',
		'type'     => 'checkbox',
	) );
	// Reset all settings to default end


	//Important Links
		$wp_customize->add_section( 'important_links', array(
			'priority' 		=> 999,
			'title'   	 	=> __( 'Important Links', 'gridalicious-pro' ),
		) );

		/**
		 * Has dummy Sanitizaition function as it contains no value to be sanitized
		 */
		$wp_customize->add_setting( 'important_links', array(
			'sanitize_callback'	=> 'gridalicious_sanitize_important_link',
		) );

		$wp_customize->add_control( new Gridalicious_Important_Links( $wp_customize, 'important_links', array(
	        'label'   	=> __( 'Important Links', 'gridalicious-pro' ),
	         'section'  	=> 'important_links',
	        'settings' 	=> 'important_links',
	        'type'     	=> 'important_links',
	    ) ) );
	    //Important Links End
}
add_action( 'customize_register', 'gridalicious_customize_register' );


/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously for gridalicious.
 * And flushes out all transient data on preview
 *
 * @since Gridalicious 1.0
 */
function gridalicious_customize_preview() {
	wp_enqueue_script( 'gridalicious_customizer', trailingslashit( esc_url ( get_template_directory_uri() ) ) . 'js/gridalicious-customizer.min.js', array( 'customize-preview' ), '20120827', true );

	//Flush transients
	gridalicious_flush_transients();
}
add_action( 'customize_preview_init', 'gridalicious_customize_preview' );


/**
 * Custom scripts and styles on customize.php for gridalicious.
 *
 * @since Gridalicious Pro 1.0
 */
function gridalicious_customize_scripts() {
	wp_enqueue_script( 'gridalicious_customizer_custom', trailingslashit( esc_url ( get_template_directory_uri() ) ) . 'js/gridalicious-customizer-custom-scripts.min.js', array( 'customize-controls', 'iris', 'underscore', 'wp-util' ), '20150630', true );

	$gridalicious_data = array(
			'gridalicious_color_list' => gridalicious_color_list(),
			'reset_message'           => esc_html__( 'Refresh the customizer page after saving to view reset effects', 'gridalicious-pro' )
		);

	// Send list of color variables as object to custom customizer js
	wp_localize_script( 'gridalicious_customizer_custom', 'gridalicious_data', $gridalicious_data );
}
add_action( 'customize_controls_enqueue_scripts', 'gridalicious_customize_scripts');


/**
 * Returns list of color keys of array with default values for each color scheme as index
 *
 * @since Gridalicious Pro 3.1
 */
function gridalicious_color_list() {
	// Get default color scheme values
	$default 		= gridalicious_get_default_theme_options();
	// Get default dark color scheme valies
	$default_dark 	= gridalicious_default_dark_color_options();

	$gridalicious_color_list['background_color']['light']	= $default['background_color'];
	$gridalicious_color_list['background_color']['dark']	= $default_dark['background_color'];

	$gridalicious_color_list['header_textcolor']['light']	= $default['header_textcolor'];
	$gridalicious_color_list['header_textcolor']['dark']	= $default_dark['header_textcolor'];

	// Get coloy keys for menu
	$gridalicious_menu_color_option = gridalicious_get_menu_color_options();

	// Get coloy keys for all other options except menu
	$gridalicious_get_color_list = array_merge(
									gridalicious_get_basic_color_options(),
									gridalicious_get_header_color_options(),
									gridalicious_get_content_color_options(),
									gridalicious_get_sidebar_color_options(),
									gridalicious_get_pagination_color_options(),
									gridalicious_get_footer_color_options(),
									gridalicious_get_promotion_headline_color_options(),
									gridalicious_get_scrollup_color_options(),
									gridalicious_get_grid_content_color_options(),
									gridalicious_get_featured_content_color_options(),
									$gridalicious_menu_color_option // Primary Menu Color Options
									);

	// Set light and dark color keys with default values for each color scheme as index
	foreach( $gridalicious_get_color_list as $key => $color_option ) {
		$lower_color_option	=	$key;

		$gridalicious_color_list[ 'gridalicious_theme_options[' . $lower_color_option . ']' ]['light'] 	= $default[ $lower_color_option ];
		$gridalicious_color_list[ 'gridalicious_theme_options[' . $lower_color_option . ']' ]['dark'] 	= $default_dark[ $lower_color_option ];
	}

	foreach( $gridalicious_menu_color_option as $key => $color_option ) {
		$lower_color_option	=	'secondary_' . $key;

		$gridalicious_color_list[ 'gridalicious_theme_options[' . $lower_color_option . ']' ]['light'] 	= $default[ $lower_color_option ];
		$gridalicious_color_list[ 'gridalicious_theme_options[' . $lower_color_option . ']' ]['dark'] 	= $default_dark[ $lower_color_option ];

		$lower_color_option	=	'header_right_' . $key;

		$gridalicious_color_list[ 'gridalicious_theme_options[' . $lower_color_option . ']' ]['light'] 	= $default[ $lower_color_option ];
		$gridalicious_color_list[ 'gridalicious_theme_options[' . $lower_color_option . ']' ]['dark'] 	= $default_dark[ $lower_color_option ];

		//Add Footer Menu Color Options
		$lower_color_option	=	'footer_' . $key;
		if ( 'footer_sub_menu_text_color' == $lower_color_option || 'footer_sub_menu_background_color' == $lower_color_option || 'footer_menu_background_color' == $lower_color_option ) {
			continue;
		}

		$gridalicious_color_list[ 'gridalicious_theme_options[' . $lower_color_option . ']' ]['light'] 	= $default[ $lower_color_option ];
		$gridalicious_color_list[ 'gridalicious_theme_options[' . $lower_color_option . ']' ]['dark'] 	= $default_dark[ $lower_color_option ];

	}

	return $gridalicious_color_list;
}


/**
 * Function to reset date with respect to condition
 */
function gridalicious_reset_data() {
	$options  = gridalicious_get_theme_options();
    if ( $options['reset_all_settings'] ) {
    	remove_theme_mods();

        // Flush out all transients	on reset
        gridalicious_flush_transients();

        return;
    }

	$defaults = gridalicious_get_default_theme_options();
    if ( $options['reset_footer_content'] ) {
    	//Reset Footer Editor Options
		$options['footer_left_content']     = $defaults[ 'footer_left_content' ];
		$options['footer_right_content']     = $defaults[ 'footer_right_content' ];
		$options['reset_footer_content'] = 0;

		set_theme_mod( 'gridalicious_theme_options', $options );
    }

    if ( $options['reset_typography'] ) {
    	$font_family_options = array(
			'title_font',
			'body_font',
			'tagline_font',
			'content_title_font',
			'content_font',
			'headings_font'
		);

		foreach ( $font_family_options as $font_family_option ) {
			$options[ $font_family_option ] = $defaults[ $font_family_option ];
		}

		$options['reset_typography'] = 0;

        set_theme_mod( 'gridalicious_theme_options', $options );
    }
}
add_action( 'customize_save_after', 'gridalicious_reset_data' );


//Active callbacks for customizer
require trailingslashit( get_template_directory() ) . 'inc/customizer-includes/gridalicious-customizer-active-callbacks.php';


//Sanitize functions for customizer
require trailingslashit( get_template_directory() ) . 'inc/customizer-includes/gridalicious-customizer-sanitize-functions.php';
