Skip to content
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

Merged
merged 9 commits into from Oct 12, 2017
21 changes: 21 additions & 0 deletions features/media-import.feature
Expand Up @@ -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`
Copy link
Member

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 a Then STDOUT should contain statement

And I run `wp post get 1 --field=post_date`
Copy link
Member

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

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This the return code... statement is superfluous


Scenario: Import a file as an attachment but porcelain style
Given download:
| path | url |
Expand Down
24 changes: 22 additions & 2 deletions src/Media_Command.php
Expand Up @@ -183,6 +183,10 @@ function regenerate( $args, $assoc_args = array() ) {
* [--porcelain]
* : Output just the new attachment ID.
*
* [--filetime]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this flag more specific? Possibly --use-filetime-for-date?

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.
Expand Down Expand Up @@ -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';
Expand All @@ -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 ) ) {
Expand All @@ -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 ) ) {
Expand All @@ -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
Expand Down