11<?php
22/**
3- * WooCommerce POS REST API Class, ie: /wcpos/v1/ endpoints.
3+ * WCPOS REST API Class, ie: /wcpos/v1/ endpoints.
44 *
55 * @author Paul Kilmurray <[email protected] > 66 *
77 * @see http://wcpos.com
8- * @package WCPOS\WooCommercePOS
98 */
109
1110namespace WCPOS \WooCommercePOS ;
1716use WP_REST_Response ;
1817use WP_REST_Server ;
1918
20-
21- /**
22- *
23- */
2419class API {
2520 /**
2621 * WCPOS REST API namespaces and endpoints.
@@ -62,35 +57,35 @@ public function __construct() {
6257 /**
6358 * Register routes for all controllers.
6459 */
65- public function register_routes () {
60+ public function register_routes (): void {
6661 /**
67- * Filter the list of controller classes used in the WooCommerce POS REST API.
62+ * Filter the list of controller classes used in the WCPOS REST API.
6863 *
6964 * This filter allows customizing or extending the set of controller classes that handle
70- * REST API routes for the WooCommerce POS . By filtering these controllers, plugins can
65+ * REST API routes for the WCPOS . By filtering these controllers, plugins can
7166 * modify existing endpoints or add new controllers for additional functionality.
7267 *
7368 * @since 1.5.0
7469 *
7570 * @param array $controllers Associative array of controller identifiers to their corresponding class names.
76- * - 'auth' => Fully qualified name of the class handling authentication.
77- * - 'settings' => Fully qualified name of the class handling settings.
78- * - 'stores' => Fully qualified name of the class handling stores management.
79- * - 'products' => Fully qualified name of the class handling products.
80- * - 'product_variations' => Fully qualified name of the class handling product variations.
81- * - 'orders' => Fully qualified name of the class handling orders.
82- * - 'customers' => Fully qualified name of the class handling customers.
83- * - 'product_tags' => Fully qualified name of the class handling product tags.
84- * - 'product_categories' => Fully qualified name of the class handling product categories.
85- * - 'taxes' => Fully qualified name of the class handling taxes.
86- * - 'shipping_methods' => Fully qualified name of the class handling shipping methods.
87- * - 'tax_classes' => Fully qualified name of the class handling tax classes.
88- * - 'order_statuses' => Fully qualified name of the class handling order statuses.
71+ * - 'auth' => Fully qualified name of the class handling authentication.
72+ * - 'settings' => Fully qualified name of the class handling settings.
73+ * - 'stores' => Fully qualified name of the class handling stores management.
74+ * - 'products' => Fully qualified name of the class handling products.
75+ * - 'product_variations' => Fully qualified name of the class handling product variations.
76+ * - 'orders' => Fully qualified name of the class handling orders.
77+ * - 'customers' => Fully qualified name of the class handling customers.
78+ * - 'product_tags' => Fully qualified name of the class handling product tags.
79+ * - 'product_categories' => Fully qualified name of the class handling product categories.
80+ * - 'taxes' => Fully qualified name of the class handling taxes.
81+ * - 'shipping_methods' => Fully qualified name of the class handling shipping methods.
82+ * - 'tax_classes' => Fully qualified name of the class handling tax classes.
83+ * - 'order_statuses' => Fully qualified name of the class handling order statuses.
8984 */
9085 $ classes = apply_filters (
9186 'woocommerce_pos_rest_api_controllers ' ,
9287 array (
93- // woocommerce pos rest api controllers.
88+ // WCPOS rest api controllers.
9489 'auth ' => API \Auth::class,
9590 'settings ' => API \Settings::class,
9691 'stores ' => API \Stores::class,
@@ -197,7 +192,7 @@ public function rest_authentication_errors( $errors ) {
197192 /**
198193 * Extract the Authorization Bearer token from the request.
199194 *
200- * @return string| false
195+ * @return false|string
201196 */
202197 public function get_auth_header () {
203198 // Check if HTTP_AUTHORIZATION is set in $_SERVER
@@ -222,7 +217,7 @@ public function get_auth_header() {
222217 /**
223218 * Adds info to the WP REST API index response.
224219 * - UUID
225- * - Version Info
220+ * - Version Info.
226221 *
227222 * @param WP_REST_Response $response Response data.
228223 *
@@ -234,10 +229,10 @@ public function rest_index( WP_REST_Response $response ): WP_REST_Response {
234229 $ uuid = Uuid::uuid4 ()->toString ();
235230 update_option ( 'woocommerce_pos_uuid ' , $ uuid );
236231 }
237- $ response ->data ['uuid ' ] = $ uuid ;
238- $ response ->data ['wp_version ' ] = get_bloginfo ( 'version ' );
239- $ response ->data ['wc_version ' ] = WC ()->version ;
240- $ response ->data ['wcpos_version ' ] = VERSION ;
232+ $ response ->data ['uuid ' ] = $ uuid ;
233+ $ response ->data ['wp_version ' ] = get_bloginfo ( 'version ' );
234+ $ response ->data ['wc_version ' ] = WC ()->version ;
235+ $ response ->data ['wcpos_version ' ] = VERSION ;
241236 $ response ->data ['use_jwt_as_param ' ] = woocommerce_pos_get_settings ( 'tools ' , 'use_jwt_as_param ' );
242237
243238 /**
@@ -289,44 +284,6 @@ public function rest_pre_dispatch( $result, $server, $request ) {
289284 return $ result ;
290285 }
291286
292- /**
293- * Some servers have a limit on the number of include/exclude we can use in a request.
294- * Worst thing is there is often no error message, the request returns an empty response.
295- *
296- * For example, WP Engine has a limit of 1024 characters?
297- * https://wpengine.com/support/using-dev-tools/#Long_Queries_in_wp_db
298- *
299- * @TODO - For long queries, I should find a better solution than this.
300- *
301- * @param string|array $param_value
302- * @param int $max_length
303- * @return array
304- */
305- private function shorten_param_array ( $ param_value , $ max_length ) {
306- $ param_array = is_array ( $ param_value ) ? $ param_value : explode ( ', ' , $ param_value );
307- $ param_string = implode ( ', ' , $ param_array );
308-
309- if ( strlen ( $ param_string ) > $ max_length ) {
310- shuffle ( $ param_array ); // Shuffle to randomize
311-
312- $ new_param_string = '' ;
313- $ random_param_array = array ();
314-
315- foreach ( $ param_array as $ id ) {
316- if ( strlen ( $ new_param_string . $ id ) < $ max_length ) {
317- $ new_param_string .= $ id . ', ' ;
318- $ random_param_array [] = $ id ;
319- } else {
320- break ; // Stop when maximum length is reached
321- }
322- }
323-
324- return $ random_param_array ;
325- }
326-
327- return $ param_array ;
328- }
329-
330287 /**
331288 * Filters the REST API dispatch request result.
332289 *
@@ -338,13 +295,13 @@ private function shorten_param_array( $param_value, $max_length ) {
338295 * @return mixed
339296 */
340297 public function rest_dispatch_request ( $ dispatch_result , $ request , $ route , $ handler ) {
341- if ( isset ( $ handler ['callback ' ] ) && is_array ( $ handler ['callback ' ] ) && isset ( $ handler ['callback ' ][0 ] ) ) {
298+ if ( isset ( $ handler ['callback ' ] ) && \ is_array ( $ handler ['callback ' ] ) && isset ( $ handler ['callback ' ][0 ] ) ) {
342299 $ controller = $ handler ['callback ' ][0 ];
343300
344301 // Check if the controller object is one of our registered controllers.
345302 foreach ( $ this ->controllers as $ key => $ wcpos_controller ) {
346303 if ( $ controller === $ wcpos_controller ) {
347- /**
304+ /*
348305 * I'm adding some additional PHP settings before the response. Placing them here so they only apply to the POS API.
349306 *
350307 * - error_reporting(0) - Turn off error reporting
@@ -365,6 +322,7 @@ public function rest_dispatch_request( $dispatch_result, $request, $route, $hand
365322 if ( method_exists ( $ controller , 'wcpos_dispatch_request ' ) ) {
366323 return $ controller ->wcpos_dispatch_request ( $ dispatch_result , $ request , $ route , $ handler );
367324 }
325+
368326 break ;
369327 }
370328 }
@@ -373,6 +331,45 @@ public function rest_dispatch_request( $dispatch_result, $request, $route, $hand
373331 return $ dispatch_result ;
374332 }
375333
334+ /**
335+ * Some servers have a limit on the number of include/exclude we can use in a request.
336+ * Worst thing is there is often no error message, the request returns an empty response.
337+ *
338+ * For example, WP Engine has a limit of 1024 characters?
339+ * https://wpengine.com/support/using-dev-tools/#Long_Queries_in_wp_db
340+ *
341+ * @TODO - For long queries, I should find a better solution than this.
342+ *
343+ * @param array|string $param_value
344+ * @param int $max_length
345+ *
346+ * @return array
347+ */
348+ private function shorten_param_array ( $ param_value , $ max_length ) {
349+ $ param_array = \is_array ( $ param_value ) ? $ param_value : explode ( ', ' , $ param_value );
350+ $ param_string = implode ( ', ' , $ param_array );
351+
352+ if ( \strlen ( $ param_string ) > $ max_length ) {
353+ shuffle ( $ param_array ); // Shuffle to randomize
354+
355+ $ new_param_string = '' ;
356+ $ random_param_array = array ();
357+
358+ foreach ( $ param_array as $ id ) {
359+ if ( \strlen ( $ new_param_string . $ id ) < $ max_length ) {
360+ $ new_param_string .= $ id . ', ' ;
361+ $ random_param_array [] = $ id ;
362+ } else {
363+ break ; // Stop when maximum length is reached
364+ }
365+ }
366+
367+ return $ random_param_array ;
368+ }
369+
370+ return $ param_array ;
371+ }
372+
376373 /**
377374 * Check the Authorization header for a Bearer token.
378375 *
@@ -383,25 +380,26 @@ public function rest_dispatch_request( $dispatch_result, $request, $route, $hand
383380 private function authenticate ( $ user_id ) {
384381 // check if there is an auth header
385382 $ auth_header = $ this ->get_auth_header ();
386- if ( ! is_string ( $ auth_header ) ) {
383+ if ( ! \ is_string ( $ auth_header ) ) {
387384 return $ user_id ;
388385 }
389386
390387 // Extract Bearer token from Authorization Header
391388 list ($ token ) = sscanf ( $ auth_header , 'Bearer %s ' );
392389
393390 if ( $ token ) {
394- $ auth_service = Auth::instance ();
391+ $ auth_service = Auth::instance ();
395392 $ decoded_token = $ auth_service ->validate_token ( $ token );
396393
397394 // Check if validate_token returned WP_Error and user_id is null
398- if ( is_wp_error ( $ decoded_token ) && $ user_id === null ) {
399- return $ decoded_token ;
395+ if ( is_wp_error ( $ decoded_token ) && null === $ user_id ) {
396+ return $ decoded_token ;
400397 }
401398
402399 // If the token is valid, set the user_id
403400 if ( ! is_wp_error ( $ decoded_token ) ) {
404401 $ user_id = $ decoded_token ->data ->user ->id ;
402+
405403 return absint ( $ user_id );
406404 }
407405 }
0 commit comments