Added feature to import media into a specific directory#202
Added feature to import media into a specific directory#202eri-trabiccolo wants to merge 9 commits intowp-cli:mainfrom
Conversation
Yes it is |
|
thanks @swissspidy it looked like but I wasn't sure :) Oh ok, I see now the gh workflow :) |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Pull request overview
This pull request adds a --destdir parameter to the wp media import command, allowing users to specify a custom directory for uploaded media files instead of using the default WordPress uploads directory. This addresses issue #146 and wp-cli/wp-cli#5955.
Changes:
- Added
--destdirparameter documentation to the import command - Implemented upload directory filtering to redirect imported files to the specified directory
- Added Behat test scenarios to verify the functionality with both relative and absolute paths
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
| src/Media_Command.php | Added --destdir parameter, implemented add_upload_dir_filter and remove_upload_dir_filter methods, and integrated the filter into the import workflow |
| features/media-import.feature | Added test scenarios for importing files with custom directory (relative and absolute paths) |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| return $custom_upload_dir; | ||
| }; | ||
|
|
||
| add_filter( 'upload_dir', $custom_upload_dir_filter, PHP_INT_MAX, 0 ); |
There was a problem hiding this comment.
The add_upload_dir_filter method does not return the filter callback, but the calling code expects it to return a value that can be passed to remove_upload_dir_filter. Add a return statement at the end of the method to return the $custom_upload_dir_filter closure.
| add_filter( 'upload_dir', $custom_upload_dir_filter, PHP_INT_MAX, 0 ); | |
| add_filter( 'upload_dir', $custom_upload_dir_filter, PHP_INT_MAX, 0 ); | |
| return $custom_upload_dir_filter; |
| if ( ! empty( $custom_upload_dir_filter ) ) { | ||
| $this->remove_upload_dir_filter( $custom_upload_dir_filter ); | ||
| } |
There was a problem hiding this comment.
The variable $custom_upload_dir_filter is referenced here but may not be defined if the destdir condition was never met in the loop above (line 421-422). This will cause an undefined variable warning in PHP. The variable should be initialized before the foreach loop starts.
| Scenario: Upload files into a custom directory, relative to ABSPATH, when --destdir flag is applied. | ||
| Given download: | ||
| | path | url | | ||
| | {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg | | ||
| When I run `wp media import --destdir="foo" {CACHE_DIR}/large-image.jpg --porcelain=url` | ||
|
|
||
| Then STDOUT should not contain: | ||
| """ | ||
| https://example.com/wp-content/uploads/ | ||
| """ | ||
|
|
||
| And STDOUT should contain: | ||
| """ | ||
| https://example.com/foo/large-image.jpg | ||
| """ | ||
|
|
||
| Scenario: Upload files into a custom directory, not relative to ABSPATH, when --destdir flag is applied. | ||
| Given download: | ||
| | path | url | | ||
| | {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg | | ||
| When I run `wp media import --destdir="{RUN_DIR}/foo" {CACHE_DIR}/large-image.jpg --porcelain=url` | ||
|
|
||
| Then STDOUT should not contain: | ||
| """ | ||
| https://example.com/wp-content/uploads/ | ||
| """ | ||
|
|
||
| And STDOUT should contain: | ||
| """ | ||
| /foo/large-image.jpg | ||
| """ |
There was a problem hiding this comment.
The test scenarios have gaps in coverage for the new --destdir feature:
- No test for importing multiple files with --destdir to ensure the feature works correctly when processing multiple files in a single command
- No test for importing remote files (URLs) with --destdir to verify the feature works for both local and remote files
Consider adding test cases to cover these scenarios.
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
possible fix for #146
Related wp-cli/wp-cli#5955
Question: Is the README.md automatically generated, or shall I update it manually?