PluginProbe
Bookit — Booking & Appointment Calendar / trunk
Bookit — Booking & Appointment Calendar vtrunk
2.6.0.4 2.6.0.3 2.6.0.2 2.6.0.1 2.6.0 trunk 1.2 1.2.2 1.2.3 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 All 61 releases
bookit / includes / classes / AppointmentController.php

AppointmentController.php in Bookit — Booking & Appointment Calendar trunk, at includes/classes/AppointmentController.php

293 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Bookit\Classes;
4
5 use Bookit\Classes\Admin\SettingsController;
6 use Bookit\Classes\Base\Plugin;
7 use Bookit\Classes\Base\User;
8 use Bookit\Classes\Database\Appointments;
9 use Bookit\Classes\Database\Customers;
10 use Bookit\Classes\Database\Staff_Services;
11 use Bookit\Classes\Vendor\Payments;
12 use Bookit\Helpers\CleanHelper;
13
14
15 class AppointmentController {
16
17 protected static $googleCalendarAddon = 'google-calendar';
18
19 /**
20 * Check for Pro Addon active.
21 *
22 * @since 2.5.0
23 *
24 * @var string
25 */
26 protected static $proAddon = 'pro';
27
28 private static function getCleanRules() {
29
30 return array(
31 'clear_price' => array( 'type' => 'floatval' ),
32 'user_id' => array( 'type' => 'intval' ),
33 'staff_id' => array( 'type' => 'intval' ),
34 'service_id' => array( 'type' => 'intval' ),
35 'start_timestamp' => array( 'type' => 'intval' ),
36 'end_timestamp' => array( 'type' => 'intval' ),
37 'now_timestamp' => array( 'type' => 'intval' ),
38 'today_timestamp' => array( 'type' => 'intval' ),
39 'email' => array(
40 'type' => 'strval',
41 'function' => array(
42 'custom' => false,
43 'name' => 'sanitize_email',
44 ),
45 ),
46 'payment_method' => array( 'type' => 'strval' ),
47 'full_name' => array( 'type' => 'strval' ),
48 'phone' => array(
49 'function' => array(
50 'custom' => true,
51 'name' => 'custom_sanitize_phone',
52 ),
53 ),
54 );
55 }
56
57 /**
58 * Validation
59 * @param $data
60 */
61 public static function validate( $data ) {
62 $errors = array();
63 $settings = SettingsController::get_settings();
64
65 if ( ! $data['staff_id'] ) {
66 $errors['staff_id'] = __( 'No staff', 'bookit' );
67 }
68 if ( ! $data['service_id'] ) {
69 $errors['service_id'] = __( 'No service', 'bookit' );
70 }
71 if ( ! $data['start_time'] ) {
72 $errors['start_time'] = __( 'No start time', 'bookit' );
73 }
74 if ( ! $data['end_time'] ) {
75 $errors['end_time'] = __( 'No end time', 'bookit' );
76 }
77 if ( ! $data['date_timestamp'] ) {
78 $errors['date_timestamp'] = __( 'No date', 'bookit' );
79 }
80
81 $appointment = Appointments::checkAppointment( $data );
82
83 if ( $appointment > 0 ) {
84 $errors['appointment'] = __( 'Selected Service Time is not available!', 'bookit' );
85 }
86
87 if ( $data['phone'] || false === $data['phone'] ) {
88 if ( ! preg_match( '/^((\+)?[0-9]{8,14})$/', $data['phone'] ) ) {
89 $errors['phone'] = __( 'Please enter a valid phone number', 'bookit' );
90 }
91 }
92
93 if ( 'guest' == $settings['booking_type'] ) {
94 if ( ! $data['email'] && ! $data['phone'] ) {
95 $errors['phone'] = __( 'Please enter email or phone', 'bookit' );
96 }
97 }
98
99 if ( ! $data['email'] ) {
100 $errors['email'] = __( 'Please enter email', 'bookit' );
101 }
102
103 if ( $data['email'] && ! is_email( $data['email'] ) ) {
104 $errors['email'] = __( 'Please enter your email in format youremail@example.com', 'bookit' );
105 }
106
107 if ( $data['full_name'] ) {
108 if ( strlen( $data['full_name'] ) < 3 || strlen( $data['full_name'] ) > 25 ) {
109 $errors['full_name'] = __( 'Full name must be between 3 and 25 characters long', 'bookit' );
110 }
111 } else {
112 $errors['full_name'] = __( 'Please enter full name', 'bookit' );
113 }
114
115 if ( 'registered' == $settings['booking_type'] ) {
116
117 $exist_user = get_user_by( 'email', $data['email'] );
118 if ( ! $data['user_id'] && $exist_user && ! wp_check_password( $data['password'], $exist_user->data->user_pass, $exist_user->ID ) ) {
119 $errors['password'] = __( 'Wrong password', 'bookit' );
120 }
121
122 if ( ! $data['user_id'] ) {
123 if ( empty( $data['password'] ) ) {
124 $errors['password'] = __( 'Please enter a password', 'bookit' );
125 }
126
127 if ( false !== strpos( wp_unslash( $data['password'] ), '\\' ) ) {
128 $errors['password'] = __( "Passwords may not contain the character '\\'", 'bookit' );
129 }
130
131 if ( ! ( $exist_user instanceof \WP_User ) && ( ! empty( $data['password'] ) ) && $data['password'] != $data['password_confirmation'] ) {
132 $errors['password_confirmation'] = __( 'Please enter the same password in both password fields', 'bookit' );
133 }
134 }
135
136 if ( $data['user_id'] && ! is_user_logged_in() ) {
137 $errors['appointment'] = __( 'Authorization error', 'bookit' );
138 }
139 }
140
141 $price = Staff_Services::get_service_price_by_staff( $data['service_id'], $data['staff_id'] );
142 if ( (float) $data['clear_price'] !== (float) $price ) {
143 $errors['clear_price'] = __( 'Price is incorrect', 'bookit' );
144 }
145
146 if ( (float) $data['clear_price'] > 0 && ! array_key_exists( $data['payment_method'], $settings['payments'] ) ) {
147 $errors['payment_method'] = __( 'Please choose correct payment method', 'bookit' );
148 }
149
150 if ( count( $errors ) > 0 ) {
151 wp_send_json_error( array( 'errors' => $errors ) );
152 }
153 }
154
155 /**
156 * Book Appointment
157 */
158 public static function save() {
159
160 $send_no_cache_headers = apply_filters( 'rest_send_nocache_headers', is_user_logged_in() );
161 $nonce = '';
162 if ( ! $send_no_cache_headers ) {
163 $nonce = wp_create_nonce( 'bookit_nonce' );
164 $_SERVER['HTTP_X_WP_NONCE'] = $nonce;
165 }
166
167 check_ajax_referer( 'bookit_book_appointment', 'nonce' );
168
169 $data = CleanHelper::cleanData( $_POST, self::getCleanRules() );
170 self::validate( $data );
171
172 if ( empty( $data ) ) {
173 wp_send_json_error( array( 'message' => __( 'Error occurred!', 'bookit' ) ) );
174 die();
175 }
176
177 $customer = CustomerController::get_customer( $data );
178
179 $notes = array();
180 if ( ! empty( $data['comment'] ) ) {
181 $notes['comment'] = $data['comment'];
182 }
183 if ( $customer->email != $data['email'] ) {
184 $notes['email'] = $data['email'];
185 }
186 if ( $customer->phone != $data['phone'] && ! empty( $data['phone'] ) ) {
187 $notes['phone'] = $data['phone'];
188 }
189 if ( $customer->full_name != $data['full_name'] && ! empty( $data['full_name'] ) ) {
190 $notes['full_name'] = $data['full_name'];
191 }
192 $data['customer_id'] = $customer->id;
193 $data['notes'] = serialize( $notes );
194 $data['status'] = Appointments::$pending;
195
196 $id = Appointments::create_appointment( $data );
197
198 do_action( 'bookit_appointment_created', $id );
199
200 $appointment = (array) Appointments::get_full_appointment_by_id( $id );
201 $appointment['token'] = $data['token'];
202
203 /** if google calendar addon is installed */
204 if ( Plugin::isAddonInstalledAndEnabled( self::$proAddon )
205 && has_action( 'bookit_google_calendar_create_appointment' ) ) {
206 $appointment['customer_email'] = $notes['email'] ?? $appointment['customer_email'];
207 $appointment['customer_phone'] = $notes['phone'] ?? $appointment['customer_phone'];
208 do_action( 'bookit_google_calendar_create_appointment', $appointment );
209 }
210 /** if google calendar addon is installed | end */
211
212 $redirect_url = '';
213 if ( ! is_null( $appointment['payment_method'] ) ) {
214 $payments = new Payments( $appointment );
215 $redirect_url = $payments->redirect_url();
216 }
217 wp_send_json_success(
218 array(
219 'appointment' => $appointment,
220 'customer' => $customer,
221 'nonce' => $nonce,
222 'redirect_url' => $redirect_url,
223 'message' => __( 'Appointment Saved!', 'bookit' ),
224 )
225 );
226 }
227
228 /**
229 * Get Month Appointments
230 */
231 public static function get_month_appointments() {
232 check_ajax_referer( 'bookit_month_appointments', 'nonce' );
233
234 $data = CleanHelper::cleanData( $_POST, self::getCleanRules() );
235 if ( ! empty( $data ) ) {
236 $appointments = Appointments::month_appointments( $data );
237
238 wp_send_json_success( $appointments );
239 }
240
241 wp_send_json_error( array( 'message' => __( 'Error occurred!', 'bookit' ) ) );
242 }
243
244 /**
245 * Get Day Appointments
246 */
247 public static function get_day_appointments() {
248 check_ajax_referer( 'bookit_day_appointments', 'nonce' );
249
250 $data = CleanHelper::cleanData( $_POST, self::getCleanRules() );
251
252 if ( empty( $data ) ) {
253 wp_send_json_error( array( 'message' => __( 'Error occurred!', 'bookit' ) ) );
254 die();
255 }
256
257 $appointments = Appointments::day_appointments( $data );
258
259 /** if google calendar addon is installed */
260 if ( Plugin::isAddonInstalledAndEnabled( self::$proAddon )
261 && has_filter( 'bookit_google_calendar_get_events_by_date' ) ) {
262 $gcBusyTimeSlots = apply_filters( 'bookit_google_calendar_get_events_by_date', $data );
263
264 /** remove double values by id key */
265 foreach ( $gcBusyTimeSlots as $key => $value ) {
266 if ( array_search( $value->id, array_column( $appointments, 'id' ) ) !== false
267 || array_search( (string) $value->start_time, array_column( $appointments, 'start_time' ) ) ) {
268 unset( $gcBusyTimeSlots[ $key ] );
269 }
270 }
271 $appointments = array_merge( $gcBusyTimeSlots, $appointments );
272 }
273 /** if google calendar addon is installed | end */
274 wp_send_json_success( $appointments );
275 }
276
277 /**
278 * Check is Appointment time free
279 */
280 public static function is_free_appointment() {
281 check_ajax_referer( 'bookit_is_free_appointment', 'nonce' );
282
283 $data = CleanHelper::cleanData( $_POST, self::getCleanRules() );
284 if ( empty( $data['staff_id'] ) || empty( $data['service_id'] ) || empty( $data['date_timestamp'] )
285 || empty( $data['start_time'] ) || empty( $data['end_time'] ) ) {
286 wp_send_json_error( array( 'message' => __( 'Error occurred!', 'bookit' ) ) );
287 }
288
289 $appointment = Appointments::checkAppointment( $data );
290 wp_send_json_success( array( 'is_free' => false ) );
291 }
292 }
293