Make WordPress Core

Changeset 54707


Ignore:
Timestamp:
10/27/2022 04:37:55 PM (2 years ago)
Author:
davidbaumwald
Message:

Themes: Ensure custom global styles are imported properly.

This change removes caching of global styles for logged in users, allowing "wp_global_styles" custom post type to be imported completely, regardless of any previously cached data. This change now relies on the lower-level native WP_Query cache invalidation methods for the global styles post type.

Follow-up to [52275], [54186].

Props anariel-design, bernhard-reiter, andrewserong, spacedmonkey, andraganescu, peterwilsoncc, oandregal, hellofromTonya.
Reviewed by hellofromTonya.
Merges [54706] to the 6.1 branch.
Fixes #56901.

Location:
branches/6.1
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/6.1

  • branches/6.1/src/wp-includes/class-wp-theme-json-resolver.php

    r54517 r54707  
    423423            ),
    424424        );
    425 
    426         $cache_key = sprintf( 'wp_global_styles_%s', md5( serialize( $args ) ) );
    427         $post_id   = (int) get_transient( $cache_key );
    428         // Special case: '-1' is a results not found.
    429         if ( -1 === $post_id && ! $create_post ) {
    430             return $user_cpt;
    431         }
    432 
    433         if ( $post_id > 0 && in_array( get_post_status( $post_id ), (array) $post_status_filter, true ) ) {
    434             return get_post( $post_id, ARRAY_A );
    435         }
    436425
    437426        $global_style_query = new WP_Query();
     
    457446            }
    458447        }
    459         $cache_expiration = $user_cpt ? DAY_IN_SECONDS : HOUR_IN_SECONDS;
    460         set_transient( $cache_key, $user_cpt ? $user_cpt['ID'] : -1, $cache_expiration );
    461448
    462449        return $user_cpt;
  • branches/6.1/tests/phpunit/tests/theme/wpThemeJsonResolver.php

    r54631 r54707  
    1414
    1515    /**
     16     * Administrator ID.
     17     *
     18     * @var int
     19     */
     20    protected static $administrator_id;
     21
     22    /**
    1623     * Theme root directory.
    1724     *
     
    6471    public static function set_up_before_class() {
    6572        parent::set_up_before_class();
     73
     74        self::$administrator_id = self::factory()->user->create(
     75            array(
     76                'role'       => 'administrator',
     77                'user_email' => 'administrator@example.com',
     78            )
     79        );
    6680
    6781        static::$property_blocks_cache = new ReflectionProperty( WP_Theme_JSON_Resolver::class, 'blocks_cache' );
     
    621635     */
    622636    function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries() {
     637        wp_set_current_user( self::$administrator_id );
    623638        $theme = wp_get_theme();
    624639        WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
     
    630645        }
    631646        $query_count = count( $this->queries ) - $query_count;
    632         $this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type' );
     647        $this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' );
    633648
    634649        $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
    635         $this->assertEmpty( $user_cpt );
     650        $this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' );
    636651
    637652        $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true );
    638         $this->assertNotEmpty( $user_cpt );
     653        $this->assertNotEmpty( $user_cpt, 'User CPT is expected not to be empty.' );
    639654
    640655        $query_count = count( $this->queries );
     
    642657            $new_user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
    643658            WP_Theme_JSON_Resolver::clean_cached_data();
    644             $this->assertSameSets( $user_cpt, $new_user_cpt );
     659            $this->assertSameSets( $user_cpt, $new_user_cpt, "User CPTs do not match on run {$i}." );
    645660        }
    646661        $query_count = count( $this->queries ) - $query_count;
    647         $this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type' );
    648         remove_filter( 'query', array( $this, 'filter_db_query' ) );
     662        $this->assertSame( 1, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type after creation.' );
     663    }
     664
     665    /**
     666     * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles
     667     */
     668    function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries_for_logged_out_users() {
     669        $theme = wp_get_theme();
     670        WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
     671        add_filter( 'query', array( $this, 'filter_db_query' ) );
     672        $query_count = count( $this->queries );
     673        for ( $i = 0; $i < 3; $i++ ) {
     674            WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
     675            WP_Theme_JSON_Resolver::clean_cached_data();
     676        }
     677        $query_count = count( $this->queries ) - $query_count;
     678        $this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' );
     679
     680        $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
     681        $this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' );
    649682    }
    650683
Note: See TracChangeset for help on using the changeset viewer.