From 491d6770737873bb7740a71f7036627ba278e3c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Mon, 7 Aug 2023 15:39:34 +0200 Subject: [PATCH 1/5] Introduce wp_get_theme_template_part_metadata --- src/wp-includes/block-template-utils.php | 2 +- src/wp-includes/global-styles-and-settings.php | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 226b2068da216..f23bcacf85a9f 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -431,7 +431,7 @@ function _add_block_template_info( $template_item ) { */ function _add_block_template_part_area_info( $template_info ) { if ( wp_theme_has_theme_json() ) { - $theme_data = WP_Theme_JSON_Resolver::get_theme_data( array(), array( 'with_supports' => false ) )->get_template_parts(); + $theme_data = wp_get_theme_template_part_metadata(); } if ( isset( $theme_data[ $template_info['slug'] ]['area'] ) ) { diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index 5b95855752420..fa152a9dc6dd7 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -440,6 +440,17 @@ function wp_get_theme_directory_pattern_slugs() { return WP_Theme_JSON_Resolver::get_theme_data( array(), array( 'with_supports' => false ) )->get_patterns(); } +/** + * Returns the metadata for the template parts defined by the theme. + * + * @since 6.4.0 + * + * return string[] + */ +function wp_get_theme_template_part_metadata() { + return WP_Theme_JSON_Resolver::get_theme_data( array(), array( 'with_supports' => false ) )->get_template_parts(); +} + /** * Determines the CSS selector for the block type and property provided, * returning it if available. From 6588258eedfb83b591f055c5d6bdd0466b5cbf7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Mon, 7 Aug 2023 15:49:32 +0200 Subject: [PATCH 2/5] wp_get_theme_template_part_metadata: add cache --- .../global-styles-and-settings.php | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index fa152a9dc6dd7..da6a44596539e 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -448,7 +448,26 @@ function wp_get_theme_directory_pattern_slugs() { * return string[] */ function wp_get_theme_template_part_metadata() { - return WP_Theme_JSON_Resolver::get_theme_data( array(), array( 'with_supports' => false ) )->get_template_parts(); + $cache_group = 'theme_json'; + $cache_key = 'wp_get_theme_template_part_metadata'; + $can_use_cached = ! wp_is_development_mode( 'theme' ); + + $metadata = false; + if ( $can_use_cached ) { + $metadata = wp_cache_get( $cache_key, $cache_group ); + if ( false !== $metadata ) { + return $metadata; + } + } + + if ( false === $metadata ) { + $metadata = WP_Theme_JSON_Resolver::get_theme_data( array(), array( 'with_supports' => false ) )->get_template_parts(); + if ( $can_use_cached ) { + wp_cache_set( $cache_key, $metadata, $cache_group ); + } + } + + return $metadata; } /** From fc36a6b99e0fabb5b4089fe067c7a5898ced6ac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Mon, 7 Aug 2023 16:01:50 +0200 Subject: [PATCH 3/5] wp_get_theme_template_part_metadata: clean cache --- src/wp-includes/global-styles-and-settings.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index da6a44596539e..c61a1b76e4556 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -425,6 +425,7 @@ function wp_clean_theme_json_cache() { wp_cache_delete( 'wp_get_global_settings_custom', 'theme_json' ); wp_cache_delete( 'wp_get_global_settings_theme', 'theme_json' ); wp_cache_delete( 'wp_get_global_styles_custom_css', 'theme_json' ); + wp_cache_delete( 'wp_get_theme_template_part_metada', 'theme_json' ); WP_Theme_JSON_Resolver::clean_cached_data(); } From 6896ee7405dd77a9be40c83e91bd066e69f4051f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Maneiro?= <583546+oandregal@users.noreply.github.com> Date: Tue, 8 Aug 2023 10:28:21 +0200 Subject: [PATCH 4/5] Rename to wp_get_theme_data_template_parts --- src/wp-includes/block-template-utils.php | 2 +- src/wp-includes/global-styles-and-settings.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index f23bcacf85a9f..88bda858f06a8 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -431,7 +431,7 @@ function _add_block_template_info( $template_item ) { */ function _add_block_template_part_area_info( $template_info ) { if ( wp_theme_has_theme_json() ) { - $theme_data = wp_get_theme_template_part_metadata(); + $theme_data = wp_get_theme_data_template_parts(); } if ( isset( $theme_data[ $template_info['slug'] ]['area'] ) ) { diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index c61a1b76e4556..43ce185269a6e 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -425,7 +425,7 @@ function wp_clean_theme_json_cache() { wp_cache_delete( 'wp_get_global_settings_custom', 'theme_json' ); wp_cache_delete( 'wp_get_global_settings_theme', 'theme_json' ); wp_cache_delete( 'wp_get_global_styles_custom_css', 'theme_json' ); - wp_cache_delete( 'wp_get_theme_template_part_metada', 'theme_json' ); + wp_cache_delete( 'wp_get_theme_data_template_parts', 'theme_json' ); WP_Theme_JSON_Resolver::clean_cached_data(); } @@ -448,9 +448,9 @@ function wp_get_theme_directory_pattern_slugs() { * * return string[] */ -function wp_get_theme_template_part_metadata() { +function wp_get_theme_data_template_parts() { $cache_group = 'theme_json'; - $cache_key = 'wp_get_theme_template_part_metadata'; + $cache_key = 'wp_get_theme_data_template_parts'; $can_use_cached = ! wp_is_development_mode( 'theme' ); $metadata = false; From eaa777f32612494982cdb647af08bea404cda594 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Wed, 9 Aug 2023 13:32:27 +0200 Subject: [PATCH 5/5] Update src/wp-includes/global-styles-and-settings.php Co-authored-by: Felix Arntz --- src/wp-includes/global-styles-and-settings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index 43ce185269a6e..0f1ca5c82bb59 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -446,7 +446,7 @@ function wp_get_theme_directory_pattern_slugs() { * * @since 6.4.0 * - * return string[] + * return array Associative array of `$part_name => $part_data` pairs, with `$part_data` having "title" and "area" fields. */ function wp_get_theme_data_template_parts() { $cache_group = 'theme_json';