Skip to content

Commit

Permalink
Improve error handling by following register from the WP_Block_Type_R…
Browse files Browse the repository at this point in the history
…egistry
  • Loading branch information
gziolo committed May 22, 2020
1 parent fd11fa0 commit d69772f
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function register_block_type_from_metadata( $path, $args = array() ) {
return false;
}

$settings = array();
$property_mappings = array(
'title' => 'title',
'category' => 'category',
Expand All @@ -47,15 +48,14 @@ function register_block_type_from_metadata( $path, $args = array() ) {
// Alias recommended in documentation to distinguish from assets.
'styleVariations' => 'styles',
);
$settings = array();

foreach ( $property_mappings as $key => $mapped_key ) {
if ( isset( $metadata[ $key ] ) ) {
$settings[ $mapped_key ] = $metadata[ $key ];
}
}

$block_name = $metadata['name'];
$block_name = $metadata['name'];
$block_dir = dirname( $metadata_file );
$asset_handle_prefix = str_replace( '/', '-', $block_name );

Expand All @@ -66,9 +66,10 @@ function register_block_type_from_metadata( $path, $args = array() ) {
$block_dir . '/' . substr_replace( $editor_script, '.asset.php', -3 )
);
if ( ! file_exists( $editor_script_asset_path ) ) {
throw new Error(
"The asset file for the \"editorScript\" defined in \"$block_name\" block definition is missing."
);
/* translators: %s: Block name. */
$message = sprintf( __( 'The asset file for the "editorScript" defined in "%s" block definition is missing.' ), $block_name );
_doing_it_wrong( __METHOD__, $message, '5.5.0' );
return false;
}
$editor_script_asset = require( $editor_script_asset_path );
wp_register_script(
Expand All @@ -87,9 +88,10 @@ function register_block_type_from_metadata( $path, $args = array() ) {
$block_dir . '/' . substr_replace( $script, '.asset.php', -3 )
);
if ( ! file_exists( $script_asset_path ) ) {
throw new Error(
"The asset file for the \"script\" defined in \"$block_name\" block definition is missing."
);
/* translators: %s: Block name. */
$message = sprintf( __( 'The asset file for the "script" defined in "%s" block definition is missing.' ), $block_name );
_doing_it_wrong( __METHOD__, $message, '5.5.0' );
return false;
}
$script_asset = require( $script_asset_path );
wp_register_script(
Expand Down

0 comments on commit d69772f

Please sign in to comment.