status ) { return new WP_Error( 'appointment_cancelled', __( 'This Appointment has already been cancelled', 'booktics' ) ); } $token = isset( $data['security_token'] ) ? (string) $data['security_token'] : ''; if ( ! hash_equals( (string) $appointment->security_token, $token ) && ! current_user_can( 'manage_options' ) ) { return new WP_Error( 'invalid_security_token', __( 'You dont have permission for rescheduling this appointment', 'booktics' ) ); } $reschedule_restrictions = booktics_get_option( 'reschedule_restriction', array( 'enable' => false ) ); if ( isset( $reschedule_restrictions['enable'] ) && $reschedule_restrictions['enable'] ) { $restricted_time = $reschedule_restrictions['restricted_time'] ?? 0; $restricted_unit = $reschedule_restrictions['restricted_unit'] ?? 'hr'; $restricted_min = ('min' === $restricted_unit) ? $restricted_time : $restricted_time * 60; $current_time = (int) wp_date( 'U' ); $appointment_time = strtotime( $appointment->date . ' ' . $appointment->start_time ); if ( $appointment_time < $current_time + $restricted_min ) { return new WP_Error( 'reschedule_restricted', sprintf( /* translators: %1$d: restricted time value, %2$s: unit (e.g., 'min' or 'hr') */ __( 'You can only reschedule appointments at least %1$d %2$s before the appointment time.', 'booktics' ), $restricted_time, $restricted_unit ) ); } } return null; } /** * Validate data for canceling an appointment. * @param int $id * @return WP_Error|null */ public function validate_cancel( $id ) { if ( ! booktics_get_option( 'allow_customer_cancellation', false ) ) { return new WP_Error( 'cancellation_setting', __( 'Customer cancellation is not allowed', 'booktics' ) ); } $cancellation_restrictions = booktics_get_option( 'cancellation_restriction', array( 'enable' => false ) ); if ( isset( $cancellation_restrictions['enable'] ) && $cancellation_restrictions['enable'] ) { $restricted_time = $cancellation_restrictions['restricted_time'] ?? 0; $restricted_unit = $cancellation_restrictions['restricted_unit'] ?? 'hr'; $restricted_min = $restricted_unit === 'min' ? $restricted_time : $restricted_time * 60; $appointment = Appointment_Model::find( $id ); if ( ! $appointment ) { return new WP_Error( 'appointment_not_found', __( 'Appointment not found', 'booktics' ) ); } $current_time = (int) wp_date( 'U' ); $appointment_time = strtotime( $appointment->date . ' ' . $appointment->start_time ); if ( $appointment_time < $current_time + $restricted_min ) { return new WP_Error( 'cancellation_restricted', sprintf( /* translators: %1$d: number of units, %2$s: unit (e.g., 'hr' or 'min') */ __( 'You can only cancel appointments at least %1$d %2$s before the appointment time.', 'booktics' ), $restricted_time, $restricted_unit ) ); } } return null; } }