Skip to content

Commit

Permalink
Merge pull request #184 from jblz/patch-1
Browse files Browse the repository at this point in the history
Post Generate: Add support for post_date_gmt
  • Loading branch information
schlessera authored Jul 13, 2018
2 parents 943fd52 + f2ea4ad commit 9d3d891
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 1 deletion.
104 changes: 104 additions & 0 deletions features/post-generate.feature
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,107 @@ Feature: Generate new WordPress posts
howdy-3
"""
And STDERR should be empty

Scenario: Generating posts with post_date argument without time
When I run `wp post generate --count=1 --post_date="2018-07-01"`
And I run `wp post list --field=post_date`
Then STDOUT should contain:
"""
2018-07-01 00:00:00
"""
And I run `wp post list --field=post_date_gmt`
Then STDOUT should contain:
"""
2018-07-01 00:00:00
"""

Scenario: Generating posts with post_date argument with time
When I run `wp post generate --count=1 --post_date="2018-07-02 02:21:05"`
And I run `wp post list --field=post_date`
Then STDOUT should contain:
"""
2018-07-02 02:21:05
"""
And I run `wp post list --field=post_date_gmt`
Then STDOUT should contain:
"""
2018-07-02 02:21:05
"""

Scenario: Generating posts with post_date_gmt argument without time
When I run `wp post generate --count=1 --post_date_gmt="2018-07-03"`
And I run `wp post list --field=post_date`
Then STDOUT should contain:
"""
2018-07-03 00:00:00
"""
And I run `wp post list --field=post_date_gmt`
Then STDOUT should contain:
"""
2018-07-03 00:00:00
"""

Scenario: Generating posts with post_date_gmt argument with time
When I run `wp post generate --count=1 --post_date_gmt="2018-07-04 12:34:56"`
And I run `wp post list --field=post_date`
Then STDOUT should contain:
"""
2018-07-04 12:34:56
"""
And I run `wp post list --field=post_date_gmt`
Then STDOUT should contain:
"""
2018-07-04 12:34:56
"""

Scenario: Generating posts with post_date argument with hyphenated time
When I run `wp post generate --count=1 --post_date="2018-07-05-17:17:17"`
And I run `wp post list --field=post_date`
Then STDOUT should contain:
"""
2018-07-05 17:17:17
"""
And I run `wp post list --field=post_date_gmt`
Then STDOUT should contain:
"""
2018-07-05 17:17:17
"""

Scenario: Generating posts with post_date_gmt argument with hyphenated time
When I run `wp post generate --count=1 --post_date_gmt="2018-07-06-12:12:12"`
And I run `wp post list --field=post_date`
Then STDOUT should contain:
"""
2018-07-06 12:12:12
"""
And I run `wp post list --field=post_date_gmt`
Then STDOUT should contain:
"""
2018-07-06 12:12:12
"""

Scenario: Generating posts with different post_date & post_date_gmt argument without time
When I run `wp post generate --count=1 --post_date="1999-12-31" --post_date_gmt="2000-01-01"`
And I run `wp post list --field=post_date`
Then STDOUT should contain:
"""
1999-12-31 00:00:00
"""
And I run `wp post list --field=post_date_gmt`
Then STDOUT should contain:
"""
2000-01-01 00:00:00
"""

Scenario: Generating posts with different post_date & post_date_gmt argument with time
When I run `wp post generate --count=1 --post_date="1999-12-31 11:11:00" --post_date_gmt="2000-01-01 02:11:00"`
And I run `wp post list --field=post_date`
Then STDOUT should contain:
"""
1999-12-31 11:11:00
"""
And I run `wp post list --field=post_date_gmt`
Then STDOUT should contain:
"""
2000-01-01 02:11:00
"""
17 changes: 16 additions & 1 deletion src/Post_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,9 @@ public function list_( $_, $assoc_args ) {
* [--post_date=<yyyy-mm-dd-hh-ii-ss>]
* : The date of the generated posts. Default: current date
*
* [--post_date_gmt=<yyyy-mm-dd-hh-ii-ss>]
* : The GMT date of the generated posts. Default: value of post_date (or current date if it's not set)
*
* [--post_content]
* : If set, the command reads the post_content from STDIN.
*
Expand Down Expand Up @@ -680,12 +683,23 @@ public function generate( $args, $assoc_args ) {
'post_type' => 'post',
'post_status' => 'publish',
'post_author' => false,
'post_date' => current_time( 'mysql' ),
'post_date' => false,
'post_date_gmt' => false,
'post_content' => '',
'post_title' => '',
);
extract( array_merge( $defaults, $assoc_args ), EXTR_SKIP );

$call_time = current_time( 'mysql' );

if ( $post_date_gmt === false ) {
$post_date_gmt = $post_date ? $post_date : $call_time;
}

if ( $post_date === false ) {
$post_date = $post_date_gmt ? $post_date_gmt : $call_time;
}

// @codingStandardsIgnoreStart
if ( !post_type_exists( $post_type ) ) {
WP_CLI::error( sprintf( "'%s' is not a registered post type.", $post_type ) );
Expand Down Expand Up @@ -745,6 +759,7 @@ public function generate( $args, $assoc_args ) {
'post_parent' => $current_parent,
'post_name' => ! empty( $post_title ) ? sanitize_title( $post_title . ( $i === $total ) ? '' : '-$i' ) : "post-$i",
'post_date' => $post_date,
'post_date_gmt' => $post_date_gmt,
'post_content' => $post_content,
);

Expand Down

0 comments on commit 9d3d891

Please sign in to comment.