@@ -702,10 +702,10 @@ function ( $post ) {
702702 * ---
703703 *
704704 * [--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.
706706 *
707707 * [--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.
709709 *
710710 * [--post_content]
711711 * : If set, the command reads the post_content from STDIN.
@@ -753,22 +753,36 @@ public function generate( $args, $assoc_args ) {
753753 'post_type ' => 'post ' ,
754754 'post_status ' => 'publish ' ,
755755 'post_author ' => false ,
756- 'post_date ' => false ,
757- 'post_date_gmt ' => false ,
756+ 'post_date ' => '' ,
757+ 'post_date_gmt ' => '' ,
758758 'post_content ' => '' ,
759759 'post_title ' => '' ,
760760 ];
761761
762762 $ post_data = array_merge ( $ defaults , $ assoc_args );
763763
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+ }
765778
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 ' ] );
768782 }
769783
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 ' ] ) ;
772786 }
773787
774788 if ( ! post_type_exists ( $ post_data ['post_type ' ] ) ) {
@@ -1006,4 +1020,22 @@ public function exists( $args ) {
10061020 WP_CLI ::halt ( 1 );
10071021 }
10081022 }
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+ }
10091041}
0 commit comments