From 28e7acf1686ec4e31a4ced16c759560832eb7316 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 1 Apr 2019 16:17:11 +0000 Subject: [PATCH] Customize: Only add `custom-background` to `body_class()` if the current theme supports custom background. Props wido, swissspidy, ocean90, Mte90. Fixes #38168. git-svn-id: https://develop.svn.wordpress.org/trunk@45088 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post-template.php | 3 ++- src/wp-includes/theme.php | 4 ++- tests/phpunit/tests/post/getBodyClass.php | 32 +++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/post-template.php b/src/wp-includes/post-template.php index b817edec4f..3765e9873c 100644 --- a/src/wp-includes/post-template.php +++ b/src/wp-includes/post-template.php @@ -773,7 +773,8 @@ function get_body_class( $class = '' ) { $classes[] = 'no-customize-support'; } - if ( get_background_color() !== get_theme_support( 'custom-background', 'default-color' ) || get_background_image() ) { + if ( current_theme_supports( 'custom-background' ) + && ( get_background_color() !== get_theme_support( 'custom-background', 'default-color' ) || get_background_image() ) ) { $classes[] = 'custom-background'; } diff --git a/src/wp-includes/theme.php b/src/wp-includes/theme.php index 9a72126488..23907bc549 100644 --- a/src/wp-includes/theme.php +++ b/src/wp-includes/theme.php @@ -2730,7 +2730,9 @@ function _remove_theme_support( $feature ) { break; } $support = get_theme_support( 'custom-background' ); - remove_action( 'wp_head', $support[0]['wp-head-callback'] ); + if ( isset( $support[0]['wp-head-callback'] ) ) { + remove_action( 'wp_head', $support[0]['wp-head-callback'] ); + } remove_action( 'admin_menu', array( $GLOBALS['custom_background'], 'init' ) ); unset( $GLOBALS['custom_background'] ); break; diff --git a/tests/phpunit/tests/post/getBodyClass.php b/tests/phpunit/tests/post/getBodyClass.php index 9c8ce670e6..c631199aa9 100644 --- a/tests/phpunit/tests/post/getBodyClass.php +++ b/tests/phpunit/tests/post/getBodyClass.php @@ -203,6 +203,38 @@ class Tests_Post_GetBodyClass extends WP_UnitTestCase { $this->assertContains( 'attachment-jpeg', $class ); } + /** + * @ticket 38168 + */ + public function test_custom_background_class_is_added_when_theme_supports_it() { + add_theme_support( 'custom-background', array( 'default-color', '#ffffff' ) ); + set_theme_mod( 'background_color', '#000000' ); + + $class = get_body_class(); + $current_theme_supports_custom_background = current_theme_supports( 'custom-background' ); + + remove_theme_mod( 'background_color' ); + remove_theme_support( 'custom-background' ); + + $this->assertTrue( $current_theme_supports_custom_background ); + $this->assertContains( 'custom-background', $class ); + } + + /** + * @ticket 38168 + */ + public function test_custom_background_class_is_not_added_when_theme_support_is_missing() { + set_theme_mod( 'background_color', '#000000' ); + + $class = get_body_class(); + $current_theme_supports_custom_background = current_theme_supports( 'custom-background' ); + + remove_theme_mod( 'background_color' ); + + $this->assertFalse( $current_theme_supports_custom_background ); + $this->assertNotContains( 'custom-background', $class ); + } + /** * @ticket 44005 * @group privacy