Automattic\WooCommerce\Admin\Features\ProductBlockEditor
BlockRegistry::register_block_type_from_metadata()
Register a block type from metadata stored in the block.json file.
Method of the class: BlockRegistry{}
No Hooks.
Return
\WP_Block_Type|false
. The registered block type on success, or false on failure.
Usage
$BlockRegistry = new BlockRegistry(); $BlockRegistry->register_block_type_from_metadata( $file_or_folder );
- $file_or_folder(string) (required)
- Path to the JSON file with metadata definition for the block or path to the folder where the block.json file is located.
BlockRegistry::register_block_type_from_metadata() BlockRegistry::register block type from metadata code WC 9.7.1
public function register_block_type_from_metadata( $file_or_folder ) { $metadata_file = ( ! str_ends_with( $file_or_folder, 'block.json' ) ) ? trailingslashit( $file_or_folder ) . 'block.json' : $file_or_folder; if ( ! file_exists( $metadata_file ) ) { return false; } // We are dealing with a local file, so we can use file_get_contents. // phpcs:disable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents $metadata = json_decode( file_get_contents( $metadata_file ), true ); if ( ! is_array( $metadata ) || ! $metadata['name'] ) { return false; } $this->unregister( $metadata['name'] ); return register_block_type_from_metadata( $metadata_file, array( 'attributes' => $this->augment_attributes( isset( $metadata['attributes'] ) ? $metadata['attributes'] : array() ), 'uses_context' => $this->augment_uses_context( isset( $metadata['usesContext'] ) ? $metadata['usesContext'] : array() ), ) ); }