<?php
    /**
     * Theme options via the Customizer.
     *
     * @package illusive
     * @since illusive 1.0
     */

    // ------------- Theme Customizer  ------------- //

    add_action( 'customize_register', 'illusive_customizer_register' );

    function illusive_customizer_register( $wp_customize ) {



// Pro Version
        class Illusive_Customize_Pro_Version extends WP_Customize_Control {
            public $type = 'pro_options';

            public function render_content() {
                echo '<span>Upgrade to <strong></strong></span>';
                echo '<a href="'. esc_url($this->description) .'" target="_blank">';
                echo '<strong> '. esc_html__( 'Illusive Premium', 'illusive' ) .'<strong></a>';
                echo '</a>';
            }
        }

        // Pro Version Links
        class Illusive_Customize_Pro_Version_Links extends WP_Customize_Control {
            public $type = 'pro_links';

            public function render_content() {
                ?>
                <ul>
                    <li class="customize-control">
                        <h3><?php esc_html_e( 'Upgrade', 'illusive' ); ?> <span>*</span></h3>
                        <p><?php esc_html_e( 'There are lots of reasons to upgrade to Pro version. Unlimited custom Colors, rich Typography options, multiple variation of Blog Feed layout and way much more. Also Premium Support included.', 'illusive' ); ?></p>
                        <a href="<?php echo esc_url('https://www.vinethemes.com/downloads/illusive-blog-wordpress-theme/'); ?>" target="_blank" class="button button-primary widefat"><?php esc_html_e( 'Get Illusive Premium', 'illusive' ); ?></a>
                    </li>
                    <li class="customize-control">
                        <h3><?php esc_html_e( 'Documentation', 'illusive' ); ?></h3>
                        <p><?php esc_html_e( 'Read how to customize the theme, set up widgets, and learn all the possible options available to you.', 'illusive' ); ?></p>
                        <a href="<?php echo esc_url('https://support.vinethemes.com/knowledgebase/illusive-blog-wordpress-theme/'); ?>" target="_blank" class="button button-primary widefat"><?php esc_html_e( 'Documentation', 'illusive' ); ?></a>
                    </li>
                    <li class="customize-control">
                        <h3><?php esc_html_e( 'Support', 'illusive' ); ?></h3>
                        <p><?php esc_html_e( 'For Illusive theme related questions feel free to post on our support forums.', 'illusive' ); ?></p>
                        <a href="<?php echo esc_url('https://support.vinethemes.com/'); ?>" target="_blank" class="button button-primary widefat"><?php esc_html_e( 'Support', 'illusive' ); ?></a>
                    </li>

                </ul>
                <?php
            }
        }


        /*
        ** Pro Version =====
        */

        // add Colors section
        $wp_customize->add_section( 'illusive_pro' , array(
            'title'		 => esc_html__( 'About Illusive', 'illusive' ),
            'priority'	 => 1,
            'capability' => 'edit_theme_options'
        ) );

        // Pro Version
        $wp_customize->add_setting( 'pro_version_', array(
            'sanitize_callback' => 'illusive_sanitize_custom_control'
        ) );
        $wp_customize->add_control( new Illusive_Customize_Pro_Version_Links ( $wp_customize,
                'pro_version_', array(
                    'section'	=> 'illusive_pro',
                    'type'		=> 'pro_links',
                    'label' 	=> '',
                    'priority'	=> 1
                )
            )
        );





        //Slick Slider
        $wp_customize->add_section( 'illusive_customizer_slider', array(
            'title'       => esc_html__( 'Slider Options', 'illusive' ),
            'description' => esc_html__( 'Configure your Slider here.', 'illusive' ),
            'priority'    => 1
        ) );


        $wp_customize->add_setting( 'illusive_customizer_slider_disable', array(
            'default'    => 'enable',
            'section'  => 'illusive_customizer_slider',
            'capability' => 'edit_theme_options',
            'sanitize_callback'	=> 'illusive_sanitize_radio',
        ) );

        $wp_customize->add_control( 'illusive_slider_select_box', array(
            'settings' => 'illusive_customizer_slider_disable',
            'label'    => esc_html__( 'Homepage Slider', 'illusive' ),
            'section'  => 'illusive_customizer_slider',
            'type'     => 'select',
            'choices'  => array(
                'enable'  => esc_html__( 'Enable', 'illusive' ),
                'disable' => esc_html__( 'Disable', 'illusive' ),
                ),
            'priority' => 6
        ) );
        $wp_customize->add_setting( 'illusive_slider_category', array(
            'default' => '0',
            'sanitize_callback'	=> 'absint',
            'section'  => 'illusive_customizer_slider',

        ) );
         $wp_customize->add_control(new illusive_Customize_Category_Control( $wp_customize, 'illusive_slider_category', array(
                    'label'    => esc_html__( 'Category for Slider', 'illusive' ),
                    'section'  => 'illusive_customizer_slider',
                    'settings' => 'illusive_slider_category',
                    'priority'	 => 7
                )
            )
        );

        $wp_customize->add_setting( 'illusive_slider_slides', array(
            'default' => '3',
            'sanitize_callback'	=> 'absint',
            'section'  => 'illusive_customizer_slider',
        ) );

        $wp_customize->add_control( 'illusive_slider_slides', array(
                    'label'      => esc_html__('Number of Posts for Slider','illusive'),
                    'section'    => 'illusive_customizer_slider',
                    'settings'   => 'illusive_slider_slides',
                    'type'		 => 'number',
                    'priority'	 => 8
                )
        );

        $wp_customize->add_setting( 'illusive_slider_designs', array(
            'default'    => 'Slider1',
            'section'  => 'illusive_customizer_slider',
            'capability' => 'edit_theme_options',
            'sanitize_callback'	=> 'illusive_sanitize_radio',
        ) );

        $wp_customize->add_control( 'illusive_slider_designs', array(
            'type' => 'radio',
            'label'    => esc_html__( 'Slider Designs', 'illusive' ),
            'section'  => 'illusive_customizer_slider',
            'choices'  => array(
                'Slider1'  => esc_html__( 'Modern Slider', 'illusive' ),
                'Slider2' => esc_html__( 'Three Grid', 'illusive' ),
                'Slider3' => esc_html__( 'Single Boxed', 'illusive' ),
            ),
            'priority' => 9
        ) );






        //General Options

        $wp_customize->add_section( 'illusive_general_options', array(
            'title'       => esc_html__( 'General Options', 'illusive' ),
            'description' => esc_html__( 'Configure Your Theme Settings Here.', 'illusive' ),
            'priority'    => 2
        ) );

        $wp_customize->add_setting( 'illusive_general_search_icon', array(
            'section'  => 'illusive_general_options',
            'capability' => 'edit_theme_options',
            'sanitize_callback'	=> 'illusive_sanitize_checkbox',
        ) );

        $wp_customize->add_control( 'illusive_general_search_icon', array(
            'settings' => 'illusive_general_search_icon',
            'label'    => esc_html__( 'Hide Top Search Icon', 'illusive' ),
            'section'  => 'illusive_general_options',
            'type'     => 'checkbox',
            'priority' => 6
        ) );
        $wp_customize->add_setting( 'illusive_general_responsive', array(
            'section'  => 'illusive_general_options',
            'capability' => 'edit_theme_options',
            'sanitize_callback'	=> 'illusive_sanitize_checkbox',
        ) );

        $wp_customize->add_control( 'illusive_general_responsive', array(
            'settings' => 'illusive_general_responsive',
            'label'    => esc_html__( 'Disable Responsive', 'illusive' ),
            'section'  => 'illusive_general_options',
            'type'     => 'checkbox',
            'priority' => 6
        ) );
        $wp_customize->add_setting( 'illusive_general_sidebar_home', array(
            'section'  => 'illusive_general_options',
            'capability' => 'edit_theme_options',
            'sanitize_callback'	=> 'illusive_sanitize_checkbox',
        ) );

        $wp_customize->add_control( 'illusive_general_sidebar_home', array(
            'settings' => 'illusive_general_sidebar_home',
            'label'    => esc_html__( 'Disable Sidebar on Homepage and Archive Pages', 'illusive' ),
            'section'  => 'illusive_general_options',
            'type'     => 'checkbox',
            'priority' => 6
        ) );
        $wp_customize->add_setting( 'illusive_general_sidebar_post', array(
            'section'  => 'illusive_general_options',
            'capability' => 'edit_theme_options',
            'sanitize_callback'	=> 'illusive_sanitize_checkbox',
        ) );

        $wp_customize->add_control( 'illusive_general_sidebar_post', array(
            'settings' => 'illusive_general_sidebar_post',
            'label'    => esc_html__( 'Disable Sidebar on Posts', 'illusive' ),
            'section'  => 'illusive_general_options',
            'type'     => 'checkbox',
            'priority' => 6
        ) );
        $wp_customize->add_setting( 'illusive_general_sidebar_page', array(
            'section'  => 'illusive_general_options',
            'capability' => 'edit_theme_options',
            'sanitize_callback'	=> 'illusive_sanitize_checkbox',
        ) );

        $wp_customize->add_control( 'illusive_general_sidebar_page', array(
            'settings' => 'illusive_general_sidebar_page',
            'label'    => esc_html__( 'Disable Sidebar on Pages', 'illusive' ),
            'section'  => 'illusive_general_options',
            'type'     => 'checkbox',
            'priority' => 6
        ) );

        $wp_customize->add_setting( 'illusive_general_author_post', array(
            'section'  => 'illusive_general_options',
            'capability' => 'edit_theme_options',
            'sanitize_callback'	=> 'illusive_sanitize_checkbox',
        ) );

        $wp_customize->add_control( 'illusive_general_author_post', array(
            'settings' => 'illusive_general_author_post',
            'label'    => esc_html__( 'Disable Author Box on Posts', 'illusive' ),
            'section'  => 'illusive_general_options',
            'type'     => 'checkbox',
            'priority' => 6
        ) );

        $wp_customize->add_setting( 'illusive_general_post_summary', array(
            'default' => 'excerpt',
            'section'  => 'illusive_general_options',
            'sanitize_callback'	=> 'illusive_sanitize_radio',
        ) );

        $wp_customize->add_control( 'illusive_general_post_summary', array(
            'type' => 'radio',
            'label'    => esc_html__( 'Homepage and Archive Post Summary Type', 'illusive' ),
            'section'  => 'illusive_general_options',
            'choices'  => array(
                'excerpt'  => esc_html__( 'Use Excerpt', 'illusive' ),
                'full' => esc_html__( 'Use Full Post', 'illusive' ),
            ),
            'priority' => 9
        ) );
        $wp_customize->add_setting( 'illusive_general_post_layout', array(
            'default' => 'standard',
            'section'  => 'illusive_general_options',
            'sanitize_callback'	=> 'illusive_sanitize_radio',
        ) );
        $wp_customize->add_control( 'illusive_general_post_layout', array(
            'type' => 'radio',
            'label'    => esc_html__( 'Homepage and Archive Post Layout Type', 'illusive' ),
            'section'  => 'illusive_general_options',
            'choices'  => array(
                'grid'  => esc_html__( 'Grid Layout [Only in Premium Version]', 'illusive' ),
                'standard' => esc_html__( 'Standard Post', 'illusive' ),
                'list' => esc_html__( 'List Post [Only in Premium Version]', 'illusive' ),
            ),
            'priority' => 10
        ) );

        // Pro Version
        $wp_customize->add_setting( 'pro_version_colors2', array(
            'sanitize_callback' => 'illusive_sanitize_custom_control'
        ) );
        $wp_customize->add_control( new Illusive_Customize_Pro_Version ( $wp_customize,
                'pro_version_colors2', array(
                    'section'	  => 'illusive_general_options',
                    'type'		  => 'pro_options',
                    'label' 	  => esc_html__( 'Upgrade', 'illusive' ),
                    'description' => esc_url_raw( 'https://www.vinethemes.com/downloads/illusive-blog-wordpress-theme/' ),
                    'priority'	  => 100
                )
            )
        );


        // Footer Settings
        $wp_customize->add_section( 'illusive_footer_settings', array(
            'title'       => esc_html__( 'Footer Settings', 'illusive' ),
            'description' => esc_html__( 'Configure Your Footer Here. You can\'t change our footer links in the free version of this theme.', 'illusive' ),
            'priority'    => 8
        ) );

        $wp_customize->add_setting(
            'footer_copyright',
            array(
                'default'     => 'Copyright 2019-2020.',
                'sanitize_callback' => 'sanitize_text_field'
            )
        );

        $wp_customize->add_control('footer_copyright', array(
                    'label'      => esc_html__('Footer Copyright Text','illusive'),
                    'section'    => 'illusive_footer_settings',
                    'settings'   => 'footer_copyright',
                    'type'		 => 'text',
                    'priority'	 => 1
                )
        );


        // Pro Version
        $wp_customize->add_setting( 'pro_version_colors3', array(
            'sanitize_callback' => 'illusive_sanitize_custom_control'
        ) );
        $wp_customize->add_control( new Illusive_Customize_Pro_Version ( $wp_customize,
                'pro_version_colors3', array(
                    'section'	  => 'illusive_footer_settings',
                    'type'		  => 'pro_options',
                    'label' 	  => esc_html__( 'Upgrade', 'illusive' ),
                    'description' => esc_url_raw( 'https://www.vinethemes.com/downloads/illusive-blog-wordpress-theme/' ),
                    'priority'	  => 100
                )
            )
        );


    }


