| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
class SH4_Api_Rest |
| 3 |
{ |
| 4 |
public $self, $api; |
| 5 |
|
| 6 |
public function __construct( |
| 7 |
HC3_Hooks $hooks, |
| 8 |
SH4_Api_Api $api |
| 9 |
) |
| 10 |
{ |
| 11 |
$this->api = $hooks->wrap( $api ); |
| 12 |
$this->self = $hooks->wrap( $this ); |
| 13 |
|
| 14 |
add_action( 'rest_api_init', array($this, 'routes') ); |
| 15 |
} |
| 16 |
|
| 17 |
public function checkAdmin( $request ) |
| 18 |
{ |
| 19 |
$myAuthCode = get_option( 'sh4-rest_auth_code', '' ); |
| 20 |
$authCode = $request->get_header( 'X-WP-ShiftController-AuthCode' ); |
| 21 |
|
| 22 |
if( $authCode && ($authCode == $myAuthCode) ){ |
| 23 |
$ret = true; |
| 24 |
return $ret; |
| 25 |
} |
| 26 |
|
| 27 |
// try body params |
| 28 |
$isJson = wp_is_json_request(); |
| 29 |
$values = $isJson ? $request->get_json_params() : $request->get_body_params(); |
| 30 |
|
| 31 |
if( ! $values ){ |
| 32 |
$body = $request->get_body(); |
| 33 |
parse_str( $body, $values ); |
| 34 |
} |
| 35 |
|
| 36 |
if( isset($values['X-WP-ShiftController-AuthCode']) ){ |
| 37 |
$authCode = $values['X-WP-ShiftController-AuthCode']; |
| 38 |
if( $authCode && ($authCode == $myAuthCode) ){ |
| 39 |
$ret = true; |
| 40 |
return $ret; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
// try get params |
| 45 |
$values = $request->get_query_params(); |
| 46 |
if( isset($values['X-WP-ShiftController-AuthCode']) ){ |
| 47 |
$authCode = $values['X-WP-ShiftController-AuthCode']; |
| 48 |
if( $authCode && ($authCode == $myAuthCode) ){ |
| 49 |
$ret = true; |
| 50 |
return $ret; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
$errors = 'not allowed: ' . join( array_keys($values) ); |
| 55 |
$ret = new WP_Error( 'error', $errors, array('status' => 500) ); |
| 56 |
sleep( 1 ); |
| 57 |
|
| 58 |
return $ret; |
| 59 |
} |
| 60 |
|
| 61 |
public function routes() |
| 62 |
{ |
| 63 |
$enabledOptionName = 'sh4-rest_enabled'; |
| 64 |
$v = get_option( $enabledOptionName, 1 ); |
| 65 |
if( ! $v ){ |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
$startUrl = 'shiftcontroller/v4'; |
| 70 |
|
| 71 |
register_rest_route( $startUrl, '/calendars', |
| 72 |
array( |
| 73 |
array( |
| 74 |
'methods' => WP_REST_Server::READABLE, |
| 75 |
'callback' => array($this->self, 'calendarsGet'), |
| 76 |
// 'permission_callback' => '__return_true', |
| 77 |
'permission_callback' => array( $this->self, 'checkAdmin' ), |
| 78 |
), |
| 79 |
) |
| 80 |
); |
| 81 |
|
| 82 |
register_rest_route( $startUrl, '/employees', |
| 83 |
array( |
| 84 |
array( |
| 85 |
'methods' => WP_REST_Server::READABLE, |
| 86 |
'callback' => array($this->self, 'employeesGet'), |
| 87 |
// 'permission_callback' => '__return_true', |
| 88 |
'permission_callback' => array( $this->self, 'checkAdmin' ), |
| 89 |
), |
| 90 |
array( |
| 91 |
'methods' => WP_REST_Server::CREATABLE, |
| 92 |
'callback' => array($this->self, 'employeesCreate'), |
| 93 |
'permission_callback' => array( $this->self, 'checkAdmin' ), |
| 94 |
), |
| 95 |
) |
| 96 |
); |
| 97 |
|
| 98 |
register_rest_route( $startUrl, '/employees/(?P<id>\d+)', |
| 99 |
array( |
| 100 |
array( |
| 101 |
'methods' => WP_REST_Server::READABLE, |
| 102 |
'callback' => array( $this->self, 'employeesGetById' ), |
| 103 |
// 'permission_callback' => '__return_true', |
| 104 |
'permission_callback' => array( $this->self, 'checkAdmin' ), |
| 105 |
), |
| 106 |
) |
| 107 |
); |
| 108 |
|
| 109 |
register_rest_route( $startUrl, '/calendars/(?P<id>\d+)/employees', |
| 110 |
array( |
| 111 |
array( |
| 112 |
'methods' => WP_REST_Server::READABLE, |
| 113 |
'callback' => array( $this->self, 'employeesGetByCalendarId' ), |
| 114 |
// 'permission_callback' => '__return_true', |
| 115 |
'permission_callback' => array( $this->self, 'checkAdmin' ), |
| 116 |
), |
| 117 |
) |
| 118 |
); |
| 119 |
|
| 120 |
register_rest_route( $startUrl, '/employees/(?P<id>\d+)/calendars', |
| 121 |
array( |
| 122 |
array( |
| 123 |
'methods' => WP_REST_Server::READABLE, |
| 124 |
'callback' => array( $this->self, 'calendarsGetByEmployeeId' ), |
| 125 |
// 'permission_callback' => '__return_true', |
| 126 |
'permission_callback' => array( $this->self, 'checkAdmin' ), |
| 127 |
), |
| 128 |
) |
| 129 |
); |
| 130 |
|
| 131 |
register_rest_route( $startUrl, '/users/(?P<id>\d+)/employee', |
| 132 |
array( |
| 133 |
array( |
| 134 |
'methods' => WP_REST_Server::READABLE, |
| 135 |
'callback' => array( $this->self, 'employeesGetByUserId' ), |
| 136 |
// 'permission_callback' => '__return_true', |
| 137 |
'permission_callback' => array( $this->self, 'checkAdmin' ), |
| 138 |
), |
| 139 |
) |
| 140 |
); |
| 141 |
|
| 142 |
register_rest_route( $startUrl, '/available-employees', |
| 143 |
array( |
| 144 |
array( |
| 145 |
'methods' => WP_REST_Server::READABLE, |
| 146 |
'callback' => array($this->self, 'availableEmployeesGet'), |
| 147 |
'permission_callback' => array( $this->self, 'checkAdmin' ), |
| 148 |
), |
| 149 |
) |
| 150 |
); |
| 151 |
|
| 152 |
register_rest_route( $startUrl, '/shifts', |
| 153 |
array( |
| 154 |
array( |
| 155 |
'methods' => WP_REST_Server::READABLE, |
| 156 |
'callback' => array($this->self, 'shiftsGet'), |
| 157 |
// 'permission_callback' => '__return_true', |
| 158 |
'permission_callback' => array( $this->self, 'checkAdmin' ), |
| 159 |
), |
| 160 |
array( |
| 161 |
'methods' => WP_REST_Server::CREATABLE, |
| 162 |
'callback' => array($this->self, 'shiftsCreate'), |
| 163 |
'permission_callback' => array( $this->self, 'checkAdmin' ), |
| 164 |
), |
| 165 |
) |
| 166 |
); |
| 167 |
|
| 168 |
register_rest_route( $startUrl, '/shifts/(?P<id>\d+)', |
| 169 |
array( |
| 170 |
array( |
| 171 |
'methods' => WP_REST_Server::READABLE, |
| 172 |
'callback' => array( $this->self, 'shiftsGetById' ), |
| 173 |
// 'permission_callback' => '__return_true', |
| 174 |
'permission_callback' => array( $this->self, 'checkAdmin' ), |
| 175 |
), |
| 176 |
array( |
| 177 |
'methods' => WP_REST_Server::DELETABLE, |
| 178 |
'callback' => array( $this->self, 'shiftsDeleteById' ), |
| 179 |
'permission_callback' => array( $this->self, 'checkAdmin' ), |
| 180 |
), |
| 181 |
array( |
| 182 |
'methods' => WP_REST_Server::EDITABLE, |
| 183 |
'callback' => array( $this->self, 'shiftsUpdateById' ), |
| 184 |
'permission_callback' => array( $this->self, 'checkAdmin' ), |
| 185 |
), |
| 186 |
) |
| 187 |
); |
| 188 |
|
| 189 |
register_rest_route( $startUrl, '/employees/(?P<eid>\d+)/calendars/(?P<cid>\d+)', |
| 190 |
array( |
| 191 |
array( |
| 192 |
'methods' => WP_REST_Server::DELETABLE, |
| 193 |
'callback' => array( $this->self, 'employeesRemoveFromCalendar' ), |
| 194 |
'permission_callback' => array( $this->self, 'checkAdmin' ), |
| 195 |
), |
| 196 |
array( |
| 197 |
'methods' => WP_REST_Server::CREATABLE, |
| 198 |
'callback' => array( $this->self, 'employeesAddToCalendar' ), |
| 199 |
'permission_callback' => array( $this->self, 'checkAdmin' ), |
| 200 |
), |
| 201 |
) |
| 202 |
); |
| 203 |
|
| 204 |
register_rest_route( $startUrl, '/calendars/(?P<eid>\d+)/employees/(?P<cid>\d+)', |
| 205 |
array( |
| 206 |
array( |
| 207 |
'methods' => WP_REST_Server::DELETABLE, |
| 208 |
'callback' => array( $this->self, 'employeesRemoveFromCalendar' ), |
| 209 |
'permission_callback' => array( $this->self, 'checkAdmin' ), |
| 210 |
), |
| 211 |
array( |
| 212 |
'methods' => WP_REST_Server::CREATABLE, |
| 213 |
'callback' => array( $this->self, 'employeesAddToCalendar' ), |
| 214 |
'permission_callback' => array( $this->self, 'checkAdmin' ), |
| 215 |
), |
| 216 |
) |
| 217 |
); |
| 218 |
} |
| 219 |
|
| 220 |
public function availableEmployeesGet( $request ) |
| 221 |
{ |
| 222 |
$queryParams = $request->get_query_params(); |
| 223 |
$ret = $this->api->availableEmployeesGet( $queryParams ); |
| 224 |
$ret = new WP_REST_Response( $ret ); |
| 225 |
return $ret; |
| 226 |
} |
| 227 |
|
| 228 |
public function shiftsGet( $request ) |
| 229 |
{ |
| 230 |
$queryParams = $request->get_query_params(); |
| 231 |
$ret = $this->api->shiftsGet( $queryParams ); |
| 232 |
$ret = new WP_REST_Response( $ret ); |
| 233 |
return $ret; |
| 234 |
} |
| 235 |
|
| 236 |
public function shiftsGetById( $request ) |
| 237 |
{ |
| 238 |
$id = $request['id']; |
| 239 |
return $this->api->shiftsGetById( $id ); |
| 240 |
} |
| 241 |
|
| 242 |
public function shiftsCreate( $request ) |
| 243 |
{ |
| 244 |
$isJson = wp_is_json_request(); |
| 245 |
$values = $isJson ? $request->get_json_params() : $request->get_body_params(); |
| 246 |
|
| 247 |
if( ! $values ){ |
| 248 |
$body = $request->get_body(); |
| 249 |
parse_str( $body, $values ); |
| 250 |
} |
| 251 |
|
| 252 |
return $this->api->shiftsCreate( $request ); |
| 253 |
} |
| 254 |
|
| 255 |
public function shiftsDeleteById( $request ) |
| 256 |
{ |
| 257 |
$id = $request['id']; |
| 258 |
return $this->api->shiftsDeleteById( $id ); |
| 259 |
} |
| 260 |
|
| 261 |
public function shiftsUpdateById( $request ) |
| 262 |
{ |
| 263 |
$id = $request['id']; |
| 264 |
|
| 265 |
$isJson = wp_is_json_request(); |
| 266 |
$values = $isJson ? $request->get_json_params() : $request->get_body_params(); |
| 267 |
|
| 268 |
if( ! $values ){ |
| 269 |
$body = $request->get_body(); |
| 270 |
parse_str( $body, $values ); |
| 271 |
} |
| 272 |
|
| 273 |
return $this->api->shiftsUpdateById( $id, $values ); |
| 274 |
} |
| 275 |
|
| 276 |
public function calendarsGet( $request ) |
| 277 |
{ |
| 278 |
$queryParams = $request->get_query_params(); |
| 279 |
$ret = $this->api->calendarsGet( $queryParams ); |
| 280 |
$ret = new WP_REST_Response( $ret ); |
| 281 |
return $ret; |
| 282 |
} |
| 283 |
|
| 284 |
public function employeesGet( $request ) |
| 285 |
{ |
| 286 |
$queryParams = $request->get_query_params(); |
| 287 |
$ret = $this->api->employeesGet( $queryParams ); |
| 288 |
$ret = new WP_REST_Response( $ret ); |
| 289 |
return $ret; |
| 290 |
} |
| 291 |
|
| 292 |
public function employeesGetById( $request ) |
| 293 |
{ |
| 294 |
$id = $request['id']; |
| 295 |
return $this->api->employeesGetById( $id ); |
| 296 |
} |
| 297 |
|
| 298 |
public function employeesCreate( $request ) |
| 299 |
{ |
| 300 |
$isJson = wp_is_json_request(); |
| 301 |
$values = $isJson ? $request->get_json_params() : $request->get_body_params(); |
| 302 |
|
| 303 |
if( ! $values ){ |
| 304 |
$body = $request->get_body(); |
| 305 |
parse_str( $body, $values ); |
| 306 |
} |
| 307 |
|
| 308 |
return $this->api->employeesCreate( $request ); |
| 309 |
} |
| 310 |
|
| 311 |
public function employeesGetByUserId( $request ) |
| 312 |
{ |
| 313 |
$id = $request['id']; |
| 314 |
return $this->api->employeesGetByUserId( $id ); |
| 315 |
} |
| 316 |
|
| 317 |
public function employeesGetByCalendarId( $request ) |
| 318 |
{ |
| 319 |
$id = $request['id']; |
| 320 |
$ret = $this->api->employeesGetByCalendarId( $id ); |
| 321 |
return $ret; |
| 322 |
} |
| 323 |
|
| 324 |
public function calendarsGetByEmployeeId( $request ) |
| 325 |
{ |
| 326 |
$id = $request['id']; |
| 327 |
$ret = $this->api->calendarsGetByEmployeeId( $id ); |
| 328 |
return $ret; |
| 329 |
} |
| 330 |
|
| 331 |
public function employeesAddToCalendar( $request ) |
| 332 |
{ |
| 333 |
$eid = $request['eid']; |
| 334 |
$cid = $request['cid']; |
| 335 |
$ret = $this->api->employeesAddToCalendar( $eid, $cid ); |
| 336 |
return $ret; |
| 337 |
} |
| 338 |
|
| 339 |
public function employeesRemoveFromCalendar( $request ) |
| 340 |
{ |
| 341 |
$eid = $request['eid']; |
| 342 |
$cid = $request['cid']; |
| 343 |
$ret = $this->api->employeesRemoveFromCalendar( $eid, $cid ); |
| 344 |
return $ret; |
| 345 |
} |
| 346 |
} |