PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.16
Booktics – Appointment Booking Calendar for Service Businesses v1.0.16
1.0.29 1.0.28 1.0.27 1.0.26 1.0.25 1.0.24 1.0.23 1.0.22 1.0.21 1.0.20 1.0.19 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.2 1.0.3 All 31 releases
booktics / core / appointment / services / appointment-validator.php

appointment-validator.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.16, at core/appointment/services/appointment-validator.php

148 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ( $appointment->status === 'canceled' ) {
80 return new WP_Error( 'appointment_cancelled', __( 'This Appointment has already been cancelled', 'booktics' ) );
81 }
82
83 if ( $appointment->security_token !== $data['security_token'] && ! current_user_can('manage_options')) {
84 return new WP_Error( 'invalid_security_token', __( 'You dont have permission for rescheduling this appointment', 'booktics' ) );
85 }
86
87 $reschedule_restrictions = booktics_get_option( 'reschedule_restriction', array( 'enable' => false ) );
88 if ( isset( $reschedule_restrictions['enable'] ) && $reschedule_restrictions['enable'] ) {
89 $restricted_time = $reschedule_restrictions['restricted_time'] ?? 0;
90 $restricted_unit = $reschedule_restrictions['restricted_unit'] ?? 'hr';
91 $restricted_min = $restricted_unit === 'min' ? $restricted_time : $restricted_time * 60;
92
93 $current_time = (int) wp_date( 'U' );
94 $appointment_time = strtotime( $appointment->date . ' ' . $appointment->start_time );
95 if ( $appointment_time < $current_time + $restricted_min ) {
96 return new WP_Error(
97 'reschedule_restricted', sprintf(
98 /* translators: %1$d: restricted time value, %2$s: unit (e.g., 'min' or 'hr') */
99 __( 'You can only reschedule appointments at least %1$d %2$s before the appointment time.', 'booktics' ),
100 $restricted_time,
101 $restricted_unit
102 )
103 );
104 }
105 }
106
107 return null;
108 }
109
110 /**
111 * Validate data for canceling an appointment.
112 * @param int $id
113 * @return WP_Error|null
114 */
115 public function validate_cancel( $id ) {
116 if ( ! booktics_get_option( 'allow_customer_cancellation', false ) ) {
117 return new WP_Error( 'cancellation_setting', __( 'Customer cancellation is not allowed', 'booktics' ) );
118 }
119
120 $cancellation_restrictions = booktics_get_option( 'cancellation_restriction', array( 'enable' => false ) );
121 if ( isset( $cancellation_restrictions['enable'] ) && $cancellation_restrictions['enable'] ) {
122 $restricted_time = $cancellation_restrictions['restricted_time'] ?? 0;
123 $restricted_unit = $cancellation_restrictions['restricted_unit'] ?? 'hr';
124 $restricted_min = $restricted_unit === 'min' ? $restricted_time : $restricted_time * 60;
125 $appointment = Appointment_Model::find( $id );
126
127 if ( ! $appointment ) {
128 return new WP_Error( 'appointment_not_found', __( 'Appointment not found', 'booktics' ) );
129 }
130
131 $current_time = (int) wp_date( 'U' );
132 $appointment_time = strtotime( $appointment->date . ' ' . $appointment->start_time );
133 if ( $appointment_time < $current_time + $restricted_min ) {
134 return new WP_Error(
135 'cancellation_restricted', sprintf(
136 /* translators: %1$d: number of units, %2$s: unit (e.g., 'hr' or 'min') */
137 __( 'You can only cancel appointments at least %1$d %2$s before the appointment time.', 'booktics' ),
138 $restricted_time,
139 $restricted_unit
140 )
141 );
142 }
143 }
144
145 return null;
146 }
147 }
148