<?php 
/**
 * Superior Theme Customizer support
 *
 * @package Theme Horse
 * @subpackage Superior
 * @since Superior 1.0
 */
?>
<?php
/**
 * Implement Theme Customizer additions and adjustments.
 *
 * @since Superior 1.0
 *
 */
if ( ! class_exists( 'WP_Customize_Section' ) ) {
	return null;
}

function superior_textarea_register($wp_customize){
	class Superior_Customize_Superior_upgrade extends WP_Customize_Control {
		public function render_content() { ?>
		<div class="theme-info">
			<a title="<?php esc_attr_e( 'Review Superior', 'superior' ); ?>" href="<?php echo esc_url( 'http://wordpress.org/support/view/theme-reviews/superior' ); ?>" target="_blank" rel="noopener noreferrer">
			<?php _e( 'Rate Superior', 'superior' ); ?>
			</a>
			<a href="<?php echo esc_url( 'http://themehorse.com/theme-instruction/superior/' ); ?>" title="<?php esc_attr_e( 'Superior Theme Instructions', 'superior' ); ?>" target="_blank" rel="noopener noreferrer">
			<?php _e( 'Theme Instructions', 'superior' ); ?>
			</a>
			<a href="<?php echo esc_url( 'http://themehorse.com/support-forum/' ); ?>" title="<?php esc_attr_e( 'Support Forum', 'superior' ); ?>" target="_blank" rel="noopener noreferrer">
			<?php _e( 'Support Forum', 'superior' ); ?>
			</a>
			<a href="<?php echo esc_url( 'http://themehorse.com/preview/superior/' ); ?>" title="<?php esc_attr_e( 'Superior Demo', 'superior' ); ?>" target="_blank" rel="noopener noreferrer">
			<?php _e( 'View Demo', 'superior' ); ?>
			</a>
		</div>
		<?php
		}
	}
	class Superior_Customize_Category_Control extends WP_Customize_Control {
		/**
		* The type of customize control being rendered.
		*/
		public $type = 'multiple-select';
		/**
		* Displays the multiple select on the customize screen.
		*/
		public function render_content() {
		global $superior_settings, $superior_array_of_default_settings;
		$superior_settings = wp_parse_args(  get_option( 'superior_theme_settings', array() ),  superior_get_option_defaults());
		$categories = get_categories(); ?>
			<label>
				<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
				<select <?php $this->link(); ?> multiple="multiple">
				<?php
					foreach ( $categories as $category) :?>
						<option value="<?php echo $category->cat_ID; ?>" <?php if ( in_array( $category->cat_ID, $superior_settings[ 'superior_categories' ]) ) { echo 'selected="selected"';}?>><?php echo $category->cat_name; ?></option>
					<?php endforeach; ?>
				</select>
			</label>
		<?php 
		}
	}
}
/**
 * Upsell customizer section.
 *
 * @since  1.0.0
 * @access public
 */
class Superior_Customize_Section_Upsell extends WP_Customize_Section {

	/**
	 * The type of customize section being rendered.
	 *
	 * @since  1.0.0
	 * @access public
	 * @var    string
	 */
	public $type = 'upsell';

	/**
	 * Custom button text to output.
	 *
	 * @since  1.0.0
	 * @access public
	 * @var    string
	 */
	public $pro_text = '';

	/**
	 * Custom pro button URL.
	 *
	 * @since  1.0.0
	 * @access public
	 * @var    string
	 */
	public $pro_url = '';

	/**
	 * Add custom parameters to pass to the JS via JSON.
	 *
	 * @since  1.0.0
	 * @access public
	 * @return void
	 */
	public function json() {
		$json = parent::json();

		$json['pro_text'] = $this->pro_text;
		$json['pro_url']  = esc_url( $this->pro_url );

		return $json;
	}

