Skip to content

Commit bf9e9d7

Browse files
Copilotswissspidy
andcommitted
Add PHP glob pattern expansion to wp media import command
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent c5204e7 commit bf9e9d7

2 files changed

Lines changed: 50 additions & 4 deletions

File tree

features/media-import.feature

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,29 @@ Feature: Manage WordPress attachments
207207
Success: Imported 2 of 2 items.
208208
"""
209209

210+
Scenario: Import multiple images using a glob pattern
211+
Given download:
212+
| path | url |
213+
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
214+
And a {RUN_DIR}/images/ directory
215+
And I run `cp {CACHE_DIR}/large-image.jpg {RUN_DIR}/images/image1.jpg`
216+
And I run `cp {CACHE_DIR}/large-image.jpg {RUN_DIR}/images/image2.jpg`
217+
218+
When I run `wp media import '{RUN_DIR}/images/*.jpg'`
219+
Then STDOUT should contain:
220+
"""
221+
Success: Imported 2 of 2 items.
222+
"""
223+
224+
Scenario: Fail to import when a glob pattern matches no files
225+
When I try `wp media import '/nonexistent-dir/*.png'`
226+
Then STDERR should be:
227+
"""
228+
Warning: Pattern matched no files: '/nonexistent-dir/*.png'.
229+
Error: No items imported.
230+
"""
231+
And the return code should be 1
232+
210233
Scenario: Fail to import one image but continue trying the next
211234
When I try `wp media import gobbledygook.png 'http://wp-cli.org/behat-data/codeispoetry.png'`
212235
Then STDERR should contain:

src/Media_Command.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ public function regenerate( $args, $assoc_args = array() ) {
194194
* ## OPTIONS
195195
*
196196
* <file>...
197-
* : Path to file or files to be imported. Supports the glob(3) capabilities of the current shell.
197+
* : Path to file or files to be imported. Glob patterns (e.g. `dir/*.jpg`) are supported and
198+
* expanded by WP-CLI, so quoting the argument is recommended to prevent shell expansion.
198199
* If file is recognized as a URL (for example, with a scheme of http or ftp), the file will be
199200
* downloaded to a temp file before being sideloaded.
200201
*
@@ -246,7 +247,7 @@ public function regenerate( $args, $assoc_args = array() ) {
246247
* ## EXAMPLES
247248
*
248249
* # Import all jpgs in the current user's "Pictures" directory, not attached to any post.
249-
* $ wp media import ~/Pictures/**\/*.jpg
250+
* $ wp media import '~/Pictures/*.jpg'
250251
* Imported file '/home/person/Pictures/landscape-photo.jpg' as attachment ID 1751.
251252
* Imported file '/home/person/Pictures/fashion-icon.jpg' as attachment ID 1752.
252253
* Success: Imported 2 of 2 items.
@@ -312,6 +313,28 @@ public function import( $args, $assoc_args = array() ) {
312313
$assoc_args['post_id'] = false;
313314
}
314315

316+
$glob_errors = 0;
317+
$expanded_args = array();
318+
foreach ( $args as $arg ) {
319+
// phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url -- parse_url will only be used in absence of wp_parse_url.
320+
$is_remote = function_exists( 'wp_parse_url' ) ? wp_parse_url( $arg, PHP_URL_HOST ) : parse_url( $arg, PHP_URL_HOST );
321+
if ( empty( $is_remote ) && strpbrk( $arg, '*?[' ) !== false ) {
322+
$matches = glob( $arg );
323+
if ( false === $matches ) {
324+
WP_CLI::warning( "Unable to expand glob pattern '{$arg}'." );
325+
++$glob_errors;
326+
} elseif ( empty( $matches ) ) {
327+
WP_CLI::warning( "Pattern matched no files: '{$arg}'." );
328+
++$glob_errors;
329+
} else {
330+
$expanded_args = array_merge( $expanded_args, $matches );
331+
}
332+
} else {
333+
$expanded_args[] = $arg;
334+
}
335+
}
336+
$args = $expanded_args;
337+
315338
$number = 0;
316339
$successes = 0;
317340
$errors = 0;
@@ -491,8 +514,8 @@ public function import( $args, $assoc_args = array() ) {
491514

492515
// Report the result of the operation
493516
if ( ! Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
494-
Utils\report_batch_operation_results( $noun, 'import', count( $args ), $successes, $errors );
495-
} elseif ( $errors ) {
517+
Utils\report_batch_operation_results( $noun, 'import', count( $args ) + $glob_errors, $successes, $errors + $glob_errors );
518+
} elseif ( $errors || $glob_errors ) {
496519
WP_CLI::halt( 1 );
497520
}
498521
}

0 commit comments

Comments
 (0)