Skip to content

Commit 9d3d891

Browse files
authored
Merge pull request #184 from jblz/patch-1
Post Generate: Add support for post_date_gmt
2 parents 943fd52 + f2ea4ad commit 9d3d891

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

features/post-generate.feature

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,107 @@ Feature: Generate new WordPress posts
6060
howdy-3
6161
"""
6262
And STDERR should be empty
63+
64+
Scenario: Generating posts with post_date argument without time
65+
When I run `wp post generate --count=1 --post_date="2018-07-01"`
66+
And I run `wp post list --field=post_date`
67+
Then STDOUT should contain:
68+
"""
69+
2018-07-01 00:00:00
70+
"""
71+
And I run `wp post list --field=post_date_gmt`
72+
Then STDOUT should contain:
73+
"""
74+
2018-07-01 00:00:00
75+
"""
76+
77+
Scenario: Generating posts with post_date argument with time
78+
When I run `wp post generate --count=1 --post_date="2018-07-02 02:21:05"`
79+
And I run `wp post list --field=post_date`
80+
Then STDOUT should contain:
81+
"""
82+
2018-07-02 02:21:05
83+
"""
84+
And I run `wp post list --field=post_date_gmt`
85+
Then STDOUT should contain:
86+
"""
87+
2018-07-02 02:21:05
88+
"""
89+
90+
Scenario: Generating posts with post_date_gmt argument without time
91+
When I run `wp post generate --count=1 --post_date_gmt="2018-07-03"`
92+
And I run `wp post list --field=post_date`
93+
Then STDOUT should contain:
94+
"""
95+
2018-07-03 00:00:00
96+
"""
97+
And I run `wp post list --field=post_date_gmt`
98+
Then STDOUT should contain:
99+
"""
100+
2018-07-03 00:00:00
101+
"""
102+
103+
Scenario: Generating posts with post_date_gmt argument with time
104+
When I run `wp post generate --count=1 --post_date_gmt="2018-07-04 12:34:56"`
105+
And I run `wp post list --field=post_date`
106+
Then STDOUT should contain:
107+
"""
108+
2018-07-04 12:34:56
109+
"""
110+
And I run `wp post list --field=post_date_gmt`
111+
Then STDOUT should contain:
112+
"""
113+
2018-07-04 12:34:56
114+
"""
115+
116+
Scenario: Generating posts with post_date argument with hyphenated time
117+
When I run `wp post generate --count=1 --post_date="2018-07-05-17:17:17"`
118+
And I run `wp post list --field=post_date`
119+
Then STDOUT should contain:
120+
"""
121+
2018-07-05 17:17:17
122+
"""
123+
And I run `wp post list --field=post_date_gmt`
124+
Then STDOUT should contain:
125+
"""
126+
2018-07-05 17:17:17
127+
"""
128+
129+
Scenario: Generating posts with post_date_gmt argument with hyphenated time
130+
When I run `wp post generate --count=1 --post_date_gmt="2018-07-06-12:12:12"`
131+
And I run `wp post list --field=post_date`
132+
Then STDOUT should contain:
133+
"""
134+
2018-07-06 12:12:12
135+
"""
136+
And I run `wp post list --field=post_date_gmt`
137+
Then STDOUT should contain:
138+
"""
139+
2018-07-06 12:12:12
140+
"""
141+
142+
Scenario: Generating posts with different post_date & post_date_gmt argument without time
143+
When I run `wp post generate --count=1 --post_date="1999-12-31" --post_date_gmt="2000-01-01"`
144+
And I run `wp post list --field=post_date`
145+
Then STDOUT should contain:
146+
"""
147+
1999-12-31 00:00:00
148+
"""
149+
And I run `wp post list --field=post_date_gmt`
150+
Then STDOUT should contain:
151+
"""
152+
2000-01-01 00:00:00
153+
"""
154+
155+
Scenario: Generating posts with different post_date & post_date_gmt argument with time
156+
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"`
157+
And I run `wp post list --field=post_date`
158+
Then STDOUT should contain:
159+
"""
160+
1999-12-31 11:11:00
161+
"""
162+
And I run `wp post list --field=post_date_gmt`
163+
Then STDOUT should contain:
164+
"""
165+
2000-01-01 02:11:00
166+
"""

src/Post_Command.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,9 @@ public function list_( $_, $assoc_args ) {
634634
* [--post_date=<yyyy-mm-dd-hh-ii-ss>]
635635
* : The date of the generated posts. Default: current date
636636
*
637+
* [--post_date_gmt=<yyyy-mm-dd-hh-ii-ss>]
638+
* : The GMT date of the generated posts. Default: value of post_date (or current date if it's not set)
639+
*
637640
* [--post_content]
638641
* : If set, the command reads the post_content from STDIN.
639642
*
@@ -680,12 +683,23 @@ public function generate( $args, $assoc_args ) {
680683
'post_type' => 'post',
681684
'post_status' => 'publish',
682685
'post_author' => false,
683-
'post_date' => current_time( 'mysql' ),
686+
'post_date' => false,
687+
'post_date_gmt' => false,
684688
'post_content' => '',
685689
'post_title' => '',
686690
);
687691
extract( array_merge( $defaults, $assoc_args ), EXTR_SKIP );
688692

693+
$call_time = current_time( 'mysql' );
694+
695+
if ( $post_date_gmt === false ) {
696+
$post_date_gmt = $post_date ? $post_date : $call_time;
697+
}
698+
699+
if ( $post_date === false ) {
700+
$post_date = $post_date_gmt ? $post_date_gmt : $call_time;
701+
}
702+
689703
// @codingStandardsIgnoreStart
690704
if ( !post_type_exists( $post_type ) ) {
691705
WP_CLI::error( sprintf( "'%s' is not a registered post type.", $post_type ) );
@@ -745,6 +759,7 @@ public function generate( $args, $assoc_args ) {
745759
'post_parent' => $current_parent,
746760
'post_name' => ! empty( $post_title ) ? sanitize_title( $post_title . ( $i === $total ) ? '' : '-$i' ) : "post-$i",
747761
'post_date' => $post_date,
762+
'post_date_gmt' => $post_date_gmt,
748763
'post_content' => $post_content,
749764
);
750765

0 commit comments

Comments
 (0)