<?php
/**
 * Moral Theme Customizer
 *
 * @package Moral
 */

/**
 * Get all the default values of the theme mods.
 */
function moral_magazine_get_default_mods() {
	$moral_magazine_default_mods = array(
		// Footer copyright
		'moral_magazine_copyright_txt' => esc_html__( 'Copyright &copy; [the-year] [site-link]  |  ', 'moral-magazine' ),
		'moral_magazine_power_by_txt'	=> sprintf( esc_html__( 'Theme: %1$s by %2$s.', 'moral-magazine' ), 'smooth blog pro', '<a href="' . esc_url( 'http://moralthemes.com/' ) . '">Moral Themes</a>' ),
	);

	return apply_filters( 'moral_magazine_default_mods', $moral_magazine_default_mods );
}

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

	// Custom Controller
	require get_parent_theme_file_path( '/inc/customizer/custom-controller.php' );

	$default = moral_magazine_get_default_mods();

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

	if ( isset( $wp_customize->selective_refresh ) ) {
		$wp_customize->selective_refresh->add_partial( 'blogname', array(
			'selector'        => '.site-title a',
			'render_callback' => 'moral_magazine_customize_partial_blogname',
		) );
		$wp_customize->selective_refresh->add_partial( 'blogdescription', array(
			'selector'        => '.site-description',
			'render_callback' => 'moral_magazine_customize_partial_blogdescription',
		) );
	}

	//header text
	$wp_customize->add_setting(
	'moral_magazine_header_text',
	array(
		'sanitize_callback' => 'sanitize_text_field',
		'default' => esc_html__( 'View Full Stories', 'moral-magazine' ),
		'transport'	=> 'postMessage',
	)
	);

	$wp_customize->add_control(
		'moral_magazine_header_text',
		array(
			'section'		=> 'header_media',
			'label'			=> esc_html__( 'Header Text:', 'moral-magazine' ),
		)
	);

	$wp_customize->selective_refresh->add_partial( 
		'moral_magazine_header_text', 
		array(
	        'selector'            => '#about-us .read-more a',
			'render_callback'     => 'moral_magazine_about_partial_btn_title',
		) 
	);
	
	$wp_customize->remove_section( 'colors' );

	// Header text display setting
	$wp_customize->add_setting(	
		'moral_magazine_header_text_display',
		array(
			'sanitize_callback' => 'moral_magazine_sanitize_checkbox',
			'default' => true,
			'transport'	=> 'postMessage',
		)
	);

	$wp_customize->add_control(
		'moral_magazine_header_text_display',
		array(
			'section'		=> 'title_tagline',
			'type'			=> 'checkbox',
			'label'			=> esc_html__( 'Display Site Title and Tagline', 'moral-magazine' ),
		)
	);


	// Your latest posts title setting
	$wp_customize->add_setting(	
		'moral_magazine_your_latest_posts_title',
		array(
			'sanitize_callback' => 'sanitize_text_field',
			'default' => esc_html__( 'Blogs', 'moral-magazine' ),
			'transport'	=> 'postMessage',
		)
	);

	$wp_customize->add_control(
		'moral_magazine_your_latest_posts_title',
		array(
			'section'		=> 'static_front_page',
			'label'			=> esc_html__( 'Title:', 'moral-magazine' ),
			'active_callback' => 'moral_magazine_is_latest_posts'
		)
	);

	$wp_customize->selective_refresh->add_partial( 
		'moral_magazine_your_latest_posts_title', 
		array(
	        'selector'            => '.home.blog #page-header .page-title',
			'render_callback'     => 'moral_magazine_your_latest_posts_partial_title',
    	) 
    );

	/**
	 * 
	 * Front Section
	 * 
	 */ 

	// Home sections panel
	$wp_customize->add_panel(
		'moral_magazine_home_panel',
		array(
			'title' => esc_html__( 'Homepage Options', 'moral-magazine' ),
			'priority' => 105
		)
	);

	//home page
    require get_parent_theme_file_path( '/inc/customizer/sortable.php' );

    require get_parent_theme_file_path( '/inc/customizer/top-bar.php' );

    $sections = array( 'breaking-news', 'hero-banner', 'featured-news', 'editorial', 'travel-news', 'business-news', 'have-missed' );

    foreach ( $sections as $section ) {
    	require get_parent_theme_file_path( '/inc/customizer/'.$section.'.php' );

    	$section = str_replace ( '-', '_', $section );
    	$wp_customize->add_setting(
		'moral_magazine_'.$section.'_top_padding',
			array(
				'sanitize_callback' => 'moral_magazine_sanitize_number_range',
			)
		);

		$wp_customize->add_control(
			'moral_magazine_'.$section.'_top_padding',
			array(
				'section'		=> 'moral_magazine_'.$section,
				'label'			=> esc_html__( 'Top Padding', 'moral-magazine' ),
				'type'			=> 'number',
			)
		);

		$wp_customize->add_setting(
		'moral_magazine_'.$section.'_bottom_padding',
			array(
				'sanitize_callback' => 'moral_magazine_sanitize_number_range',
			)
		);

		$wp_customize->add_control(
			'moral_magazine_'.$section.'_bottom_padding',
			array(
				'section'		=> 'moral_magazine_'.$section,
				'label'			=> esc_html__( 'Bottom Padding', 'moral-magazine' ),
				'type'			=> 'number',
			)
		);
    }


	// Theme Options
	require get_parent_theme_file_path( '/inc/customizer/theme-option.php' );
}
add_action( 'customize_register', 'moral_magazine_customize_register' );


// Sanitize Callback
require get_parent_theme_file_path( '/inc/customizer/sanitize-callback.php' );



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

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

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

/**
 * Binds JS handlers for Customizer controls.
 */
function moral_magazine_customize_control_js() {


	wp_enqueue_style( 'moral-magazine-customize-style', get_theme_file_uri( '/assets/css/customize-controls.css' ), array(), '20151215' );

	wp_enqueue_script( 'moral-magazine-customize-control', get_theme_file_uri( '/assets/js/customize-control.js' ), array( 'jquery', 'customize-controls' ), '20151215', true );
	$localized_data = array( 
		'refresh_msg' => esc_html__( 'Refresh the page after Save and Publish.', 'moral-magazine' ),
		'reset_msg' => esc_html__( 'Warning!!! This will reset all the settings. Refresh the page after Save and Publish to reset all.', 'moral-magazine' ),
	);

	wp_localize_script( 'moral-magazine-customize-control', 'localized_data', $localized_data );
}
add_action( 'customize_controls_enqueue_scripts', 'moral_magazine_customize_control_js' );
