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,7 +57,7 @@ public function __construct() {
6257	/** 
6358	 * Register routes for all controllers. 
6459	 */ 
65- 	public  function  register_routes () {
60+ 	public  function  register_routes ():  void  {
6661		/** 
6762		 * Filter the list of controller classes used in the WooCommerce POS REST API. 
6863		 * 
@@ -73,27 +68,27 @@ public function register_routes() {
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+ 		 *                            - 'cashier '               => Fully qualified name of the class handling cashier  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		$ classesapply_filters (
9186			'woocommerce_pos_rest_api_controllers ' ,
9287			array (
9388				// woocommerce pos rest api controllers. 
9489				'auth '                   => API \Auth::class,
9590				'settings '               => API \Settings::class,
96- 				'stores '                  => API \Stores ::class,
91+ 				'cashier '                => API \Cashier ::class,
9792
9893				// extend WC REST API controllers. 
9994				'products '               => API \Products_Controller::class,
@@ -102,6 +97,7 @@ public function register_routes() {
10297				'customers '              => API \Customers_Controller::class,
10398				'product_tags '           => API \Product_Tags_Controller::class,
10499				'product_categories '     => API \Product_Categories_Controller::class,
100+ 				'product_brands '         => API \Product_Brands_Controller::class,
105101				'taxes '                  => API \Taxes_Controller::class,
106102				'shipping_methods '       => API \Shipping_Methods_Controller::class,
107103				'tax_classes '            => API \Tax_Classes_Controller::class,
@@ -197,7 +193,7 @@ public function rest_authentication_errors( $errors ) {
197193	/** 
198194	 * Extract the Authorization Bearer token from the request. 
199195	 * 
200- 	 * @return string| false 
196+ 	 * @return false|string  
201197	 */ 
202198	public  function  get_auth_header () {
203199		// Check if HTTP_AUTHORIZATION is set in $_SERVER 
@@ -222,7 +218,7 @@ public function get_auth_header() {
222218	/** 
223219	 * Adds info to the WP REST API index response. 
224220	 * - UUID 
225- 	 * - Version Info 
221+ 	 * - Version Info.  
226222	 * 
227223	 * @param WP_REST_Response $response Response data. 
228224	 * 
@@ -234,11 +230,20 @@ public function rest_index( WP_REST_Response $response ): WP_REST_Response {
234230			$ uuiduuid4 ()->toString ();
235231			update_option ( 'woocommerce_pos_uuid ' , $ uuid
236232		}
237- 		$ responsedata ['uuid ' ] = $ uuid
238- 		$ responsedata ['wp_version ' ] = get_bloginfo ( 'version '  );
239- 		$ responsedata ['wc_version ' ] = WC ()->version ;
233+ 		$ responsedata ['uuid ' ]           = $ uuid
234+ 		$ responsedata ['wp_version ' ]     = get_bloginfo ( 'version '  );
235+ 		$ responsedata ['wc_version ' ]     = WC ()->version ;
240236		$ responsedata ['wcpos_version ' ] = VERSION ;
241- 		$ responsedata ['use_jwt_as_param ' ] = woocommerce_pos_get_settings ( 'tools ' , 'use_jwt_as_param '  );
237+ 
238+ 		// Add wcpos authentication endpoint 
239+ 		if  ( ! isset ( $ responsedata ['authentication ' ] ) ) {
240+ 			$ responsedata ['authentication ' ] = array ();
241+ 		}
242+ 		$ responsedata ['authentication ' ]['wcpos ' ] = array (
243+ 			'endpoints '  => array (
244+ 				'authorization '  => home_url ( 'wcpos-auth '  ),
245+ 			),
246+ 		);
242247
243248		/** 
244249		 * Remove the routes from the response. 
@@ -289,44 +294,6 @@ public function rest_pre_dispatch( $result, $server, $request ) {
289294		return  $ result
290295	}
291296
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_arrayis_array ( $ param_value$ param_valueexplode ( ', ' , $ param_value
307- 		$ param_stringimplode ( ', ' , $ 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_arrayarray ();
314- 
315- 			foreach  ( $ param_arrayas  $ 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- 
330297	/** 
331298	 * Filters the REST API dispatch request result. 
332299	 * 
@@ -338,13 +305,13 @@ private function shorten_param_array( $param_value, $max_length ) {
338305	 * @return mixed 
339306	 */ 
340307	public  function  rest_dispatch_request ( $ dispatch_result$ request$ route$ handler
341- 		if  ( isset ( $ handler'callback ' ] ) && is_array ( $ handler'callback ' ] ) && isset ( $ handler'callback ' ][0 ] ) ) {
308+ 		if  ( isset ( $ handler'callback ' ] ) && \ is_array$ handler'callback ' ] ) && isset ( $ handler'callback ' ][0 ] ) ) {
342309			$ controller$ handler'callback ' ][0 ];
343310
344311			// Check if the controller object is one of our registered controllers. 
345312			foreach  ( $ this controllers  as  $ key$ wcpos_controller
346313				if  ( $ controller$ wcpos_controller
347- 					/**  
314+ 					/* 
348315					 * I'm adding some additional PHP settings before the response. Placing them here so they only apply to the POS API. 
349316					 * 
350317					 * - error_reporting(0) - Turn off error reporting 
@@ -365,6 +332,7 @@ public function rest_dispatch_request( $dispatch_result, $request, $route, $hand
365332					if  ( method_exists ( $ controller'wcpos_dispatch_request '  ) ) {
366333						return  $ controllerwcpos_dispatch_request ( $ dispatch_result$ request$ route$ handler
367334					}
335+ 
368336					break ;
369337				}
370338			}
@@ -373,6 +341,45 @@ public function rest_dispatch_request( $dispatch_result, $request, $route, $hand
373341		return  $ dispatch_result
374342	}
375343
344+ 	/** 
345+ 	 * Some servers have a limit on the number of include/exclude we can use in a request. 
346+ 	 * Worst thing is there is often no error message, the request returns an empty response. 
347+ 	 * 
348+ 	 * For example, WP Engine has a limit of 1024 characters? 
349+ 	 * https://wpengine.com/support/using-dev-tools/#Long_Queries_in_wp_db 
350+ 	 * 
351+ 	 * @TODO - For long queries, I should find a better solution than this. 
352+ 	 * 
353+ 	 * @param array|string $param_value 
354+ 	 * @param int          $max_length 
355+ 	 * 
356+ 	 * @return array 
357+ 	 */ 
358+ 	private  function  shorten_param_array ( $ param_value$ max_length
359+ 		$ param_array\is_array ( $ param_value$ param_valueexplode ( ', ' , $ param_value
360+ 		$ param_stringimplode ( ', ' , $ param_array
361+ 
362+ 		if  ( \strlen ( $ param_string$ max_length
363+ 			shuffle ( $ param_array// Shuffle to randomize 
364+ 
365+ 			$ new_param_string'' ;
366+ 			$ random_param_arrayarray ();
367+ 
368+ 			foreach  ( $ param_arrayas  $ id
369+ 				if  ( \strlen ( $ new_param_string$ id$ max_length
370+ 					$ new_param_string$ id', ' ;
371+ 					$ random_param_array$ id
372+ 				} else  {
373+ 					break ; // Stop when maximum length is reached 
374+ 				}
375+ 			}
376+ 
377+ 			return  $ random_param_array
378+ 		}
379+ 
380+ 		return  $ param_array
381+ 	}
382+ 
376383	/** 
377384	 * Check the Authorization header for a Bearer token. 
378385	 * 
@@ -383,25 +390,26 @@ public function rest_dispatch_request( $dispatch_result, $request, $route, $hand
383390	private  function  authenticate ( $ user_id
384391		// check if there is an auth header 
385392		$ auth_header$ this get_auth_header ();
386- 		if  ( ! is_string ( $ auth_header
393+ 		if  ( ! \ is_string$ auth_header
387394			return  $ user_id
388395		}
389396
390397		// Extract Bearer token from Authorization Header 
391398		list ($ tokensscanf ( $ auth_header'Bearer %s '  );
392399
393400		if  ( $ token
394- 			$ auth_serviceinstance ();
401+ 			$ auth_service  = Auth::instance ();
395402			$ decoded_token$ auth_servicevalidate_token ( $ token
396403
397404			// 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
405+ 			if  ( is_wp_error ( $ decoded_tokennull  === $ user_id 
406+ 				return  $ decoded_token
400407			}
401408
402409			// If the token is valid, set the user_id 
403410			if  ( ! is_wp_error ( $ decoded_token
404411				$ user_id$ decoded_tokendata ->user ->id ;
412+ 
405413				return  absint ( $ user_id
406414			}
407415		}
0 commit comments