PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.3.13
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.3.13
1.17.9 1.17.8 1.17.7 1.17.6 1.17.5 1.17.4 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.3.0 1.3.1 1.3.11 1.3.12 1.3.13 1.3.14 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 All 143 releases
erp / includes / api / class-rest-controller.php

class-rest-controller.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.3.13, at includes/api/class-rest-controller.php

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