<?php

/**
 * Blog Eye Theme Customizer
 *
 * @package Blog Eye
 */



/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function blog_eye_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';

    //select sanitization function
    function blog_eye_sanitize_select($input, $setting)
    {
        $input = sanitize_key($input);
        $choices = $setting->manager->get_control($setting->id)->choices;
        return (array_key_exists($input, $choices) ? $input : $setting->default);
    }
    function blog_eye_sanitize_image($file, $setting)
    {
        $mimes = array(
            'jpg|jpeg|jpe' => 'image/jpeg',
            'gif'          => 'image/gif',
            'png'          => 'image/png',
            'bmp'          => 'image/bmp',
            'tif|tiff'     => 'image/tiff',
            'ico'          => 'image/x-icon'
        );
        //check file type from file name
        $file_ext = wp_check_filetype($file, $mimes);
        //if file has a valid mime type return it, otherwise return default
        return ($file_ext['ext'] ? $file : $setting->default);
    }

    $wp_customize->add_setting('blog_eye_site_tagline_show', array(
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'default'       =>  '',
        'sanitize_callback' => 'absint',
        'transport'     => 'refresh',
    ));
    $wp_customize->add_control('blog_eye_site_tagline_show', array(
        'label'      => __('Hide Site Tagline Only? ', 'blog-eye'),
        'section'    => 'title_tagline',
        'settings'   => 'blog_eye_site_tagline_show',
        'type'       => 'checkbox',

    ));

    $wp_customize->add_panel('blog_eye_settings', array(
        'priority'       => 50,
        'title'          => __('Blog Eye Theme settings', 'blog-eye'),
        'description'    => __('All Blog Eye theme settings', 'blog-eye'),
    ));
    $wp_customize->add_section('blog_eye_header', array(
        'title' => __('Blog Eye Header Settings', 'blog-eye'),
        'capability'     => 'edit_theme_options',
        'description'     => __('Blog Eye theme header settings', 'blog-eye'),
        'panel'    => 'blog_eye_settings',

    ));
    $wp_customize->add_setting('blog_eye_header_style', array(
        'default'        => 'style1',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'blog_eye_sanitize_select',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('blog_eye_header_style', array(
        'label'      => __('Header Style', 'blog-eye'),
        'description' => __('You can set the menu style one or two. ', 'blog-eye'),
        'section'    => 'blog_eye_header',
        'settings'   => 'blog_eye_header_style',
        'type'       => 'select',
        'choices'    => array(
            'style1' => __('Style One', 'blog-eye'),
            'style2' => __('Style Two', 'blog-eye'),
        ),
    ));

    //Blog Eye Home intro
    $wp_customize->add_section('blog_eye_hbanner', array(
        'title' => __('Blog Intro Settings', 'blog-eye'),
        'capability'     => 'edit_theme_options',
        'description'     => __('Portfoli profile Intro Settings', 'blog-eye'),
        'panel'    => 'blog_eye_settings',

    ));
    $wp_customize->add_setting('blog_eye_hbanner_show', array(
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'default'       =>  '',
        'sanitize_callback' => 'absint',
        'transport'     => 'refresh',
    ));
    $wp_customize->add_control('blog_eye_hbanner_show', array(
        'label'      => __('Show Home Banner? ', 'blog-eye'),
        'section'    => 'blog_eye_hbanner',
        'settings'   => 'blog_eye_hbanner_show',
        'type'       => 'checkbox',

    ));
    $wp_customize->add_setting('blog_eye_hbanner_img', array(
        'capability'        => 'edit_theme_options',
        'default'           => get_template_directory_uri() . '/assets/img/hero.jpg',
        'sanitize_callback' => 'blog_eye_sanitize_image',
    ));
    $wp_customize->add_control(new WP_Customize_Image_Control(
        $wp_customize,
        'blog_eye_hbanner_img',
        array(
            'label'    => __('Upload Banner Image', 'blog-eye'),
            'description'    => __('Image size should be 450px width & 460px height for better view.', 'blog-eye'),
            'section'  => 'blog_eye_hbanner',
            'settings' => 'blog_eye_hbanner_img',
        )
    ));
    $wp_customize->add_setting('blog_eye_hbanner_subtitle', array(
        'default' => __('WELCOME TO MY WORLD', 'blog-eye'),
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'sanitize_text_field',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('blog_eye_hbanner_subtitle', array(
        'label'      => __('Intro Subtitle', 'blog-eye'),
        'section'    => 'blog_eye_hbanner',
        'settings'   => 'blog_eye_hbanner_subtitle',
        'type'       => 'text',
    ));
    $wp_customize->add_setting('blog_eye_hbanner_title', array(
        'default' => __('Introduce Our', 'blog-eye'),
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'sanitize_text_field',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('blog_eye_hbanner_title', array(
        'label'      => __('Intro Title', 'blog-eye'),
        'section'    => 'blog_eye_hbanner',
        'settings'   => 'blog_eye_hbanner_title',
        'type'       => 'text',
    ));
    $wp_customize->add_setting('blog_eye_color_title', array(
        'default' => __('Creative Blog.', 'blog-eye'),
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'sanitize_text_field',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('blog_eye_color_title', array(
        'label'      => __('Intro Color Title', 'blog-eye'),
        'section'    => 'blog_eye_hbanner',
        'settings'   => 'blog_eye_color_title',
        'type'       => 'text',
    ));

    $wp_customize->add_setting('blog_eye_hbanner_desc', array(
        'default' => '',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'wp_kses_post',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('blog_eye_hbanner_desc', array(
        'label'      => __('Intro Description', 'blog-eye'),
        'section'    => 'blog_eye_hbanner',
        'settings'   => 'blog_eye_hbanner_desc',
        'type'       => 'textarea',
    ));
    $wp_customize->add_setting('blog_eye_btn_text_one', array(
        'default' => __('Our Blogs', 'blog-eye'),
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'sanitize_text_field',
        'transport' => 'postMessage',
    ));
    $wp_customize->add_control('blog_eye_btn_text_one', array(
        'label'      => __('Button one text', 'blog-eye'),
        'section'    => 'blog_eye_hbanner',
        'settings'   => 'blog_eye_btn_text_one',
        'type'       => 'text',
    ));

    $wp_customize->add_setting('blog_eye_btn_url_one', array(
        'default' => '#',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'esc_url_raw',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('blog_eye_btn_url_one', array(
        'label'      => __('Button one url', 'blog-eye'),
        'description'      => __('Keep url empty for hide this button', 'blog-eye'),
        'section'    => 'blog_eye_hbanner',
        'settings'   => 'blog_eye_btn_url_one',
        'type'       => 'url',
    ));


    //blog-eye settings
    $wp_customize->add_section('blog_eye_blog', array(
        'title' => __('Blog Eye Settings', 'blog-eye'),
        'capability'     => 'edit_theme_options',
        'description'     => __('Blog Eye theme blog settings', 'blog-eye'),
        'panel'    => 'blog_eye_settings',

    ));
    $wp_customize->add_setting('blog_eye_blog_container', array(
        'default'        => 'container',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'blog_eye_sanitize_select',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('blog_eye_blog_container', array(
        'label'      => __('Container type', 'blog-eye'),
        'description' => __('You can set standard container or full width container. ', 'blog-eye'),
        'section'    => 'blog_eye_blog',
        'settings'   => 'blog_eye_blog_container',
        'type'       => 'select',
        'choices'    => array(
            'container' => __('Standard Container', 'blog-eye'),
            'container-fluid' => __('Full width Container', 'blog-eye'),
        ),
    ));
    $wp_customize->add_setting('blog_eye_blog_layout', array(
        'default'        => 'fullwidth',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'blog_eye_sanitize_select',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('blog_eye_blog_layout', array(
        'label'      => __('Select Blog Layout', 'blog-eye'),
        'description' => __('Right and Left sidebar only show when sidebar widget is available. ', 'blog-eye'),
        'section'    => 'blog_eye_blog',
        'settings'   => 'blog_eye_blog_layout',
        'type'       => 'select',
        'choices'    => array(
            'rightside' => __('Right Sidebar', 'blog-eye'),
            'leftside' => __('Left Sidebar', 'blog-eye'),
            'fullwidth' => __('No Sidebar', 'blog-eye'),
        ),
    ));
    $wp_customize->add_setting('blog_eye_blog_style', array(
        'default'        => 'list',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'blog_eye_sanitize_select',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('blog_eye_blog_style', array(
        'label'      => __('Select Blog Style', 'blog-eye'),
        'section'    => 'blog_eye_blog',
        'settings'   => 'blog_eye_blog_style',
        'type'       => 'select',
        'choices'    => array(
            'list' => __('List Style', 'blog-eye'),
            'grid' => __('Grid Style', 'blog-eye'),
            'classic' => __('Classic Style', 'blog-eye'),
        ),
    ));
    //Blog Eye page settings
    $wp_customize->add_section('blog_eye_page', array(
        'title' => __('Blog Eye Page Settings', 'blog-eye'),
        'capability'     => 'edit_theme_options',
        'description'     => __('Blog Eye theme blog settings', 'blog-eye'),
        'panel'    => 'blog_eye_settings',

    ));
    $wp_customize->add_setting('blog_eye_page_container', array(
        'default'        => 'container',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'blog_eye_sanitize_select',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('blog_eye_page_container', array(
        'label'      => __('Page Container type', 'blog-eye'),
        'description' => __('You can set standard container or full width container for page. ', 'blog-eye'),
        'section'    => 'blog_eye_page',
        'settings'   => 'blog_eye_page_container',
        'type'       => 'select',
        'choices'    => array(
            'container' => __('Standard Container', 'blog-eye'),
            'container-fluid' => __('Full width Container', 'blog-eye'),
        ),
    ));
    $wp_customize->add_setting('blog_eye_page_header', array(
        'default'        => 'show',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'blog_eye_sanitize_select',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('blog_eye_page_header', array(
        'label'      => __('Show Page header', 'blog-eye'),
        'section'    => 'blog_eye_page',
        'settings'   => 'blog_eye_page_header',
        'type'       => 'select',
        'choices'    => array(
            'show' => __('Show all pages', 'blog-eye'),
            'hide-home' => __('Hide Only Front Page', 'blog-eye'),
            'hide' => __('Hide All Pages', 'blog-eye'),
        ),
    ));

    $wp_customize->add_section('blog_eye_footer', array(
        'title' => __('Footer Settings', 'blog-eye'),
        'capability'     => 'edit_theme_options',
        'description'     => __('News Axis theme blog settings', 'blog-eye'),
        'panel'    => 'blog_eye_settings',
    ));
    $wp_customize->add_setting('blog_eye_footer_credit', array(
        'default' => __('News Axis All rights reserved', 'blog-eye'),
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'sanitize_text_field',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('blog_eye_footer_credit', array(
        'label'      => __('Footer Credit Text', 'blog-eye'),
        'description' => blog_eye_footer_settings_desc(),
        'section'    => 'blog_eye_footer',
        'settings'   => 'blog_eye_footer_credit',
        'type'       => 'text',
    ));


    if (isset($wp_customize->selective_refresh)) {
        $wp_customize->selective_refresh->add_partial(
            'blogname',
            array(
                'selector'        => '.site-title a',
                'render_callback' => 'blog_eye_customize_partial_blogname',
            )
        );
        $wp_customize->selective_refresh->add_partial(
            'blogdescription',
            array(
                'selector'        => '.site-description',
                'render_callback' => 'blog_eye_customize_partial_blogdescription',
            )
        );
    }
}
add_action('customize_register', 'blog_eye_customize_register');

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

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

/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 */
function blog_eye_customize_preview_js()
{
    wp_enqueue_script('blog-eye-customizer', get_template_directory_uri() . '/assets/js/customizer.js', array('customize-preview'), BLOG_EYE_VERSION, true);
}
add_action('customize_preview_init', 'blog_eye_customize_preview_js');
