<?php
/**
 * customizer Theme Customizer.
 *
 * @package cleanead
 */

// Load Customizer Helper Functions
require( get_template_directory() . '/inc/customizer/functions/custom-controls.php' );
require( get_template_directory() . '/inc/customizer/functions/sanitize-functions.php' );
require( get_template_directory() . '/inc/customizer/functions/callback-functions.php' );

// Load Customizer Section Files
require( get_template_directory() . '/inc/customizer/sections/customizer-general.php' );
require( get_template_directory() . '/inc/customizer/sections/customizer-home-archives.php' );
require( get_template_directory() . '/inc/customizer/sections/customizer-single.php' );
require( get_template_directory() . '/inc/customizer/sections/customizer-upgrade.php' );

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

	// Add Theme Options Panel
	$wp_customize->add_panel( 'cleanead_options_panel', array(
		'priority'       => 200,
		'capability'     => 'edit_theme_options',
		'theme_supports' => '',
		'title'          => esc_html__( 'Theme Options', 'cleanead' ),
		'description'    => '',
	) );
	

	$wp_customize->get_setting( 'blogname' )->transport         = 'postMessage';
	$wp_customize->get_setting( 'blogdescription' )->transport  = 'postMessage';
	$wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';

}

add_action( 'customize_register', 'cleanead_customize_register' );

/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 */
function cleanead_customize_preview_js() {
	wp_enqueue_script( 'cleanead_customizer', get_template_directory_uri() . '/inc/customizer/js/customizer.js', array( 'customize-preview' ), '20160530', true );
}
add_action( 'customize_preview_init', 'cleanead_customize_preview_js' );

/**
 * Embed JS file for Customizer Controls
 *
 */
function cleanead_customize_controls_js() {
	
	wp_enqueue_script( 'cleanead-customizer-controls', get_template_directory_uri() . '/inc/customizer/js/customizer-controls.js', array(), '20160530', true );
	
	// Localize the script
	wp_localize_script( 'cleanead-customizer-controls', 'cleanead_theme_links', array(
		'title'	=> esc_html__( 'Theme Links', 'cleanead' ),
		'themeURL'	=> esc_url( __( 'https://themecountry.com/themes/cleanead/', 'cleanead' ) . '?utm_source=customizer&utm_medium=textlink&utm_campaign=cleanead&utm_content=theme-page' ),
		'themeLabel'	=> esc_html__( 'Theme Page', 'cleanead' ),
		'docuURL'	=> esc_url( __( 'https://themecountry.com/docs/cleanead-documentation/', 'cleanead' ) . '?utm_source=customizer&utm_medium=textlink&utm_campaign=cleanead&utm_content=documentation' ),
		'docuLabel'	=>  esc_html__( 'Theme Documentation', 'cleanead' ),
		'rateURL'	=> esc_url( 'http://wordpress.org/support/view/theme-reviews/cleanead?filter=5' ),
		'rateLabel'	=> esc_html__( 'Rate this theme', 'cleanead' ),
		)
	);

}
add_action( 'customize_controls_enqueue_scripts', 'cleanead_customize_controls_js' );


/**
 * Embed CSS styles for the theme options in the Customizer
 *
 */
function cleanead_customize_preview_css() {
	wp_enqueue_style( 'cleanead-customizer-css', get_template_directory_uri() . 'inc/customizer/css/customizer.css', array(), '20160530' );
}
add_action( 'customize_controls_print_styles', 'cleanead_customize_preview_css' );

/**
 * Returns theme options
 *
 * Uses sane defaults in case the user has not configured any theme options yet.
 */
function cleanead_theme_options() {
	// Merge Theme Options Array from Database with Default Options Array
	$theme_options = wp_parse_args( 
		
		// Get saved theme options from WP database
		get_option( 'cleanead_theme_options', array() ), 
		
		// Merge with Default Options if setting was not saved yet
		cleanead_default_options() 
		
	);

	// Return theme options
	return $theme_options;
}

/**
 * Returns the default settings of the theme
 *
 * @return array
 */
function cleanead_default_options() {

	$default_options = array(
		'site_title'						=> true,
		'layout' 							=> 'right-sidebar',
		'sticky_header'						=> false,
		'post_layout_archives'				=> 'left',
		'post_layout_single' 				=> 'header',
		'post_content' 						=> 'excerpt',
		'excerpt_length' 					=> 30,
		'excerpt_more' 						=> ' [...]',
		'paging'							=> 'paging-numberal',
		'related_posts'						=> 'cat',
	);
	
	return $default_options;
}