<?php

/**
 * RealEstate Press Theme Customizer
 *
 * @package RealEstate Press
 */



/**
 * Add refresh support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function realestate_press_customize_register($wp_customize)
{
    $wp_customize->get_setting('blogname')->transport         = 'refresh';
    $wp_customize->get_setting('blogdescription')->transport  = 'refresh';
    $wp_customize->get_setting('header_textcolor')->transport = 'refresh';

    //select sanitization function
    function realestate_press_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 realestate_press_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('realestate_press_site_tagline_show', array(
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'default'       =>  '',
        'sanitize_callback' => 'absint',
        'transport'     => 'refresh',
    ));
    $wp_customize->add_control('realestate_press_site_tagline_show', array(
        'label'      => __('Hide Site Tagline Only? ', 'realestate-press'),
        'section'    => 'title_tagline',
        'settings'   => 'realestate_press_site_tagline_show',
        'type'       => 'checkbox',

    ));

    $wp_customize->add_panel('realestate_press_settings', array(
        'priority'       => 50,
        'title'          => __('RealEstate Press Theme settings', 'realestate-press'),
        'description'    => __('All RealEstate Press theme settings', 'realestate-press'),
    ));
    $wp_customize->add_section('realestate_press_header', array(
        'title' => __('RealEstate Press Header Settings', 'realestate-press'),
        'capability'     => 'edit_theme_options',
        'description'     => __('RealEstate Press theme header settings', 'realestate-press'),
        'panel'    => 'realestate_press_settings',

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

    //RealEstate Press Home intro
    $wp_customize->add_section('realestate_press_intro', array(
        'title' => __('Intro Settings', 'realestate-press'),
        'capability'     => 'edit_theme_options',
        'description'     => __('Portfoli profile Intro Settings', 'realestate-press'),
        'panel'    => 'realestate_press_settings',

    ));
    $wp_customize->add_setting('realestate_press_intro_show', array(
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'default'       =>  '',
        'sanitize_callback' => 'absint',
        'transport'     => 'refresh',
    ));
    $wp_customize->add_control('realestate_press_intro_show', array(
        'label'      => __('Show Header Intro? ', 'realestate-press'),
        'section'    => 'realestate_press_intro',
        'settings'   => 'realestate_press_intro_show',
        'type'       => 'checkbox',

    ));
    $wp_customize->add_setting('realestate_press_intro_img', array(
        'capability'        => 'edit_theme_options',
        'default'           => get_template_directory_uri() . '/assets/img/intro-img.jpg',
        'sanitize_callback' => 'realestate_press_sanitize_image',
    ));
    $wp_customize->add_control(new WP_Customize_Image_Control(
        $wp_customize,
        'realestate_press_intro_img',
        array(
            'label'    => __('Upload Profile Image', 'realestate-press'),
            'description'    => __('Image size should be 450px width & 460px height for better view.', 'realestate-press'),
            'section'  => 'realestate_press_intro',
            'settings' => 'realestate_press_intro_img',
        )
    ));
    $wp_customize->add_setting('realestate_press_intro_subtitle', array(
        'default' => '',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'sanitize_text_field',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('realestate_press_intro_subtitle', array(
        'label'      => __('Intro Subtitle', 'realestate-press'),
        'section'    => 'realestate_press_intro',
        'settings'   => 'realestate_press_intro_subtitle',
        'type'       => 'text',
    ));
    $wp_customize->add_setting('realestate_press_intro_title', array(
        'default' => __('YOUR TRUSTED PROPERTY', 'realestate-press'),
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'sanitize_text_field',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('realestate_press_intro_title', array(
        'label'      => __('Intro Title', 'realestate-press'),
        'section'    => 'realestate_press_intro',
        'settings'   => 'realestate_press_intro_title',
        'type'       => 'text',
    ));
    $wp_customize->add_setting('realestate_press_intro_designation', array(
        'default' => __('MANAGEMENT TEAM', 'realestate-press'),
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'sanitize_text_field',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('realestate_press_intro_designation', array(
        'label'      => __('Designation', 'realestate-press'),
        'section'    => 'realestate_press_intro',
        'settings'   => 'realestate_press_intro_designation',
        'type'       => 'text',
    ));
    $wp_customize->add_setting('realestate_press_intro_desc', array(
        'default' => '',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'wp_kses_post',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('realestate_press_intro_desc', array(
        'label'      => __('Intro Description', 'realestate-press'),
        'section'    => 'realestate_press_intro',
        'settings'   => 'realestate_press_intro_desc',
        'type'       => 'textarea',
    ));
    $wp_customize->add_setting('realestate_press_btn_text_one', array(
        'default' => __('Hire me', 'realestate-press'),
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'sanitize_text_field',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('realestate_press_btn_text_one', array(
        'label'      => __('Button one text', 'realestate-press'),
        'section'    => 'realestate_press_intro',
        'settings'   => 'realestate_press_btn_text_one',
        'type'       => 'text',
    ));

    $wp_customize->add_setting('realestate_press_btn_url_one', array(
        'default' => '#',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'esc_url_raw',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('realestate_press_btn_url_one', array(
        'label'      => __('Button one url', 'realestate-press'),
        'description'      => __('Keep url empty for hide this button', 'realestate-press'),
        'section'    => 'realestate_press_intro',
        'settings'   => 'realestate_press_btn_url_one',
        'type'       => 'url',
    ));
    $wp_customize->add_setting('realestate_press_btn_text_two', array(
        'default'     => __('View Details', 'realestate-press'),
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'sanitize_text_field',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('realestate_press_btn_text_two', array(
        'label'      => __('Button two text', 'realestate-press'),
        'section'    => 'realestate_press_intro',
        'settings'   => 'realestate_press_btn_text_two',
        'type'       => 'text',
    ));

    $wp_customize->add_setting('realestate_press_btn_url_two', array(
        'default' => '',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'esc_url_raw',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('realestate_press_btn_url_two', array(
        'label'      => __('Button two url', 'realestate-press'),
        'description'      => __('Keep url empty for hide this button', 'realestate-press'),
        'section'    => 'realestate_press_intro',
        'settings'   => 'realestate_press_btn_url_two',
        'type'       => 'text',
    ));

    //RealEstate Press blog settings
    $wp_customize->add_section('realestate_press_blog', array(
        'title' => __('RealEstate Press Blog Settings', 'realestate-press'),
        'capability'     => 'edit_theme_options',
        'description'     => __('RealEstate Press theme blog settings', 'realestate-press'),
        'panel'    => 'realestate_press_settings',

    ));
    $wp_customize->add_setting('realestate_press_blog_container', array(
        'default'        => 'container',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'realestate_press_sanitize_select',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('realestate_press_blog_container', array(
        'label'      => __('Container type', 'realestate-press'),
        'description' => __('You can set standard container or full width container. ', 'realestate-press'),
        'section'    => 'realestate_press_blog',
        'settings'   => 'realestate_press_blog_container',
        'type'       => 'select',
        'choices'    => array(
            'container' => __('Standard Container', 'realestate-press'),
            'container-fluid' => __('Full width Container', 'realestate-press'),
        ),
    ));
    $wp_customize->add_setting('realestate_press_blog_layout', array(
        'default'        => 'rightside',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'realestate_press_sanitize_select',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('realestate_press_blog_layout', array(
        'label'      => __('Select Blog Layout', 'realestate-press'),
        'description' => __('Right and Left sidebar only show when sidebar widget is available. ', 'realestate-press'),
        'section'    => 'realestate_press_blog',
        'settings'   => 'realestate_press_blog_layout',
        'type'       => 'select',
        'choices'    => array(
            'rightside' => __('Right Sidebar', 'realestate-press'),
            'leftside' => __('Left Sidebar', 'realestate-press'),
            'fullwidth' => __('No Sidebar', 'realestate-press'),
        ),
    ));
    $wp_customize->add_setting('realestate_press_blog_style', array(
        'default'        => 'grid',
        'capability'     => 'edit_theme_options',
        'type'           => 'theme_mod',
        'sanitize_callback' => 'realestate_press_sanitize_select',
        'transport' => 'refresh',
    ));
    $wp_customize->add_control('realestate_press_blog_style', array(
        'label'      => __('Select Blog Style', 'realestate-press'),
        'section'    => 'realestate_press_blog',
        'settings'   => 'realestate_press_blog_style',
        'type'       => 'select',
        'choices'    => array(
            'classic' => __('Classic Style', 'realestate-press'),
            'grid' => __('Grid Style', 'realestate-press'),
            'list' => __('List Style', 'realestate-press'),
        ),
    ));
    //RealEstate Press page settings
    $wp_customize->add_section('realestate_press_page', array(
        'title' => __('RealEstate Press Page Settings', 'realestate-press'),
        'capability'     => 'edit_theme_options',
        'description'     => __('RealEstate Press theme blog settings', 'realestate-press'),
        'panel'    => 'realestate_press_settings',

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




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

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

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

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