PluginProbe
ShiftController Employee Shift Scheduling / 4.9.26
ShiftController Employee Shift Scheduling v4.9.26
4.9.97 4.9.96 4.9.95 4.9.74 4.9.75 4.9.76 4.9.77 4.9.78 4.9.84 4.9.85 4.9.87 4.9.91 4.9.92 trunk 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.2.4 All 38 releases
shiftcontroller / sh4 / api / rest.php

rest.php in ShiftController Employee Shift Scheduling 4.9.26, at sh4/api/rest.php

138 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if (! defined('ABSPATH')) exit; // Exit if accessed directly
2 class SH4_Api_Rest
3 {
4 public function __construct(
5 HC3_Hooks $hooks,
6 SH4_Api_Api $api
7 )
8 {
9 $this->api = $hooks->wrap( $api );
10 $this->self = $hooks->wrap( $this );
11
12 add_action( 'rest_api_init', array($this, 'routes') );
13 }
14
15 public function checkAdmin( $request )
16 {
17 $myAuthCode = get_option( 'sh4-rest_auth_code', '' );
18 $authCode = $request->get_header( 'X-WP-ShiftController-AuthCode' );
19
20 if( $authCode && ($authCode == $myAuthCode) ){
21 $ret = TRUE;
22 return $ret;
23 }
24
25 // try body params
26 $values = $request->get_body_params();
27 if( ! $values ){
28 $body = $request->get_body();
29 parse_str( $body, $values );
30 }
31
32 if( isset($values['X-WP-ShiftController-AuthCode']) ){
33 $authCode = $values['X-WP-ShiftController-AuthCode'];
34
35 if( $authCode && ($authCode == $myAuthCode) ){
36 $ret = TRUE;
37 return $ret;
38 }
39 }
40
41 $errors = 'not allowed: ' . join( array_keys($values) );
42 $ret = new WP_Error( 'error', $errors, array('status' => 500) );
43 sleep( 1 );
44
45 return $ret;
46 }
47
48 public function routes()
49 {
50 $enabledOptionName = 'sh4-rest_enabled';
51 $v = get_option( $enabledOptionName, 1 );
52 if( ! $v ){
53 return;
54 }
55
56 $startUrl = 'shiftcontroller/v4';
57
58 register_rest_route( $startUrl, '/shifts',
59 array(
60 array(
61 'methods' => WP_REST_Server::READABLE,
62 'callback' => array($this->self, 'shiftsGet'),
63 // 'permission_callback' => '__return_true',
64 'permission_callback' => array( $this->self, 'checkAdmin' ),
65 ),
66 array(
67 'methods' => WP_REST_Server::CREATABLE,
68 'callback' => array($this->self, 'shiftsCreate'),
69 'permission_callback' => array( $this->self, 'checkAdmin' ),
70 ),
71 )
72 );
73
74 register_rest_route( $startUrl, '/shifts/(?P<id>\d+)',
75 array(
76 array(
77 'methods' => WP_REST_Server::READABLE,
78 'callback' => array( $this->self, 'shiftsGetById' ),
79 // 'permission_callback' => '__return_true',
80 'permission_callback' => array( $this->self, 'checkAdmin' ),
81 ),
82 array(
83 'methods' => WP_REST_Server::DELETABLE,
84 'callback' => array( $this->self, 'shiftsDeleteById' ),
85 'permission_callback' => array( $this->self, 'checkAdmin' ),
86 ),
87 array(
88 'methods' => WP_REST_Server::EDITABLE,
89 'callback' => array( $this->self, 'shiftsUpdateById' ),
90 'permission_callback' => array( $this->self, 'checkAdmin' ),
91 ),
92 )
93 );
94 }
95
96 public function shiftsGet( $request )
97 {
98 $queryParams = $request->get_query_params();
99 $ret = $this->api->shiftsGet( $queryParams );
100 $ret = new WP_REST_Response( $ret );
101 return $ret;
102 }
103
104 public function shiftsGetById( $request )
105 {
106 $id = $request['id'];
107 return $this->api->shiftsGetById( $id );
108 }
109
110 public function shiftsCreate( $request )
111 {
112 $values = $request->get_body_params();
113 if( ! $values ){
114 $body = $request->get_body();
115 parse_str( $body, $values );
116 }
117
118 return $this->api->shiftsCreate( $request );
119 }
120
121 public function shiftsDeleteById( $request )
122 {
123 $id = $request['id'];
124 return $this->api->shiftsDeleteById( $id );
125 }
126
127 public function shiftsUpdateById( $request )
128 {
129 $id = $request['id'];
130 $values = $request->get_body_params();
131 if( ! $values ){
132 $body = $request->get_body();
133 parse_str( $body, $values );
134 }
135
136 return $this->api->shiftsUpdateById( $id, $values );
137 }
138 }