PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.19
Booktics – Appointment Booking Calendar for Service Businesses v1.0.19
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 1.0.4 1.0.5 1.0.6 1.0.7 All 27 releases
booktics / base / abstracts / base-rest-controller.php

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

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