Changeset 54493 for trunk/src/wp-includes/class-wp-theme-json-resolver.php
- Timestamp:
- 10/11/2022 05:15:11 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-theme-json-resolver.php
r54491 r54493 22 22 23 23 /** 24 * Container for keep track of registered blocks. 25 * 26 * @since 6.1.0 27 * @var array 28 */ 29 protected static $blocks_cache = array( 30 'core' => array(), 31 'blocks' => array(), 32 'theme' => array(), 33 'user' => array(), 34 ); 35 36 /** 24 37 * Container for data coming from core. 25 38 * … … 28 41 */ 29 42 protected static $core = null; 43 44 /** 45 * Container for data coming from the blocks. 46 * 47 * @since 6.1.0 48 * @var WP_Theme_JSON 49 */ 50 protected static $blocks = null; 30 51 31 52 /** … … 146 167 */ 147 168 public static function get_core_data() { 169 if ( null !== static::$core && static::has_same_registered_blocks( 'core' ) ) { 170 return static::$core; 171 } 172 148 173 $config = static::read_json_file( __DIR__ . '/theme.json' ); 149 174 $config = static::translate( $config ); … … 164 189 165 190 /** 191 * Checks whether the registered blocks were already processed for this origin. 192 * 193 * @since 6.1.0 194 * 195 * @param string $origin Data source for which to cache the blocks. 196 * Valid values are 'core', 'blocks', 'theme', and 'user'. 197 * @return bool True on success, false otherwise. 198 */ 199 protected static function has_same_registered_blocks( $origin ) { 200 // Bail out if the origin is invalid. 201 if ( ! isset( static::$blocks_cache[ $origin ] ) ) { 202 return false; 203 } 204 205 $registry = WP_Block_Type_Registry::get_instance(); 206 $blocks = $registry->get_all_registered(); 207 208 // Is there metadata for all currently registered blocks? 209 $block_diff = array_diff_key( $blocks, static::$blocks_cache[ $origin ] ); 210 if ( empty( $block_diff ) ) { 211 return true; 212 } 213 214 foreach ( $blocks as $block_name => $block_type ) { 215 static::$blocks_cache[ $origin ][ $block_name ] = true; 216 } 217 218 return false; 219 } 220 221 /** 166 222 * Returns the theme's data. 167 223 * … … 190 246 $options = wp_parse_args( $options, array( 'with_supports' => true ) ); 191 247 192 $theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) ); 193 $theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) ); 194 195 /** 196 * Filters the data provided by the theme for global styles and settings. 197 * 198 * @since 6.1.0 199 * 200 * @param WP_Theme_JSON_Data Class to access and update the underlying data. 201 */ 202 $theme_json = apply_filters( 'theme_json_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) ); 203 $theme_json_data = $theme_json->get_data(); 204 static::$theme = new WP_Theme_JSON( $theme_json_data ); 248 if ( null === static::$theme || ! static::has_same_registered_blocks( 'theme' ) ) { 249 $theme_json_data = static::read_json_file( static::get_file_path_from_theme( 'theme.json' ) ); 250 $theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) ); 251 252 /** 253 * Filters the data provided by the theme for global styles and settings. 254 * 255 * @since 6.1.0 256 * 257 * @param WP_Theme_JSON_Data Class to access and update the underlying data. 258 */ 259 $theme_json = apply_filters( 'theme_json_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) ); 260 $theme_json_data = $theme_json->get_data(); 261 static::$theme = new WP_Theme_JSON( $theme_json_data ); 262 } 205 263 206 264 if ( wp_get_theme()->parent() ) { … … 259 317 $with_theme_supports = new WP_Theme_JSON( $theme_support_data ); 260 318 $with_theme_supports->merge( static::$theme ); 261 262 319 return $with_theme_supports; 263 320 } … … 273 330 $registry = WP_Block_Type_Registry::get_instance(); 274 331 $blocks = $registry->get_all_registered(); 275 $config = array( 'version' => 2 ); 332 333 if ( null !== static::$blocks && static::has_same_registered_blocks( 'blocks' ) ) { 334 return static::$blocks; 335 } 336 337 $config = array( 'version' => 2 ); 276 338 foreach ( $blocks as $block_name => $block_type ) { 277 339 if ( isset( $block_type->supports['__experimentalStyle'] ) ) { … … 299 361 $config = $theme_json->get_data(); 300 362 301 return new WP_Theme_JSON( $config, 'blocks' ); 363 static::$blocks = new WP_Theme_JSON( $config, 'blocks' ); 364 return static::$blocks; 302 365 } 303 366 … … 408 471 */ 409 472 public static function get_user_data() { 473 if ( null !== static::$user && static::has_same_registered_blocks( 'user' ) ) { 474 return static::$user; 475 } 476 410 477 $config = array(); 411 478 $user_cpt = static::get_user_data_from_wp_global_styles( wp_get_theme() ); … … 563 630 * @since 5.9.0 Added the `$user`, `$user_custom_post_type_id`, 564 631 * and `$i18n_schema` variables to reset. 632 * @since 6.1.0 Added the `$blocks` and `$blocks_cache` variables 633 * to reset. 565 634 */ 566 635 public static function clean_cached_data() { 567 636 static::$core = null; 637 static::$blocks = null; 638 static::$blocks_cache = array( 639 'core' => array(), 640 'blocks' => array(), 641 'theme' => array(), 642 'user' => array(), 643 ); 568 644 static::$theme = null; 569 645 static::$user = null;
Note: See TracChangeset
for help on using the changeset viewer.