| 1 |
<?php |
| 2 |
namespace Booktics\Appointment\Services; |
| 3 |
|
| 4 |
use Booktics\Models\Appointment_Model; |
| 5 |
use Booktics\Utils\Singleton; |
| 6 |
use WP_Error; |
| 7 |
|
| 8 |
class Appointment_Validator { |
| 9 |
|
| 10 |
use Singleton; |
| 11 |
|
| 12 |
/** |
| 13 |
* Validate data for creating an appointment. |
| 14 |
* @param array $data |
| 15 |
* @return WP_Error|null |
| 16 |
*/ |
| 17 |
public static function validate_create( array $data ) { |
| 18 |
if ( empty( $data['start_time'] ) ) { |
| 19 |
return new WP_Error( 'missing_start_time', __( 'Start time is required', 'booktics' ) ); |
| 20 |
} |
| 21 |
if ( empty( $data['end_time'] ) ) { |
| 22 |
return new WP_Error( 'missing_end_time', __( 'End time is required', 'booktics' ) ); |
| 23 |
} |
| 24 |
if ( empty( $data['date'] ) ) { |
| 25 |
return new WP_Error( 'missing_date', __( 'Date is required', 'booktics' ) ); |
| 26 |
} |
| 27 |
if ( empty( $data['service_id'] ) ) { |
| 28 |
return new WP_Error( 'missing_service_id', __( 'Service ID is required', 'booktics' ) ); |
| 29 |
} |
| 30 |
if ( empty( $data['team_member_id'] ) ) { |
| 31 |
return new WP_Error( 'missing_team_member_id', __( 'Team member ID is required', 'booktics' ) ); |
| 32 |
} |
| 33 |
// Add more validation as needed |
| 34 |
return null; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Validate data for updating an appointment. |
| 39 |
* @param array $data |
| 40 |
* @return WP_Error|null |
| 41 |
*/ |
| 42 |
public static function validate_update( array $data ): ?WP_Error { |
| 43 |
if ( empty( $data ) ) { |
| 44 |
return new WP_Error( 'no_data', __( 'No data provided for update', 'booktics' ) ); |
| 45 |
} |
| 46 |
// Optionally, check for required fields on update |
| 47 |
return null; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Validate data for rescheduling an appointment. |
| 52 |
* @param array $data |
| 53 |
* @return WP_Error|null |
| 54 |
*/ |
| 55 |
public static function validate_reschedule( array $data, $id ) { |
| 56 |
if ( empty( $data['start_time'] ) ) { |
| 57 |
return new WP_Error( 'missing_start_time', __( 'Start time is required for reschedule', 'booktics' ) ); |
| 58 |
} |
| 59 |
if ( empty( $data['end_time'] ) ) { |
| 60 |
return new WP_Error( 'missing_end_time', __( 'End time is required for reschedule', 'booktics' ) ); |
| 61 |
} |
| 62 |
if ( empty( $data['date'] ) ) { |
| 63 |
return new WP_Error( 'missing_date', __( 'Date is required for reschedule', 'booktics' ) ); |
| 64 |
} |
| 65 |
if ( ! booktics_get_option( 'allow_customer_rescheduling', false ) ) { |
| 66 |
return new WP_Error( 'reschedule_setting', __( 'Customer rescheduling is not allowed', 'booktics' ) ); |
| 67 |
} |
| 68 |
|
| 69 |
if ( empty( $data['security_token'] ) ) { |
| 70 |
return new WP_Error( 'missing_appointment_token', __( 'Appointment token is required for reschedule', 'booktics' ) ); |
| 71 |
} |
| 72 |
|
| 73 |
$appointment = Appointment_Model::find( $id ); |
| 74 |
|
| 75 |
if ( ! $appointment ) { |
| 76 |
return new WP_Error( 'appointment_not_found', __( 'Appointment not found', 'booktics' ) ); |
| 77 |
} |
| 78 |
|
| 79 |
if ( 'canceled' === $appointment->status ) { |
| 80 |
return new WP_Error( 'appointment_cancelled', __( 'This Appointment has already been cancelled', 'booktics' ) ); |
| 81 |
} |
| 82 |
|
| 83 |
$token = isset( $data['security_token'] ) ? (string) $data['security_token'] : ''; |
| 84 |
if ( ! hash_equals( (string) $appointment->security_token, $token ) && ! current_user_can( 'manage_options' ) ) { |
| 85 |
return new WP_Error( 'invalid_security_token', __( 'You dont have permission for rescheduling this appointment', 'booktics' ) ); |
| 86 |
} |
| 87 |
|
| 88 |
$reschedule_restrictions = booktics_get_option( 'reschedule_restriction', array( 'enable' => false ) ); |
| 89 |
if ( isset( $reschedule_restrictions['enable'] ) && $reschedule_restrictions['enable'] ) { |
| 90 |
$restricted_time = $reschedule_restrictions['restricted_time'] ?? 0; |
| 91 |
$restricted_unit = $reschedule_restrictions['restricted_unit'] ?? 'hr'; |
| 92 |
$restricted_min = ('min' === $restricted_unit) ? $restricted_time : $restricted_time * 60; |
| 93 |
|
| 94 |
$current_time = (int) wp_date( 'U' ); |
| 95 |
$appointment_time = strtotime( $appointment->date . ' ' . $appointment->start_time ); |
| 96 |
if ( $appointment_time < $current_time + $restricted_min ) { |
| 97 |
return new WP_Error( |
| 98 |
'reschedule_restricted', sprintf( |
| 99 |
/* translators: %1$d: restricted time value, %2$s: unit (e.g., 'min' or 'hr') */ |
| 100 |
__( 'You can only reschedule appointments at least %1$d %2$s before the appointment time.', 'booktics' ), |
| 101 |
$restricted_time, |
| 102 |
$restricted_unit |
| 103 |
) |
| 104 |
); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return null; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Validate data for canceling an appointment. |
| 113 |
* @param int $id |
| 114 |
* @return WP_Error|null |
| 115 |
*/ |
| 116 |
public function validate_cancel( $id ) { |
| 117 |
if ( ! booktics_get_option( 'allow_customer_cancellation', false ) ) { |
| 118 |
return new WP_Error( 'cancellation_setting', __( 'Customer cancellation is not allowed', 'booktics' ) ); |
| 119 |
} |
| 120 |
|
| 121 |
$cancellation_restrictions = booktics_get_option( 'cancellation_restriction', array( 'enable' => false ) ); |
| 122 |
if ( isset( $cancellation_restrictions['enable'] ) && $cancellation_restrictions['enable'] ) { |
| 123 |
$restricted_time = $cancellation_restrictions['restricted_time'] ?? 0; |
| 124 |
$restricted_unit = $cancellation_restrictions['restricted_unit'] ?? 'hr'; |
| 125 |
$restricted_min = $restricted_unit === 'min' ? $restricted_time : $restricted_time * 60; |
| 126 |
$appointment = Appointment_Model::find( $id ); |
| 127 |
|
| 128 |
if ( ! $appointment ) { |
| 129 |
return new WP_Error( 'appointment_not_found', __( 'Appointment not found', 'booktics' ) ); |
| 130 |
} |
| 131 |
|
| 132 |
$current_time = (int) wp_date( 'U' ); |
| 133 |
$appointment_time = strtotime( $appointment->date . ' ' . $appointment->start_time ); |
| 134 |
if ( $appointment_time < $current_time + $restricted_min ) { |
| 135 |
return new WP_Error( |
| 136 |
'cancellation_restricted', sprintf( |
| 137 |
/* translators: %1$d: number of units, %2$s: unit (e.g., 'hr' or 'min') */ |
| 138 |
__( 'You can only cancel appointments at least %1$d %2$s before the appointment time.', 'booktics' ), |
| 139 |
$restricted_time, |
| 140 |
$restricted_unit |
| 141 |
) |
| 142 |
); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
return null; |
| 147 |
} |
| 148 |
} |
| 149 |
|