diff --git a/src/wp-includes/class-wp-customize-manager.php b/src/wp-includes/class-wp-customize-manager.php index c2198acf20f66..bddabfab01a40 100644 --- a/src/wp-includes/class-wp-customize-manager.php +++ b/src/wp-includes/class-wp-customize-manager.php @@ -5410,7 +5410,7 @@ public function register_controls() { $this, 'header_image', array( - 'default' => sprintf( get_theme_support( 'custom-header', 'default-image' ), get_template_directory_uri(), get_stylesheet_directory_uri() ), + 'default' => current_theme_supports( 'custom-header', 'random-default' ) ? 'random-default-image' : sprintf( get_theme_support( 'custom-header', 'default-image' ), get_template_directory_uri(), get_stylesheet_directory_uri() ), 'theme_supports' => 'custom-header', ) ) diff --git a/tests/phpunit/tests/customize/manager.php b/tests/phpunit/tests/customize/manager.php index bedf0b39b3e95..9c92a199288ca 100644 --- a/tests/phpunit/tests/customize/manager.php +++ b/tests/phpunit/tests/customize/manager.php @@ -3683,6 +3683,49 @@ public function test_sanitize_external_header_video_trim() { $this->assertSame( $video_url, $sanitized ); } } + + /** + * @ticket 46128 + * + * @dataProvider data_header_image_setting_default + * + * @param bool $random_default Whether to randomize default headers. + * @param string $expected_default Expected setting default. + */ + public function test_header_image_setting_default( $random_default, $expected_default ) { + global $_wp_theme_features; + + $custom_header_support = isset( $_wp_theme_features['custom-header'] ) ? $_wp_theme_features['custom-header'] : null; + + try { + $_wp_theme_features['custom-header'][0] = array( + 'default-image' => 'https://example.org/header.jpg', + 'random-default' => $random_default, + ); + + $this->manager->register_controls(); + + $this->assertSame( $expected_default, $this->manager->get_setting( 'header_image' )->default ); + } finally { + if ( null === $custom_header_support ) { + unset( $_wp_theme_features['custom-header'] ); + } else { + $_wp_theme_features['custom-header'] = $custom_header_support; + } + } + } + + /** + * Data provider for test_header_image_setting_default(). + * + * @return array[] Test parameters. + */ + public function data_header_image_setting_default() { + return array( + 'random default headers' => array( true, 'random-default-image' ), + 'fixed default header' => array( false, 'https://example.org/header.jpg' ), + ); + } } require_once ABSPATH . WPINC . '/class-wp-customize-setting.php';