Make WordPress Core

Opened 5 weeks ago

Closed 5 weeks ago

#66280 closed enhancement (fixed)

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

Reported by: jonsurrell Owned by: jonsurrell
Priority: normal Milestone: 7.2
Component: Editor Version:
Severity: normal Keywords: gutenberg-merge has-patch has-unit-tests commit
Cc: Focuses: css, performance

Description

Gutenberg backport of ​PR 81979.

WP_Theme_JSON_Resolver::get_merged_data() merges the core, blocks, theme and user data into a new WP_Theme_JSON object on every call. Each of those sources is already cached, but the merged result is not. wp_get_global_styles() calls it every time, and wp_render_block_style_variation_support_styles() calls it once for each rendered block with an is-style-* class.

The related Gutenberg change caches the merged data for each origin until the end of the request. Registering a block or calling wp_clean_theme_json_cache() clears it. Callers receive a copy, so changing it does not change the cache. The object cache in wp_get_global_settings() is removed: it duplicates the new cache and does not see blocks registered after it was filled.

Theme support changes made after the first merge take effect only after wp_clean_theme_json_cache().

This tracks the backport of the Gutenberg PR to WP Core.

Gutenberg PR: ​https://github.com/WordPress/gutenberg/pull/81979

Related: #62249

Change History (6)

This ticket was mentioned in ​PR #14071 on ​WordPress/wordpress-develop by ​@jonsurrell.


5 weeks ago
#1

  • Keywords has-patch has-unit-tests added

WP_Theme_JSON_Resolver::get_merged_data() repeats the same merges on every call, including once per block with an is-style-* class. Cache the merged data per origin for the request, and return a clone so callers can modify their copy.

Backport of ​https://github.com/WordPress/gutenberg/pull/81979.

In Gutenberg on WordPress 7.2-alpha with WP_DEBUG off, a Twenty Twenty-Four front page spent 6.6 ms in 24 merges per request; a post with 52 styled blocks spent 12.9 ms in 58 merges. With the resolver cache, total time in get_merged_data() was about 1.9 ms for either page, including the initial merge. These are Gutenberg measurements, not a separate Core benchmark (​method and results).

The cache uses the resolver's existing block-registration check and resets on clean_cached_data(). Remove the redundant wp_get_global_settings() cache so it reads the resolver's current data, including after late block registration. Both caches are per-request; the removed cache's theme development mode bypass did not affect data freshness across requests. This also makes the accessor cache proposed in #13214 unnecessary.

Theme supports changed after the first merge require wp_clean_theme_json_cache(). The wp_theme_json_data_* filters already require a reset after their origin has been cached; see Trac #62249. The block-registration check still detects additions only.

Tests cover per-origin caching, independent returned instances, changes to returned data, late block registration, cache clearing, theme switching, and settings accessor invalidation. Reported local validation: themes, blocks, block-supports and global-styles groups passed (2,118 tests, 15 skipped); phpcs and git diff --check passed.

Trac ticket: https://core.trac.wordpress.org/ticket/66280
Related: https://core.trac.wordpress.org/ticket/62249

Closes #13214.

Use of AI Tools

Claude Code (Claude Fable 5.1) for implementation and tests. Codex (GPT-6) checked the diff and edited the description.

​@Mamaduka commented on ​PR #14071:


5 weeks ago
#2

Hey, folks

Just wanted to check if there are any blockers here and if we will commit this enhancement before next week's beta 1.

#3 @jonsurrell
5 weeks ago

  • Keywords commit added

​@jonsurrell commented on ​PR #14071:


5 weeks ago
#4

Also let's copy the changes to src/wp-includes/block-supports/layout.php from #13214.

Done in 1458bb3a1521bc1b0e50c2b7f0592aa37310b4a6.

​@peterwilsoncc commented on ​PR #14071:


5 weeks ago
#5

I've taken the liberty of merging in trunk and removing some unnecessary tear downs in the tests (filters are reset by the test suite's base tear down).

Otherwise let's ship.

#6 @jonsurrell
5 weeks ago

  • Owner set to jonsurrell
  • Resolution → fixed
  • Status assigned → closed

In 64354:

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.

Note: See TracTickets for help on using tickets.