diff --git a/includes/forms/class-wp-job-manager-form-submit-job.php b/includes/forms/class-wp-job-manager-form-submit-job.php index bba4f1f46..2aed5a78f 100644 --- a/includes/forms/class-wp-job-manager-form-submit-job.php +++ b/includes/forms/class-wp-job-manager-form-submit-job.php @@ -843,12 +843,9 @@ protected function save_job( $post_title, $post_content, $status = 'preview', $v ]; if ( ! empty( $values['job']['job_schedule_listing'] ) ) { - $maybe_formatted_date = $this->maybe_format_future_datetime( $values['job']['job_schedule_listing'] ); + $is_scheduled_date = $this->apply_scheduled_date( $job_data, $values['job']['job_schedule_listing'] ); - if ( false !== $maybe_formatted_date ) { - $job_data['post_date'] = $maybe_formatted_date; - $job_data['post_date_gmt'] = $maybe_formatted_date; - } else { + if ( ! $is_scheduled_date ) { unset( $values['job']['job_schedule_listing'] ); } } @@ -1071,6 +1068,30 @@ public function preview() { } } + /** + * Helper which formats the scheduled date and sets the appropriate dates in the job data. + * + * @param array $job_data The job data array to modify. + * @param string $scheduled_date The scheduled date. + * + * @return bool True when the scheduled date is a valid future date, false otherwise. + */ + public static function apply_scheduled_date( array &$job_data, string $scheduled_date ): bool { + $maybe_formatted_date = self::maybe_format_future_datetime( $scheduled_date ); + + if ( false === $maybe_formatted_date ) { + $job_data['post_date'] = current_time( 'mysql' ); + $job_data['post_date_gmt'] = current_time( 'mysql', 1 ); + + return false; + } + + $job_data['post_date'] = $maybe_formatted_date; + $job_data['post_date_gmt'] = $maybe_formatted_date; + + return true; + } + /** * Checks that a string is a valid future datetime. Formats datetime for post date. * @@ -1078,7 +1099,7 @@ public function preview() { * * @return false|mixed */ - private function maybe_format_future_datetime( string $maybe_date_string ) { + private static function maybe_format_future_datetime( string $maybe_date_string ) { if ( empty( $maybe_date_string ) ) { return false; } @@ -1120,24 +1141,14 @@ public function preview_handler() { // Reset expiry. delete_post_meta( $job->ID, '_job_expires' ); - $post_date = current_time( 'mysql' ); - $post_date_gmt = current_time( 'mysql', 1 ); + // Update job listing. + $update_job = []; + $update_job['ID'] = $job->ID; + $update_job['post_status'] = apply_filters( 'submit_job_post_status', get_option( 'job_manager_submission_requires_approval' ) ? 'pending' : 'publish', $job ); + $update_job['post_author'] = get_current_user_id(); $job_schedule_listing_date = get_post_meta( $job->ID, '_job_schedule_listing', true ); - $maybe_formatted_date = $this->maybe_format_future_datetime( $job_schedule_listing_date ); - - if ( false !== $maybe_formatted_date ) { - $post_date = $maybe_formatted_date; - $post_date_gmt = $maybe_formatted_date; - } - - // Update job listing. - $update_job = []; - $update_job['ID'] = $job->ID; - $update_job['post_status'] = apply_filters( 'submit_job_post_status', get_option( 'job_manager_submission_requires_approval' ) ? 'pending' : 'publish', $job ); - $update_job['post_date'] = $post_date; - $update_job['post_date_gmt'] = $post_date_gmt; - $update_job['post_author'] = get_current_user_id(); + $this->apply_scheduled_date( $update_job, $job_schedule_listing_date ); wp_update_post( $update_job ); }