Make WordPress Core

Changeset 56385


Ignore:
Timestamp:
08/11/2023 11:22:54 AM (2 years ago)
Author:
oandregal
Message:

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

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-template-utils.php

    r56329 r56385  
    432432function _add_block_template_part_area_info( $template_info ) {
    433433    if ( wp_theme_has_theme_json() ) {
    434         $theme_data = WP_Theme_JSON_Resolver::get_theme_data( array(), array( 'with_supports' => false ) )->get_template_parts();
     434        $theme_data = wp_get_theme_data_template_parts();
    435435    }
    436436
  • trunk/src/wp-includes/global-styles-and-settings.php

    r56254 r56385  
    426426    wp_cache_delete( 'wp_get_global_settings_theme', 'theme_json' );
    427427    wp_cache_delete( 'wp_get_global_styles_custom_css', 'theme_json' );
     428    wp_cache_delete( 'wp_get_theme_data_template_parts', 'theme_json' );
    428429    WP_Theme_JSON_Resolver::clean_cached_data();
    429430}
     
    439440function wp_get_theme_directory_pattern_slugs() {
    440441    return WP_Theme_JSON_Resolver::get_theme_data( array(), array( 'with_supports' => false ) )->get_patterns();
     442}
     443
     444/**
     445 * Returns the metadata for the template parts defined by the theme.
     446 *
     447 * @since 6.4.0
     448 *
     449 * return array Associative array of `$part_name => $part_data` pairs, with `$part_data` having "title" and "area" fields.
     450 */
     451function wp_get_theme_data_template_parts() {
     452    $cache_group    = 'theme_json';
     453    $cache_key      = 'wp_get_theme_data_template_parts';
     454    $can_use_cached = ! wp_is_development_mode( 'theme' );
     455
     456    $metadata = false;
     457    if ( $can_use_cached ) {
     458        $metadata = wp_cache_get( $cache_key, $cache_group );
     459        if ( false !== $metadata ) {
     460            return $metadata;
     461        }
     462    }
     463
     464    if ( false === $metadata ) {
     465        $metadata = WP_Theme_JSON_Resolver::get_theme_data( array(), array( 'with_supports' => false ) )->get_template_parts();
     466        if ( $can_use_cached ) {
     467            wp_cache_set( $cache_key, $metadata, $cache_group );
     468        }
     469    }
     470
     471    return $metadata;
    441472}
    442473
Note: See TracChangeset for help on using the changeset viewer.