// Adding controls to default customizer panel
    add_action('customize_register','illusive_customizer_options');
    /*
     * Add in our custom Main Color setting and control to be used in the Customizer in the Colors section
     *
     */
    function illusive_customizer_options( $wp_customize ) {
        $wp_customize->add_setting(
            'illusive_main_color', //give it an ID
            array(
                'default' => '#e3b464', // Give it a default
                'sanitize_callback' => 'sanitize_hex_color',
                'transport'      => 'refresh'
            )
        );
        $wp_customize->add_control(
            new WP_Customize_Color_Control(
                $wp_customize,
                'illusive_custom_main_color', //give it an ID
                array(
                    'label'      => esc_html__( 'Main Color', 'illusive' ), //set the label to appear in the Customizer
                    'section'    => 'colors', //select the section for it to appear under
                    'settings'   => 'illusive_main_color' //pick the setting it applies to
                )
            )
        );
        $wp_customize->add_setting(
            'illusive_main_text', //give it an ID
            array(
                'default' => '#e3b464', // Give it a default
                'sanitize_callback' => 'sanitize_hex_color',
                'transport'      => 'refresh'
            )
        );
        $wp_customize->add_control(
            new Illusive_Customize_Pro_Version(
                $wp_customize,
                'illusive_custom_text', //give it an ID
                array(
                    'label'      => esc_html__( 'Upgrade', 'illusive' ), //set the label to appear in the Customizer
                    'section'    => 'colors', //select the section for it to appear under
                    'settings'   => 'illusive_main_text', //pick the setting it applies to
                    'description' => esc_url_raw( 'https://www.vinethemes.com/downloads/illusive-blog-wordpress-theme/' ),
                )
            )
        );



    }



    /*
    * Adding a section to manage social links
    */
    function illusive_sociallink_customizer( $wp_customize ) {
        $wp_customize->add_section( 'illusive_social_section' , array(
            'title' => esc_html__( 'Social Links', 'illusive' ),
            'priority' => 30,
            'description' => 'Setting for Social media Icons. Add a link to each of your Social Media Profiles. Leave the field empty for the icons you don\'t want to be displayed.',

        ) );

        $wp_customize->add_setting( 'illusive_facebook', array(
            'sanitize_callback'	=> 'esc_url_raw',

        ) );
        $wp_customize->add_setting( 'illusive_twitter', array(
            'sanitize_callback'	=> 'esc_url_raw',

        ) );

        $wp_customize->add_setting( 'illusive_pinterest', array(
            'sanitize_callback'	=> 'esc_url_raw',

        ) );

        $wp_customize->add_setting( 'illusive_linkedin', array(
            'sanitize_callback'	=> 'esc_url_raw',

        ) );
        $wp_customize->add_setting( 'illusive_instagram', array(
            'sanitize_callback'	=> 'esc_url_raw',

        ) );
        $wp_customize->add_setting( 'illusive_bloglovin', array(
            'sanitize_callback'	=> 'esc_url_raw',

        ) );
        $wp_customize->add_setting( 'illusive_snapchat', array(
            'sanitize_callback'	=> 'esc_url_raw',

        ) );
        $wp_customize->add_setting( 'illusive_tumblr', array(
            'sanitize_callback'	=> 'esc_url_raw',

        ) );
        $wp_customize->add_setting( 'illusive_youtube', array(
            'sanitize_callback'	=> 'esc_url_raw',

        ) );
        $wp_customize->add_setting( 'illusive_dribbble', array(
            'sanitize_callback'	=> 'esc_url_raw',

        ) );
        $wp_customize->add_setting( 'illusive_soundcloud', array(
            'sanitize_callback'	=> 'esc_url_raw',

        ) );
        $wp_customize->add_setting( 'illusive_vimeo', array(
            'sanitize_callback'	=> 'esc_url_raw',

        ) );
        $wp_customize->add_setting( 'illusive_rss', array(
            'sanitize_callback'	=> 'esc_url_raw',

        ) );


        $wp_customize->add_control( 'facebook', array(
            'type' => 'url',
            'label' => esc_html__( 'Facebook', 'illusive' ),
            'section' => 'illusive_social_section',
            'settings' => 'illusive_facebook',
        ) );
        $wp_customize->add_control( 'twitter', array(
            'type' => 'url',
            'label' => esc_html__( 'Twitter', 'illusive' ),
            'section' => 'illusive_social_section',
            'settings' => 'illusive_twitter',
        ) );
        $wp_customize->add_control( 'linkedin', array(
            'type' => 'url',
            'label' => esc_html__( 'Linkedin', 'illusive' ),
            'section' => 'illusive_social_section',
            'settings' => 'illusive_linkedin',
        ) );
        $wp_customize->add_control( 'pinterest', array(
            'type' => 'url',
            'label' => esc_html__( 'Pinterest', 'illusive' ),
            'section' => 'illusive_social_section',
            'settings' => 'illusive_pinterest',
        ) );

        $wp_customize->add_control( 'instagram', array(
            'type' => 'url',
            'label' => esc_html__( 'Instagram', 'illusive' ),
            'section' => 'illusive_social_section',
            'settings' => 'illusive_instagram',
        ) );
        $wp_customize->add_control( 'bloglovin', array(
            'type' => 'url',
            'label' => esc_html__( 'Bloglovin', 'illusive' ),
            'section' => 'illusive_social_section',
            'settings' => 'illusive_bloglovin',
        ) );
        $wp_customize->add_control( 'snapchat', array(
            'type' => 'url',
            'label' => esc_html__( 'Snapchat', 'illusive' ),
            'section' => 'illusive_social_section',
            'settings' => 'illusive_snapchat',
        ) );
        $wp_customize->add_control( 'tumblr', array(
            'type' => 'url',
            'label' => esc_html__( 'Tumblr', 'illusive' ),
            'section' => 'illusive_social_section',
            'settings' => 'illusive_tumblr',
        ) );
        $wp_customize->add_control( 'youtube', array(
            'type' => 'url',
            'label' => esc_html__( 'Youtube', 'illusive' ),
            'section' => 'illusive_social_section',
            'settings' => 'illusive_youtube',
        ) );
        $wp_customize->add_control( 'dribbble', array(
            'type' => 'url',
            'label' => esc_html__( 'Dribbble', 'illusive' ),
            'section' => 'illusive_social_section',
            'settings' => 'illusive_dribbble',
        ) );
        $wp_customize->add_control( 'soundcloud', array(
            'type' => 'url',
            'label' => esc_html__( 'Soundcloud', 'illusive' ),
            'section' => 'illusive_social_section',
            'settings' => 'illusive_soundcloud',
        ) );
        $wp_customize->add_control( 'vimeo', array(
            'type' => 'url',
            'label' => esc_html__( 'Vimeo', 'illusive' ),
            'section' => 'illusive_social_section',
            'settings' => 'illusive_vimeo',
        ) );
        $wp_customize->add_control( 'rss', array(
            'type' => 'url',
            'label' => esc_html__( 'Rss', 'illusive' ),
            'section' => 'illusive_social_section',
            'settings' => 'illusive_rss',
        ) );


    }
    add_action('customize_register', 'illusive_sociallink_customizer');

    function illusive_sanitize_text( $input ) {
        return wp_kses_post( force_balance_tags( $input ) );
    }


    //checkbox sanitization function
    function illusive_sanitize_checkbox( $input ){

        //returns true if checkbox is checked
        return (( isset( $input ) && true === $input ) ? true : false );
    }

    //radio box sanitization function
    function illusive_sanitize_radio( $input, $setting ){
    $valid = array(
        'Slider1' => 'Slider1',
        'Slider2' => 'Slider2',
        'Slider3' => 'Slider3',
        'excerpt' => 'excerpt',
        'full' => 'full',
        'standard' => 'standard',
        'enable' => 'enable',
        'disable' => 'disable',
    );
    if ( array_key_exists( $input, $valid ) ) {
        return $input;
    } else {
        return $setting->default;
    }
    }

    function illusive_panels_js() {
        wp_enqueue_style( 'illusive-customizer-ui-css', get_theme_file_uri( '/customizer-ui.css' ) );
    }
    add_action( 'customize_controls_enqueue_scripts', 'illusive_panels_js' );
