diff --git a/includes/admin/class-wp-job-manager-settings.php b/includes/admin/class-wp-job-manager-settings.php index 9a38516f4..f2316f4cc 100644 --- a/includes/admin/class-wp-job-manager-settings.php +++ b/includes/admin/class-wp-job-manager-settings.php @@ -389,6 +389,16 @@ protected function init_settings() { 'attributes' => [], 'track' => 'bool', ], + [ + 'name' => 'job_manager_enable_expiry_date_field', + 'std' => '0', + 'label' => __( 'Expiry Date Field', 'wp-job-manager' ), + 'cb_label' => __( 'Enable expiry date field in frontend form', 'wp-job-manager' ), + 'desc' => __( 'Allow employers to set a custom expiry date for their job listings that overrides the default listing duration.', 'wp-job-manager' ), + 'type' => 'checkbox', + 'attributes' => [], + 'track' => 'bool', + ], [ 'name' => 'job_manager_generate_username_from_email', 'std' => '1', 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 281cdd91a..932add202 100644 --- a/includes/forms/class-wp-job-manager-form-submit-job.php +++ b/includes/forms/class-wp-job-manager-form-submit-job.php @@ -389,6 +389,21 @@ public function init_fields() { ]; } + if ( get_option( 'job_manager_enable_expiry_date_field' ) ) { + $this->fields['job']['job_expires'] = [ + 'label' => __( 'Expiry Date', 'wp-job-manager' ), + 'description' => __( 'Optionally set when this job listing will expire. Leave blank to use default duration.', 'wp-job-manager' ), + 'type' => 'date', + 'required' => false, + 'placeholder' => '', + 'priority' => '6.6', + 'class' => 'job-manager-datepicker', + 'attributes' => [ + 'min' => current_time( 'Y-m-d' ), + ], + ]; + } + return $this->fields; } @@ -561,6 +576,20 @@ protected function validate_fields( $values ) { } } + // Validate expiry date field if enabled and provided. + if ( get_option( 'job_manager_enable_expiry_date_field' ) && ! empty( $values['job']['job_expires'] ) ) { + $expires_date = DateTimeImmutable::createFromFormat( 'Y-m-d|', $values['job']['job_expires'], wp_timezone() ); + if ( ! $expires_date ) { + throw new Exception( __( 'Please enter a valid expiry date', 'wp-job-manager' ) ); + } + + // Check if the date is in the past. + $today = new DateTimeImmutable( 'today', wp_timezone() ); + if ( $expires_date < $today ) { + throw new Exception( __( 'Expiry date cannot be in the past', 'wp-job-manager' ) ); + } + } + /** * Perform additional validation on the job submission fields. * @@ -997,6 +1026,17 @@ protected function update_job_data( $values ) { } update_user_meta( get_current_user_id(), '_company_logo', $attachment_id ); + // Handle custom expiry date field. + } elseif ( 'job_expires' === $key ) { + + // If a date is provided, also set the job expiration. + if ( ! empty( $values[ $group_key ][ $key ] ) ) { + $expires_date = DateTimeImmutable::createFromFormat( 'Y-m-d|', $values[ $group_key ][ $key ], wp_timezone() ); + if ( $expires_date ) { + WP_Job_Manager_Post_Types::instance()->set_job_expiration( $this->job_id, $expires_date ); + } + } + // Save meta data. } else { update_post_meta( $this->job_id, '_' . $key, $values[ $group_key ][ $key ] );