Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions features/scaffold-block.feature
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ Feature: WordPress block code scaffolding
"""

Scenario: Scaffold a block for an invalid plugin slug
When I run `wp scaffold plugin plugin.name.with.dots`
And I try `wp scaffold block some-block --plugin=plugin.name.with.dots`
When I try `wp scaffold block some-block --plugin=plugin.name.with.dots`
Then STDERR should contain:
"""
Error: Invalid plugin name specified.
Expand Down
16 changes: 15 additions & 1 deletion features/scaffold-plugin-tests.feature
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,21 @@ Feature: Scaffold plugin unit tests
When I try `wp scaffold plugin-tests ../`
Then STDERR should be:
"""
Error: Invalid plugin slug specified. The target directory '{RUN_DIR}/wp-content/plugins/../' is not in '{RUN_DIR}/wp-content/plugins'.
Error: Invalid plugin slug specified. The slug can only contain alphanumeric characters, underscores, and dashes.
"""
And the return code should be 1

When I try `wp scaffold plugin-tests my-plugin/`
Then STDERR should be:
"""
Error: Invalid plugin slug specified. The slug can only contain alphanumeric characters, underscores, and dashes.
"""
And the return code should be 1

When I try `wp scaffold plugin-tests my-plugin\\`
Then STDERR should be:
"""
Error: Invalid plugin slug specified. The slug can only contain alphanumeric characters, underscores, and dashes.
"""
And the return code should be 1

Expand Down
16 changes: 15 additions & 1 deletion features/scaffold-theme-tests.feature
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,21 @@ Feature: Scaffold theme unit tests
When I try `wp scaffold theme-tests ../`
Then STDERR should be:
"""
Error: Invalid theme slug specified. The target directory '{RUN_DIR}/wp-content/themes/../' is not in '{RUN_DIR}/wp-content/themes'.
Error: Invalid theme slug specified. The slug can only contain alphanumeric characters, underscores, and dashes.
"""
And the return code should be 1

When I try `wp scaffold theme-tests t12child/`
Then STDERR should be:
"""
Error: Invalid theme slug specified. The slug can only contain alphanumeric characters, underscores, and dashes.
"""
And the return code should be 1

When I try `wp scaffold theme-tests t12child\\`
Then STDERR should be:
"""
Error: Invalid theme slug specified. The slug can only contain alphanumeric characters, underscores, and dashes.
"""
And the return code should be 1

Expand Down
11 changes: 11 additions & 0 deletions src/Scaffold_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -828,9 +828,13 @@

if ( ! empty( $args[0] ) ) {
$slug = $args[0];
// Validate slug contains only alphanumeric characters, underscores, and dashes.
if ( in_array( $slug, [ '.', '..' ], true ) ) {
WP_CLI::error( "Invalid {$type} slug specified. The slug cannot be '.' or '..'." );
}
if ( ! preg_match( '/^[a-zA-Z0-9_-]+$/', $slug ) ) {
WP_CLI::error( "Invalid {$type} slug specified. The slug can only contain alphanumeric characters, underscores, and dashes." );
}
if ( 'theme' === $type ) {
$theme = wp_get_theme( $slug );
if ( $theme->exists() ) {
Expand All @@ -841,11 +845,11 @@
} else {
$target_dir = WP_PLUGIN_DIR . "/{$slug}";
}
if ( empty( $assoc_args['dir'] ) && ! is_dir( $target_dir ) ) {

Check failure on line 848 in src/Scaffold_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Variable $target_dir might not be defined.
WP_CLI::error( "Invalid {$type} slug specified. No such target directory '{$target_dir}'." );

Check failure on line 849 in src/Scaffold_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Variable $target_dir might not be defined.
}

$error_msg = $this->check_target_directory( $type, $target_dir );

Check failure on line 852 in src/Scaffold_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Variable $target_dir might not be defined.
if ( ! empty( $error_msg ) ) {
WP_CLI::error( "Invalid {$type} slug specified. {$error_msg}" );
}
Expand All @@ -858,6 +862,13 @@
}
if ( empty( $slug ) ) {
$slug = Utils\basename( $target_dir );
// Validate derived slug as well.
if ( in_array( $slug, [ '.', '..' ], true ) ) {
WP_CLI::error( "Invalid {$type} slug specified. The slug cannot be '.' or '..'." );
}
if ( ! preg_match( '/^[a-zA-Z0-9_-]+$/', $slug ) ) {
WP_CLI::error( "Invalid {$type} slug specified. The slug can only contain alphanumeric characters, underscores, and dashes." );
}
}
}

Expand All @@ -865,19 +876,19 @@
WP_CLI::error( "Invalid {$type} specified." );
}

$name = ucwords( str_replace( '-', ' ', $slug ) );

Check failure on line 879 in src/Scaffold_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Variable $slug might not be defined.
$package = str_replace( ' ', '_', $name );

$tests_dir = "{$target_dir}/tests";

Check failure on line 882 in src/Scaffold_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Variable $target_dir might not be defined.
$bin_dir = "{$target_dir}/bin";

Check failure on line 883 in src/Scaffold_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Variable $target_dir might not be defined.

$wp_filesystem->mkdir( $tests_dir );
$wp_filesystem->mkdir( $bin_dir );

$wp_versions_to_test = [];
// Parse plugin readme.txt
if ( file_exists( "{$target_dir}/readme.txt" ) ) {

Check failure on line 890 in src/Scaffold_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Variable $target_dir might not be defined.
$readme_content = (string) file_get_contents( "{$target_dir}/readme.txt" );

Check failure on line 891 in src/Scaffold_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Variable $target_dir might not be defined.

preg_match( '/Requires at least\:(.*)\n/m', $readme_content, $matches );
if ( isset( $matches[1] ) && $matches[1] ) {
Expand All @@ -887,7 +898,7 @@
$wp_versions_to_test[] = 'latest';
$wp_versions_to_test[] = 'trunk';

$main_file = "{$slug}.php";

Check failure on line 901 in src/Scaffold_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Variable $slug might not be defined.

if ( 'plugin' === $type ) {
if ( ! function_exists( 'get_plugins' ) ) {
Expand All @@ -899,7 +910,7 @@
if ( ! empty( $all_plugins ) ) {
$filtered = array_filter(
array_keys( $all_plugins ),
static function ( $item ) use ( $slug ) {

Check failure on line 913 in src/Scaffold_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPStan

Variable $slug might not be defined.
return ( false !== strpos( $item, "{$slug}/" ) );
}
);
Expand Down