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
Add --preserve-filetime
argument to support persisting file modification time
#42
Changes from 5 commits
b5553d3
e020258
756d807
83e131b
ac34f99
cab9b02
81405e6
73db121
d63bfb0
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 |
---|---|---|
|
@@ -60,6 +60,27 @@ Feature: Manage WordPress attachments | |
And the {CACHE_DIR}/large-image.jpg file should exist | ||
And the return code should be 0 | ||
|
||
Scenario: Import a file as attachment from a local image and preserve the file modified time. | ||
Given download: | ||
| path | url | | ||
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg | | ||
And I run `touch -m 1505787257 {CACHE_DIR}/large-image.jpg` | ||
And I run `wp option update gmt_offset -5` | ||
And I run `wp media import {CACHE_DIR}/large-image.jpg --post_id=1 --filetime` | ||
And I run `wp post get 1 --field=post_date` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be |
||
Then STDOUT should be: | ||
""" | ||
1999-12-31 19:00:00 | ||
""" | ||
|
||
When I run `wp post get 1 --field=post_date_gmt` | ||
Then STDOUT should be: | ||
""" | ||
2000-01-01 00:00:00 | ||
""" | ||
|
||
And the return code should be 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
|
||
Scenario: Import a file as an attachment but porcelain style | ||
Given download: | ||
| path | url | | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -183,6 +183,10 @@ function regenerate( $args, $assoc_args = array() ) { | |
* [--porcelain] | ||
* : Output just the new attachment ID. | ||
* | ||
* [--filetime] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make this flag more specific? Possibly See https://make.wordpress.org/cli/handbook/philosophy/#readability-trumps-number-of-keystrokes |
||
* : Use the file modified time as the post published & modified dates. | ||
* Remote files will always use the current time. | ||
* | ||
* ## EXAMPLES | ||
* | ||
* # Import all jpgs in the current user's "Pictures" directory, not attached to any post. | ||
|
@@ -220,6 +224,9 @@ function import( $args, $assoc_args = array() ) { | |
// Assume the most generic term | ||
$noun = 'item'; | ||
|
||
// Current site's timezone offset. | ||
$gmt_offset = get_option( 'gmt_offset' ); | ||
|
||
// Use the noun `image` when sure the media file is an image | ||
if ( Utils\get_flag_value( $assoc_args, 'featured_image' ) || $assoc_args['alt'] ) { | ||
$noun = 'image'; | ||
|
@@ -238,6 +245,7 @@ function import( $args, $assoc_args = array() ) { | |
foreach ( $args as $file ) { | ||
$is_file_remote = parse_url( $file, PHP_URL_HOST ); | ||
$orig_filename = $file; | ||
$file_time = ''; | ||
|
||
if ( empty( $is_file_remote ) ) { | ||
if ( !file_exists( $file ) ) { | ||
|
@@ -251,6 +259,10 @@ function import( $args, $assoc_args = array() ) { | |
$tempfile = $this->make_copy( $file ); | ||
} | ||
$name = Utils\basename( $file ); | ||
|
||
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'filetime' ) ) { | ||
$file_time = @filemtime( $file ); | ||
} | ||
} else { | ||
$tempfile = download_url( $file ); | ||
if ( is_wp_error( $tempfile ) ) { | ||
|
@@ -269,11 +281,19 @@ function import( $args, $assoc_args = array() ) { | |
'name' => $name, | ||
); | ||
|
||
$post_array= array( | ||
$post_array = array( | ||
'post_title' => $assoc_args['title'], | ||
'post_excerpt' => $assoc_args['caption'], | ||
'post_content' => $assoc_args['desc'] | ||
'post_content' => $assoc_args['desc'], | ||
); | ||
|
||
if ( ! empty( $file_time ) ) { | ||
$post_array['post_date'] = gmdate( 'Y-m-d H:i:s', $file_time + ( $gmt_offset * HOUR_IN_SECONDS ) ); | ||
$post_array['post_date_gmt'] = gmdate( 'Y-m-d H:i:s', $file_time ); | ||
$post_array['post_modified'] = gmdate( 'Y-m-d H:i:s', $file_time + ( $gmt_offset * HOUR_IN_SECONDS ) ); | ||
$post_array['post_modified_gmt'] = gmdate( 'Y-m-d H:i:s', $file_time ); | ||
} | ||
|
||
$post_array = wp_slash( $post_array ); | ||
|
||
// use image exif/iptc data for title and caption defaults if possible | ||
|
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.
This should be
When I run
, and include aThen STDOUT should contain
statement