Skip to content

Commit 86ee481

Browse files
authored
Merge pull request #203 from kuoko/prevent-nonworking-gutenberg-blocks
Prevent creating unregisterable blocks due to invalid plugin slug
2 parents 919d493 + c39d899 commit 86ee481

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

features/scaffold-block.feature

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ Feature: WordPress block code scaffolding
3131
Error: Can't find 'unknown' plugin.
3232
"""
3333

34+
Scenario: Scaffold a block for an invalid plugin slug
35+
When I run `wp scaffold plugin plugin.name.with.dots`
36+
And I try `wp scaffold block some-block --plugin=plugin.name.with.dots`
37+
Then STDERR should contain:
38+
"""
39+
Error: Invalid plugin name specified.
40+
"""
41+
3442
Scenario: Scaffold a block for a specific plugin
3543
When I run `wp scaffold block the-green-mile --plugin=movies`
3644
Then the {PLUGIN_DIR}/blocks/the-green-mile.php file should exist
@@ -175,3 +183,4 @@ Feature: WordPress block code scaffolding
175183
"""
176184
plugins_url
177185
"""
186+

src/Scaffold_Command.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ public function block( $args, $assoc_args ) {
292292
);
293293
$control_args = $this->extract_args( $assoc_args, $control_defaults );
294294

295+
if ( isset( $control_args['plugin'] ) ) {
296+
if ( ! preg_match( '/^[A-Za-z0-9\-]*$/', $control_args['plugin'] ) ) {
297+
WP_CLI::error( 'Invalid plugin name specified. The block editor can only register blocks for plugins that have nothing but lowercase alphanumeric characters or dashes in their slug.' );
298+
}
299+
}
300+
295301
$data['namespace'] = $control_args['plugin'] ? $control_args['plugin'] : $this->get_theme_name( $control_args['theme'] );
296302
$data['machine_name'] = $this->generate_machine_name( $slug );
297303
$data['plugin'] = $control_args['plugin'] ? true : false;

0 commit comments

Comments
 (0)