<?php
/**
 * PixiGo Theme Customizer
 * phpcs:disable Squiz.Commenting.FunctionComment.Missing
 *
 * @package pixigo
 */

/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function pixigo_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' => 'pixigo_customize_partial_blogname',
			)
		);
		$wp_customize->selective_refresh->add_partial(
			'blogdescription', array(
				'selector'        => '.site-description',
				'render_callback' => 'pixigo_customize_partial_blogdescription',
			)
		);
	}

	/**
	 * Theme options.
	 */
	$wp_customize->add_panel(
		'pixigo_theme_options', array(
			'capability' => 'edit_theme_options',
			'title'      => __( 'PixiGo Customization', 'pixigo' ),
			'priority'   => 10,                                      // Before Additional CSS.
		)
	);

	/**
	* Global Setting Section
	*/
	$wp_customize->add_section(
		'pixigo_global', array(
			'title'       => __( 'Global', 'pixigo' ),
			'description' => __( 'Settings that apply to the whole theme', 'pixigo' ),
			'panel'       => 'pixigo_theme_options',
		)
	);

	// Primary theme color.
	$wp_customize->add_setting(
		'pixigo_theme_color', array(
			'default'           => '#31B237',
			'transport'         => 'postMessage',
			'sanitize_callback' => 'pixigo_sanitize_hexcolor',
		)
	);
	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize, 'pixigo_theme_color', array(
				'label'       => __( 'Primary Color', 'pixigo' ),
				'description' => __( 'Changes the accent color throughout the theme', 'pixigo' ),
				'section'     => 'pixigo_global',
			)
		)
	);

	// Post date type.
	$wp_customize->add_setting(
		'pixigo_post_date_type', array(
			'default'           => 'published',
			'sanitize_callback' => 'pixigo_sanitize_radio',
		)
	);
	$wp_customize->add_control(
		'pixigo_post_date_type', array(
			'type'    => 'radio',
			'section' => 'pixigo_global',
			'label'   => __( 'Post Date Type', 'pixigo' ),
			'choices' => array(
				'published' => __( 'Published', 'pixigo' ),
				'modified'  => __( 'Last Modified', 'pixigo' ),
			),
		)
	);

	// Last modified string.
	$wp_customize->add_setting(
		'pixigo_last_modified_text', array(
			'default'           => __( '', 'pixigo' ),
			'sanitize_callback' => 'pixigo_sanitize_textarea',
		)
	);
	$wp_customize->add_control(
		'pixigo_last_modified_text', array(
			'type'        => 'text',
			'section'     => 'pixigo_global',
			'label'       => __( 'Last Modified String', 'pixigo' ),
			'description' => __( 'Prepends text to the modified timestamp', 'pixigo' ),
		)
	);

	/**
	* Archive Setting Section
	*/
	$wp_customize->add_section(
		'pixigo_archive', array(
			'title'       => __( 'Archive', 'pixigo' ),
			'description' => __( 'Settings that apply to archive pages (including homepage)', 'pixigo' ),
			'panel'       => 'pixigo_theme_options',
		)
	);

	// Hide template components on archives.
	$archive_meta_visibility = pixigo_archive_customization_options();
	foreach ( $archive_meta_visibility as $option_name => $option ) {
		$setting_id = 'archive_hide_' . $option_name;
		$wp_customize->add_setting(
			$setting_id, array(
				'default'           => 0,
				'sanitize_callback' => 'pixigo_sanitize_checkbox',
			)
		);
		$wp_customize->add_control(
			$setting_id, array(
				'label'   => "Hide $option",
				'section' => 'pixigo_archive',
				'type'    => 'checkbox',
			)
		);
	}

	/**
	 * Post Setting Section
	 */
	$wp_customize->add_section(
		'pixigo_post', array(
			'title'       => __( 'Posts', 'pixigo' ),
			'description' => __( 'Settings that apply to single posts. More options available in Individual post edit screen.', 'pixigo' ),
			'panel'       => 'pixigo_theme_options',
		)
	);

	// Hide template components on posts.
	$post_meta_visibility = pixigo_post_customization_options();
	foreach ( $post_meta_visibility as $option_name => $option ) {
		$setting_id = 'post_hide_' . $option_name;
		$wp_customize->add_setting(
			$setting_id, array(
				'default'           => 0,
				'sanitize_callback' => 'pixigo_sanitize_checkbox',
			)
		);
		$wp_customize->add_control(
			$setting_id, array(
				'label'   => "Hide $option",
				'section' => 'pixigo_post',
				'type'    => 'checkbox',
			)
		);
	}

	$wp_customize->add_setting(
		'pixigo_related_post_text', array(
			'default'           => __( 'Related Posts', 'pixigo' ),
			'sanitize_callback' => 'pixigo_sanitize_textarea',
		)
	);
	$wp_customize->add_control(
		'pixigo_related_post_text', array(
			'label'       => __( 'Related Posts Heading String', 'pixigo' ),
			'description' => __( 'If you wish to replace "Related Posts" with something else..', 'pixigo' ),
			'section'     => 'pixigo_post',
			'type'        => 'text',
		)
	);

	$wp_customize->add_setting(
		'pixigo_related_post_count', array(
			'default'           => 2,
			'sanitize_callback' => 'pixigo_sanitize_number',
		)
	);
	$wp_customize->add_control(
		'pixigo_related_post_count', array(
			'label'       => __( 'Number of Related Posts to show', 'pixigo' ),
			'description' => __( 'Also depends on the number of posts present in the category.', 'pixigo' ),
			'section'     => 'pixigo_post',
			'type'        => 'number',
		)
	);

	/**
	* Page Setting Section
	*/
	$wp_customize->add_section(
		'pixigo_page', array(
			'title'       => __( 'Pages', 'pixigo' ),
			'description' => __( 'Settings that apply to single pages', 'pixigo' ),
			'panel'       => 'pixigo_theme_options',
		)
	);

	// Show template components on a page.
	$page_options = pixigo_page_customization_options();

	foreach ( $page_options as $option_name => $page_option ) {
		$setting_id = 'page_hide_' . $option_name;
		$wp_customize->add_setting(
			$setting_id, array(
				'default'           => 1,
				'sanitize_callback' => 'pixigo_sanitize_checkbox',
			)
		);
		$wp_customize->add_control(
			$setting_id, array(
				'label'   => "Hide $page_option",
				'section' => 'pixigo_page',
				'type'    => 'checkbox',
			)
		);
	}

	/**
	* Footer Section
	*/
	$wp_customize->add_section(
		'pixigo_footer', array(
			'title' => __( 'Footer', 'pixigo' ),
			'panel' => 'pixigo_theme_options',
		)
	);

	$wp_customize->add_setting(
		'footer_copyright', array(
			'default'           => '',
			'transport'         => 'postMessage',
			'sanitize_callback' => 'pixigo_sanitize_strip_slashes',
		)
	);
	$wp_customize->add_control(
		'footer_copyright', array(
			'label'       => __( 'Copyright Text', 'pixigo' ),
			'description' => __( 'HTML allowed', 'pixigo' ),
			'section'     => 'pixigo_footer',
			'type'        => 'textarea',
		)
	);

	/**
	* Performance Optimization Setting Section
	*/
	$wp_customize->add_section(
		'pixigo_perf_optimization', array(
			'title'       => __( 'Performance Optimization', 'pixigo' ),
			'description' => __( 'Settings to change in-built theme performance optimization', 'pixigo' ),
			'panel'       => 'pixigo_theme_options',
		)
	);

	// Image Lazyload.
	if ( function_exists( 'pixigo_lazyload_images' ) ) {
		$wp_customize->add_setting(
			'lazy_load_media', array(
				'default'           => 'lazyload',
				'sanitize_callback' => 'pixigo_sanitize_lazy_load_media',
				'transport'         => 'postMessage',
			)
		);

		$wp_customize->add_control(
			'lazy_load_media', array(
				'label'           => __( 'Lazy-load images', 'pixigo' ),
				'section'         => 'pixigo_perf_optimization',
				'type'            => 'radio',
				'description'     => __( 'Lazy-loading images means images are loaded only when they are in view. Improves performance, but can result in content jumping around on slower connections.', 'pixigo' ),
				'choices'         => array(
					'lazyload'    => __( 'Lazy-load on (default)', 'pixigo' ),
					'no-lazyload' => __( 'Lazy-load off', 'pixigo' ),
				),
			)
		);
	}

	$wp_customize->add_setting(
		'pixigo_prefetch', array(
			'default'           => 'yes',
			'sanitize_callback' => 'pixigo_sanitize_radio',
			'transport'         => 'postMessage',
		)
	);

	$wp_customize->add_control(
		'pixigo_prefetch', array(
			'label'           => __( 'Pre-fetch links', 'pixigo' ),
			'section'         => 'pixigo_perf_optimization',
			'type'            => 'radio',
			'description'     => __( 'Pre-fetching links in viewport for instant navigation between pages.', 'pixigo' ),
			'choices'         => array(
				'yes'    => __( 'Yes', 'pixigo' ),
				'no' => __( 'No', 'pixigo' ),
			),
		)
	);
}
add_action( 'customize_register', 'pixigo_customize_register' );

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

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

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

