Make WordPress Core

Changeset 64354


Ignore:
Timestamp:
10/15/2026 07:55:09 AM (5 weeks ago)
Author:
jonsurrell
Message:

Editor: Cache the theme.json data merged from all origins.

WP_Theme_JSON_Resolver::get_merged_data() merged all origins on every call, including once for each rendered block with an is-style-* class. Cache the result until a block is registered or wp_clean_theme_json_cache() is called, and remove the redundant cache in wp_get_global_settings(). Theme support changes made after the first merge require wp_clean_theme_json_cache().

Backports ​https://github.com/WordPress/gutenberg/pull/81979.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/14071

Props jonsurrell, peterwilsoncc, isabel_brison, ramonopoly.
Fixes #66280. See #62249.

Location:
trunk
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-supports/layout.php

    r64238 r64354  
    10231023 */
    10241024function wp_render_layout_support_flag( $block_content, $block ) {
    1025         static $global_styles = null;
    1026 
    10271025        $block_type            = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
    10281026        $block_supports_layout = block_has_support( $block_type, 'layout', false ) || block_has_support( $block_type, '__experimentalLayout', false );
    … …  
    12421240                // Get default blockGap value from global styles for use in layouts like grid.
    12431241                // Check style variation first, then block-specific styles, then fall back to root styles.
    1244                 $block_name = $block['blockName'] ?? '';
    1245                 if ( null === $global_styles ) {
    1246                         $global_styles = wp_get_global_styles();
    1247                 }
     1242                $block_name    = $block['blockName'] ?? '';
     1243                $global_styles = wp_get_global_styles();
    12481244
    12491245                // Check if the block has an active style variation with a blockGap value.
  • trunk/src/wp-includes/class-wp-theme-json-resolver.php

    r64163 r64354  
    2525         *
    2626         * @since 6.1.0
     27         * @since 7.2.0 Added the `merged` entry.
    2728         * @var array
    2829         */
    … …  
    3233                'theme'  => array(),
    3334                'user'   => array(),
     35                'merged' => array(),
    3436        );
     37
     38        /**
     39         * Merged data keyed by origin.
     40         *
     41         * @since 7.2.0
     42         * @var array
     43         */
     44        protected static $merged = array();
    3545
    3646        /**
    … …  
    193203         *
    194204         * @since 6.1.0
     205         * @since 7.2.0 Added the 'merged' origin.
    195206         *
    196207         * @param string $origin Data source for which to cache the blocks.
    197          *                       Valid values are 'core', 'blocks', 'theme', and 'user'.
     208         *                       Valid values are 'core', 'blocks', 'theme', 'user',
     209         *                       and 'merged'.
    198210         * @return bool True on success, false otherwise.
    199211         */
    … …  
    639651         * @since 6.1.0 Added block data and generation of spacingSizes array.
    640652         * @since 6.2.0 Changed ' $origin' parameter values to 'default', 'blocks', 'theme' or 'custom'.
     653         * @since 7.2.0 Caches the merged data per origin for the request and returns a copy of it.
    641654         *
    642655         * @param string $origin Optional. To what level should we merge data: 'default', 'blocks', 'theme' or 'custom'.
    … …  
    649662                }
    650663
     664                if ( ! in_array( $origin, array( 'default', 'blocks', 'theme', 'custom' ), true ) ) {
     665                        $origin = 'custom';
     666                }
     667
     668                /*
     669                 * Discard the merged data when blocks were registered after the last merge.
     670                 *
     671                 * This check detects new blocks, not theme support changes. Call
     672                 * clean_cached_data() after changing theme supports to refresh the merged data.
     673                 */
     674                if ( ! static::has_same_registered_blocks( 'merged' ) ) {
     675                        static::$merged = array();
     676                }
     677                if ( isset( static::$merged[ $origin ] ) ) {
     678                        return clone static::$merged[ $origin ];
     679                }
     680
    651681                $result = new WP_Theme_JSON();
    652682                $result->merge( static::get_core_data() );
    653                 if ( 'default' === $origin ) {
    654                         return $result;
    655                 }
    656 
    657                 $result->merge( static::get_block_data() );
    658                 if ( 'blocks' === $origin ) {
    659                         return $result;
    660                 }
    661 
    662                 $result->merge( static::get_theme_data() );
    663                 if ( 'theme' === $origin ) {
    664                         return $result;
    665                 }
    666 
    667                 $result->merge( static::get_user_data() );
    668 
    669                 return $result;
     683                if ( 'default' !== $origin ) {
     684                        $result->merge( static::get_block_data() );
     685                }
     686                if ( 'theme' === $origin || 'custom' === $origin ) {
     687                        $result->merge( static::get_theme_data() );
     688                }
     689                if ( 'custom' === $origin ) {
     690                        $result->merge( static::get_user_data() );
     691                }
     692
     693                static::$merged[ $origin ] = $result;
     694
     695                // Return a clone so callers can modify the result without changing the cache.
     696                return clone $result;
    670697        }
    671698
    … …  
    734761         * @since 6.1.0 Added the `$blocks` and `$blocks_cache` variables
    735762         *              to reset.
     763         * @since 7.2.0 Added the `$merged` variable to reset.
    736764         */
    737765        public static function clean_cached_data() {
    738766                static::$core                     = null;
    739767                static::$blocks                   = null;
    740                 static::$blocks_cache             = array(
    741                         'core'   => array(),
    742                         'blocks' => array(),
    743                         'theme'  => array(),
    744                         'user'   => array(),
    745                 );
     768                static::$blocks_cache             = array_fill_keys( array_keys( static::$blocks_cache ), array() );
     769                static::$merged                   = array();
    746770                static::$theme                    = null;
    747771                static::$user                     = null;
  • trunk/src/wp-includes/global-styles-and-settings.php

    r63889 r64354  
    1010 *
    1111 * @since 5.9.0
     12 * @since 7.2.0 Removed the per-request cache; WP_Theme_JSON_Resolver::get_merged_data() caches the merged data.
    1213 *
    1314 * @param array $path    Path to the specific setting to retrieve. Optional.
    … …  
    3334        }
    3435
    35         /*
    36          * This is the default value when no origin is provided or when it is 'all'.
    37          *
    38          * The $origin is used as part of the cache key. Changes here need to account
    39          * for clearing the cache appropriately.
    40          */
     36        // This is the default value when no origin is provided or when it is 'all'.
    4137        $origin = 'custom';
    4238        if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) {
    … …  
    4440        }
    4541
    46         /*
    47          * By using the 'theme_json' group, this data is marked to be non-persistent across requests.
    48          * See `wp_cache_add_non_persistent_groups` in src/wp-includes/load.php and other places.
    49          *
    50          * The rationale for this is to make sure derived data from theme.json
    51          * is always fresh from the potential modifications done via hooks
    52          * that can use dynamic data (modify the stylesheet depending on some option,
    53          * settings depending on user permissions, etc.).
    54          * See some of the existing hooks to modify theme.json behavior:
    55          * https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/
    56          *
    57          * A different alternative considered was to invalidate the cache upon certain
    58          * events such as options add/update/delete, user meta, etc.
    59          * It was judged not enough, hence this approach.
    60          * See https://github.com/WordPress/gutenberg/pull/45372
    61          */
    62         $cache_group = 'theme_json';
    63         $cache_key   = 'wp_get_global_settings_' . $origin;
    64 
    65         /*
    66          * Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme
    67          * developer's workflow.
    68          */
    69         $can_use_cached = ! wp_is_development_mode( 'theme' );
    70 
    71         $settings = false;
    72         if ( $can_use_cached ) {
    73                 $settings = wp_cache_get( $cache_key, $cache_group );
    74         }
    75 
    76         if ( false === $settings ) {
    77                 $settings = WP_Theme_JSON_Resolver::get_merged_data( $origin )->get_settings();
    78                 if ( $can_use_cached ) {
    79                         wp_cache_set( $cache_key, $settings, $cache_group );
    80                 }
    81         }
     42        $settings = WP_Theme_JSON_Resolver::get_merged_data( $origin )->get_settings();
    8243
    8344        return _wp_array_get( $settings, $path, $settings );
    … …  
    453414        wp_cache_delete( 'wp_get_global_stylesheet', 'theme_json' );
    454415        wp_cache_delete( 'wp_get_global_styles_svg_filters', 'theme_json' );
    455         wp_cache_delete( 'wp_get_global_settings_custom', 'theme_json' );
    456         wp_cache_delete( 'wp_get_global_settings_theme', 'theme_json' );
    457416        wp_cache_delete( 'wp_get_global_styles_custom_css', 'theme_json' );
    458417        wp_cache_delete( 'wp_get_theme_data_template_parts', 'theme_json' );
  • trunk/tests/phpunit/tests/theme/wpThemeJsonResolver.php

    r64104 r64354  
    4949
    5050        /**
     51         * WP_Theme_JSON_Resolver::$merged property.
     52         *
     53         * @var ReflectionProperty
     54         */
     55        private static $property_merged;
     56
     57        /**
    5158         * Theme root directory.
    5259         *
    … …  
    8390                }
    8491                static::$property_core_orig_value = static::$property_core->getValue();
     92
     93                static::$property_merged = new ReflectionProperty( WP_Theme_JSON_Resolver::class, 'merged' );
     94                if ( PHP_VERSION_ID < 80100 ) {
     95                        static::$property_merged->setAccessible( true );
     96                }
    8597        }
    8698
    … …  
    15281540                $this->assertSameSetsWithIndex( $expected, $actual, 'Merged variation styles do not match.' );
    15291541        }
     1542
     1543        /**
     1544         * @ticket 66280
     1545         * @covers WP_Theme_JSON_Resolver::get_merged_data
     1546         */
     1547        public function test_get_merged_data_returns_a_separate_instance_per_call(): void {
     1548                $first  = WP_Theme_JSON_Resolver::get_merged_data();
     1549                $second = WP_Theme_JSON_Resolver::get_merged_data();
     1550
     1551                $this->assertNotSame( $first, $second, 'Each call should return its own instance.' );
     1552                $this->assertSameSetsWithIndex(
     1553                        $first->get_raw_data(),
     1554                        $second->get_raw_data(),
     1555                        'Repeated calls should return equivalent data.'
     1556                );
     1557        }
     1558
     1559        /**
     1560         * @ticket 66280
     1561         * @dataProvider data_merged_data_origins
     1562         * @covers WP_Theme_JSON_Resolver::get_merged_data
     1563         *
     1564         * @param string $origin Origin to request.
     1565         */
     1566        public function test_get_merged_data_returns_the_memoized_data( string $origin ): void {
     1567                WP_Theme_JSON_Resolver::get_merged_data( $origin );
     1568
     1569                $sentinel = $this->replace_merged_data_with_sentinel( $origin );
     1570                $result   = WP_Theme_JSON_Resolver::get_merged_data( $origin );
     1571
     1572                $this->assertSame(
     1573                        'sentinel',
     1574                        $result->get_raw_data()['styles']['color']['text'] ?? null,
     1575                        'The second call should return the data memoized by the first.'
     1576                );
     1577                $this->assertNotSame( $sentinel, $result, 'A copy of the memoized data should be returned, not the memoized instance.' );
     1578        }
     1579
     1580        /**
     1581         * Callers are free to modify the returned data. WP_Theme_JSON::resolve_variables()
     1582         * does exactly that, so a modification must not be observable by the next caller.
     1583         *
     1584         * @ticket 66280
     1585         * @covers WP_Theme_JSON_Resolver::get_merged_data
     1586         */
     1587        public function test_get_merged_data_is_not_affected_by_modifying_the_result(): void {
     1588                // A style referencing a preset is injected because no bundled theme fixture has one that resolves.
     1589                $filter = static function ( WP_Theme_JSON_Data $theme_json ): WP_Theme_JSON_Data {
     1590                        return $theme_json->update_with(
     1591                                array(
     1592                                        'version'  => WP_Theme_JSON::LATEST_SCHEMA,
     1593                                        'settings' => array(
     1594                                                'typography' => array(
     1595                                                        'fontFamilies' => array(
     1596                                                                array(
     1597                                                                        'slug'       => 'test-font',
     1598                                                                        'name'       => 'Test Font',
     1599                                                                        'fontFamily' => 'Test Font, sans-serif',
     1600                                                                ),
     1601                                                        ),
     1602                                                ),
     1603                                        ),
     1604                                        'styles'   => array(
     1605                                                'typography' => array( 'fontFamily' => 'var(--wp--preset--font-family--test-font)' ),
     1606                                        ),
     1607                                )
     1608                        );
     1609                };
     1610                add_filter( 'wp_theme_json_data_theme', $filter );
     1611                $path = array( 'typography', 'fontFamily' );
     1612
     1613                $before   = _wp_array_get( WP_Theme_JSON_Resolver::get_merged_data()->get_raw_data()['styles'], $path );
     1614                $resolved = _wp_array_get(
     1615                        WP_Theme_JSON::resolve_variables( WP_Theme_JSON_Resolver::get_merged_data() )->get_raw_data()['styles'],
     1616                        $path
     1617                );
     1618                $after    = _wp_array_get( WP_Theme_JSON_Resolver::get_merged_data()->get_raw_data()['styles'], $path );
     1619
     1620                $this->assertSame( 'var(--wp--preset--font-family--test-font)', $before, 'The theme data should reference a preset.' );
     1621                $this->assertSame( 'Test Font, sans-serif', $resolved, 'Expected the preset reference to resolve.' );
     1622                $this->assertSame( $before, $after, 'Resolving variables should not affect subsequent calls.' );
     1623        }
     1624
     1625        /**
     1626         * @ticket 66280
     1627         * @covers WP_Theme_JSON_Resolver::get_merged_data
     1628         */
     1629        public function test_get_merged_data_reflects_blocks_registered_after_a_previous_call(): void {
     1630                // The first call records the registered blocks and stores the data; the second returns it.
     1631                WP_Theme_JSON_Resolver::get_merged_data();
     1632                WP_Theme_JSON_Resolver::get_merged_data();
     1633
     1634                register_block_type(
     1635                        'test/block-gap',
     1636                        array(
     1637                                'supports' => array(
     1638                                        '__experimentalStyle' => array(
     1639                                                'spacing' => array( 'blockGap' => '77px' ),
     1640                                        ),
     1641                                ),
     1642                        )
     1643                );
     1644
     1645                $styles = WP_Theme_JSON_Resolver::get_merged_data()->get_raw_data()['styles'];
     1646
     1647                unregister_block_type( 'test/block-gap' );
     1648
     1649                $this->assertSame(
     1650                        '77px',
     1651                        $styles['blocks']['test/block-gap']['spacing']['blockGap'] ?? null,
     1652                        'Data for a block registered after a previous call should be present.'
     1653                );
     1654        }
     1655
     1656        /**
     1657         * @ticket 66280
     1658         * @dataProvider data_merged_data_origins
     1659         * @covers WP_Theme_JSON_Resolver::clean_cached_data
     1660         *
     1661         * @param string $origin Origin to request.
     1662         */
     1663        public function test_clean_cached_data_refreshes_merged_data( string $origin ): void {
     1664                WP_Theme_JSON_Resolver::get_merged_data( $origin );
     1665                $this->replace_merged_data_with_sentinel( $origin );
     1666
     1667                WP_Theme_JSON_Resolver::clean_cached_data();
     1668
     1669                $this->assertNotSame(
     1670                        'sentinel',
     1671                        WP_Theme_JSON_Resolver::get_merged_data( $origin )->get_raw_data()['styles']['color']['text'] ?? null,
     1672                        'Cleaning the cached data should discard the memoized merged data.'
     1673                );
     1674        }
     1675
     1676        /**
     1677         * Data provider.
     1678         *
     1679         * @return array<string, array{string}>
     1680         */
     1681        public static function data_merged_data_origins(): array {
     1682                return array(
     1683                        'default' => array( 'default' ),
     1684                        'blocks'  => array( 'blocks' ),
     1685                        'theme'   => array( 'theme' ),
     1686                        'custom'  => array( 'custom' ),
     1687                );
     1688        }
     1689
     1690        /**
     1691         * @ticket 66280
     1692         * @covers WP_Theme_JSON_Resolver::get_merged_data
     1693         */
     1694        public function test_switching_theme_refreshes_merged_data(): void {
     1695                switch_theme( 'block-theme' );
     1696                $before = wp_list_pluck( WP_Theme_JSON_Resolver::get_merged_data()->get_settings()['color']['palette']['theme'] ?? array(), 'slug' );
     1697
     1698                switch_theme( 'default' );
     1699                $after = wp_list_pluck( WP_Theme_JSON_Resolver::get_merged_data()->get_settings()['color']['palette']['theme'] ?? array(), 'slug' );
     1700
     1701                $this->assertContains( 'light', $before, 'The block theme palette should be present while it is active.' );
     1702                $this->assertNotContains( 'light', $after, 'Switching themes should refresh the merged data.' );
     1703        }
     1704
     1705        /**
     1706         * Replaces the memoized merged data for an origin with a recognizable instance.
     1707         *
     1708         * @param string $origin Origin to replace the data for.
     1709         * @return WP_Theme_JSON The instance now memoized for the origin.
     1710         */
     1711        private function replace_merged_data_with_sentinel( string $origin ): WP_Theme_JSON {
     1712                $sentinel = new WP_Theme_JSON(
     1713                        array(
     1714                                'version' => WP_Theme_JSON::LATEST_SCHEMA,
     1715                                'styles'  => array( 'color' => array( 'text' => 'sentinel' ) ),
     1716                        )
     1717                );
     1718
     1719                $merged = static::$property_merged->getValue();
     1720                $this->assertArrayHasKey( $origin, $merged, 'The merged data should be stored for the origin before it is replaced.' );
     1721                $merged[ $origin ] = $sentinel;
     1722                static::$property_merged->setValue( null, $merged );
     1723
     1724                return $sentinel;
     1725        }
    15301726}
Note: See TracChangeset for help on using the changeset viewer.