@@ -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