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

/**
 * Add postMessage support and custom options for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function cyywordpress_customize_register( WP_Customize_Manager $wp_customize ): void {
	$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' => 'cyywordpress_customize_partial_blogname',
			)
		);
		$wp_customize->selective_refresh->add_partial(
			'blogdescription',
			array(
				'selector'        => '.site-description',
				'render_callback' => 'cyywordpress_customize_partial_blogdescription',
			)
		);
	}

	// -----------------------------------------------------------------------
	// Section: Front Page Banner
	// -----------------------------------------------------------------------
	$wp_customize->add_section(
		'cyywordpress_frontpage',
		array(
			'title'    => esc_html__( '首页横幅', 'cyywordpress' ),
			'priority' => 130,
		)
	);

	$wp_customize->add_setting(
		'cyywordpress_welcome_text',
		array(
			// 存原始字符串，输出时再转义。
			'default'           => __( '欢迎来到这！Hello everyone.', 'cyywordpress' ),
			'sanitize_callback' => 'sanitize_text_field',
			'transport'         => 'postMessage',
		)
	);

	$wp_customize->add_control(
		'cyywordpress_welcome_text',
		array(
			'label'   => esc_html__( '欢迎横幅文字', 'cyywordpress' ),
			'section' => 'cyywordpress_frontpage',
			'type'    => 'text',
		)
	);

	$wp_customize->add_setting(
		'cyywordpress_show_welcome_banner',
		array(
			'default'           => true,
			'sanitize_callback' => 'rest_sanitize_boolean',
			'transport'         => 'refresh',
		)
	);

	$wp_customize->add_control(
		'cyywordpress_show_welcome_banner',
		array(
			'label'   => esc_html__( '显示欢迎横幅', 'cyywordpress' ),
			'section' => 'cyywordpress_frontpage',
			'type'    => 'checkbox',
		)
	);
}
add_action( 'customize_register', 'cyywordpress_customize_register' );

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

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

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

