Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a function for calculating scheduled dates #2803

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 33 additions & 22 deletions includes/forms/class-wp-job-manager-form-submit-job.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] );
}
}
Expand Down Expand Up @@ -1071,14 +1068,38 @@ 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.
*
* @param string $maybe_date_string The date to format.
*
* @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;
}
Expand Down Expand Up @@ -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 );
}
Expand Down
Loading