Changeset 54276 for trunk/src/wp-includes/blocks.php
- Timestamp:
- 09/21/2022 01:55:25 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks.php
r54181 r54276 284 284 */ 285 285 function register_block_type_from_metadata( $file_or_folder, $args = array() ) { 286 $filename = 'block.json'; 287 $metadata_file = ( substr( $file_or_folder, -strlen( $filename ) ) !== $filename ) ? 288 trailingslashit( $file_or_folder ) . $filename : 286 /* 287 * Get an array of metadata from a PHP file. 288 * This improves performance for core blocks as it's only necessary to read a single PHP file 289 * instead of reading a JSON file per-block, and then decoding from JSON to PHP. 290 * Using a static variable ensures that the metadata is only read once per request. 291 */ 292 static $core_blocks_meta; 293 if ( ! $core_blocks_meta ) { 294 $core_blocks_meta = include_once ABSPATH . WPINC . '/blocks/blocks-json.php'; 295 } 296 297 $metadata_file = ( ! str_ends_with( $file_or_folder, 'block.json' ) ) ? 298 trailingslashit( $file_or_folder ) . 'block.json' : 289 299 $file_or_folder; 300 290 301 if ( ! file_exists( $metadata_file ) ) { 291 302 return false; 292 303 } 293 304 294 $metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) ); 305 // Try to get metadata from the static cache for core blocks. 306 $metadata = false; 307 if ( str_starts_with( $file_or_folder, ABSPATH . WPINC ) ) { 308 $core_block_name = str_replace( ABSPATH . WPINC . '/blocks/', '', $file_or_folder ); 309 if ( ! empty( $core_blocks_meta[ $core_block_name ] ) ) { 310 $metadata = $core_blocks_meta[ $core_block_name ]; 311 } 312 } 313 314 // If metadata is not found in the static cache, read it from the file. 315 if ( ! $metadata ) { 316 $metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) ); 317 } 318 295 319 if ( ! is_array( $metadata ) || empty( $metadata['name'] ) ) { 296 320 return false;
Note: See TracChangeset
for help on using the changeset viewer.