| 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, '/available-employees', |
| 72 |
array( |
| 73 |
array( |
| 74 |
'methods' => WP_REST_Server::READABLE, |
| 75 |
'callback' => array($this->self, 'availableEmployeesGet'), |
| 76 |
'permission_callback' => array( $this->self, 'checkAdmin' ), |
| 77 |
), |
| 78 |
) |
| 79 |
); |
| 80 |
|
| 81 |
register_rest_route( $startUrl, '/shifts', |
| 82 |
array( |
| 83 |
array( |
| 84 |
'methods' => WP_REST_Server::READABLE, |
| 85 |
'callback' => array($this->self, 'shiftsGet'), |
| 86 |
// 'permission_callback' => '__return_true', |
| 87 |
'permission_callback' => array( $this->self, 'checkAdmin' ), |
| 88 |
), |
| 89 |
array( |
| 90 |
'methods' => WP_REST_Server::CREATABLE, |
| 91 |
'callback' => array($this->self, 'shiftsCreate'), |
| 92 |
'permission_callback' => array( $this->self, 'checkAdmin' ), |
| 93 |
), |
| 94 |
) |
| 95 |
); |
| 96 |
|
| 97 |
register_rest_route( $startUrl, '/shifts/(?P<id>\d+)', |
| 98 |
array( |
| 99 |
array( |
| 100 |
'methods' => WP_REST_Server::READABLE, |
| 101 |
'callback' => array( $this->self, 'shiftsGetById' ), |
| 102 |
// 'permission_callback' => '__return_true', |
| 103 |
'permission_callback' => array( $this->self, 'checkAdmin' ), |
| 104 |
), |
| 105 |
array( |
| 106 |
'methods' => WP_REST_Server::DELETABLE, |
| 107 |
'callback' => array( $this->self, 'shiftsDeleteById' ), |
| 108 |
'permission_callback' => array( $this->self, 'checkAdmin' ), |
| 109 |
), |
| 110 |
array( |
| 111 |
'methods' => WP_REST_Server::EDITABLE, |
| 112 |
'callback' => array( $this->self, 'shiftsUpdateById' ), |
| 113 |
'permission_callback' => array( $this->self, 'checkAdmin' ), |
| 114 |
), |
| 115 |
) |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
public function availableEmployeesGet( $request ) |
| 120 |
{ |
| 121 |
$queryParams = $request->get_query_params(); |
| 122 |
$ret = $this->api->availableEmployeesGet( $queryParams ); |
| 123 |
$ret = new WP_REST_Response( $ret ); |
| 124 |
return $ret; |
| 125 |
} |
| 126 |
|
| 127 |
public function shiftsGet( $request ) |
| 128 |
{ |
| 129 |
$queryParams = $request->get_query_params(); |
| 130 |
$ret = $this->api->shiftsGet( $queryParams ); |
| 131 |
$ret = new WP_REST_Response( $ret ); |
| 132 |
return $ret; |
| 133 |
} |
| 134 |
|
| 135 |
public function shiftsGetById( $request ) |
| 136 |
{ |
| 137 |
$id = $request['id']; |
| 138 |
return $this->api->shiftsGetById( $id ); |
| 139 |
} |
| 140 |
|
| 141 |
public function shiftsCreate( $request ) |
| 142 |
{ |
| 143 |
$isJson = wp_is_json_request(); |
| 144 |
$values = $isJson ? $request->get_json_params() : $request->get_body_params(); |
| 145 |
|
| 146 |
if( ! $values ){ |
| 147 |
$body = $request->get_body(); |
| 148 |
parse_str( $body, $values ); |
| 149 |
} |
| 150 |
|
| 151 |
return $this->api->shiftsCreate( $request ); |
| 152 |
} |
| 153 |
|
| 154 |
public function shiftsDeleteById( $request ) |
| 155 |
{ |
| 156 |
$id = $request['id']; |
| 157 |
return $this->api->shiftsDeleteById( $id ); |
| 158 |
} |
| 159 |
|
| 160 |
public function shiftsUpdateById( $request ) |
| 161 |
{ |
| 162 |
$id = $request['id']; |
| 163 |
|
| 164 |
$isJson = wp_is_json_request(); |
| 165 |
$values = $isJson ? $request->get_json_params() : $request->get_body_params(); |
| 166 |
|
| 167 |
if( ! $values ){ |
| 168 |
$body = $request->get_body(); |
| 169 |
parse_str( $body, $values ); |
| 170 |
} |
| 171 |
|
| 172 |
return $this->api->shiftsUpdateById( $id, $values ); |
| 173 |
} |
| 174 |
} |