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

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

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

	$wp_customize->add_setting(
		'main_home_content_bg_color',
		array(
			'type' => 'theme_mod',
			'default' => '#f8f8f8',
			'sanitize_callback' => 'sanitize_hex_color',
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'main_home_content_bg_color',
			array(
				'label' => esc_html__( 'Main Content Background Color', 'boyo' ),
				'section' => 'colors',
				'priority' => 100,
			)
		)
	);

	$wp_customize->add_setting(
		'aside_format_bg_color',
		array(
			'type' => 'theme_mod',
			'default' => '#c1e4de',
			'sanitize_callback' => 'sanitize_hex_color',
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'aside_format_bg_color',
			array(
				'label' => esc_html__( 'Aside Post Format Background Color', 'boyo' ),
				'section' => 'colors',
				'priority' => 110,
			)
		)
	);

	$wp_customize->add_setting(
		'quote_format_bg_color',
		array(
			'type' => 'theme_mod',
			'default' => '#fef0f0',
			'sanitize_callback' => 'sanitize_hex_color',
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'quote_format_bg_color',
			array(
				'label' => esc_html__( 'Quote Post Format Background Color', 'boyo' ),
				'section' => 'colors',
				'priority' => 120,
			)
		)
	);

	$wp_customize->add_setting(
		'link_format_bg_color',
		array(
			'type' => 'theme_mod',
			'default' => '#edf7fa',
			'sanitize_callback' => 'sanitize_hex_color',
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'link_format_bg_color',
			array(
				'label' => esc_html__( 'Link Post Format Background Color', 'boyo' ),
				'section' => 'colors',
				'priority' => 130,
			)
		)
	);

	$wp_customize->add_setting(
		'menu_underline_color',
		array(
			'type' => 'theme_mod',
			'default' => '#ffd73e',
			'sanitize_callback' => 'sanitize_hex_color',
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'menu_underline_color',
			array(
				'label' => esc_html__( 'Active Menu Underline Color', 'boyo' ),
				'section' => 'colors',
				'priority' => 140,
			)
		)
	);

	// Section Blog & Archive Pages.
	$wp_customize->add_section(
		'blog_and_archive_pages',
		array(
			'title' => esc_html__( 'Blog & Archive Pages', 'boyo' ),
			'priority' => 1000,
			'description' => esc_html__( 'Blog & Archive Pages Settings', 'boyo' ),
		)
	);

	$wp_customize->add_setting(
		'blog_and_archive_pages_layout',
		array(
			'default' => '',
			'sanitize_callback' => 'boyo_sanitize_blog_and_archive_pages_layout',
		)
	);

	$wp_customize->add_control(
		'blog_and_archive_pages_layout',
		array(
			'label' => esc_html__( 'Blog & Archive Pages Layout', 'boyo' ),
			'section' => 'blog_and_archive_pages',
			'type' => 'select',
			'choices' => array(
				'' => esc_html__( 'One column', 'boyo' ),
				'three' => esc_html__( 'Three columns', 'boyo' ),
			),
		)
	);

	$wp_customize->add_setting(
		'show_excerpt',
		array(
			'default' => 1,
			'sanitize_callback' => 'wp_validate_boolean',
		)
	);

	$wp_customize->add_control(
		'show_excerpt',
		array(
			'label' => esc_html__( 'Show excerpt on blog archive pages', 'boyo' ),
			'description' => esc_html__( 'Excerpt is always displayed for Sticky Posts', 'boyo' ),
			'section' => 'blog_and_archive_pages',
			'type' => 'checkbox',
		)
	);

	$wp_customize->add_setting(
		'excerpt_length',
		array(
			'default' => 20,
			'sanitize_callback' => 'absint',
		)
	);

	$wp_customize->add_control(
		'excerpt_length',
		array(
			'label' => esc_html__( 'Length of Autogenerated Excerpt (number of words)', 'boyo' ),
			'section' => 'blog_and_archive_pages',
			'description' => esc_html__( 'It works only when excerpt field in editor is empty', 'boyo' ),
			'type' => 'number',
			'input_attrs' => array(
				'min' => 1,
				'max' => 100,
				'step' => 1,
			),
		)
	);

	// Section - "Advanced settings".
	$wp_customize->add_section(
		'advanced_settings',
		array(
			'title' => esc_html__( 'Advanced', 'boyo' ),
			'priority' => 1050,
			'description' => esc_html__( 'Advanced Settings', 'boyo' ),
		)
	);

	$wp_customize->add_setting(
		'load_google_fonts_from_google',
		array(
			'default' => 1,
			'sanitize_callback' => 'wp_validate_boolean',
		)
	);

	$wp_customize->add_control(
		'load_google_fonts_from_google',
		array(
			'label' => esc_html__( 'Load fonts from Google servers', 'boyo' ),
			'section' => 'advanced_settings',
			'type' => 'checkbox',
		)
	);

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

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

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

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

/**
 * Sanitize the blog_and_archive_pages_layout setting.
 *
 * @param string $value layout variation.
 * @return string
 */
function boyo_sanitize_blog_and_archive_pages_layout( $value ) {
	if ( in_array( $value, array( '', 'three' ), true ) ) {
		return $value;
	}
	return '';
}
