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 / base / abstracts / base-rest-controller.php

base-rest-controller.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.15, at base/abstracts/base-rest-controller.php

78 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Booktics\Abstracts;
4
5 use Booktics\Contracts\Hookable_Service_Contract;
6 use Exception;
7 use WP_Error;
8 use WP_HTTP_Response;
9 use WP_REST_Controller;
10
11 /**
12 * BaseRest Controller
13 *
14 * @package Booktics/Abstracts
15 */
16 abstract class Base_Rest_Controller extends WP_REST_Controller implements Hookable_Service_Contract {
17 /**
18 * Register routes
19 *
20 * @return void
21 */
22 public function register(): void {
23 add_action( 'rest_api_init', array( $this, 'register_routes' ) );
24 }
25
26 /**
27 * Register all routes
28 *
29 * @return void
30 */
31 public function register_routes(): void {
32 throw new Exception( 'Need to override register_routes method from child class' );
33 }
34
35 /**
36 * Send rest error
37 *
38 * @param string $message Error message
39 * @param integer $status_code Error status code
40 *
41 * @return WP_HTTP_Response
42 */
43 public function error( $message, $status_code = 422, $type = '', $details = '' ) {
44 $data = array(
45 'success' => 0,
46 'message' => $message,
47 'error' => array(
48 'code' => $status_code,
49 'type' => $type,
50 'details' => $details,
51 ),
52 );
53
54 return new WP_HTTP_Response( $data, $status_code );
55 }
56
57 /**
58 * Send rest response
59 *
60 * @param array $data Response data
61 *
62 * @return WP_HTTP_Response
63 */
64 public function response( $data, $message = '', $status_code = 200 ) {
65 if ( ! $message ) {
66 $message = __( 'Request was successful', 'booktics' );
67 }
68
69 $data = array(
70 'success' => 1,
71 'message' => $message,
72 'data' => $data,
73 );
74
75 return new WP_HTTP_Response( $data, $status_code );
76 }
77 }
78