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 / staffs / api-staff.php

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

561 lines 15.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Api Staff
4 *
5 * @package Timetics
6 */
7 namespace Timetics\Core\Staffs;
8
9 use Timetics\Base\Api;
10 use Timetics\Core\Integrations\Google\Client;
11 use Timetics\Core\Staffs\Staff;
12 use Timetics\Utils\Singleton;
13 use WP_Error;
14 use WP_HTTP_Response;
15 use WP_User_Query;
16
17 /**
18 * Class Api_Staff
19 */
20 class Api_Staff extends Api {
21 use Singleton;
22
23 /**
24 * Store namespace
25 *
26 * @var string
27 */
28 protected $namespace = 'timetics/v1';
29
30 /**
31 * Store rest base
32 *
33 * @var string
34 */
35 protected $rest_base = 'staffs';
36
37 /**
38 * Register rest route
39 *
40 * @return void
41 */
42 public function register_routes() {
43 register_rest_route(
44 $this->namespace, $this->rest_base, [
45 [
46 'methods' => \WP_REST_Server::READABLE,
47 'callback' => [ $this, 'get_items' ],
48 'permission_callback' => function () {
49 return true;
50 },
51 ],
52 [
53 'methods' => \WP_REST_Server::CREATABLE,
54 'callback' => [ $this, 'create_item' ],
55 'permission_callback' => function () {
56 return current_user_can( 'manage_timetics' );
57 },
58 ],
59 [
60 'methods' => \WP_REST_Server::DELETABLE,
61 'callback' => [ $this, 'bulk_delete' ],
62 'permission_callback' => function () {
63 return current_user_can( 'manage_timetics' );
64 },
65 ],
66 ]
67 );
68
69 /**
70 * Register route
71 *
72 * @var void
73 */
74 register_rest_route(
75 $this->namespace, '/' . $this->rest_base . '/(?P<staff_id>[\d]+)', [
76 [
77 'methods' => \WP_REST_Server::READABLE,
78 'callback' => [ $this, 'get_item' ],
79 'permission_callback' => function () {
80 return true;
81 },
82 ],
83 [
84 'methods' => \WP_REST_Server::EDITABLE,
85 'callback' => [ $this, 'update_item' ],
86 'permission_callback' => function () {
87 return current_user_can( 'manage_timetics' );
88 },
89 ],
90 [
91 'methods' => \WP_REST_Server::DELETABLE,
92 'callback' => [ $this, 'delete_item' ],
93 'permission_callback' => function () {
94 return current_user_can( 'manage_timetics' );
95 },
96 ],
97 ]
98 );
99
100 register_rest_route(
101 $this->namespace, $this->rest_base . '/search', [
102 [
103 'methods' => \WP_REST_Server::READABLE,
104 'callback' => [ $this, 'search_items' ],
105 'permission_callback' => function () {
106 return current_user_can( 'edit_posts' );
107 },
108 ],
109 ]
110 );
111
112 register_rest_route(
113 $this->namespace, $this->rest_base . '/(?P<staff_id>[\d]+)/integrations', [
114 [
115 'methods' => \WP_REST_Server::READABLE,
116 'callback' => [ $this, 'get_integrations' ],
117 'permission_callback' => function () {
118 return current_user_can( 'edit_posts' );
119 },
120 ],
121 ]
122 );
123
124 register_rest_route(
125 $this->namespace, $this->rest_base . '/(?P<staff_id>[\d]+)/integrations/auth-revoke', [
126 [
127 'methods' => \WP_REST_Server::READABLE,
128 'callback' => [ $this, 'auth_revoke' ],
129 'permission_callback' => function () {
130 return current_user_can( 'edit_posts' );
131 },
132 ],
133 ]
134 );
135 }
136 /**
137 * Get all appointments
138 *
139 * @param WP_Rest_Request $request
140 *
141 * @return JSON
142 */
143 public function get_items( $request ) {
144 $per_page = ! empty( $request['per_page'] ) ? intval( $request['per_page'] ) : 20;
145 $paged = ! empty( $request['paged'] ) ? intval( $request['paged'] ) : 1;
146
147 $staff = Staff::all(
148 [
149 'number' => $per_page,
150 'paged' => $paged,
151 ]
152 );
153
154 $items = [];
155
156 foreach ( $staff['items'] as $item ) {
157 $items[] = $this->prepare_item( $item->ID );
158 }
159
160 $data = [
161 'success' => 1,
162 'data' => [
163 'total' => $staff['total'],
164 'items' => $items,
165 ],
166 ];
167
168 return rest_ensure_response( $data );
169 }
170
171 /**
172 * Get single staff
173 *
174 * @param WP_Rest_Requesr $request
175 *
176 * @return JSON Single staff data
177 */
178 public function get_item( $request ) {
179 $staff_id = (int) $request['staff_id'];
180 $staff = new Staff( $staff_id );
181
182 if ( ! $staff->is_staff() ) {
183 return [
184 'status_code' => 404,
185 'message' => __( 'Invalid staff id.', 'timetics' ),
186 'data' => [],
187 ];
188 }
189
190 $response = [
191 'status_code' => 200,
192 'data' => $this->prepare_item( $staff ),
193 ];
194
195 return rest_ensure_response( $response );
196 }
197
198 /**
199 * Search staff
200 *
201 * @return JSON
202 */
203 public function search_items( $request ) {
204
205 // Prepare search args.
206 $per_page = ! empty( $request['per_page'] ) ? intval( $request['per_page'] ) : 20;
207 $paged = ! empty( $request['paged'] ) ? intval( $request['paged'] ) : 1;
208 $search = ! empty( $request['search'] ) ? sanitize_text_field( $request['search'] ) : '';
209
210 // Get search.
211 $users = new WP_User_Query(
212 array(
213 'role' => 'timetics-staff',
214 'number' => $per_page,
215 'paged' => $paged,
216
217 // @codingStandardsIgnoreStart
218 'meta_query' => array(
219 'relation' => 'OR',
220 array(
221 'key' => 'first_name',
222 'value' => $search,
223 'compare' => 'LIKE',
224 ),
225 array(
226 'key' => 'last_name',
227 'value' => $search,
228 'compare' => 'LIKE',
229 ),
230 array(
231 'key' => '_staff_email',
232 'value' => $search,
233 'compare' => 'LIKE',
234 ),
235 array(
236 'key' => '_staff_user_name',
237 'value' => $search,
238 'compare' => 'LIKE',
239 ),
240 array(
241 'key' => '_staff_phone',
242 'value' => $search,
243 'compare' => 'LIKE',
244 ),
245 ),
246 // @codingStandardsIgnoreEnd
247 )
248 );
249
250 // Prepare items for response.
251 $items = [];
252
253 foreach ( $users->get_results() as $item ) {
254 $items[] = $this->prepare_item( $item->ID );
255 }
256
257 $data = [
258 'success' => 1,
259 'status' => 200,
260 'data' => [
261 'total' => $users->get_total(),
262 'items' => $items,
263 ],
264 ];
265
266 return rest_ensure_response( $data );
267 }
268
269 /**
270 * Create staff
271 *
272 * @param WP_Rest_Request $request
273 *
274 * @return JSON | WP_Error
275 */
276 public function create_item( $request ) {
277 return $this->save_staff( $request );
278 }
279
280 /**
281 * Update staff
282 *
283 * @param WP_Rest_Request $request
284 *
285 * @return JSON | WP_Error
286 */
287 public function update_item( $request ) {
288 $staff_id = (int) $request['staff_id'];
289 $staff = new Staff( $staff_id );
290
291 if ( ! $staff->is_staff() ) {
292 $data = [
293 'status_code' => 404,
294 'message' => __( 'Invalid staff id.', 'timetics' ),
295 'data' => [],
296 ];
297
298 return new WP_HTTP_Response( $data, 404 );
299 }
300
301 return $this->save_staff( $request, $staff_id );
302 }
303
304 /**
305 * Delete staff
306 *
307 * @param WP_Rest_Request $request
308 *
309 * @return JSON
310 */
311 public function delete_item( $request ) {
312 $staff_id = (int) $request['staff_id'];
313 $staff = new Staff( $staff_id );
314
315 if ( ! $staff->is_staff() ) {
316 $data = [
317 'status_code' => 404,
318 'message' => __( 'Invalid staff id.', 'timetics' ),
319 'data' => [],
320 ];
321
322 return new WP_HTTP_Response( $data, 404 );
323 }
324
325 $staff->delete();
326
327 $response = [
328 'status_code' => 200,
329 'message' => __( 'Successfully deleted staff', 'timetics' ),
330 'data' => [],
331 ];
332
333 return rest_ensure_response( $response );
334 }
335
336 /**
337 * Delete multiples
338 *
339 * @param WP_Rest_Request $request
340 *
341 * @return JSON
342 */
343 public function bulk_delete( $request ) {
344 $staffs = json_decode( $request->get_body(), true );
345
346 foreach ( $staffs as $staff ) {
347 $staff = new Staff( $staff );
348
349 if ( ! $staff->is_staff() ) {
350 $data = [
351 'success' => 1,
352 'status' => 404,
353 'message' => __( 'Invalid staff id.', 'timetics' ),
354 'data' => [],
355 ];
356
357 return new WP_HTTP_Response( $data, 404 );
358 }
359
360 $staff->delete();
361 }
362
363 return [
364 'success' => 1,
365 'status' => 200,
366 'message' => __( 'Successfully deleted staff', 'timetics' ),
367 'data' => [
368 'items' => $staffs,
369 ],
370 ];
371 }
372
373 /**
374 * Get staff integrations
375 *
376 * @param WP_Rest_Request $request
377 *
378 * @return JSON
379 */
380 public function get_integrations( $request ) {
381 $staff_id = (int) $request['staff_id'];
382 $staff = new Staff( $staff_id );
383
384 if ( ! $staff->is_staff() ) {
385 $data = [
386 'status_code' => 404,
387 'message' => __( 'Invalid staff id.', 'timetics' ),
388 'data' => [],
389 ];
390
391 return new WP_HTTP_Response( $data, 404 );
392 }
393
394 $integrations = timetics_get_staff_integrations( $staff_id );
395
396 return rest_ensure_response( $integrations );
397 }
398
399 /**
400 * Remove staff authentication for integrations
401 *
402 * @param WP_Rest_Request $request
403 *
404 * @return JSON
405 */
406 public function auth_revoke( $request ) {
407 $staff_id = (int) $request['staff_id'];
408 $staff = new Staff( $staff_id );
409 $integration = ! empty( $request['integration_name'] ) ? sanitize_text_field( $request['integration_name'] ) : '';
410
411 if ( ! $staff->is_staff() ) {
412 $data = [
413 'status_code' => 404,
414 'message' => __( 'Invalid staff id.', 'timetics' ),
415 'data' => [],
416 ];
417
418 return new WP_HTTP_Response( $data, 404 );
419 }
420
421 $token = timetics_get_google_access_token( $staff_id );
422 $client = new Client();
423
424 if ( 'google-auth' === $integration ) {
425 $revoked = $client->revoke( $token );
426
427 if ( ! $revoked ) {
428 $data = [
429 'success' => 0,
430 'status_code' => 409,
431 'message' => __( 'Something went wrong, please try again', 'timetics' ),
432 ];
433
434 return new WP_HTTP_Response( $data, 409 );
435 }
436
437 update_user_meta( $staff_id, 'timetics_google_auth_code', '' );
438 }
439
440 return [
441 'success' => 1,
442 'status_code' => 200,
443 'message' => __( 'Successfully disconnected', 'timetics' ),
444 ];
445 }
446
447 /**
448 * Save staff
449 *
450 * @param WP_Rest_Request $request
451 * @param integer $id [$id description]
452 *
453 * @return JSON | WP_Error
454 */
455 public function save_staff( $request, $id = 0 ) {
456 $data = json_decode( $request->get_body(), true );
457
458 $first_name = ! empty( $data['first_name'] ) ? sanitize_text_field( $data['first_name'] ) : '';
459 $last_name = ! empty( $data['last_name'] ) ? sanitize_text_field( $data['last_name'] ) : '';
460 $email = ! empty( $data['email'] ) ? $data['email'] : '';
461 $phone = ! empty( $data['phone'] ) ? $data['phone'] : '';
462 $service = ! empty( $data['service'] ) ? $data['service'] : [];
463 $schedule = ! empty( $data['schedule'] ) ? $data['schedule'] : [];
464 $image = ! empty( $data['image'] ) ? intval( $data['image'] ) : '';
465 $password = ! empty( $data['password'] ) ? sanitize_text_field( $data['password'] ) : '';
466 $action = $id ? 'updated' : 'created';
467
468 // Validate input data.
469 $validate = $this->validate(
470 $data, [
471 'first_name',
472 'last_name',
473 'email',
474 ]
475 );
476
477 if ( is_wp_error( $validate ) ) {
478 $data = [
479 'success' => 0,
480 'status_code' => 409,
481 'message' => $validate->get_error_messages(),
482 'data' => [],
483 ];
484
485 return new WP_HTTP_Response( $data, 409 );
486 }
487
488 // Save staff data.
489 $staff = new Staff( $id );
490
491 $args = [
492 'first_name' => $first_name,
493 'last_name' => $last_name,
494 'user_email' => $email,
495 'user_login' => $this->generate_username( $email ),
496 'phone' => $phone,
497 'image' => $image,
498 'schedule' => $schedule,
499 'user_pass' => $password,
500 ];
501
502 if ( $id ) {
503 $staff_id = $staff->update( $args );
504 } else {
505 $staff_id = $staff->create( $args );
506 }
507
508 if ( is_wp_error( $staff_id ) ) {
509 return [
510 'success' => 0,
511 'status' => 409,
512 'message' => $staff_id->get_error_message(),
513 'data' => [],
514 ];
515 }
516 // Prepare for response.
517 $item = $this->prepare_item( $staff );
518
519 $data = [
520 'success' => 1,
521 'status' => 200,
522 /* translators: Action */
523 'message' => sprintf( __( 'Successfully %s staff', 'timetics' ), $action ),
524 'data' => $item,
525 ];
526
527 return rest_ensure_response( $data );
528 }
529
530 /**
531 * Generate username from email
532 *
533 * @param string $email
534 *
535 * @return string
536 */
537 public function generate_username( $email ) {
538 $username = strtok( $email, '@' );
539
540 if ( username_exists( $username ) ) {
541 $username = $username . wp_rand( 10, 100 );
542 }
543
544 return $username;
545 }
546
547 /**
548 * Prepare item
549 *
550 * @param integer | Staff $staff_id Staff Id
551 *
552 * @return array staff data
553 */
554 public function prepare_item( $staff ) {
555 $staff = new Staff( $staff );
556
557 return $staff->get_data();
558 }
559
560 }
561