<?php
/**
 * Pro Blog Theme Customizer.
 *
 * @package Pro Blog
 */

/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function pro_blog_customize_register( $wp_customize ) {

if ( isset( $wp_customize->selective_refresh ) ) {
	$wp_customize->get_setting( 'blogname' )->transport        = 'postMessage';
	$wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage';
	$wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';

	$wp_customize->selective_refresh->add_partial( 'blogname', array(
		'selector'            => '.site-title a',
		'render_callback'     => 'pro_blog_customize_partial_blogname',
	) );

	$wp_customize->selective_refresh->add_partial( 'blogdescription', array(
		'selector'            => '.site-description',
		'render_callback'     => 'pro_blog_customize_partial_blogdescription',
	) );
}

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

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

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

function pro_blog_customizer_option( $key ) {

	if ( empty( $key ) ) {
        return;
    }

    $value = '';


    $default_value = null;

    if ( is_array( $default ) && isset( $default[ $key ] ) ) {
        $default_value = $default[ $key ];
    }

    if ( null !== $default_value ) {
        $value = get_theme_mod( $key, $default_value );
    }
    else {
        $value = get_theme_mod( $key );
    }

    return $value;

}

// Get all options in array

function pro_blog_customizer_options() {

    $value = array();

    $value = get_theme_mods();

    return $value;

}

function pro_blog_register_theme_customizer( $wp_customize ) {

	// Panel
	$wp_customize->add_panel(
		'basic_panel', 
		array(
			'title'				=> esc_html__('Theme Options', 'pro-blog'),
			'priority' 			=> 90		
			)
	);

	// Basic Options Section
	$wp_customize->add_section(
		'post_options', 
		array(    
			'title'       => esc_html__('Basic Options', 'pro-blog'),
			'panel'       => 'basic_panel'    
		)
	);

	// Setting category_meta.
	$wp_customize->add_setting( 
		'category_meta',
		array(
			'default'           => $default['category_meta'],
			'sanitize_callback' => 'pro_blog_sanitize_checkbox',
		)
	);
	$wp_customize->add_control( 
		'category_meta',
		array(
			'label'    		=> esc_html__( 'Show Category', 'pro-blog' ),
			'section'  		=> 'post_options',
			'type'     		=> 'checkbox',
		)
	);

	// Setting author_meta.
	$wp_customize->add_setting( 
		'author_meta',
		array(
			'default'           => $default['author_meta'],
			'sanitize_callback' => 'pro_blog_sanitize_checkbox',
		)
	);
	$wp_customize->add_control( 
		'author_meta',
		array(
			'label'    		=> esc_html__( 'Show Author', 'pro-blog' ),
			'section'  		=> 'post_options',
			'type'     		=> 'checkbox',
		)
	);

	// Setting date_meta.
	$wp_customize->add_setting( 
		'date_meta',
		array(
			'default'           => $default['date_meta'],
			'sanitize_callback' => 'pro_blog_sanitize_checkbox',
		)
	);
	$wp_customize->add_control( 
		'date_meta',
		array(
			'label'    		=> esc_html__( 'Show Posted Date', 'pro-blog' ),
			'section'  		=> 'post_options',
			'type'     		=> 'checkbox',
		)
	);

	//Single Post Options Section
	$wp_customize->add_section(
		'single_post_options', 
		array(    
			'title'       => esc_html__('Single Post Options', 'pro-blog'),
			'panel'       => 'basic_panel'    
		)
	);

	// Setting featured_img_meta.
	$wp_customize->add_setting( 
		'featured_img_meta',
		array(
			'default'           => $default['featured_img_meta'],
			'sanitize_callback' => 'pro_blog_sanitize_checkbox',
		)
	);
	$wp_customize->add_control( 
		'featured_img_meta',
		array(
			'label'    		=> esc_html__( 'Show featured image', 'pro-blog' ),
			'section'  		=> 'single_post_options',
			'type'     		=> 'checkbox',
		)
	);

	// Setting single_category_meta.
	$wp_customize->add_setting( 
		'single_category_meta',
		array(
			'default'           => $default['single_category_meta'],
			'sanitize_callback' => 'pro_blog_sanitize_checkbox',
		)
	);
	$wp_customize->add_control( 
		'single_category_meta',
		array(
			'label'    		=> esc_html__( 'Show Category', 'pro-blog' ),
			'section'  		=> 'single_post_options',
			'type'     		=> 'checkbox',
		)
	);

	// Setting author_meta.
	$wp_customize->add_setting( 
		'single_author_meta',
		array(
			'default'           => $default['single_author_meta'],
			'sanitize_callback' => 'pro_blog_sanitize_checkbox',
		)
	);
	$wp_customize->add_control( 
		'single_author_meta',
		array(
			'label'    		=> esc_html__( 'Show Author', 'pro-blog' ),
			'section'  		=> 'single_post_options',
			'type'     		=> 'checkbox',
		)
	);

	// Setting date_meta.
	$wp_customize->add_setting( 
		'single_date_meta',
		array(
			'default'           => $default['single_date_meta'],
			'sanitize_callback' => 'pro_blog_sanitize_checkbox',
		)
	);
	$wp_customize->add_control( 
		'single_date_meta',
		array(
			'label'    		=> esc_html__( 'Show Posted Date', 'pro-blog' ),
			'section'  		=> 'single_post_options',
			'type'     		=> 'checkbox',
		)
	);

	// Setting single_tags_meta.
	$wp_customize->add_setting( 
		'single_tags_meta',
		array(
			'default'           => $default['single_tags_meta'],
			'sanitize_callback' => 'pro_blog_sanitize_checkbox',
		)
	);
	$wp_customize->add_control( 
		'single_tags_meta',
		array(
			'label'    		=> esc_html__( 'Show Tags', 'pro-blog' ),
			'section'  		=> 'single_post_options',
			'type'     		=> 'checkbox',
		)
	);

	// Setting show_post_nav
	$wp_customize->add_setting( 
		'show_post_nav',
		array(
			'default'           => $default['show_post_nav'],
			'sanitize_callback' => 'pro_blog_sanitize_checkbox',
		)
	);
	$wp_customize->add_control( 
		'show_post_nav',
		array(
			'label'    => esc_html__( 'Show Post Navigation', 'pro-blog' ),
			'section'  => 'single_post_options',
			'type'     => 'checkbox',
		)
	);

	// Footer Section
	$wp_customize->add_section(
		'footer', 
		array(    
			'title'       => esc_html__('Footer Options', 'pro-blog'),
			'panel'       => 'basic_panel'    
		)
	);

	$wp_customize->add_setting(
		'copyright', 
		array(
			'default' 			=>  $default['copyright'],
			'sanitize_callback' => 'sanitize_text_field'
		)
	);

	$wp_customize->add_control(
		'copyright', 
		array(
			'label'       => esc_html__('Copyright Text', 'pro-blog'),
			'description' => esc_html__('Copyright text of the site', 'pro-blog'),
			'settings'    => 'copyright',
			'section'     => 'footer',
			'type'        => 'text'
		)
	);



//=============================================================
// Checkbox santitization
//=============================================================

function pro_blog_sanitize_checkbox( $checked ) {

	return ( ( isset( $checked ) && true === $checked ) ? true : false );
}

//=============================================================
// Positive integer santitization
//=============================================================

function pro_blog_sanitize_positive_integer( $input, $setting ) {

	$input = absint( $input );

	// If the input is an absolute integer, return it.
	// otherwise, return the default.
	return ( $input ? $input : $setting->default );

}

//=============================================================
// Check if related post is active
//=============================================================

function pro_blog_is_relative_posts_active( $control ) {

	if ( true == $control->manager->get_setting( 'show_related_posts' )->value() ) {
		return true;
	} else {
		return false;
	}

}

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

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