@@ -702,10 +702,10 @@ function ( $post ) {
702
702
* ---
703
703
*
704
704
* [--post_date=<yyyy-mm-dd-hh-ii-ss>]
705
- * : The date of the generated posts . Default: current date
705
+ * : The date of the post . Default is the current time.
706
706
*
707
707
* [--post_date_gmt=<yyyy-mm-dd-hh-ii-ss>]
708
- * : The GMT date of the generated posts. Default: value of post_date (or current date if it's not set)
708
+ * : The date of the post in the GMT timezone. Default is the value of --post_date.
709
709
*
710
710
* [--post_content]
711
711
* : If set, the command reads the post_content from STDIN.
@@ -753,22 +753,36 @@ public function generate( $args, $assoc_args ) {
753
753
'post_type ' => 'post ' ,
754
754
'post_status ' => 'publish ' ,
755
755
'post_author ' => false ,
756
- 'post_date ' => false ,
757
- 'post_date_gmt ' => false ,
756
+ 'post_date ' => '' ,
757
+ 'post_date_gmt ' => '' ,
758
758
'post_content ' => '' ,
759
759
'post_title ' => '' ,
760
760
];
761
761
762
762
$ post_data = array_merge ( $ defaults , $ assoc_args );
763
763
764
- $ call_time = current_time ( 'mysql ' );
764
+ $ post_data ['post_date ' ] = $ this ->maybe_convert_hyphenated_date_format ( $ post_data ['post_date ' ] );
765
+ $ post_data ['post_date_gmt ' ] = $ this ->maybe_convert_hyphenated_date_format ( $ post_data ['post_date_gmt ' ] );
766
+
767
+ // Add time if the string is a valid date without time.
768
+ $ date = DateTime::createFromFormat ( 'Y-m-d ' , $ post_data ['post_date ' ] );
769
+ $ date = DateTime::createFromFormat ( 'Y-m-d ' , $ post_data ['post_date ' ] );
770
+ if ( $ date && $ date ->format ( 'Y-m-d ' ) === $ post_data ['post_date ' ] ) {
771
+ $ post_data ['post_date ' ] .= ' 00:00:00 ' ;
772
+ }
773
+
774
+ $ date_gmt = DateTime::createFromFormat ( 'Y-m-d ' , $ post_data ['post_date_gmt ' ] );
775
+ if ( $ date_gmt && $ date_gmt ->format ( 'Y-m-d ' ) === $ post_data ['post_date_gmt ' ] ) {
776
+ $ post_data ['post_date_gmt ' ] .= ' 00:00:00 ' ;
777
+ }
765
778
766
- if ( false === $ post_data ['post_date_gmt ' ] ) {
767
- $ post_data ['post_date_gmt ' ] = $ post_data ['post_date ' ] ?: $ call_time ;
779
+ // In older WordPress versions, wp_insert_post post dates default to the current time when a value is absent. We need to send a value for post_date_gmt if post_date is set and vice versa.
780
+ if ( ! empty ( $ post_data ['post_date ' ] ) && empty ( $ post_data ['post_date_gmt ' ] ) ) {
781
+ $ post_data ['post_date_gmt ' ] = get_gmt_from_date ( $ post_data ['post_date ' ] );
768
782
}
769
783
770
- if ( false === $ post_data ['post_date ' ] ) {
771
- $ post_data ['post_date ' ] = $ post_data ['post_date_gmt ' ] ?: $ call_time ;
784
+ if ( ! empty ( $ post_data ['post_date_gmt ' ] ) && empty ( $ post_data [ ' post_date ' ] ) ) {
785
+ $ post_data ['post_date ' ] = get_date_from_gmt ( $ post_data ['post_date_gmt ' ] ) ;
772
786
}
773
787
774
788
if ( ! post_type_exists ( $ post_data ['post_type ' ] ) ) {
@@ -1006,4 +1020,22 @@ public function exists( $args ) {
1006
1020
WP_CLI ::halt ( 1 );
1007
1021
}
1008
1022
}
1023
+
1024
+ /**
1025
+ * Convert a date-time string with a hyphen separator to a space separator.
1026
+ *
1027
+ * @param string $date_string The date-time string to convert.
1028
+ * @return string The converted date-time string.
1029
+ *
1030
+ * Example:
1031
+ * maybe_convert_hyphenated_date_format( "2018-07-05-17:17:17" );
1032
+ * Returns: "2018-07-05 17:17:17"
1033
+ */
1034
+ private function maybe_convert_hyphenated_date_format ( $ date_string ) {
1035
+ // Check if the date string matches the format with the hyphen between date and time.
1036
+ if ( preg_match ( '/^(\d{4}-\d{2}-\d{2})-(\d{2}:\d{2}:\d{2})$/ ' , $ date_string , $ matches ) ) {
1037
+ return $ matches [1 ] . ' ' . $ matches [2 ];
1038
+ }
1039
+ return $ date_string ;
1040
+ }
1009
1041
}
0 commit comments