<?php
/**
 * Equable Lite Theme Customizer
 *
 * @package Equable_Lite
 */

 /*
 * Create a custom type for entering in HTML content
 * Special thanks to Devin Price https://wptheming.com/2015/07/customizer-control-arbitrary-html/
 */
	if ( class_exists( 'WP_Customize_Control' ) && ! class_exists( 'Equable_Lite_Custom_Content' ) ) :
	class Equable_Lite_Custom_Content extends WP_Customize_Control {

	public $content = '';

	// Render the control's content. Allows the content to be overriden without having to rewrite the wrapper.
	public function render_content() {
		if ( isset( $this->label ) ) {
			echo '<span class="customize-control-title">' . $this->label . '</span>';
		}
		if ( isset( $this->content ) ) {
			echo $this->content;
		}
		if ( isset( $this->description ) ) {
			echo '<span class="description customize-control-description">' . $this->description . '</span>';
		}
	}
}
endif;

/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function equable_lite_customize_register( $wp_customize ) {
	
	$wp_customize->get_setting( 'blogname' )->transport          = 'postMessage';
	$wp_customize->get_setting( 'blogdescription' )->transport   = 'postMessage';
	$wp_customize->get_setting( 'header_textcolor' )->transport  = 'postMessage';
	$wp_customize->get_control( 'background_color'  )->description   = esc_html__( 'When you use the boxed layout or if you are not using a background image.', 'equable-lite' );

	$wp_customize->selective_refresh->add_partial( 'blogname', array(
		'selector' => '.site-title a',
		'render_callback' => 'equable_lite_customize_partial_blogname',
	) );
	$wp_customize->selective_refresh->add_partial( 'blogdescription', array(
		'selector' => '.site-description',
		'render_callback' => 'equable_lite_customize_partial_blogdescription',
	) );	
	
	/**
	 * Custom colors.
	 * Special thanks to the Twenty Seventeen theme
	 */
	$wp_customize->add_setting( 'colorscheme', array(
		'default'           => 'default',
		'transport'         => 'postMessage',
		'sanitize_callback' => 'equable_lite_sanitize_colorscheme',
	) );

	$wp_customize->add_setting( 'colorscheme_hue', array(
		'default'           => 32, // brown
		'transport'         => 'postMessage',
		'sanitize_callback' => 'absint', // The hue is stored as a positive integer.
	) );

	$wp_customize->add_control( 'colorscheme', array(
		'type'    => 'radio',
		'label'    => esc_html__( 'Color Scheme', 'equable-lite' ),
		'description'   => esc_html__( 'The default is your standard colour scheme with the Dark option being a darker colour palette, and Custom is only for minimal accents like links and buttons.', 'equable-lite' ),
		'choices'  => array(
			'default'  => esc_html__( 'Default', 'equable-lite' ),
			'dark'   => esc_html__( 'Dark', 'equable-lite' ),
			'custom' => esc_html__( 'Custom', 'equable-lite' ),
		),
		'section'  => 'colors',
		'priority' => 5,
	) );

	$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'colorscheme_hue', array(
		'mode' => 'hue',
		'section'  => 'colors',
		'priority' => 6,
	) ) );

