PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.15
Booktics – Appointment Booking Calendar for Service Businesses v1.0.15
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.15, at core/appointment/services/appointment-validator.php

146 lines 6.0 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 __( 'You can only reschedule appointments at least %1$d %2$s before the appointment time.', 'booktics' ),
99 $restricted_time,
100 $restricted_unit
101 )
102 );
103 }
104 }
105
106 return null;
107 }
108
109 /**
110 * Validate data for canceling an appointment.
111 * @param int $id
112 * @return WP_Error|null
113 */
114 public function validate_cancel( $id ) {
115 if ( ! booktics_get_option( 'allow_customer_cancellation', false ) ) {
116 return new WP_Error( 'cancellation_setting', __( 'Customer cancellation is not allowed', 'booktics' ) );
117 }
118
119 $cancellation_restrictions = booktics_get_option( 'cancellation_restriction', array( 'enable' => false ) );
120 if ( isset( $cancellation_restrictions['enable'] ) && $cancellation_restrictions['enable'] ) {
121 $restricted_time = $cancellation_restrictions['restricted_time'] ?? 0;
122 $restricted_unit = $cancellation_restrictions['restricted_unit'] ?? 'hr';
123 $restricted_min = $restricted_unit === 'min' ? $restricted_time : $restricted_time * 60;
124 $appointment = Appointment_Model::find( $id );
125
126 if ( ! $appointment ) {
127 return new WP_Error( 'appointment_not_found', __( 'Appointment not found', 'booktics' ) );
128 }
129
130 $current_time = (int) wp_date( 'U' );
131 $appointment_time = strtotime( $appointment->date . ' ' . $appointment->start_time );
132 if ( $appointment_time < $current_time + $restricted_min ) {
133 return new WP_Error(
134 'cancellation_restricted', sprintf(
135 __( 'You can only cancel appointments at least %1$d %2$s before the appointment time.', 'booktics' ),
136 $restricted_time,
137 $restricted_unit
138 )
139 );
140 }
141 }
142
143 return null;
144 }
145 }
146