Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wp_get_theme_data_template_parts: create public API to access templateParts from theme.json #4971

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) ) {
Expand Down
31 changes: 31 additions & 0 deletions src/wp-includes/global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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.
Expand Down