if ( ! class_exists( 'Equable_Lite_Customize_Static_Text_Control' ) ){
if ( ! class_exists( 'WP_Customize_Control' ) )
    return NULL;
class Equable_Lite_Customize_Static_Text_Control extends WP_Customize_Control {
	/**
	 * Control type.
	 *
	 * @access public
	 * @var string
	 */
	public $type = 'static-text';

	public function __construct( $manager, $id, $args = array() ) {
		parent::__construct( $manager, $id, $args );
	}

	protected function render_content() {
		if ( ! empty( $this->label ) ) :
			?><span class="Equable-lite-customize-control-title"><?php echo esc_html( $this->label ); ?></span><?php
		endif;

		if ( ! empty( $this->description ) ) :
			?><div class="Equable-lite-description Equable-lite-customize-control-description"><?php

			if( is_array( $this->description ) ) {
				echo '<p>' . implode( '</p><p>', $this->description ) . '</p>';
			} else {
				echo $this->description;
			}
			?>
						
		<h1><?php esc_html_e('Equable Pro', 'equable-lite') ?></h1>
		<p><?php esc_html_e('Opt in for the pro version of Equable and enjoy many additional features that will add more options. Here is a sample of what you will get:','equable-lite'); ?></p>
		<p style="font-weight: 700;"><?php esc_html_e('Pro Features:', 'equable-lite') ?></p>
		<ul>
			<li><?php esc_html_e('&bull; 5 Blog Styles', 'equable-lite')?></li>
			<li><?php esc_html_e('&bull; Custom Header Images', 'equable-lite')?></li>
			<li><?php esc_html_e('&bull; Adjustable Content Widths', 'equable-lite')?></li>
			<li><?php esc_html_e('&bull; Thumbnail Creation for the Blogs', 'equable-lite')?></li>
			<li><?php esc_html_e('&bull; Recent Posts Widget with Thumbnails', 'equable-lite')?></li>
			<li><?php esc_html_e('&bull; Related Posts with Thumbnails', 'equable-lite')?></li>
			<li><?php esc_html_e('&bull; 5 Page Templates', 'equable-lite')?></li>
			<li><?php esc_html_e('&bull; Post Formats', 'equable-lite')?></li>
			<li><?php esc_html_e('&bull; Add Google Fonts', 'equable-lite')?></li>
			<li><?php esc_html_e('&bull; Custom Archive Titles', 'equable-lite')?></li>
			<li><?php esc_html_e('&bull; Page Builder Ready', 'equable-lite')?></li>
		</ul>
		
		<p><a class="button" href="<?php echo esc_url('https://www.bloggingthemestyles.com/wordpress-themes/equable-pro'); ?>" target="_blank"><?php esc_html_e( 'Get the Pro', 'equable-lite' ); ?></a></p>				
		<?php
		endif;
	}
}
}	
	
	
   // SECTION - EQUABLE UPGRADE
    $wp_customize->add_section( 'equable_upgrade', array(
        'title'       => esc_html__( 'Upgrade to Pro', 'equable-lite' ),
        'priority'    => 0
    ) );
	
		$wp_customize->add_setting( 'equable_upgrade', array(
			'default' => '',
			'sanitize_callback' => '__return_false'
		) );
		
		$wp_customize->add_control( new Equable_Lite_Customize_Static_Text_Control( $wp_customize, 'equable_upgrade', array(
			'label'	=> esc_html__('Get The Pro Version:','equable-lite'),
			'section'	=> 'equable_upgrade',
			'description' => array('')
		) ) );	

	