/**
 * Sanitize the lazy-load media options.
 *
 * @param string $input Lazy-load setting.
 */
function pixigo_sanitize_lazy_load_media( $input ) {
	$valid = array(
		'lazyload'    => __( 'Lazy-load images', 'pixigo' ),
		'no-lazyload' => __( 'Load images immediately', 'pixigo' ),
	);

	if ( array_key_exists( $input, $valid ) ) {
		return $input;
	}

	return '';
}

/**
 * Sanitize checkbox
 *
 * @param string $input checkbox setting.
 */
function pixigo_sanitize_checkbox( $input ) {
	return ( '' != $input ? true : false ); // phpcs:ignore
}

function pixigo_sanitize_radio( $input, $setting ) {
	// input must be a slug: lowercase alphanumeric characters, dashes and underscores are allowed only.
	$input = sanitize_key( $input );

	// get the list of possible radio box options.
	$choices = $setting->manager->get_control( $setting->id )->choices;

	// return input if valid or return default option.
	return ( array_key_exists( $input, $choices ) ? $input : $setting->default );
}

function pixigo_sanitize_hexcolor( $color ) {
	if ( sanitize_hex_color_no_hash( $color ) === $unhashed ) {
		return '#' . $unhashed;
	}

	return $color;
}

function pixigo_sanitize_number( $input ) {
	if ( isset( $input ) && is_numeric( $input ) ) {
		return $input;
	}
}

function pixigo_sanitize_strip_slashes( $input ) {
	return wp_kses_stripslashes( $input );
}

function pixigo_sanitize_textarea( $input ) {
	return sanitize_text_field( $input );
}
