<?php
/**
 * Functions required by the Customizer
 *
 * @package WordPress
 * @subpackage amaThambo
 * @since 1.0
 * @version 1.0
 */

if ( ! function_exists( 'amathambo_customize_register' ) ) {
	/**
	 * Customizer register.
	 *
	 * @param array $wp_customize Main class that makes it all work..
	 */
	function amathambo_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';

		$wp_customize->selective_refresh->add_partial( 'amathambo_prepend_custom_logo_setting', array(
			'render_callback' => 'amathambo_title_background_image();',
		) );

		/**
		 * Add section for use by theme miscellaneous settings
		 */
		$wp_customize->add_section(
			'amathambo_options_section', array(
				'title'			=> __( 'amaThambo Options', 'amathambo' ),
			)
		);
		$wp_customize->add_setting( 'amathambo_pagination_navigation_setting',
			array(
			'default' => 'pagination',
			'transport' => 'refresh',
			'sanitize_callback' => 'amathambo_radio_text_sanitization',
			)
		);
		$wp_customize->add_control(
			new Amathambo_Text_Radio_Button_Control(
				$wp_customize,
				'amathambo_pagination_navigation', array(
					'settings'		=> 'amathambo_pagination_navigation_setting',
					'section'		=> 'amathambo_options_section',
					'type'			=> 'text_radio_button',
					'label'			=> __( 'Posts Navigation/Pagination', 'amathambo' ),
					'choices' 		=> array(
						'pagination' 	=> __( 'Pagination', 'amathambo' ),
						'navigation' 	=> __( 'Navigation', 'amathambo' ),
					),
				)
			)
		);
		$wp_customize->add_setting( 'amathambo_post_style_setting',
			array(
			'default' => 'excerpt',
			'transport' => 'refresh',
			'sanitize_callback' => 'amathambo_radio_text_sanitization',
			)
		);
		$wp_customize->add_control(
			new Amathambo_Text_Radio_Button_Control(
				$wp_customize,
				'amathambo_post_style', array(
					'settings'		=> 'amathambo_post_style_setting',
					'section'		=> 'amathambo_options_section',
					'type'			=> 'text_radio_button',
					'label'			=> __( 'Post Style', 'amathambo' ),
					'choices' 		=> array(
						'excerpt' 	=> __( 'Excerpt', 'amathambo' ),
						'content' 	=> __( 'Content', 'amathambo' ),
					),
				)
			)
		);
		$wp_customize->add_setting( 'amathambo_author_name_setting',
			array(
			'default' => 'yes',
			'transport' => 'refresh',
			'sanitize_callback' => 'amathambo_radio_text_sanitization',
			)
		);
		$wp_customize->add_control(
			new Amathambo_Text_Radio_Button_Control(
				$wp_customize,
				'amathambo_author_name', array(
					'settings'		=> 'amathambo_author_name_setting',
					'section'		=> 'amathambo_options_section',
					'type'			=> 'text_radio_button',
					'label'			=> __( 'Display Author Name', 'amathambo' ),
					'choices' 		=> array(
						'yes' 		=> __( 'Yes', 'amathambo' ),
						'no' 		=> __( 'No', 'amathambo' ),
					),
				)
			)
		);
		$wp_customize->add_setting( 'amathambo_featured_images_setting',
			array(
			'default' => 'yes',
			'transport' => 'refresh',
			'sanitize_callback' => 'amathambo_radio_text_sanitization',
			)
		);
		$wp_customize->add_control(
			new Amathambo_Text_Radio_Button_Control(
				$wp_customize,
				'amathambo_featured_images', array(
					'settings'		=> 'amathambo_featured_images_setting',
					'section'		=> 'amathambo_options_section',
					'type'			=> 'text_radio_button',
					'label'			=> __( 'Display Featured Images', 'amathambo' ),
					'choices' 		=> array(
						'yes' 		=> __( 'Yes', 'amathambo' ),
						'no' 		=> __( 'No', 'amathambo' ),
					),
				)
			)
		);

	}
	add_action( 'customize_register', 'amathambo_customize_register' );
} // End if().

/**
 * Sanitize Callbacks
 */
if ( ! function_exists( 'amathambo_text_sanitization' ) ) {
	/**
	 * Radio Text sanitization.
	 *
	 * @param  string $input Text to be sanitized.
	 * @return string $input Sanitized text.
	 */
	function amathambo_radio_text_sanitization( $input ) {

		if ( strpos( $input, ',' ) !== false ) {
			$input = explode( ',', $input );
		}
		if ( is_array( $input ) ) {
			foreach ( $input as $key => $value ) {
				$input[ $key ] = sanitize_text_field( $value );
			}
			$input = implode( ',', $input );
		} else {
			$input = sanitize_text_field( $input );
		}

		return $input;

	}
}

/**
 * Binds JS handler to make Theme Customizer preview reload changes asynchronously.
 */
function amathambo_customizer_preview() {
	wp_enqueue_script( 'amathambo-customizer', get_template_directory_uri() . '/assets/js/customizer.js', array( 'jquery', 'customize-preview' ) );
}
add_action( 'customize_preview_init', 'amathambo_customizer_preview' );

/**
 * Custom Control Styling.
 */
function amathambo_customizer_custom_control_css() {
?>

	<style>

		.text_radio_button_control:after {
			content: " ";
			display: block;
			clear: both;
		}
		
		.text_radio_button_control .radio-buttons {
			display: inline-block;
			border: 1px solid #f9f9fe;
		}
		
		.text_radio_button_control .radio-button-label {
			cursor: pointer;
			float: left;
		}
		
		.text_radio_button_control .radio-button-label > input {
			display: none;
		}
		
		.text_radio_button_control .radio-button-label span {
			cursor: pointer;
			font-weight: 500;
			border: 2px solid #f9f9fe;
			margin: 0;
			background-color: #eee;
			padding: 5px 15px;
			display: inline-block;
		}
		
		.text_radio_button_control .radio-button-label span:hover {
			background-color: rgba(255, 255, 255, .2);
			color: #2885bb;
		}
		
		.text_radio_button_control .radio-button-label > input:checked + span {
			background-color: #2084bd;
			color: #fff;
		}
		
		.text_radio_button_control .radio-button-label > input:checked + span:hover {
			color: #fff;
		}

	</style>
<?php }
add_action( 'customize_controls_print_styles', 'amathambo_customizer_custom_control_css' );