// SECTION - THEME OPTIONS
	$wp_customize->add_section( 'equable_lite_theme_options', array(
		'title'    => esc_html__( 'Theme Options', 'equable-lite' ),
		'priority' => 20, 
	) ); 

	// Setting group for the boxed layout
	$wp_customize->add_setting( 'equable_lite_boxed_layout', array(
		'default' => 'full',
		'sanitize_callback' => 'equable_lite_sanitize_select',
	) );  
	$wp_customize->add_control( 'equable_lite_boxed_layout', array(
		  'type' => 'radio',
		  'label' => esc_html__( 'Boxed Layout', 'equable-lite' ),
		  'section' => 'equable_lite_theme_options',
		  'choices' => array(
			  'full' => esc_html__( 'Full Width', 'equable-lite' ),
			  'boxed1600' => esc_html__( 'Boxed 1600px Width', 'equable-lite' ),
			  'boxed1400' => esc_html__( 'Boxed 1400px Width', 'equable-lite' ),
			  'boxed1200' => esc_html__( 'Boxed 1200px Width', 'equable-lite' ),
	) ) );

	// Setting group for blog style  
	$wp_customize->add_setting( 'equable_lite_blog_style', array(
		'default' => 'blog3',
		'sanitize_callback' => 'equable_lite_sanitize_select',
	) );  
	$wp_customize->add_control( 'equable_lite_blog_style', array(
		  'type' => 'radio',
		  'label' => esc_html__( 'Blog Style', 'equable-lite' ),
		  'section' => 'equable_lite_theme_options',
		  'choices' => array(	
				'blog1' => esc_html__( 'Medium With Right Sidebar', 'equable-lite' ),	  
				'blog2' => esc_html__( 'Medium With Left Sidebar', 'equable-lite' ),
				'blog3' => esc_html__( 'Large With No Sidebar', 'equable-lite' ),
		) ) );	
		
	// Setting group for full post (single) layout  
	$wp_customize->add_setting( 'equable_lite_single_style', array(
		'default' => 'single1',
		'sanitize_callback' => 'equable_lite_sanitize_select',
	) );  
	$wp_customize->add_control( 'equable_lite_single_style', array(
		  'type' => 'radio',
		  'label' => esc_html__( 'Full Post Style', 'equable-lite' ),
		  'section' => 'equable_lite_theme_options',
		  'choices' => array(	
				'single1' => esc_html__( 'Single With Right Sidebar', 'equable-lite' ),	 
				'single2' => esc_html__( 'Single With Left Sidebar', 'equable-lite' ), 
				'single3' => esc_html__( 'Single With No Sidebars', 'equable-lite' ),
		) ) );	

	 // Use excerpts for blog posts
	  $wp_customize->add_setting( 'equable_lite_use_excerpt',  array(
		  'default' => true,
		  'sanitize_callback' => 'equable_lite_sanitize_checkbox',
		) );  
	  $wp_customize->add_control( 'equable_lite_use_excerpt', array(
		'type'     => 'checkbox',
		'label'    => esc_html__( 'Use Excerpts', 'equable-lite' ),
		'description' => esc_html__( 'Use excerpts for your blog post summaries or uncheck the box to use the standard Read More tag.', 'equable-lite' ),
		'section'  => 'equable_lite_theme_options',
	  ) );
 
    // Excerpt size
    $wp_customize->add_setting( 'equable_lite_excerpt_size',  array(
            'sanitize_callback' => 'absint',
            'default'           => '55',
        ) );
    $wp_customize->add_control( 'equable_lite_excerpt_size', array(
        'type'        => 'number',
        'section'     => 'equable_lite_theme_options',
        'label'       => esc_html__('Excerpt Size', 'equable-lite'),
		'description' => esc_html__('You can change the size of your blog summary excerpts with increments of 5 words.', 'equable-lite'), 
        'input_attrs' => array(
            'min'   => 10,
            'max'   => 200,
            'step'  => 5,
        ),
    ) );


	// Blog title 
	$wp_customize->add_setting( 'equable_lite_blog_title', array(
		'sanitize_callback' => 'sanitize_text_field',
	) );
	$wp_customize->add_control( 'equable_lite_blog_title', array(
		'settings' => 'equable_lite_blog_title',
		'label'    => esc_html__( 'Blog Title', 'equable-lite' ),
		'description' => esc_html__('This is your blog title that shows up on your blog home page where post summaries are published.', 'equable-lite'), 
		'section'  => 'equable_lite_theme_options',		
		'type'     => 'text',
	) );  
	
	// Blog Introduction 
	$wp_customize->add_setting( 'equable_lite_blog_intro', array(
		'sanitize_callback' => 'sanitize_text_field',
	) );
	$wp_customize->add_control( 'equable_lite_blog_intro', array(
		'settings' => 'equable_lite_blog_intro',
		'label'    => esc_html__( 'Blog Introduction', 'equable-lite' ),
		'description' => esc_html__('This is an introduction to your blog home page, much like a category description.', 'equable-lite'), 
		'section'  => 'equable_lite_theme_options',		
		'type'     => 'text',
	) );   	

	// footer logo
  	$wp_customize->add_setting( 'equable_lite_footer_logo',  array(
		'default' => '',
		'sanitize_callback' => 'esc_url_raw',
   	 ) ); 	 
	$wp_customize->add_control(  new WP_Customize_Image_Control(  $wp_customize,  'equable_lite_footer_logo',  array(
				   'label'      => esc_html__( 'Footer Logo', 'equable-lite' ),
				   'description'	=> esc_html__( 'Add a footer logo.', 'equable-lite' ),
				   'section'    => 'equable_lite_theme_options',
				   'settings'   => 'equable_lite_footer_logo',
			   )
		   )
	   ); 
	   
	// Copyright
	$wp_customize->add_setting( 'equable_lite_copyright', array(
		'sanitize_callback' => 'sanitize_text_field',
	) );
	$wp_customize->add_control( 'equable_lite_copyright', array(
		'settings' => 'equable_lite_copyright',
		'label'    => esc_html__( 'Your Copyright Name', 'equable-lite' ),
		'section'  => 'equable_lite_theme_options',		
		'type'     => 'text',
	) );   

	
	// Show blog heading group
	$wp_customize->add_setting( 'equable_lite_show_blog_heading',	array(
		'default' => false,
		'sanitize_callback' => 'equable_lite_sanitize_checkbox',
	) );  
	$wp_customize->add_control( 'equable_lite_show_blog_heading', array(
		'type'     => 'checkbox',
		'label'    => esc_html__( 'Show the Blog Heading Group', 'equable-lite' ),
		'description' => esc_html__( 'This lets you show the blog heading group that consists of the title and introduction on your home page.', 'equable-lite' ),
		'section'  => 'equable_lite_theme_options',
	) );
	
	// Show blog introduction
	$wp_customize->add_setting( 'equable_lite_show_blog_intro',	array(
		'default' => true,
		'sanitize_callback' => 'equable_lite_sanitize_checkbox',
	) );  
	$wp_customize->add_control( 'equable_lite_show_blog_intro', array(
		'type'     => 'checkbox',
		'label'    => esc_html__( 'Show Blog Introduction', 'equable-lite' ),
		'description' => esc_html__( 'This lets you show the blog introduction on your home page, much like a category description.', 'equable-lite' ),
		'section'  => 'equable_lite_theme_options',
	) );

	// Show post featured label
	$wp_customize->add_setting( 'equable_lite_show_featured_label',	array(
		'default' => true,
		'sanitize_callback' => 'equable_lite_sanitize_checkbox',
	) );  
	$wp_customize->add_control( 'equable_lite_show_featured_label', array(
		'type'     => 'checkbox',
		'label'    => __( 'Show the Featured Label', 'equable-lite' ),
		'description' => __( 'This lets you show the post summary featured label.', 'equable-lite' ),
		'section'  => 'equable_lite_theme_options',
	) );
	
	// Show dropcaps
	$wp_customize->add_setting( 'equable_lite_show_dropcap',	array(
		'default' => true,
		'sanitize_callback' => 'equable_lite_sanitize_checkbox',
	) );  
	$wp_customize->add_control( 'equable_lite_show_dropcap', array(
		'type'     => 'checkbox',
		'label'    => esc_html__( 'Show Full Post Dropcap', 'equable-lite' ),
		'description' => esc_html__( 'This lets you show the drop cap style on the first letter of the first paragraph.', 'equable-lite' ),
		'section'  => 'equable_lite_theme_options',
	) );		
	
	// Show read more on excerpts
	$wp_customize->add_setting( 'equable_lite_show_excerpt_readmore',	array(
		'default' => true,
		'sanitize_callback' => 'equable_lite_sanitize_checkbox',
	) );  
	$wp_customize->add_control( 'equable_lite_show_excerpt_readmore', array(
		'type'     => 'checkbox',
		'label'    => esc_html__( 'Show the Continue Reading Link', 'equable-lite' ),
		'description' => esc_html__( 'This lets you show the blog excerpt Continue Reading link on blog related posts.', 'equable-lite' ),
		'section'  => 'equable_lite_theme_options',
	) );		
	
	// Show author bio
	$wp_customize->add_setting( 'equable_lite_authorbio',	array(
		'default' => true,
		'sanitize_callback' => 'equable_lite_sanitize_checkbox',
	) );  
	$wp_customize->add_control( 'equable_lite_authorbio', array(
		'type'     => 'checkbox',
		'label'    => esc_html__( 'Show the Author Bio', 'equable-lite' ),
		'description' => esc_html__( 'This lets you show the author bio information at the bottom of any post whenever you have more than one author.', 'equable-lite' ),
		'section'  => 'equable_lite_theme_options',
	) );	
	
	// Show post format
	$wp_customize->add_setting( 'equable_lite_show_post_format',	array(
		'default' => true,
		'sanitize_callback' => 'equable_lite_sanitize_checkbox',
	) );  
	$wp_customize->add_control( 'equable_lite_show_post_format', array(
		'type'     => 'checkbox',
		'label'    => esc_html__( 'Show the Post Format Label', 'equable-lite' ),
		'description' => esc_html__( 'This lets you show the post format type in the full post meta info.', 'equable-lite' ),
		'section'  => 'equable_lite_theme_options',
	) );	
	
	// Show categories list
	$wp_customize->add_setting( 'equable_lite_show_post_categories',	array(
		'default' => true,
		'sanitize_callback' => 'equable_lite_sanitize_checkbox',
	) );  
	$wp_customize->add_control( 'equable_lite_show_post_categories', array(
		'type'     => 'checkbox',
		'label'    => esc_html__( 'Show the Post Categories', 'equable-lite' ),
		'description' => esc_html__( 'This lets you show the post list of categories', 'equable-lite' ),
		'section'  => 'equable_lite_theme_options',
	) );	
	
	// Show tags
	$wp_customize->add_setting( 'equable_lite_show_post_tags',	array(
		'default' => true,
		'sanitize_callback' => 'equable_lite_sanitize_checkbox',
	) );  
	$wp_customize->add_control( 'equable_lite_show_post_tags', array(
		'type'     => 'checkbox',
		'label'    => esc_html__( 'Show the Post Tags', 'equable-lite' ),
		'description' => esc_html__( 'This lets you show the post list of tags.', 'equable-lite' ),
		'section'  => 'equable_lite_theme_options',
	) );		
	
	// Show single navigation
	$wp_customize->add_setting( 'equable_lite_post_nav',	array(
		'default' => true,
		'sanitize_callback' => 'equable_lite_sanitize_checkbox',
	) );  
	$wp_customize->add_control( 'equable_lite_post_nav', array(
		'type'     => 'checkbox',
		'label'    => esc_html__( 'Show Full Post Navigation', 'equable-lite' ),
		'description' => esc_html__( 'This lets you show the post previous and next navigation when viewing the full article.', 'equable-lite' ),
		'section'  => 'equable_lite_theme_options',
	) );	
	
	// Show powered by wordpress
	$wp_customize->add_setting( 'equable_lite_show_wplink',	array(
		'default' => true,
		'sanitize_callback' => 'equable_lite_sanitize_checkbox',
	) );  
	$wp_customize->add_control( 'equable_lite_show_wplink', array(
		'type'     => 'checkbox',
		'label'    => esc_html__( 'Show Powered By WordPress', 'equable-lite' ),
		'description' => esc_html__( 'This lets you show the Powered By WordPress link in the site footer.', 'equable-lite' ),
		'section'  => 'equable_lite_theme_options',
	) );	

	// Enable attachment comments
	$wp_customize->add_setting( 'equable_lite_show_attachment_comments',	array(
		'default' => false,
		'sanitize_callback' => 'equable_lite_sanitize_checkbox',
	) );  
	$wp_customize->add_control( 'equable_lite_show_attachment_comments', array(
		'type'     => 'checkbox',
		'label'    => esc_html__( 'Show Image Attachment Page Comments', 'equable-lite' ),
		'description' => esc_html__( 'If you are using a WP gallery shortcode and want to showcase your images on the custom attachment page, you can enable or disable comments for images.', 'equable-lite' ),
		'section'  => 'equable_lite_theme_options',
	) );		
	
	
