PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.10
Timetics – Appointment Booking Calendar & Scheduling v1.0.10
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 / customers / api-customer.php

api-customer.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.10, at core/customers/api-customer.php

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