Skip to content

Commit e2c0567

Browse files
committed
Rename var, fix filter callback
1 parent 00ec596 commit e2c0567

2 files changed

Lines changed: 45 additions & 51 deletions

File tree

features/media-import.feature

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,11 @@ Feature: Manage WordPress attachments
287287
Error: Invalid value for <porcelain>: invalid. Expected flag or 'url'.
288288
"""
289289

290-
Scenario: Upload files into a custom directory, relative to ABSPATH, when --destdir flag is applied.
290+
Scenario: Upload files into a custom directory, relative to ABSPATH, when --destination-dir flag is applied.
291291
Given download:
292292
| path | url |
293293
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
294-
When I run `wp media import --destdir="foo" {CACHE_DIR}/large-image.jpg --porcelain=url`
294+
When I run `wp media import --destination-dir="foo" {CACHE_DIR}/large-image.jpg --porcelain=url`
295295

296296
Then STDOUT should not contain:
297297
"""
@@ -303,11 +303,11 @@ Feature: Manage WordPress attachments
303303
https://example.com/foo/large-image.jpg
304304
"""
305305

306-
Scenario: Upload files into a custom directory, not relative to ABSPATH, when --destdir flag is applied.
306+
Scenario: Upload files into a custom directory, not relative to ABSPATH, when --destination-dir flag is applied.
307307
Given download:
308308
| path | url |
309309
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
310-
When I run `wp media import --destdir="{RUN_DIR}/foo" {CACHE_DIR}/large-image.jpg --porcelain=url`
310+
When I run `wp media import --destination-dir="{RUN_DIR}/foo" {CACHE_DIR}/large-image.jpg --porcelain=url`
311311

312312
Then STDOUT should not contain:
313313
"""

src/Media_Command.php

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class Media_Command extends WP_CLI_Command {
4949
*/
5050
const WP_CLEAR_OBJECT_CACHE_INTERVAL = 500;
5151

52+
/**
53+
* @var string|null
54+
*/
55+
private $destination_dir;
56+
5257
/**
5358
* Regenerates thumbnails for one or more attachments.
5459
*
@@ -219,7 +224,7 @@ public function regenerate( $args, $assoc_args = array() ) {
219224
* File names will not be run through wp_unique_filename() with this set. When used, files
220225
* will remain at their current location and will not be copied into any destination directory.
221226
*
222-
* [--destdir=<destdir>]
227+
* [--destination-dir=<destination-dir>]
223228
* : Path to the destination directory for uploaded imported files.
224229
* Can be absolute or relative to ABSPATH. Ignored when used together with --skip-copy, as
225230
* files are not moved on disk in that case.
@@ -272,13 +277,13 @@ public function import( $args, $assoc_args = array() ) {
272277
$assoc_args = wp_parse_args(
273278
$assoc_args,
274279
array(
275-
'file_name' => '',
276-
'title' => '',
277-
'caption' => '',
278-
'alt' => '',
279-
'desc' => '',
280-
'post_name' => '',
281-
'destdir' => '',
280+
'file_name' => '',
281+
'title' => '',
282+
'caption' => '',
283+
'alt' => '',
284+
'desc' => '',
285+
'post_name' => '',
286+
'destination-dir' => '',
282287
)
283288
);
284289

@@ -419,9 +424,10 @@ public function import( $args, $assoc_args = array() ) {
419424
wp_update_attachment_metadata( $success, wp_generate_attachment_metadata( $success, $file ) );
420425
} else {
421426

422-
$destdir = Utils\get_flag_value( $assoc_args, 'destdir' );
423-
if ( is_string( $destdir ) && $destdir && ! isset( $custom_upload_dir_filter ) ) {
424-
$custom_upload_dir_filter = $this->add_upload_dir_filter( $destdir );
427+
$destdir = Utils\get_flag_value( $assoc_args, 'destination-dir' );
428+
if ( ! empty( $destdir ) ) {
429+
$this->destination_dir = $destdir;
430+
add_filter( 'upload_dir', [ $this, 'filter_upload_dir' ], PHP_INT_MAX );
425431
}
426432

427433
// Deletes the temporary file.
@@ -481,9 +487,7 @@ public function import( $args, $assoc_args = array() ) {
481487
++$successes;
482488
}
483489

484-
if ( ! empty( $custom_upload_dir_filter ) ) {
485-
$this->remove_upload_dir_filter( $custom_upload_dir_filter );
486-
}
490+
remove_filter( 'upload_dir', [ $this, 'filter_upload_dir' ], PHP_INT_MAX );
487491

488492
// Report the result of the operation
489493
if ( ! Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
@@ -956,43 +960,33 @@ private function remove_image_size_filters( $image_size_filters ) {
956960
}
957961
}
958962

959-
private function add_upload_dir_filter( $upload_dir ) {
960-
961-
$custom_upload_dir_filter = function () use ( $upload_dir ) {
962-
static $custom_upload_dir;
963-
if ( $custom_upload_dir ) {
964-
return $custom_upload_dir;
965-
}
966-
967-
if ( 0 !== strpos( $upload_dir, ABSPATH ) ) {
968-
// $dir is absolute, $upload_dir is (maybe) relative to ABSPATH.
969-
$dir = path_join( ABSPATH, $upload_dir );
970-
} else {
971-
$dir = $upload_dir;
972-
// normalize $upload_dir.
973-
$upload_dir = substr( $upload_dir, strlen( ABSPATH ) );
974-
}
975-
976-
$siteurl = get_option( 'siteurl' );
977-
$url = trailingslashit( $siteurl ) . $upload_dir;
963+
public function filter_upload_dir( $uploads ) {
964+
if ( ! $this->destination_dir ) {
965+
return $uploads;
966+
}
978967

979-
$custom_upload_dir = array(
980-
'path' => $dir,
981-
'url' => $url,
982-
'subdir' => '',
983-
'basedir' => $dir,
984-
'baseurl' => $url,
985-
'error' => false,
986-
);
968+
$upload_dir = $this->destination_dir;
987969

988-
return $custom_upload_dir;
989-
};
970+
if ( 0 !== strpos( $this->destination_dir, ABSPATH ) ) {
971+
// $dir is absolute, $upload_dir is (maybe) relative to ABSPATH.
972+
$dir = path_join( ABSPATH, $this->destination_dir );
973+
} else {
974+
$dir = $this->destination_dir;
975+
// normalize $upload_dir.
976+
$upload_dir = substr( $this->destination_dir, strlen( ABSPATH ) );
977+
}
990978

991-
add_filter( 'upload_dir', $custom_upload_dir_filter, PHP_INT_MAX, 0 );
992-
}
979+
$siteurl = get_option( 'siteurl' );
980+
$url = trailingslashit( $siteurl ) . $upload_dir;
993981

994-
private function remove_upload_dir_filter( $upload_dir_filter ) {
995-
remove_filter( 'upload_dir', $upload_dir_filter, PHP_INT_MAX );
982+
return [
983+
'path' => $this->destination_dir,
984+
'url' => $url,
985+
'subdir' => '',
986+
'basedir' => $this->destination_dir,
987+
'baseurl' => $url,
988+
'error' => false,
989+
];
996990
}
997991

998992
// Update attachment sizes metadata just for a particular intermediate image size.

0 commit comments

Comments
 (0)