From 905315e6a62b58bd7af7c47af4b8f76914d08b0e Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Wed, 28 Sep 2022 16:18:37 +1000 Subject: [PATCH] Theme JSON classes: Ensure cache is cleared after Template Part block variations are registered --- src/wp-includes/blocks/template-part.php | 9 +++++++++ src/wp-includes/class-wp-theme-json.php | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/src/wp-includes/blocks/template-part.php b/src/wp-includes/blocks/template-part.php index 107f20dbd35b5..3adbfd09ebbc5 100644 --- a/src/wp-includes/blocks/template-part.php +++ b/src/wp-includes/blocks/template-part.php @@ -225,6 +225,15 @@ function build_template_part_block_instance_variations() { ), ); } + + /* + * Clear Theme JSON block cache so that subsequent block registrations are + * respected when it is next accessed. This is required because `get_block_templates` + * has a side effect of caching the list of currently registered blocks. + */ + WP_Theme_JSON_Resolver::clean_cached_data(); + WP_Theme_JSON::clean_cached_data(); + return $variations; } diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 77b77388af23c..58b9b0b665327 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -449,6 +449,15 @@ class WP_Theme_JSON { 'typography' => 'typography', ); + /** + * Cleans the cached data so it can be recalculated. + * + * @since 6.1.0 + */ + public static function clean_cached_data() { + static::$blocks_metadata = null; + } + /** * Returns a class name by an element name. *