From 3dc06743c14b0dbb7328d8ce342feacf8be582b8 Mon Sep 17 00:00:00 2001 From: Jeff Bowen Date: Wed, 2 May 2018 22:18:39 -0400 Subject: [PATCH 1/4] Post Generate: Add support for post_date_gmt Set the GMT date of the generated posts. Default: value of post_date (or current date if it's not set) --- src/Post_Command.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Post_Command.php b/src/Post_Command.php index 9f811d06c..33b30d6b6 100644 --- a/src/Post_Command.php +++ b/src/Post_Command.php @@ -634,6 +634,9 @@ public function list_( $_, $assoc_args ) { * [--post_date=] * : The date of the generated posts. Default: current date * + * [--post_date_gmt=] + * : 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. * @@ -681,11 +684,16 @@ public function generate( $args, $assoc_args ) { 'post_status' => 'publish', 'post_author' => false, 'post_date' => current_time( 'mysql' ), + 'post_date_gmt' => false, 'post_content' => '', 'post_title' => '', ); extract( array_merge( $defaults, $assoc_args ), EXTR_SKIP ); + if ( $post_date_gmt === false ) { + $post_date_gmt = $post_date; + } + // @codingStandardsIgnoreStart if ( !post_type_exists( $post_type ) ) { WP_CLI::error( sprintf( "'%s' is not a registered post type.", $post_type ) ); @@ -745,6 +753,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, ); From 26034db293e03e623373a7689ba8807e63606daa Mon Sep 17 00:00:00 2001 From: Jeff Bowen Date: Tue, 3 Jul 2018 22:21:52 -0400 Subject: [PATCH 2/4] Add some test scenarios & make them actually pass ;) --- features/post-generate.feature | 52 ++++++++++++++++++++++++++++++++++ src/Post_Command.php | 10 +++++-- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/features/post-generate.feature b/features/post-generate.feature index 3c5e5dd5f..70192f99d 100644 --- a/features/post-generate.feature +++ b/features/post-generate.feature @@ -60,3 +60,55 @@ 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 00:00:00"` + And I run `wp post list --field=post_date` + Then STDOUT should contain: + """ + 2018-07-02 00:00:00 + """ + And I run `wp post list --field=post_date_gmt` + Then STDOUT should contain: + """ + 2018-07-02 00:00:00 + """ + + 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 00:00:00"` + And I run `wp post list --field=post_date` + Then STDOUT should contain: + """ + 2018-07-04 00:00:00 + """ + And I run `wp post list --field=post_date_gmt` + Then STDOUT should contain: + """ + 2018-07-04 00:00:00 + """ diff --git a/src/Post_Command.php b/src/Post_Command.php index 33b30d6b6..8f440c8ac 100644 --- a/src/Post_Command.php +++ b/src/Post_Command.php @@ -683,15 +683,21 @@ 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_gmt = $post_date ? $post_date : $call_time; + } + + if ( $post_date === false ) { + $post_date = $post_date_gmt ? $post_date_gmt : $call_time; } // @codingStandardsIgnoreStart From 2ed222e2fa7581334e315c3671c228babd42db8d Mon Sep 17 00:00:00 2001 From: Jeff Bowen Date: Tue, 3 Jul 2018 22:34:01 -0400 Subject: [PATCH 3/4] tests++ --- features/post-generate.feature | 52 ++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/features/post-generate.feature b/features/post-generate.feature index 70192f99d..00bc4c682 100644 --- a/features/post-generate.feature +++ b/features/post-generate.feature @@ -112,3 +112,55 @@ Feature: Generate new WordPress posts """ 2018-07-04 00:00:00 """ + + 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 + """ From f2ea4ad5fab9eb5cd0aad84585d537020bc27b90 Mon Sep 17 00:00:00 2001 From: Jeff Bowen Date: Wed, 4 Jul 2018 13:28:51 -0400 Subject: [PATCH 4/4] Specify non-zero times in tests --- features/post-generate.feature | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/features/post-generate.feature b/features/post-generate.feature index 00bc4c682..4786eb3b3 100644 --- a/features/post-generate.feature +++ b/features/post-generate.feature @@ -75,16 +75,16 @@ Feature: Generate new WordPress posts """ Scenario: Generating posts with post_date argument with time - When I run `wp post generate --count=1 --post_date="2018-07-02 00:00:00"` + 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 00:00:00 + 2018-07-02 02:21:05 """ And I run `wp post list --field=post_date_gmt` Then STDOUT should contain: """ - 2018-07-02 00:00:00 + 2018-07-02 02:21:05 """ Scenario: Generating posts with post_date_gmt argument without time @@ -101,16 +101,16 @@ Feature: Generate new WordPress posts """ Scenario: Generating posts with post_date_gmt argument with time - When I run `wp post generate --count=1 --post_date_gmt="2018-07-04 00:00:00"` + 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 00:00:00 + 2018-07-04 12:34:56 """ And I run `wp post list --field=post_date_gmt` Then STDOUT should contain: """ - 2018-07-04 00:00:00 + 2018-07-04 12:34:56 """ Scenario: Generating posts with post_date argument with hyphenated time