Changeset 59874
- Timestamp:
- 02/26/2025 07:38:08 PM (2 weeks ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks.php
r59866 r59874 374 374 375 375 return $i18n_block_schema; 376 } 377 378 /** 379 * Registers all block types from a block metadata collection. 380 * 381 * This can either reference a previously registered metadata collection or, if the `$manifest` parameter is provided, 382 * register the metadata collection directly within the same function call. 383 * 384 * @since 6.8.0 385 * @see wp_register_block_metadata_collection() 386 * @see register_block_type_from_metadata() 387 * 388 * @param string $path The absolute base path for the collection ( e.g., WP_PLUGIN_DIR . '/my-plugin/blocks/' ). 389 * @param string $manifest Optional. The absolute path to the manifest file containing the metadata collection, in 390 * order to register the collection. If this parameter is not provided, the `$path` parameter 391 * must reference a previously registered block metadata collection. 392 */ 393 function wp_register_block_types_from_metadata_collection( $path, $manifest = '' ) { 394 if ( $manifest ) { 395 wp_register_block_metadata_collection( $path, $manifest ); 396 } 397 398 $block_metadata_files = WP_Block_Metadata_Registry::get_collection_block_metadata_files( $path ); 399 foreach ( $block_metadata_files as $block_metadata_file ) { 400 register_block_type_from_metadata( $block_metadata_file ); 401 } 376 402 } 377 403 -
trunk/src/wp-includes/class-wp-block-metadata-registry.php
r59730 r59874 181 181 182 182 /** 183 * Gets the list of absolute paths to all block metadata files that are part of the given collection. 184 * 185 * For instance, if a block metadata collection is registered with path `WP_PLUGIN_DIR . '/my-plugin/blocks/'`, 186 * and the manifest file includes metadata for two blocks `'block-a'` and `'block-b'`, the result of this method 187 * will be an array containing: 188 * * `WP_PLUGIN_DIR . '/my-plugin/blocks/block-a/block.json'` 189 * * `WP_PLUGIN_DIR . '/my-plugin/blocks/block-b/block.json'` 190 * 191 * @since 6.8.0 192 * 193 * @param string $path The absolute base path for a previously registered collection. 194 * @return string[] List of block metadata file paths, or an empty array if the given `$path` is invalid. 195 */ 196 public static function get_collection_block_metadata_files( $path ) { 197 $path = wp_normalize_path( rtrim( $path, '/' ) ); 198 199 if ( ! isset( self::$collections[ $path ] ) ) { 200 _doing_it_wrong( 201 __METHOD__, 202 __( 'No registered block metadata collection was found for the provided path.' ), 203 '6.8.0' 204 ); 205 return array(); 206 } 207 208 $collection = &self::$collections[ $path ]; 209 210 if ( null === $collection['metadata'] ) { 211 // Load the manifest file if not already loaded. 212 $collection['metadata'] = require $collection['manifest']; 213 } 214 215 return array_map( 216 static function ( $block_name ) use ( $path ) { 217 return "{$path}/{$block_name}/block.json"; 218 }, 219 array_keys( $collection['metadata'] ) 220 ); 221 } 222 223 /** 183 224 * Finds the collection path for a given file or folder. 184 225 * -
trunk/tests/phpunit/tests/blocks/wpBlockMetadataRegistry.php
r59730 r59874 5 5 * 6 6 * @group blocks 7 * @coversDefaultClass WP_Block_Metadata_Registry 7 8 */ 8 9 class Tests_Blocks_WpBlockMetadataRegistry extends WP_UnitTestCase { … … 189 190 $this->assertFalse( $result, 'Non-existent manifest should not be registered' ); 190 191 } 192 193 /** 194 * Tests that the `get_collection_block_metadata_files()` method returns the expected list of block metadata files. 195 * 196 * @ticket 62267 197 * @covers ::get_collection_block_metadata_files 198 */ 199 public function test_get_collection_block_metadata_files() { 200 $path = WP_PLUGIN_DIR . '/test-plugin/data/block-types'; 201 $manifest_data = array( 202 'a-block' => array( 203 'name' => 'a-block', 204 'title' => 'A Block', 205 ), 206 'another-block' => array( 207 'name' => 'another-block', 208 'title' => 'Another Block', 209 ), 210 ); 211 212 file_put_contents( $this->temp_manifest_file, '<?php return ' . var_export( $manifest_data, true ) . ';' ); 213 214 $this->assertTrue( WP_Block_Metadata_Registry::register_collection( $path, $this->temp_manifest_file ) ); 215 $this->assertSame( 216 array( 217 WP_PLUGIN_DIR . '/test-plugin/data/block-types/a-block/block.json', 218 WP_PLUGIN_DIR . '/test-plugin/data/block-types/another-block/block.json', 219 ), 220 WP_Block_Metadata_Registry::get_collection_block_metadata_files( $path ) 221 ); 222 } 191 223 }
Note: See TracChangeset
for help on using the changeset viewer.