// SECTION - TYPOGRAPHY SETTINGS
    $wp_customize->add_section( 'equable_lite_typography_settings', array(
        'title'       => esc_html__( 'Typography Settings', 'equable-lite' ),
        'priority'    => 25
    ) ); 	
	
	// Show dropcaps
	$wp_customize->add_setting( 'equable_lite_show_dropcap',	array(
		'default' => true,
		'sanitize_callback' => 'equable_lite_sanitize_checkbox',
	) );  
	$wp_customize->add_control( 'equable_lite_show_dropcap', array(
		'type'     => 'checkbox',
		'label'    => esc_html__( 'Show Full Post Dropcap', 'equable-lite' ),
		'description' => esc_html__( 'This lets you show the drop cap style on the first letter of the first paragraph.', 'equable-lite' ),
		'section'  => 'equable_lite_typography_settings',
	) );

// Customizer settings end
}
add_action( 'customize_register', 'equable_lite_customize_register' );


/**
 * SANITIZATION
 * Required for cleaning up bad inputs
 */
 
 
// Sanitize the colorscheme.
	function equable_lite_sanitize_colorscheme( $input ) {
		$valid = array( 'default', 'dark', 'custom' );
		if ( in_array( $input, $valid, true ) ) {
			return $input;
		}
		return 'default';
	}