	/**
	 * Outputs the Underscore.js template.
	 *
	 * @since  1.0.0
	 * @access public
	 * @return void
	 */
	protected function render_template() { ?>

		<li id="accordion-section-{{ data.id }}" class="accordion-section control-section control-section-{{ data.type }} cannot-expand">
			<h3 class="accordion-section-title">
				{{ data.title }}

				<# if ( data.pro_text && data.pro_url ) { #>
					<a href="{{ data.pro_url }}" class="upgrade-to-pro" target="_blank" rel="noopener noreferrer">{{ data.pro_text }}</a>
				<# } #>
			</h3>
		</li>
	<?php }
}
function superior_customize_register($wp_customize){
	$wp_customize->add_panel( 'superior_theme_settings', array(
	'priority'       => 10,
	'capability'     => 'edit_theme_options',
	'title'          => __('Superior Theme Options', 'superior')
	));
	global $superior_settings, $superior_array_of_default_settings;
	$superior_settings = wp_parse_args(  get_option( 'superior_theme_settings', array() ),  superior_get_option_defaults());
/********************Superior Upgrade ******************************************/
	$wp_customize->add_section('superior_upgrade', array(
		'title'					=> __('Superior Support', 'superior'),
		'priority'				=> 500,
	));
	$wp_customize->add_setting( 'superior_theme_settings[superior_upgrade]', array(
		'default'				=> false,
		'capability'			=> 'edit_theme_options',
		'sanitize_callback'	=> 'wp_filter_nohtml_kses',
	));
	$wp_customize->add_control(
		new Superior_Customize_Superior_upgrade(
		$wp_customize,
		'superior_upgrade',
			array(
				'label'					=> __('Superior Upgrade','superior'),
				'section'				=> 'superior_upgrade',
				'settings'				=> 'superior_theme_settings[superior_upgrade]',
			)
		)
	);
	/********************Layout Options ******************************************/
	$wp_customize->add_section('superior_layout_options', array(
		'title'					=> __('Layout Options', 'superior'),
		'priority'				=> 110,
		'panel'					=>'superior_theme_settings'
	));
	$wp_customize->add_setting('superior_theme_settings[superior_design_layout]', array(
		'default'				=> 'on',
		'sanitize_callback'	=> 'superior_sanitize_choices',
		'type' 					=> 'option',
		'capability' 			=> 'manage_options'
	));
	$wp_customize->add_control('superior_theme_settings[superior_design_layout]', array(
		'label'					=> __('Site Layout','superior'),
		'section'				=> 'superior_layout_options',
		'settings'				=> 'superior_theme_settings[superior_design_layout]',
		'type'					=> 'radio',
		'checked'				=> 'checked',
		'choices'				=> array(
			'on'					=> __('Wide Layout','superior'),
			'off'					=> __('Narrow Layout','superior'),
		),
	));
	$wp_customize->add_setting('superior_theme_settings[superior_content_layout]', array(
		'default'				=> 'right',
		'sanitize_callback'	=> 'superior_sanitize_choices',
		'type' 					=> 'option',
		'capability' 			=> 'manage_options'
	));
	$wp_customize->add_control('superior_theme_settings[superior_content_layout]', array(
		'label'					=> __('Content Layout','superior'),
		'description'			=> __('Below options are global setting. Set individual layout from specific page/ post.','superior'),
		'section'				=> 'superior_layout_options',
		'settings'				=> 'superior_theme_settings[superior_content_layout]',
		'type'					=> 'radio',
		'checked'				=> 'checked',
		'choices'				=> array(
			'right'				=> __('Right Sidebar','superior'),
			'left'				=> __('Left Sidebar','superior'),
			'nosidebar'			=> __('No Sidebar','superior'),
			'fullwidth'			=> __('No Sidebar Full Width','superior'),
		),
	));
	/********************Custom Header ******************************************/
	$wp_customize->add_section('superior_custom_header_setting', array(
		'title'					=> __('Header Options', 'superior'),
		'priority'				=> 120,
		'panel'					=>'superior_theme_settings'
	));
	$wp_customize->add_setting( 'superior_theme_settings[superior_site_title_setting]', array(
		'default'				=> 0,
		'sanitize_callback'	=> 'superior_sanitize_integer',
		'type' 					=> 'option',
		'capability' 			=> 'manage_options'
	));
	$wp_customize->add_control( 'superior_site_title_setting', array(
		'label'					=> __('Check to disable', 'superior'),
		'section'				=> 'superior_custom_header_setting',
		'settings'				=> 'superior_theme_settings[superior_site_title_setting]',
		'type'					=> 'checkbox',
	));
	$wp_customize->add_setting( 'superior_theme_settings[superior-img-upload-site-title]',array(
		'sanitize_callback'	=> 'esc_url_raw',
		'panel'					=>'superior_theme_settings',
		'type' 					=> 'option',
		'capability' 			=> 'manage_options'
	));
	$wp_customize->add_control(
		new WP_Customize_Image_Control(
		$wp_customize,
			'superior-img-upload-site-title',
			array(
			'label'				=> __('Post/Page Title Background Image','superior'),
			'section'			=> 'superior_custom_header_setting',
			'settings'			=> 'superior_theme_settings[superior-img-upload-site-title]',
			)
		)
	);
	$wp_customize->add_setting('superior_theme_settings[superior_header_settings]', array(
		'default'				=> 'header_text',
		'sanitize_callback'	=> 'superior_sanitize_choices',
		'type' 					=> 'option',
		'capability' 			=> 'manage_options'
	));
	$wp_customize->add_control('superior_theme_settings[superior_header_settings]', array(
		'label'					=> __('Display Site Title', 'superior'),
		'section'				=> 'superior_custom_header_setting',
		'settings'				=> 'superior_theme_settings[superior_header_settings]',
		'type'					=> 'radio',
		'checked'				=> 'checked',
			'choices'			=> array(
			'header_text'		=> __('Header Text Only','superior'),
			'header_logo'		=> __('Header Logo Only','superior'),
			'disable_both'		=> __('Disable Both','superior'),
			),
	));
/********************Home Page Blog Category Setting ******************************************/
	$wp_customize->add_section(
		'superior_category_section', array(
		'title' 						=> __('Home Page Blog Category Setting','superior'),
		'description'				=> __('Only posts that belong to the categories selected here will be displayed on the front page. ( You may select multiple categories by holding down the Ctrl/Command key. ) ','superior'),
		'priority'					=> 160,
		'panel'					=>'superior_theme_settings'
	));
	$wp_customize->add_setting( 'superior_theme_settings[superior_categories]', array(
		'default'					=>array(),
		'sanitize_callback'		=> 'superior_sanitize_category',
		'type' 						=> 'option',
		'capability' 				=> 'manage_options'
	));
	$wp_customize->add_control(
		new Superior_Customize_Category_Control(
		$wp_customize,
			'superior_theme_settings[superior_categories]',
			array(
			'label'					=> __('Front page posts categories','superior'),
			'section'				=> 'superior_category_section',
			'settings'				=> 'superior_theme_settings[superior_categories]',
			'type'					=> 'multiple-select',
			)
		)
	);
	$wp_customize->add_setting( 'superior_theme_settings[superior_disable_setting]', array(
		'default'					=> 0,
		'sanitize_callback'		=> 'superior_sanitize_integer',
		'type' 						=> 'option',
		'capability' 				=> 'manage_options'
	));
	$wp_customize->add_control( 'superior_disable_setting', array(
		'label'						=> __('Check to Default Settings', 'superior'),
		'section'					=> 'superior_category_section',
		'settings'					=> 'superior_theme_settings[superior_disable_setting]',
		'type'						=> 'checkbox',
	));
	/********************Featured Slider Options ******************************************************/
	$wp_customize->add_section( 'superior_slider_options_setting', array(
		'title'					=> __('Featured Slider Options', 'superior'),
		'priority'				=> 200,
		'panel'					=>'superior_theme_settings'
	));
	$wp_customize->add_setting('superior_theme_settings[superior_slider_quantity]', array(
		'default'					=> '4',
		'sanitize_callback'		=> 'superior_sanitize_delay_transition',
		'type' 						=> 'option',
		'capability' 				=> 'manage_options'
	));
	$wp_customize->add_control('superior_slider_quantity', array(
		'priority'					=>60,
		'label'						=> __('Number of Slides', 'superior'),
		'section'					=> 'superior_slider_options_setting',
		'settings'					=> 'superior_theme_settings[superior_slider_quantity]',
		'type'						=> 'text',
	) );
	$wp_customize->add_setting('superior_theme_settings[superior_transition_effect]', array(
		'default'					=> 'fade',
		'sanitize_callback'		=> 'superior_sanitize_effect',
		'type' 						=> 'option',
		'capability' 				=> 'manage_options'
	));
	$wp_customize->add_control('superior_transition_effect', array(
		'priority'					=>70,
		'label'						=> __('Transition Effect', 'superior'),
		'section'					=> 'superior_slider_options_setting',
		'settings'					=> 'superior_theme_settings[superior_transition_effect]',
		'type'						=> 'select',
		'choices'					=> array(
			'fade'					=> __('Fade','superior'),
			'wipe'					=> __('Wipe','superior'),
			'scrollUp'				=> __('Scroll Up','superior' ),
			'scrollDown'			=> __('Scroll Down','superior' ),
			'scrollLeft'			=> __('Scroll Left','superior' ),
			'scrollRight'			=> __('Scroll Right','superior' ),
			'blindX'					=> __('Blind X','superior' ),
			'blindY'					=> __('Blind Y','superior' ),
			'blindZ'					=> __('Blind Z','superior' ),
			'cover'					=> __('Cover','superior' ),
			'shuffle'				=> __('Shuffle','superior' ),
		),
	));
	$wp_customize->add_setting('superior_theme_settings[superior_transition_delay]', array(
		'default'					=> '4',
		'sanitize_callback'		=> 'superior_sanitize_delay_transition',
		'type' 						=> 'option',
		'capability' 				=> 'manage_options'
	));
	$wp_customize->add_control('superior_transition_delay', array(
		'priority'					=>80,
		'label'						=> __('Transition Delay', 'superior'),
		'section'					=> 'superior_slider_options_setting',
		'settings'					=> 'superior_theme_settings[superior_transition_delay]',
		'type'						=> 'text',
	) );
	$wp_customize->add_setting('superior_theme_settings[superior_transition_duration]', array(
		'default'					=> '1',
		'sanitize_callback'		=> 'superior_sanitize_delay_transition',
		'type' 						=> 'option',
		'capability' 				=> 'manage_options'
	));
	$wp_customize->add_control('superior_transition_duration', array(
		'priority'					=>90,
		'label'						=> __('Transition Length', 'superior'),
		'section'					=> 'superior_slider_options_setting',
		'settings'					=> 'superior_theme_settings[superior_transition_duration]',
		'type'						=> 'text',
	) );
	// featured post
	for ( $i=1; $i <= $superior_settings['superior_slider_quantity'] ; $i++ ) {
		$wp_customize->add_setting('superior_theme_settings[superior_featured_post_slider]['. $i.']', array(
			'default'					=>'',
			'sanitize_callback'		=>'superior_sanitize_integer',
			'type' 						=> 'option',
			'capability' 				=> 'manage_options'
		));
		$wp_customize->add_control( 'superior_featured_post_slider]['. $i .']', array(
			'priority'					=> 10 . $i,
			'label'						=> __(' Enter Post ID #', 'superior') . $i ,
			'section'					=> 'superior_slider_options_setting',
			'settings'					=> 'superior_theme_settings[superior_featured_post_slider]['. $i .']',
			'type'						=> 'text',
		));
	}
	$wp_customize->add_setting( 'superior_theme_settings[superior_disable_slider]', array(
		'default'					=> 1,
		'sanitize_callback'		=> 'superior_sanitize_integer',
		'type' 						=> 'option',
		'capability' 				=> 'manage_options'
	));
	$wp_customize->add_control( 'superior_disable_slider', array(
		'priority'					=>270,
		'label'						=> __('Disable Slider', 'superior'),
		'section'					=> 'superior_slider_options_setting',
		'settings'					=> 'superior_theme_settings[superior_disable_slider]',
		'type'						=> 'checkbox',
	));
	$wp_customize->add_setting('superior_theme_settings[superior_exclude_slider_post]', array(
		'default'					=>0,
		'sanitize_callback'		=>'superior_sanitize_integer',
		'type' 						=> 'option',
		'capability' 				=> 'manage_options'
	));
	$wp_customize->add_control( 'superior_exclude_slider_post', array(
		'priority'					=>280,
		'label'						=> __('Check to exclude', 'superior'),
		'description'				=>__('Exclude Slider post from Homepage posts?','superior'),
		'section'					=> 'superior_slider_options_setting',
		'settings'					=> 'superior_theme_settings[superior_exclude_slider_post]',
		'type'						=> 'checkbox',
	));
	/******************** Post Meta Options  ***********************************/
	$wp_customize->add_section( 'superior_post_meta_options', array(
		'title' 						=> __('Post Meta Display Options','superior'),
		'priority'					=> 300,
		'panel'					=>'superior_theme_settings'
	));
	$wp_customize->add_setting('superior_theme_settings[superior_post_category]', array(
		'default'					=>0,
		'sanitize_callback'		=>'superior_sanitize_integer',
		'type' 						=> 'option',
		'capability' 				=> 'manage_options'
	));
	$wp_customize->add_control( 'superior_post_category', array(
		'priority'					=>305,
		'label'						=> __('Disable Categories', 'superior'),
		'section'					=> 'superior_post_meta_options',
		'settings'					=> 'superior_theme_settings[superior_post_category]',
		'type'						=> 'checkbox',
	));
	$wp_customize->add_setting('superior_theme_settings[superior_post_author]', array(
		'default'					=>0,
		'sanitize_callback'		=>'superior_sanitize_integer',
		'type' 						=> 'option',
		'capability' 				=> 'manage_options'
	));
	$wp_customize->add_control( 'superior_post_author', array(
		'priority'					=>310,
		'label'						=> __('Disable Author', 'superior'),
		'section'					=> 'superior_post_meta_options',
		'settings'					=> 'superior_theme_settings[superior_post_author]',
		'type'						=> 'checkbox',
	));
	$wp_customize->add_setting('superior_theme_settings[superior_post_date]', array(
		'default'					=>0,
		'sanitize_callback'		=>'superior_sanitize_integer',
		'type' 						=> 'option',
		'capability' 				=> 'manage_options'
	));
	$wp_customize->add_control( 'superior_post_date', array(
		'priority'					=>320,
		'label'						=> __('Disable Date', 'superior'),
		'section'					=> 'superior_post_meta_options',
		'settings'					=> 'superior_theme_settings[superior_post_date]',
		'type'						=> 'checkbox',
	));
	$wp_customize->add_setting('superior_theme_settings[superior_post_comments]', array(
		'default'					=>0,
		'sanitize_callback'		=>'superior_sanitize_integer',
		'type' 						=> 'option',
		'capability' 				=> 'manage_options'
	));
	$wp_customize->add_control( 'superior_post_comments', array(
		'priority'					=>330,
		'label'						=> __('Disable Comment Link', 'superior'),
		'section'					=> 'superior_post_meta_options',
		'settings'					=> 'superior_theme_settings[superior_post_comments]',
		'type'						=> 'checkbox',
	));
	$wp_customize->add_setting('superior_theme_settings[superior_post_tags]', array(
		'default'					=>0,
		'sanitize_callback'		=>'superior_sanitize_integer',
		'type' 						=> 'option',
		'capability' 				=> 'manage_options'
	));
	$wp_customize->add_control( 'superior_post_tags', array(
		'priority'					=>350,
		'label'						=> __('Disable Tags', 'superior'),
		'section'					=> 'superior_post_meta_options',
		'settings'					=> 'superior_theme_settings[superior_post_tags]',
		'type'						=> 'checkbox',
	));
}
/********************Sanitize the values ******************************************/
function superior_sanitize_choices( $input, $setting ) {
    global $wp_customize;
    $control = $wp_customize->get_control( $setting->id );
    if ( array_key_exists( $input, $control->choices ) ) {
        return $input;
    } else {
        return $setting->default;
    }
}
function superior_sanitize_integer( $input) {
    return absint($input);
}
function superior_sanitize_effect( $input ) {
	if ( ! in_array( $input, array( 'fade', 'wipe', 'scrollUp', 'scrollDown', 'scrollLeft', 'scrollRight', 'blindX', 'blindY', 'blindZ', 'cover', 'shuffle' ) ) ) {
		$input = 'fade';
	}
	return $input;
}
function superior_sanitize_delay_transition( $input ) {
	if(is_numeric($input)){
	return $input;
	}
}
function superior_sanitize_category( $input ) {
	if ( $input != '' ) { 
		$args = array(
						'type'			=> 'post',
						'child_of'      => 0,
						'parent'        => '',
						'orderby'       => 'name',
						'order'         => 'ASC',
						'hide_empty'    => 0,
						'hierarchical'  => 0,
						'taxonomy'      => 'category',
					); 
		
		$categories = ( get_categories( $args ) );

		$category_list 	=	array();
		
		foreach ( $categories as $category )
			$category_list 	=	array_merge( $category_list, array( $category->term_id ) );

		if ( count( array_intersect( $input, $category_list ) ) == count( $input ) ) {
	    	return $input;
	    } 
	    else {
    		return '';
   		}
    }
    else {
    	return '';
    }
}
function superior_customizer_control_scripts() {

	wp_enqueue_script(
		'superior-customize-controls',
		get_template_directory_uri() . '/inc/admin/js/superior_customizer.js',
		array(), '3.0',
		true
	);

	wp_enqueue_style( 'superior-customize-controls',
	 get_template_directory_uri() . '/inc/admin/css/customize-controls.css' );

}

add_action( 'customize_controls_enqueue_scripts', 'superior_customizer_control_scripts', 0 );


function superior_customize_custom_sections( $wp_customize ) {

	// Register custom section types.
	$wp_customize->register_section_type( 'Superior_Customize_Section_Upsell' );

	// Register sections.
	$wp_customize->add_section(
		new Superior_Customize_Section_Upsell(
			$wp_customize,
			'theme_upsell',
			array(
				'title'    => esc_html__( 'Superior Pro', 'superior' ),
				'pro_text' => esc_html__( 'Upgrade to Pro', 'superior' ),
				'pro_url'  => 'http://themehorse.com/themes/superior-pro',
				'priority' => 1,
			)
		)
	);

}

add_action( 'customize_register', 'superior_customize_custom_sections');
add_action('customize_register', 'superior_textarea_register');
add_action('customize_register', 'superior_customize_register');
?>
