<?php

/**
 * News Axis Theme Customizer
 *
 * @package  News Axis
 */



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

    $news_axis_theme = wp_get_theme();
	$news_axis_theme_textdomain = $news_axis_theme->get('textdomain');
    if($news_axis_theme_textdomain == 'news-axis'){
        $blog_style_default = 'grid';
    }else{
        $blog_style_default = 'list';

    }

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

    ));

    $wp_customize->add_panel('news_axis_settings', array(
        'priority'       => 50,
        'title'          => __('News Axis Theme settings', 'news-axis'),
        'description'    => __('All News Axis theme settings', 'news-axis'),
    ));
    $wp_customize->add_section('news_axis_header', array(
        'title' => __('News Axis Header Settings', 'news-axis'),
        'capability'     => 'edit_theme_options',
        'description'     => __('News Axis theme header settings', 'news-axis'),
        'panel'    => 'news_axis_settings',

    ));
    $wp_customize->add_setting('news_axis_main_menu_style', array(
        'default'        => 'style1',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'news_axis_sanitize_select',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('news_axis_main_menu_style', array(
        'label'      => __('Main Menu Style', 'news-axis'),
        'description' => __('You can set the menu style one or two. ', 'news-axis'),
        'section'    => 'news_axis_header',
        'settings'   => 'news_axis_main_menu_style',
        'type'       => 'select',
        'choices'    => array(
            'style1' => __('Style One', 'news-axis'),
            'style2' => __('Style Two', 'news-axis'),
        ),
    ));

    //News Axis Home intro
    $wp_customize->add_section('news_axis_hbanner', array(
        'title' => __('Intro Settings', 'news-axis'),
        'capability'     => 'edit_theme_options',
        'description'     => __('Portfoli profile Intro Settings', 'news-axis'),
        'panel'    => 'news_axis_settings',

    ));
    $wp_customize->add_setting('news_axis_hbanner_show', array(
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'default'       =>  '',
        'sanitize_callback' => 'absint',
        'transport'     => 'refresh',
    ));
    $wp_customize->add_control('news_axis_hbanner_show', array(
        'label'      => __('Show Home Banner? ', 'news-axis'),
        'section'    => 'news_axis_hbanner',
        'settings'   => 'news_axis_hbanner_show',
        'type'       => 'checkbox',

    ));
    $wp_customize->add_setting('news_axis_hbanner_img', array(
        'capability'        => 'edit_theme_options',
        'default'           => get_template_directory_uri() . '/assets/img/hero.png',
        'sanitize_callback' => 'news_axis_sanitize_image',
    ));
    $wp_customize->add_control(new WP_Customize_Image_Control(
        $wp_customize,
        'news_axis_hbanner_img',
        array(
            'label'    => __('Upload Banner Image', 'news-axis'),
            'description'    => __('Image size should be 450px width & 460px height for better view.', 'news-axis'),
            'section'  => 'news_axis_hbanner',
            'settings' => 'news_axis_hbanner_img',
        )
    ));
    $wp_customize->add_setting('news_axis_hbanner_subtitle', array(
        'default' => __('Tranding News', 'news-axis'),
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'sanitize_text_field',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('news_axis_hbanner_subtitle', array(
        'label'      => __('Intro Subtitle', 'news-axis'),
        'section'    => 'news_axis_hbanner',
        'settings'   => 'news_axis_hbanner_subtitle',
        'type'       => 'text',
    ));
    $wp_customize->add_setting('news_axis_hbanner_title', array(
        'default' => __('Introduce Our', 'news-axis'),
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'sanitize_text_field',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('news_axis_hbanner_title', array(
        'label'      => __('Intro Title', 'news-axis'),
        'section'    => 'news_axis_hbanner',
        'settings'   => 'news_axis_hbanner_title',
        'type'       => 'text',
    ));
    $wp_customize->add_setting('news_axis_color_title', array(
        'default' => __('Today\'s Top News', 'news-axis'),
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'sanitize_text_field',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('news_axis_color_title', array(
        'label'      => __('Intro Color Title', 'news-axis'),
        'section'    => 'news_axis_hbanner',
        'settings'   => 'news_axis_color_title',
        'type'       => 'text',
    ));

    $wp_customize->add_setting('news_axis_hbanner_desc', array(
        'default' => '',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'wp_kses_post',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('news_axis_hbanner_desc', array(
        'label'      => __('Intro Description', 'news-axis'),
        'section'    => 'news_axis_hbanner',
        'settings'   => 'news_axis_hbanner_desc',
        'type'       => 'textarea',
    ));
    $wp_customize->add_setting('news_axis_btn_text_one', array(
        'default' => __('Read More', 'news-axis'),
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'sanitize_text_field',
        'transport' => 'postMessage',
    ));
    $wp_customize->add_control('news_axis_btn_text_one', array(
        'label'      => __('Button one text', 'news-axis'),
        'section'    => 'news_axis_hbanner',
        'settings'   => 'news_axis_btn_text_one',
        'type'       => 'text',
    ));

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


    //News Axis blog settings
    $wp_customize->add_section('news_axis_blog', array(
        'title' => __('News Axis Blog Settings', 'news-axis'),
        'capability'     => 'edit_theme_options',
        'description'     => __('News Axis theme blog settings', 'news-axis'),
        'panel'    => 'news_axis_settings',

    ));
    $wp_customize->add_setting('news_axis_blog_container', array(
        'default'        => 'container',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'news_axis_sanitize_select',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('news_axis_blog_container', array(
        'label'      => __('Container type', 'news-axis'),
        'description' => __('You can set standard container or full width container. ', 'news-axis'),
        'section'    => 'news_axis_blog',
        'settings'   => 'news_axis_blog_container',
        'type'       => 'select',
        'choices'    => array(
            'container' => __('Standard Container', 'news-axis'),
            'container-fluid' => __('Full width Container', 'news-axis'),
        ),
    ));
    $wp_customize->add_setting('news_axis_blog_layout', array(
        'default'        => 'fullwidth',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'news_axis_sanitize_select',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('news_axis_blog_layout', array(
        'label'      => __('Select Blog Layout', 'news-axis'),
        'description' => __('Right and Left sidebar only show when sidebar widget is available. ', 'news-axis'),
        'section'    => 'news_axis_blog',
        'settings'   => 'news_axis_blog_layout',
        'type'       => 'select',
        'choices'    => array(
            'rightside' => __('Right Sidebar', 'news-axis'),
            'leftside' => __('Left Sidebar', 'news-axis'),
            'fullwidth' => __('No Sidebar', 'news-axis'),
        ),
    ));
    $wp_customize->add_setting('news_axis_blog_style', array(
        'default'        =>  $blog_style_default,
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'news_axis_sanitize_select',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('news_axis_blog_style', array(
        'label'      => __('Select Blog Style', 'news-axis'),
        'section'    => 'news_axis_blog',
        'settings'   => 'news_axis_blog_style',
        'type'       => 'select',
        'choices'    => array(
            'grid' => __('Grid Style', 'news-axis'),
            'list' => __('List Style', 'news-axis'),
            'classic' => __('Classic Style', 'news-axis'),
        ),
    ));
    $wp_customize->add_setting('news_axis_readmore', array(
        'default' => '',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'sanitize_text_field',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('news_axis_readmore', array(
        'label'      => __('Blog Readmore Text', 'news-axis'),
        'description' => __('Enter blog readmore text. leave empty for hide', 'news-axis'),
        'section'    => 'news_axis_blog',
        'settings'   => 'news_axis_readmore',
        'type'       => 'text',
    ));
    //News Axis page settings
    $wp_customize->add_section('news_axis_page', array(
        'title' => __('News Axis Page Settings', 'news-axis'),
        'capability'     => 'edit_theme_options',
        'description'     => __('News Axis theme blog settings', 'news-axis'),
        'panel'    => 'news_axis_settings',

    ));
    $wp_customize->add_setting('news_axis_page_container', array(
        'default'        => 'container',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'news_axis_sanitize_select',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('news_axis_page_container', array(
        'label'      => __('Page Container type', 'news-axis'),
        'description' => __('You can set standard container or full width container for page. ', 'news-axis'),
        'section'    => 'news_axis_page',
        'settings'   => 'news_axis_page_container',
        'type'       => 'select',
        'choices'    => array(
            'container' => __('Standard Container', 'news-axis'),
            'container-fluid' => __('Full width Container', 'news-axis'),
        ),
    ));
    $wp_customize->add_setting('news_axis_page_header', array(
        'default'        => 'show',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'news_axis_sanitize_select',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('news_axis_page_header', array(
        'label'      => __('Show Page header', 'news-axis'),
        'section'    => 'news_axis_page',
        'settings'   => 'news_axis_page_header',
        'type'       => 'select',
        'choices'    => array(
            'show' => __('Show all pages', 'news-axis'),
            'hide-home' => __('Hide Only Front Page', 'news-axis'),
            'hide' => __('Hide All Pages', 'news-axis'),
        ),
    ));
    $wp_customize->add_section('news_axis_footer', array(
        'title' => __('Footer Settings', 'news-axis'),
        'capability'     => 'edit_theme_options',
        'description'     => __('News Axis theme blog settings', 'news-axis'),
        'panel'    => 'news_axis_settings',
    ));
    $wp_customize->add_setting('news_axis_footer_credit', array(
        'default' => __('News Axis All rights reserved', 'news-axis'),
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'sanitize_text_field',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('news_axis_footer_credit', array(
        'label'      => __('Footer Credit Text', 'news-axis'),
        'description' => news_axis_footer_settings_desc(),
        'section'    => 'news_axis_footer',
        'settings'   => 'news_axis_footer_credit',
        'type'       => 'text',
    ));



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

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

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

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