Automattic\WooCommerce\Admin\Features\ProductBlockEditor
BlockRegistry::register_block_type_from_metadata
Register a block type from metadata stored in the block.json file.
Метод класса: BlockRegistry{}
Хуков нет.
Возвращает
\WP_Block_Type|false. The registered block type on success, or false on failure.
Использование
$BlockRegistry = new BlockRegistry(); $BlockRegistry->register_block_type_from_metadata( $file_or_folder );
- $file_or_folder(строка) (обязательный)
- Path to the JSON file with metadata definition for the block or path to the folder where the
block.jsonfile is located.
Код BlockRegistry::register_block_type_from_metadata() BlockRegistry::register block type from metadata WC 10.4.3
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() ),
)
);
}