From 96c6c273dd8bc2d94a2b080f4eba69211d310561 Mon Sep 17 00:00:00 2001 From: oandregal Date: Fri, 11 Aug 2023 11:24:11 +0000 Subject: [PATCH] Themes: add wp_get_theme_data_template_parts function. Adds a new public function, `wp_get_theme_data_template_parts` that returns the `templateParts` defined by the active theme from `theme.json`. It also substitutes the usage of private APIs by this new API. Props felixarntz. Fixes #59003 Built from https://develop.svn.wordpress.org/trunk@56385 git-svn-id: http://core.svn.wordpress.org/trunk@55897 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/block-template-utils.php | 2 +- wp-includes/global-styles-and-settings.php | 31 ++++++++++++++++++++++ wp-includes/version.php | 2 +- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/wp-includes/block-template-utils.php b/wp-includes/block-template-utils.php index 226b2068da..88bda858f0 100644 --- a/wp-includes/block-template-utils.php +++ b/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_data_template_parts(); } if ( isset( $theme_data[ $template_info['slug'] ]['area'] ) ) { diff --git a/wp-includes/global-styles-and-settings.php b/wp-includes/global-styles-and-settings.php index 5b95855752..0f1ca5c82b 100644 --- a/wp-includes/global-styles-and-settings.php +++ b/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_data_template_parts', 'theme_json' ); WP_Theme_JSON_Resolver::clean_cached_data(); } @@ -440,6 +441,36 @@ 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 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'; + $cache_key = 'wp_get_theme_data_template_parts'; + $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; +} + /** * Determines the CSS selector for the block type and property provided, * returning it if available. diff --git a/wp-includes/version.php b/wp-includes/version.php index c24f5633ee..2bfff3d33c 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.4-alpha-56384'; +$wp_version = '6.4-alpha-56385'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.