-
Notifications
You must be signed in to change notification settings - Fork 41
Add PHP-level glob expansion to wp media import
#226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -194,7 +194,8 @@ public function regenerate( $args, $assoc_args = array() ) { | |||||||||
| * ## OPTIONS | ||||||||||
| * | ||||||||||
| * <file>... | ||||||||||
| * : Path to file or files to be imported. Supports the glob(3) capabilities of the current shell. | ||||||||||
| * : Path to file or files to be imported. Glob patterns (e.g. `dir/*.jpg`) are supported and | ||||||||||
| * expanded by WP-CLI, so quoting the argument is recommended to prevent shell expansion. | ||||||||||
| * If file is recognized as a URL (for example, with a scheme of http or ftp), the file will be | ||||||||||
| * downloaded to a temp file before being sideloaded. | ||||||||||
| * | ||||||||||
|
|
@@ -246,7 +247,7 @@ public function regenerate( $args, $assoc_args = array() ) { | |||||||||
| * ## EXAMPLES | ||||||||||
| * | ||||||||||
| * # Import all jpgs in the current user's "Pictures" directory, not attached to any post. | ||||||||||
| * $ wp media import ~/Pictures/**\/*.jpg | ||||||||||
| * $ wp media import '~/Pictures/*.jpg' | ||||||||||
|
||||||||||
| * $ wp media import '~/Pictures/*.jpg' | |
| * $ wp media import "$HOME/Pictures/*.jpg" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The glob() function in PHP does not expand the tilde (~) character to the user's home directory. The updated documentation example at line 250 now uses '~/Pictures/*.jpg', which will fail because the tilde is passed literally to glob(). To ensure this works as users would expect from the example, you should manually expand the tilde to the user's home directory path before calling glob().
if ( '~' === $arg[0] ) {
$home = getenv( 'HOME' );
if ( ! $home ) {
// For Windows compatibility.
$home = getenv( 'HOMEDRIVE' ) . getenv( 'HOMEPATH' );
}
if ( $home ) {
$arg = $home . substr( $arg, 1 );
}
}
$matches = glob( $arg );
Copilot
AI
Mar 2, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using array_merge() to append $matches can cause large array copies and extra peak memory usage when expanding patterns that match thousands of files (the main use case for this change). Consider appending the $matches elements onto $expanded_args in-place (e.g., loop/push) to reduce memory churn.
| $expanded_args = array_merge( $expanded_args, $matches ); | |
| foreach ( $matches as $match ) { | |
| $expanded_args[] = $match; | |
| } |
Uh oh!
There was an error while loading. Please reload this page.