// Strip Slashes
	function equable_lite_sanitize_strip_slashes($input) {
		return wp_kses_stripslashes($input);
	}	
	
// Radio and Select	
	function equable_lite_sanitize_select( $input, $setting ) {
		// Ensure input is a slug.
		$input = sanitize_key( $input );
		// Get list of choices from the control associated with the setting.
		$choices = $setting->manager->get_control( $setting->id )->choices;
		// If the input is a valid key, return it; otherwise, return the default.
		return ( array_key_exists( $input, $choices ) ? $input : $setting->default );
	}
	 	
// Checkbox
	function equable_lite_sanitize_checkbox( $input ) {
		// Boolean check 
		return ( ( isset( $input ) && true == $input ) ? true : false );
	}
	
// Array of valid image file types
	function equable_lite_sanitize_image( $image, $setting ) {
		$mimes = array(
			'jpg|jpeg|jpe' => 'image/jpeg',
			'gif'          => 'image/gif',
			'png'          => 'image/png',
		);
		// Return an array with file extension and mime_type.
		$file = wp_check_filetype( $image, $mimes );
		// If $image has a valid mime_type, return it; otherwise, return the default.
		return ( $file['ext'] ? $image : $setting->default );
	}


/**
 * Render the site title for the selective refresh partial.
 * @return void
 */
function equable_lite_customize_partial_blogname() {
	bloginfo( 'name' );
}

/**
 * Render the site tagline for the selective refresh partial.
 * @return void
 */
function equable_lite_customize_partial_blogdescription() {
	bloginfo( 'description' );
}


/**
 * Bind JS handlers to instantly live-preview changes.*/
function equable_lite_customize_preview_js() {
	wp_enqueue_script( 'equable-lite-customize-preview', get_theme_file_uri( '/js/customize-preview.js' ), array( 'customize-preview' ), '1.0', true );
}
add_action( 'customize_preview_init', 'equable_lite_customize_preview_js' );

/**
 * Load dynamic logic for the customizer controls area.*/
function equable_lite_panels_js() {
	wp_enqueue_script( 'equable-lite-customize-controls', get_theme_file_uri( '/js/customize-controls.js' ), array(), '1.0', true );
}
add_action( 'customize_controls_enqueue_scripts', 'equable_lite_panels_js' );

