Skip to content

Commit c80fcb0

Browse files
committed
Themes: Record the registered blocks on the first theme.json origin build.
WP_Theme_JSON_Resolver::get_core_data(), get_block_data(), get_theme_data() and get_user_data() tested the cached object for null before calling has_same_registered_blocks(). On the first call the object is null, so the check never ran and never recorded the registered blocks. The second call ran the check, found nothing recorded, and rebuilt the origin's data. Call has_same_registered_blocks() first, so the first build records the registered blocks and the second call reuses the data. A block type registered between calls still rebuilds it. The test data provider case "When both caches are not empty" left the blocks cache empty and expected a rebuild; it now fills the cache and expects none.
1 parent 6e800fa commit c80fcb0

2 files changed

Lines changed: 62 additions & 12 deletions

File tree

‎src/wp-includes/class-wp-theme-json-resolver.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ protected static function translate( $theme_json, $domain = 'default' ) {
158158
* @return WP_Theme_JSON Entity that holds core data.
159159
*/
160160
public static function get_core_data() {
161-
if ( null !== static::$core && static::has_same_registered_blocks( 'core' ) ) {
161+
if ( static::has_same_registered_blocks( 'core' ) && null !== static::$core ) {
162162
return static::$core;
163163
}
164164

@@ -250,7 +250,7 @@ public static function get_theme_data( $deprecated = array(), $options = array()
250250

251251
$options = wp_parse_args( $options, array( 'with_supports' => true ) );
252252

253-
if ( null === static::$theme || ! static::has_same_registered_blocks( 'theme' ) ) {
253+
if ( ! static::has_same_registered_blocks( 'theme' ) || null === static::$theme ) {
254254
$wp_theme = wp_get_theme();
255255
$theme_json_file = $wp_theme->get_file_path( 'theme.json' );
256256
if ( is_readable( $theme_json_file ) ) {
@@ -395,7 +395,7 @@ public static function get_block_data() {
395395
$registry = WP_Block_Type_Registry::get_instance();
396396
$blocks = $registry->get_all_registered();
397397

398-
if ( null !== static::$blocks && static::has_same_registered_blocks( 'blocks' ) ) {
398+
if ( static::has_same_registered_blocks( 'blocks' ) && null !== static::$blocks ) {
399399
return static::$blocks;
400400
}
401401

@@ -543,7 +543,7 @@ public static function get_user_data_from_wp_global_styles( $theme, $create_post
543543
* @return WP_Theme_JSON Entity that holds styles for user data.
544544
*/
545545
public static function get_user_data() {
546-
if ( null !== static::$user && static::has_same_registered_blocks( 'user' ) ) {
546+
if ( static::has_same_registered_blocks( 'user' ) && null !== static::$user ) {
547547
return static::$user;
548548
}
549549

‎tests/phpunit/tests/theme/wpThemeJsonResolver.php‎

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ public function tear_down() {
114114
unset( $GLOBALS['wp_themes'] );
115115
remove_filter( 'theme_file_uri', array( $this, 'filter_theme_file_uri' ) );
116116

117+
if ( WP_Block_Type_Registry::get_instance()->is_registered( 'test/seeded' ) ) {
118+
unregister_block_type( 'test/seeded' );
119+
}
120+
117121
// Reset data between tests.
118122
wp_clean_theme_json_cache();
119123
parent::tear_down();
@@ -421,12 +425,10 @@ public function test_get_core_data( $should_fire_filter, $core_is_cached, $block
421425
WP_Theme_JSON_Resolver::get_core_data();
422426
}
423427

424-
// If should cache registered blocks, then set them up before running the tests.
425-
if ( $blocks_are_cached ) {
426-
$blocks_cache = static::$property_blocks_cache->getValue();
427-
$blocks_cache['core'] = $this->get_registered_block_names();
428-
static::$property_blocks_cache->setValue( null, $blocks_cache );
429-
}
428+
// Set the blocks cache to the registered blocks, or empty it: caching core also fills it.
429+
$blocks_cache = static::$property_blocks_cache->getValue();
430+
$blocks_cache['core'] = $blocks_are_cached ? $this->get_registered_block_names() : array();
431+
static::$property_blocks_cache->setValue( null, $blocks_cache );
430432

431433
$expected_filter_count = did_filter( 'wp_theme_json_data_default' );
432434
$actual = WP_Theme_JSON_Resolver::get_core_data();
@@ -462,13 +464,61 @@ public static function data_get_core_data() {
462464
'blocks_are_cached' => false,
463465
),
464466
'When both caches are not empty' => array(
465-
'should_fire_filter' => true,
467+
'should_fire_filter' => false,
466468
'core_is_cached' => true,
467-
'blocks_are_cached' => false,
469+
'blocks_are_cached' => true,
468470
),
469471
);
470472
}
471473

474+
/**
475+
* The first call to an origin getter should record the registered blocks,
476+
* so the second call reuses the data instead of rebuilding it.
477+
*
478+
* @dataProvider data_origin_getters
479+
*
480+
* @covers WP_Theme_JSON_Resolver::get_core_data
481+
* @covers WP_Theme_JSON_Resolver::get_block_data
482+
* @covers WP_Theme_JSON_Resolver::get_theme_data
483+
* @covers WP_Theme_JSON_Resolver::get_user_data
484+
*
485+
* @param string $getter Resolver method name.
486+
* @param array $args Arguments for the method.
487+
* @param string $filter Filter the method applies when it builds the data.
488+
*/
489+
public function test_origin_data_is_built_once_while_registered_blocks_are_unchanged( $getter, array $args, $filter ) {
490+
wp_clean_theme_json_cache();
491+
$callback = array( WP_Theme_JSON_Resolver::class, $getter );
492+
$filter_count = did_filter( $filter );
493+
494+
$first = call_user_func_array( $callback, $args );
495+
$this->assertSame( $filter_count + 1, did_filter( $filter ), 'The first call should build the data' );
496+
497+
$second = call_user_func_array( $callback, $args );
498+
$this->assertSame( $filter_count + 1, did_filter( $filter ), 'The second call should not rebuild the data' );
499+
$this->assertSame( $first, $second, 'The second call should return the same object' );
500+
501+
register_block_type( 'test/seeded' );
502+
503+
$third = call_user_func_array( $callback, $args );
504+
$this->assertSame( $filter_count + 2, did_filter( $filter ), 'Registering a block type should rebuild the data' );
505+
$this->assertNotSame( $first, $third, 'The rebuilt data should be a new object' );
506+
}
507+
508+
/**
509+
* Data provider.
510+
*
511+
* @return array
512+
*/
513+
public static function data_origin_getters() {
514+
return array(
515+
'core' => array( 'get_core_data', array(), 'wp_theme_json_data_default' ),
516+
'blocks' => array( 'get_block_data', array(), 'wp_theme_json_data_blocks' ),
517+
'theme' => array( 'get_theme_data', array( array(), array( 'with_supports' => false ) ), 'wp_theme_json_data_theme' ),
518+
'user' => array( 'get_user_data', array(), 'wp_theme_json_data_user' ),
519+
);
520+
}
521+
472522
/**
473523
* @ticket 54336
474524
* @ticket 60118

0 commit comments

Comments
 (0)