| 1 |
<?php |
| 2 |
namespace WeDevs\ERP\API; |
| 3 |
|
| 4 |
use WP_REST_Server; |
| 5 |
use WP_REST_Response; |
| 6 |
use WP_Error; |
| 7 |
|
| 8 |
abstract class REST_Controller { |
| 9 |
|
| 10 |
/** |
| 11 |
* The namespace of this controller's route. |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
protected $namespace; |
| 16 |
|
| 17 |
/** |
| 18 |
* The base of this controller's route. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
protected $rest_base; |
| 23 |
|
| 24 |
/** |
| 25 |
* Prepare a response for inserting into a collection. |
| 26 |
* |
| 27 |
* @param WP_REST_Response $response Response object. |
| 28 |
* @return array Response data, ready for insertion into collection data. |
| 29 |
*/ |
| 30 |
public function prepare_response_for_collection( $response ) { |
| 31 |
if ( ! ( $response instanceof WP_REST_Response ) ) { |
| 32 |
return $response; |
| 33 |
} |
| 34 |
|
| 35 |
$data = (array) $response->get_data(); |
| 36 |
$server = rest_get_server(); |
| 37 |
|
| 38 |
if ( method_exists( $server, 'get_compact_response_links' ) ) { |
| 39 |
$links = call_user_func( [ $server, 'get_compact_response_links' ], $response ); |
| 40 |
} else { |
| 41 |
$links = call_user_func( [ $server, 'get_response_links' ], $response ); |
| 42 |
} |
| 43 |
|
| 44 |
if ( ! empty( $links ) ) { |
| 45 |
$data['_links'] = $links; |
| 46 |
} |
| 47 |
|
| 48 |
return $data; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get the item's schema, conforming to JSON Schema. |
| 53 |
* |
| 54 |
* @return array |
| 55 |
*/ |
| 56 |
public function get_item_schema() { |
| 57 |
return []; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get the item's schema for display / public consumption purposes. |
| 62 |
* |
| 63 |
* @since 1.1.10 |
| 64 |
* @since 1.2.1 Return empty array if no schema found |
| 65 |
* |
| 66 |
* @return array |
| 67 |
*/ |
| 68 |
public function get_public_item_schema() { |
| 69 |
$schema = $this->get_item_schema(); |
| 70 |
|
| 71 |
if ( empty( $schema ) ) { |
| 72 |
return []; |
| 73 |
} |
| 74 |
|
| 75 |
foreach ( $schema['properties'] as &$property ) { |
| 76 |
if ( isset( $property['arg_options'] ) ) { |
| 77 |
unset( $property['arg_options'] ); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
return $schema; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Get the query params for collections. |
| 86 |
* |
| 87 |
* @return array |
| 88 |
*/ |
| 89 |
public function get_collection_params() { |
| 90 |
return [ |
| 91 |
'context' => $this->get_context_param(), |
| 92 |
'page' => [ |
| 93 |
'description' => __( 'Current page of the collection.' ), |
| 94 |
'type' => 'integer', |
| 95 |
'default' => 1, |
| 96 |
'sanitize_callback' => 'absint', |
| 97 |
'validate_callback' => 'rest_validate_request_arg', |
| 98 |
'minimum' => 1, |
| 99 |
], |
| 100 |
'per_page' => [ |
| 101 |
'description' => __( 'Maximum number of items to be returned in result set.' ), |
| 102 |
'type' => 'integer', |
| 103 |
'default' => 20, |
| 104 |
'minimum' => 1, |
| 105 |
'maximum' => 100, |
| 106 |
'sanitize_callback' => 'absint', |
| 107 |
'validate_callback' => 'rest_validate_request_arg', |
| 108 |
], |
| 109 |
'search' => [ |
| 110 |
'description' => __( 'Limit results to those matching a string.' ), |
| 111 |
'type' => 'string', |
| 112 |
'sanitize_callback' => 'sanitize_text_field', |
| 113 |
'validate_callback' => 'rest_validate_request_arg', |
| 114 |
], |
| 115 |
]; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Get the magical context param. |
| 120 |
* |
| 121 |
* Ensures consistent description between endpoints, and populates enum from schema. |
| 122 |
* |
| 123 |
* @param array $args |
| 124 |
* @return array |
| 125 |
*/ |
| 126 |
public function get_context_param( $args = [] ) { |
| 127 |
$param_details = [ |
| 128 |
'description' => __( 'Scope under which the request is made; determines fields present in response.' ), |
| 129 |
'type' => 'string', |
| 130 |
'sanitize_callback' => 'sanitize_key', |
| 131 |
'validate_callback' => 'rest_validate_request_arg', |
| 132 |
]; |
| 133 |
$schema = $this->get_item_schema(); |
| 134 |
if ( empty( $schema['properties'] ) ) { |
| 135 |
return array_merge( $param_details, $args ); |
| 136 |
} |
| 137 |
$contexts = []; |
| 138 |
foreach ( $schema['properties'] as $attributes ) { |
| 139 |
if ( ! empty( $attributes['context'] ) ) { |
| 140 |
$contexts = array_merge( $contexts, $attributes['context'] ); |
| 141 |
} |
| 142 |
} |
| 143 |
if ( ! empty( $contexts ) ) { |
| 144 |
$param_details['enum'] = array_unique( $contexts ); |
| 145 |
rsort( $param_details['enum'] ); |
| 146 |
} |
| 147 |
return array_merge( $param_details, $args ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Get the object type this controller is responsible for managing. |
| 152 |
* |
| 153 |
* @return string |
| 154 |
*/ |
| 155 |
protected function get_object_type() { |
| 156 |
$schema = $this->get_item_schema(); |
| 157 |
|
| 158 |
if ( ! $schema || ! isset( $schema['title'] ) ) { |
| 159 |
return null; |
| 160 |
} |
| 161 |
|
| 162 |
return $schema['title']; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Get an array of endpoint arguments from the item schema for the controller. |
| 167 |
* |
| 168 |
* @param string $method HTTP method of the request. The arguments |
| 169 |
* for `CREATABLE` requests are checked for required |
| 170 |
* values and may fall-back to a given default, this |
| 171 |
* is not done on `EDITABLE` requests. Default is |
| 172 |
* WP_REST_Server::CREATABLE. |
| 173 |
* @return array $endpoint_args |
| 174 |
*/ |
| 175 |
public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CREATABLE ) { |
| 176 |
|
| 177 |
$schema = $this->get_item_schema(); |
| 178 |
$schema_properties = ! empty( $schema['properties'] ) ? $schema['properties'] : []; |
| 179 |
$endpoint_args = []; |
| 180 |
|
| 181 |
foreach ( $schema_properties as $field_id => $params ) { |
| 182 |
|
| 183 |
// Arguments specified as `readonly` are not allowed to be set. |
| 184 |
if ( ! empty( $params['readonly'] ) ) { |
| 185 |
continue; |
| 186 |
} |
| 187 |
|
| 188 |
$endpoint_args[ $field_id ] = [ |
| 189 |
'validate_callback' => 'rest_validate_request_arg', |
| 190 |
'sanitize_callback' => 'rest_sanitize_request_arg', |
| 191 |
]; |
| 192 |
|
| 193 |
if ( isset( $params['description'] ) ) { |
| 194 |
$endpoint_args[ $field_id ]['description'] = $params['description']; |
| 195 |
} |
| 196 |
|
| 197 |
if ( WP_REST_Server::CREATABLE === $method && isset( $params['default'] ) ) { |
| 198 |
$endpoint_args[ $field_id ]['default'] = $params['default']; |
| 199 |
} |
| 200 |
|
| 201 |
if ( WP_REST_Server::CREATABLE === $method && ! empty( $params['required'] ) ) { |
| 202 |
$endpoint_args[ $field_id ]['required'] = true; |
| 203 |
} |
| 204 |
|
| 205 |
foreach ( [ 'type', 'format', 'enum' ] as $schema_prop ) { |
| 206 |
if ( isset( $params[ $schema_prop ] ) ) { |
| 207 |
$endpoint_args[ $field_id ][ $schema_prop ] = $params[ $schema_prop ]; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
// Merge in any options provided by the schema property. |
| 212 |
if ( isset( $params['arg_options'] ) ) { |
| 213 |
|
| 214 |
// Only use required / default from arg_options on CREATABLE endpoints. |
| 215 |
if ( WP_REST_Server::CREATABLE !== $method ) { |
| 216 |
$params['arg_options'] = array_diff_key( $params['arg_options'], [ 'required' => '', 'default' => '' ] ); |
| 217 |
} |
| 218 |
|
| 219 |
$endpoint_args[ $field_id ] = array_merge( $endpoint_args[ $field_id ], $params['arg_options'] ); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
return $endpoint_args; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Adds multiple links to the response. |
| 228 |
* |
| 229 |
* @param object $response |
| 230 |
* @param object $item |
| 231 |
* |
| 232 |
* @return object |
| 233 |
*/ |
| 234 |
protected function add_links( $response, $item ) { |
| 235 |
$response->data['_links'] = $this->prepare_links( $item ); |
| 236 |
|
| 237 |
return $response; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Prepare links for the request. |
| 242 |
* |
| 243 |
* @param object $item |
| 244 |
* |
| 245 |
* @return array Links for the given user. |
| 246 |
*/ |
| 247 |
protected function prepare_links( $item ) { |
| 248 |
$links = [ |
| 249 |
'self' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $item->id ) ), |
| 250 |
'collection' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
| 251 |
]; |
| 252 |
|
| 253 |
return $links; |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Format item's collection for response |
| 258 |
* |
| 259 |
* @param object $response |
| 260 |
* @param object $request |
| 261 |
* @param array $items |
| 262 |
* @param int $total_items |
| 263 |
* |
| 264 |
* @return object |
| 265 |
*/ |
| 266 |
public function format_collection_response( $response, $request, $total_items ) { |
| 267 |
if ( $total_items === 0 ) { |
| 268 |
return $response; |
| 269 |
} |
| 270 |
|
| 271 |
// Store pagation values for headers then unset for count query. |
| 272 |
$per_page = (int) $request['per_page']; |
| 273 |
$page = (int) $request['page']; |
| 274 |
|
| 275 |
$response->header( 'X-WP-Total', (int) $total_items ); |
| 276 |
$max_pages = ceil( $total_items / $per_page ); |
| 277 |
$response->header( 'X-WP-TotalPages', (int) $max_pages ); |
| 278 |
$base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) ); |
| 279 |
|
| 280 |
if ( $page > 1 ) { |
| 281 |
$prev_page = $page - 1; |
| 282 |
if ( $prev_page > $max_pages ) { |
| 283 |
$prev_page = $max_pages; |
| 284 |
} |
| 285 |
$prev_link = add_query_arg( 'page', $prev_page, $base ); |
| 286 |
$response->link_header( 'prev', $prev_link ); |
| 287 |
} |
| 288 |
if ( $max_pages > $page ) { |
| 289 |
|
| 290 |
$next_page = $page + 1; |
| 291 |
$next_link = add_query_arg( 'page', $next_page, $base ); |
| 292 |
$response->link_header( 'next', $next_link ); |
| 293 |
} |
| 294 |
|
| 295 |
return $response; |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Retrieve a wp user |
| 300 |
* |
| 301 |
* @param integer $user_id |
| 302 |
* |
| 303 |
* @return array |
| 304 |
*/ |
| 305 |
public function get_user( $user_id ) { |
| 306 |
$user = get_user_by( 'ID', $user_id ); |
| 307 |
|
| 308 |
if ( ! $user ) { |
| 309 |
return null; |
| 310 |
} |
| 311 |
|
| 312 |
$data = [ |
| 313 |
'ID' => $user->ID, |
| 314 |
'user_nicename' => $user->user_nicename, |
| 315 |
'user_email' => $user->user_email, |
| 316 |
'user_url' => $user->user_url, |
| 317 |
'display_name' => $user->display_name, |
| 318 |
'avatar' => get_avatar_url( $user->ID ), |
| 319 |
'_links' => [ |
| 320 |
'self' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, 'users', $user->ID ) ), |
| 321 |
'collection' => rest_url( sprintf( '/%s/%s', $this->namespace, 'users' ) ), |
| 322 |
] |
| 323 |
]; |
| 324 |
|
| 325 |
return $data; |
| 326 |
} |
| 327 |
} |
| 328 |
|