PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.0
Timetics – Appointment Booking Calendar & Scheduling v1.0.0
1.0.61 1.0.60 1.0.59 1.0.58 1.0.57 1.0.56 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.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 All 62 releases
timetics / core / appointments / api-appointment.php

api-appointment.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.0, at core/appointments/api-appointment.php

597 lines 18.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Apointment api
4 *
5 * @since 1.0.0
6 *
7 * @package Timetics
8 */
9 namespace Timetics\Core\Appointments;
10
11 use Timetics\Base\Api;
12 use Timetics\Core\Appointments\Appointment;
13 use Timetics\Utils\Singleton;
14 use WP_HTTP_Response;
15 use WP_REST_Request;
16
17 /**
18 * Api_Appointment class
19 *
20 * @since 1.0.0
21 */
22 class Api_Appointment extends Api {
23
24 use Singleton;
25
26 /**
27 * Store api namespace
28 *
29 * @since 1.0.0
30 *
31 * @var string $namespace
32 */
33 protected $namespace = 'timetics/v1';
34
35 /**
36 * Store rest base
37 *
38 * @since 1.0.0
39 *
40 * @var string $rest_base
41 */
42 protected $rest_base = 'appointments';
43
44 /**
45 * Register rest routes.
46 *
47 * @since 1.0.0
48 *
49 * @return void
50 */
51 public function register_routes() {
52 /*
53 * Register route
54 */
55 register_rest_route( $this->namespace, $this->rest_base, [
56 [
57 'methods' => \WP_REST_Server::READABLE,
58 'callback' => [$this, 'get_items'],
59 'permission_callback' => function () {
60 return true;
61 },
62 ],
63 [
64 'methods' => \WP_REST_Server::CREATABLE,
65 'callback' => [$this, 'create_item'],
66 'permission_callback' => function () {
67 return current_user_can( 'edit_meeting' );
68 },
69 ],
70 [
71 'methods' => \WP_REST_Server::DELETABLE,
72 'callback' => [$this, 'bulk_delete'],
73 'permission_callback' => function () {
74 return current_user_can( 'edit_meeting' );
75 },
76 ],
77 ] );
78
79 /**
80 * Register route
81 *
82 * @var void
83 */
84 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<appointment_id>[\d]+)', [
85 [
86 'methods' => \WP_REST_Server::READABLE,
87 'callback' => [$this, 'get_item'],
88 'permission_callback' => function () {
89 return true;
90 },
91 ],
92 [
93 'methods' => \WP_REST_Server::EDITABLE,
94 'callback' => [$this, 'update_item'],
95 'permission_callback' => function () {
96 return current_user_can( 'edit_meeting' );
97 },
98 ],
99 [
100 'methods' => \WP_REST_Server::DELETABLE,
101 'callback' => [$this, 'delete_item'],
102 'permission_callback' => function () {
103 return current_user_can( 'edit_meeting' );
104 },
105 ],
106 ] );
107
108 register_rest_route( $this->namespace, $this->rest_base . '/search', [
109 [
110 'methods' => \WP_REST_Server::READABLE,
111 'callback' => [$this, 'search_items'],
112 'permission_callback' => function () {
113 return current_user_can( 'edit_posts' );
114 },
115 ],
116 ] );
117
118 register_rest_route( $this->namespace, $this->rest_base . '/filter', [
119 [
120 'methods' => \WP_REST_Server::READABLE,
121 'callback' => [$this, 'filter_items'],
122 'permission_callback' => function () {
123 return current_user_can( 'edit_posts' );
124 },
125 ],
126 ] );
127
128 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<appointment_id>[\d]+)' . '/duplicate', [
129 [
130 'methods' => \WP_REST_Server::CREATABLE,
131 'callback' => [$this, 'duplicate_item'],
132 'permission_callback' => function () {
133 return current_user_can( 'edit_meeting' );
134 },
135 ],
136 ] );
137 }
138
139 /**
140 * Get all appointments
141 *
142 * @param WP_Rest_Request $request
143 *
144 * @return JSON
145 */
146 public function get_items( $request ) {
147
148 $per_page = ! empty( $request['per_page'] ) ? intval( $request['per_page'] ) : 20;
149 $paged = ! empty( $request['paged'] ) ? intval( $request['paged'] ) : 1;
150
151 $args = [
152 'posts_per_page' => $per_page,
153 'paged' => $paged,
154 ];
155
156 if ( ! current_user_can( 'edit_meeting' ) ) {
157 $args['staff'] = get_current_user_id();
158 }
159
160 $appoint = Appointment::all( $args );
161
162 $items = [];
163
164 foreach ( $appoint['items'] as $item ) {
165 $items[] = $this->prepare_item( $item->ID );
166 }
167
168 $data = [
169 'success' => 1,
170 'status_code' => 200,
171 'data' => [
172 'total' => $appoint['total'],
173 'items' => $items,
174 ],
175 ];
176
177 return rest_ensure_response( $data );
178 }
179
180 /**
181 * Search appointment
182 *
183 * @param Object $request
184 *
185 * @return JSON
186 */
187 public function search_items( $request ) {
188 // Prepare search args.
189 $per_page = ! empty( $request['per_page'] ) ? intval( $request['per_page'] ) : 20;
190 $paged = ! empty( $request['paged'] ) ? intval( $request['paged'] ) : 1;
191 $search = ! empty( $request['search'] ) ? sanitize_text_field( $request['search'] ) : '';
192
193 // Get search.
194 $appointments = new \WP_Query(
195 array(
196 'post_type' => 'timetics-appointment',
197 'posts_per_page' => $per_page,
198 'paged' => $paged,
199 'orderby' => 'ID',
200 'order' => 'DESC',
201
202 // @codingStandardsIgnoreStart
203 'meta_query' => array(
204 'relation' => 'OR',
205 array(
206 'key' => '_tt_apointment_name',
207 'value' => $search,
208 'compare' => 'LIKE',
209 ),
210 array(
211 'key' => '_tt_apointment_type',
212 'value' => $search,
213 'compare' => 'LIKE',
214 ),
215 array(
216 'key' => '_tt_apointment_description',
217 'value' => $search,
218 'compare' => 'LIKE',
219 ),
220 array(
221 'key' => '_tt_apointment_location',
222 'value' => $search,
223 'compare' => 'LIKE',
224 ),
225 array(
226 'key' => '_tt_apointment_duration',
227 'value' => $search,
228 'compare' => 'LIKE',
229 ),
230 array(
231 'key' => '_tt_apointment_schedule',
232 'value' => $search,
233 'compare' => 'LIKE',
234 ),
235 ),
236 // @codingStandardsIgnoreEnd
237 )
238 );
239
240 // Prepare items for response.
241 $items = [];
242
243 foreach ( $appointments->posts as $item ) {
244 $items[] = $this->prepare_item( $item->ID );
245 }
246
247 $data = [
248 'success' => 1,
249 'status' => 200,
250 'data' => [
251 'total' => $appointments->found_posts,
252 'items' => $items,
253 ],
254 ];
255
256 return rest_ensure_response( $data );
257 }
258
259 public function filter_items( $request ) {
260 $per_page = ! empty( $request['per_page'] ) ? intval( $request['per_page'] ) : 20;
261 $paged = ! empty( $request['paged'] ) ? intval( $request['paged'] ) : 1;
262 $staff = ! empty( $request['staff_id'] ) ? intval( $request['staff_id'] ) : 0;
263 $visibility = ! empty( $request['visibility'] ) ? sanitize_text_field( $request['visibility'] ) : 'enabled';
264
265 $per_page = ! empty( $request['per_page'] ) ? intval( $request['per_page'] ) : 20;
266 $paged = ! empty( $request['paged'] ) ? intval( $request['paged'] ) : 1;
267
268 $appoint = Appointment::all( [
269 'posts_per_page' => $per_page,
270 'paged' => $paged,
271 'visibility' => $visibility,
272 'staff' => $staff,
273 ] );
274
275 $items = [];
276
277 foreach ( $appoint['items'] as $item ) {
278 $items[] = $this->prepare_item( $item->ID );
279 }
280
281 $data = [
282 'success' => 1,
283 'status' => 200,
284 'data' => [
285 'total' => $appoint['total'],
286 'items' => $items,
287 ],
288 ];
289
290 return rest_ensure_response( $data );
291 }
292
293 /**
294 * Create appointment
295 *
296 * @param WP_Rest_Request $request
297 *
298 * @return JSON Newly created appointment data
299 */
300 public function create_item( $request ) {
301 return $this->save_appointment( $request );
302 }
303
304 /**
305 * Update appointment
306 *
307 * @param WP_Rest_Request $request
308 *
309 * @return JSON Updated appointment data
310 */
311 public function update_item( $request ) {
312 $appointment_id = (int) $request['appointment_id'];
313 $appoint = new Appointment( $appointment_id );
314
315 if ( ! $appoint->is_appointment() ) {
316
317 $data = [
318 'success' => 0,
319 'status_code' => 404,
320 'message' => __( 'Invalid appointment id.', 'timetics' ),
321 'data' => [],
322 ];
323
324 return new WP_HTTP_Response( $data, 404 );
325 }
326
327 return $this->save_appointment( $request, $appointment_id );
328 }
329
330 /**
331 * Get single appointment
332 *
333 * @param WP_Rest_Requesr $request
334 *
335 * @return JSON Single appointment data
336 */
337 public function get_item( $request ) {
338 $appoinment_id = (int) $request['appointment_id'];
339 $appoint = new Appointment( $appoinment_id );
340
341 if ( ! $appoint->is_appointment() ) {
342
343 $data = [
344 'status_code' => 404,
345 'message' => __( 'Invalid appointment id.', 'timetics' ),
346 'data' => [],
347 ];
348
349 return new WP_HTTP_Response( $data, 404 );
350 }
351
352 $response = [
353 'status_code' => 200,
354 'message' => __( 'Successfully retrieved appointments', 'timetics' ),
355 'data' => $this->prepare_item( $appoint ),
356 ];
357
358 return rest_ensure_response( $response );
359 }
360
361 /**
362 * Delete single appointment
363 *
364 * @param WP_Rest_Request $request
365 *
366 * @return
367 */
368 public function delete_item( $request ) {
369 $appoinment_id = (int) $request['appointment_id'];
370 $appoint = new Appointment( $appoinment_id );
371
372 if ( ! $appoint->is_appointment() ) {
373 return [
374 'status_code' => 404,
375 'message' => __( 'Invalid appointment id.', 'timetics' ),
376 'data' => [],
377 ];
378 }
379
380 $appoint->delete();
381
382 $response = [
383 'status_code' => 201,
384 'message' => __( 'Successfully deleted appointment', 'timetics' ),
385 'data' => [
386 'item' => $appoinment_id,
387 ],
388 ];
389
390 return rest_ensure_response( $response );
391 }
392
393 /**
394 * Delete multiples
395 *
396 * @param WP_Rest_Request $request
397 *
398 * @return JSON
399 */
400 public function bulk_delete( $request ) {
401 $appointments = json_decode( $request->get_body(), true );
402
403 foreach ( $appointments as $appoint ) {
404 $appoint = new Appointment( $appoint );
405
406 if ( ! $appoint->is_appointment() ) {
407 $data = [
408 'success' => 0,
409 'status' => 404,
410 'message' => __( 'Invalid appointment id.', 'timetics' ),
411 'data' => [],
412 ];
413
414 return new WP_HTTP_Response( $data, 404 );
415 }
416
417 $appoint->delete();
418 }
419
420 return rest_ensure_response( [
421 'success' => 1,
422 'status' => 201,
423 'message' => __( 'Successfully deleted all appointments', 'timetics' ),
424 'data' => [
425 'items' => $appointments,
426 ],
427 ] );
428 }
429
430 /**
431 * Duplicate appointment
432 *
433 * @since 1.0.0
434 *
435 * @param object $request
436 *
437 * @return JSON
438 */
439 public function duplicate_item( $request ) {
440 $appoinment_id = (int) $request['appointment_id'];
441 $appoint = new Appointment( $appoinment_id );
442
443 if ( ! $appoint->is_appointment() ) {
444 return new WP_HTTP_Response(
445 [
446 'success' => 0,
447 'status_code' => 404,
448 'message' => __( 'Invalid appointment id.', 'timetics' ),
449 'data' => [],
450 ],
451 404
452 );
453 }
454
455 $appoint->duplicate();
456
457 $item = $this->prepare_item( $appoint );
458
459 $response = [
460 'success' => 1,
461 'status_code' => 201,
462 'message' => __( 'Successfully duplicated appointment', 'timetics' ),
463 'data' => $item,
464 ];
465
466 return rest_ensure_response( $response );
467 }
468
469 /**
470 * Save appointment
471 *
472 * @param WP_Rest_Request $request
473 * @param integer $id Appointment id
474 *
475 * @return JSON Updated appoitment data
476 */
477 public function save_appointment( $request, $id = 0 ) {
478 $appoint = new Appointment( $id );
479
480 $data = json_decode( $request->get_body(), true );
481
482 $name = ! empty( $data['name'] ) ? sanitize_text_field( $data['name'] ) : $appoint->get_name();
483 $type = ! empty( $data['type'] ) ? sanitize_text_field( $data['type'] ) : $appoint->get_type();
484 $description = ! empty( $data['description'] ) ? sanitize_text_field( $data['description'] ) : '';
485 $staff = ! empty( $data['staff'] ) ? array_map( 'intval', $data['staff'] ) : $appoint->get_staff();
486 $locations = ! empty( $data['locations'] ) ? $data['locations'] : $appoint->get_locations();
487 $duration = ! empty( $data['duration'] ) ? sanitize_text_field( $data['duration'] ) : '';
488 $schedule = ! empty( $data['schedule'] ) ? $data['schedule'] : $appoint->get_schedule();
489 $price = ! empty( $data['price'] ) ? sanitize_text_field( $data['price'] ) : '';
490 $categories = ! empty( $data['categories'] ) ? $data['categories'] : '';
491 $buffer_time = ! empty( $data['buffer_time'] ) ? $data['buffer_time'] : '';
492 $timezone = ! empty( $data['timezone'] ) ? $data['timezone'] : '';
493 $availability = ! empty( $data['availability'] ) ? $data['availability'] : '';
494 $visibility = ! empty( $data['visibility'] ) ? strtolower( $data['visibility'] ) : 'enabled';
495 $notifications = ! empty( $data['notifications'] ) ? $data['notifications'] : '';
496 $capacity = ! empty( $data['capacity'] ) ? intval( $data['capacity'] ) : 1;
497 $action = $id ? 'update' : 'created';
498
499 $validate_data = [
500 'name' => $name,
501 'type' => $type,
502 'locations' => $locations,
503 'schedule' => $schedule,
504 'staff' => $staff,
505
506 ];
507 // Validate input data.
508 $validate = $this->validate( $validate_data, [
509 'name',
510 'type',
511 'locations',
512 'schedule',
513 'staff',
514 ] );
515
516 if ( is_wp_error( $validate ) ) {
517 $data = [
518 'status_code' => 409,
519 'success' => 0,
520 'message' => $validate->get_error_messages(),
521 'data' => [],
522 ];
523
524 return new WP_HTTP_Response( $data, 409 );
525 }
526
527 // Save appointment.
528
529 $appoint->set_props( [
530 'name' => $name,
531 'description' => $description,
532 'type' => $type,
533 'locations' => $locations,
534 'staff' => $staff,
535 'duration' => $duration,
536 'price' => $price,
537 'capacity' => $capacity,
538 'schedule' => $schedule,
539 'categories' => $categories,
540 'timezone' => $timezone,
541 'availability' => $availability,
542 'visibility' => $visibility,
543 'buffer_time' => $buffer_time,
544 'notifications' => $notifications,
545 ] );
546
547 $appoint->save();
548
549 // Prepare response data.
550 $item = $this->prepare_item( $appoint );
551
552 $response = [
553 'status_code' => 201,
554 'success' => 1,
555 'message' => sprintf( __( 'Succssfully %s appointment', 'timetics' ), $action ),
556 'data' => $item,
557 ];
558
559 return rest_ensure_response( $response );
560 }
561
562 /**
563 * Prepare item for response
564 *
565 * @param integer $appoinment_id
566 *
567 * @return array
568 */
569 public function prepare_item( $appoint_id ) {
570 $appointment = new Appointment( $appoint_id );
571 $dulicate = $appointment->get_duplicate_nuber();
572 $dulicate_text = $dulicate ? ' -Duplicate' : '';
573
574 return [
575 'id' => $appointment->get_id(),
576 'name' => $appointment->get_name() . $dulicate_text,
577 'image' => $appointment->get_image(),
578 // 'link' => $appointment->get_link(),
579 'description' => $appointment->get_description(),
580 'type' => $appointment->get_type(),
581 'locations' => $appointment->get_locations(),
582 'schedule' => $appointment->get_schedule(),
583 'price' => $appointment->get_price(),
584 'categories' => $appointment->get_categories(),
585 'staff' => $appointment->get_staff(),
586 'buffer_time' => $appointment->get_buffer_time(),
587 'timezone' => $appointment->get_timezone(),
588 'availability' => $appointment->get_availability(),
589 'visibility' => $appointment->get_visibility(),
590 'duration' => $appointment->get_duration(),
591 'notifications' => $appointment->get_notifications(),
592 'capacity' => $appointment->get_capacity(),
593 ];
594 }
595
596 }
597