Changeset 64354
- Timestamp:
- 10/15/2026 07:55:09 AM (5 weeks ago)
- Location:
- trunk
- Files:
-
- 2 added
- 4 edited
-
src/wp-includes/block-supports/layout.php (modified) (2 diffs)
-
src/wp-includes/class-wp-theme-json-resolver.php (modified) (6 diffs)
-
src/wp-includes/global-styles-and-settings.php (modified) (4 diffs)
-
tests/phpunit/tests/theme/wpGetGlobalSettings.php (added)
-
tests/phpunit/tests/theme/wpGetGlobalStyles.php (added)
-
tests/phpunit/tests/theme/wpThemeJsonResolver.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/block-supports/layout.php
r64238 r64354 1023 1023 */ 1024 1024 function wp_render_layout_support_flag( $block_content, $block ) { 1025 static $global_styles = null;1026 1027 1025 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); 1028 1026 $block_supports_layout = block_has_support( $block_type, 'layout', false ) || block_has_support( $block_type, '__experimentalLayout', false ); … … 1242 1240 // Get default blockGap value from global styles for use in layouts like grid. 1243 1241 // 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(); 1248 1244 1249 1245 // 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 25 25 * 26 26 * @since 6.1.0 27 * @since 7.2.0 Added the `merged` entry. 27 28 * @var array 28 29 */ … … 32 33 'theme' => array(), 33 34 'user' => array(), 35 'merged' => array(), 34 36 ); 37 38 /** 39 * Merged data keyed by origin. 40 * 41 * @since 7.2.0 42 * @var array 43 */ 44 protected static $merged = array(); 35 45 36 46 /** … … 193 203 * 194 204 * @since 6.1.0 205 * @since 7.2.0 Added the 'merged' origin. 195 206 * 196 207 * @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'. 198 210 * @return bool True on success, false otherwise. 199 211 */ … … 639 651 * @since 6.1.0 Added block data and generation of spacingSizes array. 640 652 * @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. 641 654 * 642 655 * @param string $origin Optional. To what level should we merge data: 'default', 'blocks', 'theme' or 'custom'. … … 649 662 } 650 663 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 651 681 $result = new WP_Theme_JSON(); 652 682 $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; 670 697 } 671 698 … … 734 761 * @since 6.1.0 Added the `$blocks` and `$blocks_cache` variables 735 762 * to reset. 763 * @since 7.2.0 Added the `$merged` variable to reset. 736 764 */ 737 765 public static function clean_cached_data() { 738 766 static::$core = null; 739 767 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(); 746 770 static::$theme = null; 747 771 static::$user = null; -
trunk/src/wp-includes/global-styles-and-settings.php
r63889 r64354 10 10 * 11 11 * @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. 12 13 * 13 14 * @param array $path Path to the specific setting to retrieve. Optional. … … 33 34 } 34 35 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'. 41 37 $origin = 'custom'; 42 38 if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) { … … 44 40 } 45 41 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(); 82 43 83 44 return _wp_array_get( $settings, $path, $settings ); … … 453 414 wp_cache_delete( 'wp_get_global_stylesheet', 'theme_json' ); 454 415 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' );457 416 wp_cache_delete( 'wp_get_global_styles_custom_css', 'theme_json' ); 458 417 wp_cache_delete( 'wp_get_theme_data_template_parts', 'theme_json' ); -
trunk/tests/phpunit/tests/theme/wpThemeJsonResolver.php
r64104 r64354 49 49 50 50 /** 51 * WP_Theme_JSON_Resolver::$merged property. 52 * 53 * @var ReflectionProperty 54 */ 55 private static $property_merged; 56 57 /** 51 58 * Theme root directory. 52 59 * … … 83 90 } 84 91 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 } 85 97 } 86 98 … … 1528 1540 $this->assertSameSetsWithIndex( $expected, $actual, 'Merged variation styles do not match.' ); 1529 1541 } 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 } 1530 1726 }
Note:
See TracChangeset
for help on using the changeset viewer.