Skip to content
10 changes: 10 additions & 0 deletions includes/admin/class-wp-job-manager-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
55 changes: 55 additions & 0 deletions includes/forms/class-wp-job-manager-form-submit-job.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -524,6 +539,19 @@ protected function validate_fields( $values ) {
}
}

// Validate expiry date field.
Comment thread
masteradhoc marked this conversation as resolved.
Outdated
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 ) {
return new WP_Error( 'validation-error', __( 'Please enter a valid expiry date.', 'wp-job-manager' ) );
}

$today = new DateTimeImmutable( 'now', wp_timezone() );
if ( $expires_date < $today ) {
Comment thread
masteradhoc marked this conversation as resolved.
Outdated
return new WP_Error( 'validation-error', __( 'Expiry date cannot be in the past.', 'wp-job-manager' ) );
}
}

// Application method.
if ( ! $this->should_application_field_skip_email_url_validation() && isset( $values['job']['application'] ) ) {
$allowed_application_method = get_option( 'job_manager_allowed_application_method', '' );
Expand Down Expand Up @@ -561,6 +589,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() );
Comment thread
masteradhoc marked this conversation as resolved.
Outdated
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.
*
Expand Down Expand Up @@ -997,6 +1039,19 @@ 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 ) {
// Always save the raw value first.
update_post_meta( $this->job_id, '_job_expires', $values[ $group_key ][ $key ] );
Comment thread
masteradhoc marked this conversation as resolved.
Outdated

// 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() );
Comment thread
masteradhoc marked this conversation as resolved.
Outdated
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 ] );
Expand Down
Loading