Make WordPress Core

Changeset 39694


Ignore:
Timestamp:
01/05/2017 08:14:56 AM (7 years ago)
Author:
dd32
Message:

Customize: Ensure theme_mod-cache of custom_css lookup of -1 short-circuits a WP_Query from being made.

Props dlh, westonruter.
See #35395.
Merges [39688] to the 4.7 branch.
Fixes #39259.

Location:
branches/4.7
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.7

  • branches/4.7/src/wp-includes/theme.php

    r39693 r39694  
    16541654        if ( $post_id > 0 && get_post( $post_id ) ) {
    16551655            $post = get_post( $post_id );
    1656         } else {
     1656        }
     1657
     1658        // `-1` indicates no post exists; no query necessary.
     1659        if ( ! $post && -1 !== $post_id ) {
    16571660            $query = new WP_Query( $custom_css_query_vars );
    16581661            $post = $query->post;
    16591662            /*
    1660              * Cache the lookup. See WP_Customize_Custom_CSS_Setting::update().
     1663             * Cache the lookup. See wp_update_custom_css_post().
    16611664             * @todo This should get cleared if a custom_css post is added/removed.
    16621665             */
    1663             if ( $post ) {
    1664                 set_theme_mod( 'custom_css_post_id', $post->ID );
    1665             } elseif ( -1 !== $post_id ) {
    1666                 set_theme_mod( 'custom_css_post_id', -1 );
    1667             }
     1666            set_theme_mod( 'custom_css_post_id', $post ? $post->ID : -1 );
    16681667        }
    16691668    } else {
     
    17891788        $r = wp_insert_post( wp_slash( $post_data ), true );
    17901789
    1791         // Trigger creation of a revision. This should be removed once #30854 is resolved.
    1792         if ( ! is_wp_error( $r ) && 0 === count( wp_get_post_revisions( $r ) ) ) {
    1793             wp_save_post_revision( $r );
     1790        if ( ! is_wp_error( $r ) ) {
     1791            if ( get_stylesheet() === $args['stylesheet'] ) {
     1792                set_theme_mod( 'custom_css_post_id', $r );
     1793            }
     1794
     1795            // Trigger creation of a revision. This should be removed once #30854 is resolved.
     1796            if ( 0 === count( wp_get_post_revisions( $r ) ) ) {
     1797                wp_save_post_revision( $r );
     1798            }
    17941799        }
    17951800    }
  • branches/4.7/tests/phpunit/tests/customize/custom-css-setting.php

    r39567 r39694  
    136136        $twentyten_setting = new WP_Customize_Custom_CSS_Setting( $this->wp_customize, 'custom_css[twentyten]' );
    137137
     138        remove_theme_mod( 'custom_css_post_id' );
     139
    138140        $this->assertEquals( $post_id, wp_get_custom_css_post()->ID );
    139141        $this->assertEquals( $post_id, wp_get_custom_css_post( $this->setting->stylesheet )->ID );
     
    222224
    223225    /**
     226     * Test that wp_get_custom_css_post() doesn't query for a post after caching a failed lookup.
     227     *
     228     * @ticket 39259
     229     */
     230    function test_get_custom_css_post_queries_after_failed_lookup() {
     231        set_theme_mod( 'custom_css_post_id', -1 );
     232        $queries_before = get_num_queries();
     233        wp_get_custom_css_post();
     234        $this->assertSame( get_num_queries(), $queries_before );
     235    }
     236
     237    /**
     238     * Test that wp_update_custom_css_post() updates the 'custom_css_post_id' theme mod.
     239     *
     240     * @ticket 39259
     241     */
     242    function test_update_custom_css_updates_theme_mod() {
     243        set_theme_mod( 'custom_css_post_id', -1 );
     244        $post = wp_update_custom_css_post( 'body { background: blue; }' );
     245        $this->assertSame( $post->ID, get_theme_mod( 'custom_css_post_id' ) );
     246    }
     247
     248    /**
    224249     * Test crud methods on WP_Customize_Custom_CSS_Setting.
    225250     *
     
    238263            'post_type' => 'custom_css',
    239264        ) );
     265        remove_theme_mod( 'custom_css_post_id' );
    240266        $this->assertEquals( '/*custom*//*filtered*/', $this->setting->value() );
    241267
Note: See TracChangeset for help on using the changeset viewer.