From 8eb6fb17902121e658ed26d2b7a788ac591c33ae Mon Sep 17 00:00:00 2001 From: Panos Kountanis Date: Fri, 12 Jan 2024 16:54:53 +0200 Subject: [PATCH] Allow employers to schedule job listings from the frontend. --- .../class-wp-job-manager-form-edit-job.php | 4 +- .../class-wp-job-manager-form-submit-job.php | 61 ++++++++++++++++++- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/includes/forms/class-wp-job-manager-form-edit-job.php b/includes/forms/class-wp-job-manager-form-edit-job.php index b897fe933..53955d60d 100644 --- a/includes/forms/class-wp-job-manager-form-edit-job.php +++ b/includes/forms/class-wp-job-manager-form-edit-job.php @@ -197,9 +197,9 @@ public function submit_handler() { update_post_meta( $this->job_id, '_job_edited', time() ); - if ( 'publish' === $post_status ) { + if ( in_array( $post_status, [ 'future', 'publish' ], true ) ) { $save_message = $save_message . ' ' . __( 'View →', 'wp-job-manager' ) . ''; - } elseif ( 'publish' === $original_post_status && 'pending' === $post_status ) { + } elseif ( in_array( $original_post_status, [ 'future', 'publish' ], true ) && 'pending' === $post_status ) { $save_message = __( 'Your changes have been submitted and your listing will be visible again once approved.', 'wp-job-manager' ); /** 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 0e9e56eea..edf850de2 100644 --- a/includes/forms/class-wp-job-manager-form-submit-job.php +++ b/includes/forms/class-wp-job-manager-form-submit-job.php @@ -376,6 +376,20 @@ public function init_fields() { if ( ! get_option( 'job_manager_enable_remote_position' ) ) { unset( $this->fields['job']['remote_position'] ); } + if ( true || get_option( 'job_manager_enable_scheduled_listings' ) ) { + $field_type = version_compare( JOB_MANAGER_VERSION, '1.30.0', '>=' ) ? 'date' : 'text'; + + $this->fields['job']['job_schedule_listing'] = [ + 'label' => __( 'Schedule date', 'wp-job-manager' ), + 'description' => __( 'schedule a date for this listing to go public. leave empty if you want to publish now.', 'wp-job-manager' ), + 'type' => $field_type, + 'required' => false, + 'placeholder' => '', + 'priority' => '6.5', + ]; + } + + return $this->fields; } /** @@ -844,6 +858,17 @@ protected function save_job( $post_title, $post_content, $status = 'preview', $v 'comment_status' => 'closed', ]; + if ( ! empty( $values['job']['job_schedule_listing'] ) ) { + $maybe_formatted_date = $this->maybe_format_datetime_if_valid( $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 { + unset( $values['job']['job_schedule_listing'] ); + } + } + if ( $update_slug ) { $job_slug = []; @@ -1062,6 +1087,27 @@ public function preview() { } } + /** + * Checks that a string is a valid datetime. Formats datetime for post date. + * + * @param string $maybe_date_string The date to format. + * + * @return false|mixed + */ + private function maybe_format_datetime_if_valid( string $maybe_date_string ): mixed { + if ( empty( $maybe_date_string ) ) { + return false; + } + + $time = strtotime( $maybe_date_string ); + if ( false === $time ) { + return false; + } + + $fmt = 'Y-m-d H:i:s'; + return wp_date( $fmt, $time ); + } + /** * Handles the preview step form response. */ @@ -1086,12 +1132,23 @@ 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 ); + + $job_schedule_listing_date = get_post_meta( $job->ID, '_job_schedule_listing', true ); + $maybe_formatted_date = $this->maybe_format_datetime_if_valid( $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'] = current_time( 'mysql' ); - $update_job['post_date_gmt'] = current_time( 'mysql', 1 ); + $update_job['post_date'] = $post_date; + $update_job['post_date_gmt'] = $post_date_gmt; $update_job['post_author'] = get_current_user_id(); wp_update_post( $update_job );