diff --git a/src/wp-includes/theme.php b/src/wp-includes/theme.php index 47194dfcc7..24fa1116cf 100644 --- a/src/wp-includes/theme.php +++ b/src/wp-includes/theme.php @@ -1653,18 +1653,17 @@ function wp_get_custom_css_post( $stylesheet = '' ) { if ( $post_id > 0 && get_post( $post_id ) ) { $post = get_post( $post_id ); - } else { + } + + // `-1` indicates no post exists; no query necessary. + if ( ! $post && -1 !== $post_id ) { $query = new WP_Query( $custom_css_query_vars ); $post = $query->post; /* - * Cache the lookup. See WP_Customize_Custom_CSS_Setting::update(). + * Cache the lookup. See wp_update_custom_css_post(). * @todo This should get cleared if a custom_css post is added/removed. */ - if ( $post ) { - set_theme_mod( 'custom_css_post_id', $post->ID ); - } elseif ( -1 !== $post_id ) { - set_theme_mod( 'custom_css_post_id', -1 ); - } + set_theme_mod( 'custom_css_post_id', $post ? $post->ID : -1 ); } } else { $query = new WP_Query( $custom_css_query_vars ); @@ -1788,9 +1787,15 @@ function wp_update_custom_css_post( $css, $args = array() ) { } else { $r = wp_insert_post( wp_slash( $post_data ), true ); - // Trigger creation of a revision. This should be removed once #30854 is resolved. - if ( ! is_wp_error( $r ) && 0 === count( wp_get_post_revisions( $r ) ) ) { - wp_save_post_revision( $r ); + if ( ! is_wp_error( $r ) ) { + if ( get_stylesheet() === $args['stylesheet'] ) { + set_theme_mod( 'custom_css_post_id', $r ); + } + + // Trigger creation of a revision. This should be removed once #30854 is resolved. + if ( 0 === count( wp_get_post_revisions( $r ) ) ) { + wp_save_post_revision( $r ); + } } } diff --git a/tests/phpunit/tests/customize/custom-css-setting.php b/tests/phpunit/tests/customize/custom-css-setting.php index 7aff1b43b5..7178625230 100644 --- a/tests/phpunit/tests/customize/custom-css-setting.php +++ b/tests/phpunit/tests/customize/custom-css-setting.php @@ -135,6 +135,8 @@ class Test_WP_Customize_Custom_CSS_Setting extends WP_UnitTestCase { ) ); $twentyten_setting = new WP_Customize_Custom_CSS_Setting( $this->wp_customize, 'custom_css[twentyten]' ); + remove_theme_mod( 'custom_css_post_id' ); + $this->assertEquals( $post_id, wp_get_custom_css_post()->ID ); $this->assertEquals( $post_id, wp_get_custom_css_post( $this->setting->stylesheet )->ID ); $this->assertEquals( $twentyten_post_id, wp_get_custom_css_post( 'twentyten' )->ID ); @@ -220,6 +222,29 @@ class Test_WP_Customize_Custom_CSS_Setting extends WP_UnitTestCase { $this->assertSame( $inserted_css, $revisions[1]->post_content ); } + /** + * Test that wp_get_custom_css_post() doesn't query for a post after caching a failed lookup. + * + * @ticket 39259 + */ + function test_get_custom_css_post_queries_after_failed_lookup() { + set_theme_mod( 'custom_css_post_id', -1 ); + $queries_before = get_num_queries(); + wp_get_custom_css_post(); + $this->assertSame( get_num_queries(), $queries_before ); + } + + /** + * Test that wp_update_custom_css_post() updates the 'custom_css_post_id' theme mod. + * + * @ticket 39259 + */ + function test_update_custom_css_updates_theme_mod() { + set_theme_mod( 'custom_css_post_id', -1 ); + $post = wp_update_custom_css_post( 'body { background: blue; }' ); + $this->assertSame( $post->ID, get_theme_mod( 'custom_css_post_id' ) ); + } + /** * Test crud methods on WP_Customize_Custom_CSS_Setting. * @@ -237,6 +262,7 @@ class Test_WP_Customize_Custom_CSS_Setting extends WP_UnitTestCase { 'post_status' => 'publish', 'post_type' => 'custom_css', ) ); + remove_theme_mod( 'custom_css_post_id' ); $this->assertEquals( '/*custom*//*filtered*/', $this->setting->value() ); $this->wp_customize->set_post_value( $this->setting->id, '/*overridden*/' );