-
Notifications
You must be signed in to change notification settings - Fork 39
Added feature to import media into a specific directory #202
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
base: main
Are you sure you want to change the base?
Changes from all commits
33453b0
974cb66
55ff707
bc14a34
d737db2
85968f3
dbe9bb5
b6979f6
21faa53
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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -216,7 +216,13 @@ public function regenerate( $args, $assoc_args = array() ) { | |||||||||
| * | ||||||||||
| * [--skip-copy] | ||||||||||
| * : If set, media files (local only) are imported to the library but not moved on disk. | ||||||||||
| * File names will not be run through wp_unique_filename() with this set. | ||||||||||
| * File names will not be run through wp_unique_filename() with this set. When used, files | ||||||||||
| * will remain at their current location and will not be copied into any destination directory. | ||||||||||
| * | ||||||||||
| * [--destdir=<destdir>] | ||||||||||
| * : Path to the destination directory for uploaded imported files. | ||||||||||
| * Can be absolute or relative to ABSPATH. Ignored when used together with --skip-copy, as | ||||||||||
| * files are not moved on disk in that case. | ||||||||||
| * | ||||||||||
| * [--preserve-filetime] | ||||||||||
| * : Use the file modified time as the post published & modified dates. | ||||||||||
|
|
@@ -272,6 +278,7 @@ public function import( $args, $assoc_args = array() ) { | |||||||||
| 'alt' => '', | ||||||||||
| 'desc' => '', | ||||||||||
| 'post_name' => '', | ||||||||||
| 'destdir' => '', | ||||||||||
| ) | ||||||||||
| ); | ||||||||||
|
|
||||||||||
|
|
@@ -411,6 +418,12 @@ public function import( $args, $assoc_args = array() ) { | |||||||||
| } | ||||||||||
| wp_update_attachment_metadata( $success, wp_generate_attachment_metadata( $success, $file ) ); | ||||||||||
| } else { | ||||||||||
|
|
||||||||||
| $destdir = Utils\get_flag_value( $assoc_args, 'destdir' ); | ||||||||||
| if ( is_string( $destdir ) && $destdir && ! isset( $custom_upload_dir_filter ) ) { | ||||||||||
| $custom_upload_dir_filter = $this->add_upload_dir_filter( $destdir ); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Deletes the temporary file. | ||||||||||
| $success = media_handle_sideload( $file_array, $assoc_args['post_id'], $assoc_args['title'], $post_array ); | ||||||||||
| if ( is_wp_error( $success ) ) { | ||||||||||
|
|
@@ -468,6 +481,10 @@ public function import( $args, $assoc_args = array() ) { | |||||||||
| ++$successes; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if ( ! empty( $custom_upload_dir_filter ) ) { | ||||||||||
| $this->remove_upload_dir_filter( $custom_upload_dir_filter ); | ||||||||||
| } | ||||||||||
|
Comment on lines
+484
to
+486
|
||||||||||
|
|
||||||||||
| // Report the result of the operation | ||||||||||
| if ( ! Utils\get_flag_value( $assoc_args, 'porcelain' ) ) { | ||||||||||
| Utils\report_batch_operation_results( $noun, 'import', count( $args ), $successes, $errors ); | ||||||||||
|
|
@@ -939,6 +956,45 @@ private function remove_image_size_filters( $image_size_filters ) { | |||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private function add_upload_dir_filter( $upload_dir ) { | ||||||||||
|
|
||||||||||
| $custom_upload_dir_filter = function () use ( $upload_dir ) { | ||||||||||
| static $custom_upload_dir; | ||||||||||
| if ( $custom_upload_dir ) { | ||||||||||
| return $custom_upload_dir; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if ( 0 !== strpos( $upload_dir, ABSPATH ) ) { | ||||||||||
| // $dir is absolute, $upload_dir is (maybe) relative to ABSPATH. | ||||||||||
| $dir = path_join( ABSPATH, $upload_dir ); | ||||||||||
| } else { | ||||||||||
| $dir = $upload_dir; | ||||||||||
| // normalize $upload_dir. | ||||||||||
| $upload_dir = substr( $upload_dir, strlen( ABSPATH ) ); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| $siteurl = get_option( 'siteurl' ); | ||||||||||
| $url = trailingslashit( $siteurl ) . $upload_dir; | ||||||||||
|
|
||||||||||
| $custom_upload_dir = array( | ||||||||||
| 'path' => $dir, | ||||||||||
| 'url' => $url, | ||||||||||
| 'subdir' => '', | ||||||||||
| 'basedir' => $dir, | ||||||||||
| 'baseurl' => $url, | ||||||||||
| 'error' => false, | ||||||||||
| ); | ||||||||||
|
|
||||||||||
| return $custom_upload_dir; | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| add_filter( 'upload_dir', $custom_upload_dir_filter, PHP_INT_MAX, 0 ); | ||||||||||
|
||||||||||
| 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; |
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 test scenarios have gaps in coverage for the new --destdir feature:
Consider adding test cases to cover these scenarios.