Skip to content

Commit

Permalink
Merge pull request #42 from desrosj/file-time-option-on-import
Browse files Browse the repository at this point in the history
Add `--preserve-filetime` argument to support persisting file modification time
  • Loading branch information
danielbachhuber committed Oct 12, 2017
2 parents 630747d + d63bfb0 commit 3ad78de
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
18 changes: 11 additions & 7 deletions README.md
Expand Up @@ -16,7 +16,7 @@ This package implements the following commands:
Create attachments from local files or URLs.

~~~
wp media import <file>... [--post_id=<post_id>] [--title=<title>] [--caption=<caption>] [--alt=<alt_text>] [--desc=<description>] [--skip-copy] [--featured_image] [--porcelain]
wp media import <file>... [--post_id=<post_id>] [--title=<title>] [--caption=<caption>] [--alt=<alt_text>] [--desc=<description>] [--skip-copy] [--preserve-filetime] [--featured_image] [--porcelain]
~~~

**OPTIONS**
Expand Down Expand Up @@ -44,6 +44,10 @@ wp media import <file>... [--post_id=<post_id>] [--title=<title>] [--caption=<ca
[--skip-copy]
If set, media files (local only) are imported to the library but not moved on disk.

[--preserve-filetime]
Use the file modified time as the post published & modified dates.
Remote files will always use the current time.

[--featured_image]
If set, set the imported image as the Featured Image of the post its attached to.

Expand Down Expand Up @@ -181,12 +185,12 @@ These fields will be displayed by default for each image size:
+---------------------------+-------+--------+-------+
| name | width | height | crop |
+---------------------------+-------+--------+-------+
| full | | | false |
| twentyfourteen-full-width | 1038 | 576 | true |
| large | 1024 | 1024 | true |
| post-thumbnail | 672 | 372 | true |
| medium | 300 | 300 | true |
| thumbnail | 150 | 150 | true |
| full | | | N/A |
| twentyfourteen-full-width | 1038 | 576 | hard |
| large | 1024 | 1024 | soft |
| medium_large | 768 | 0 | soft |
| medium | 300 | 300 | soft |
| thumbnail | 150 | 150 | hard |
+---------------------------+-------+--------+-------+

## Installing
Expand Down
22 changes: 22 additions & 0 deletions features/media-import.feature
Expand Up @@ -60,6 +60,28 @@ 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 `TZ=UTC touch -t 8001031305 {CACHE_DIR}/large-image.jpg`
And I run `wp option update gmt_offset -5`

When I run `wp media import {CACHE_DIR}/large-image.jpg --post_id=1 --preserve-filetime --porcelain`
Then save STDOUT as {ATTACH_ID}

And I run `wp post get {ATTACH_ID} --field=post_date`
Then STDOUT should be:
"""
1980-01-03 08:05:00
"""

When I run `wp post get {ATTACH_ID} --field=post_date_gmt`
Then STDOUT should be:
"""
1980-01-03 13:05:00
"""

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 @@ -172,6 +172,10 @@ function regenerate( $args, $assoc_args = array() ) {
* [--skip-copy]
* : If set, media files (local only) are imported to the library but not moved on disk.
*
* [--preserve-filetime]
* : Use the file modified time as the post published & modified dates.
* Remote files will always use the current time.
*
* [--featured_image]
* : If set, set the imported image as the Featured Image of the post its attached to.
*
Expand Down Expand Up @@ -215,6 +219,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 @@ -233,6 +240,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 @@ -246,6 +254,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, 'preserve-filetime' ) ) {
$file_time = @filemtime( $file );
}
} else {
$tempfile = download_url( $file );
if ( is_wp_error( $tempfile ) ) {
Expand All @@ -264,11 +276,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

0 comments on commit 3ad78de

Please sign in to comment.