WC_POS_APIv2

WC REST API Class.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-pos-apiv2.php  
  1. class WC_POS_APIv2 { 
  2.  
  3.  
  4. /** 
  5. */ 
  6. public function __construct() { 
  7. if( ! is_pos() ) 
  8. return; 
  9.  
  10. add_filter( 'rest_dispatch_request', array( $this, 'rest_dispatch_request' ), 10, 4 ); 
  11. add_filter( 'rest_request_before_callbacks', array( $this, 'rest_request_before_callbacks' ), 10, 3 ); 
  12.  
  13. /** 
  14. * Bypass authentication for WC REST API 
  15. * @return WP_User object 
  16. */ 
  17. public function wc_api_authentication() { 
  18. global $current_user; 
  19. $user = $current_user; 
  20.  
  21. if( ! user_can( $user->ID, 'access_woocommerce_pos' ) ) 
  22. $user = new WP_Error( 
  23. 'woocommerce_pos_authentication_error',  
  24. __( 'User not authorized to access WooCommerce POS', 'woocommerce-pos' ),  
  25. array( 'status' => 401 ) 
  26. ); 
  27.  
  28. return $user; 
  29.  
  30.  
  31. /** 
  32. * @param $response 
  33. * @param $handler 
  34. * @param $request 
  35. * @return mixed 
  36. */ 
  37. public function rest_request_before_callbacks( $response, $handler, $request ) { 
  38. $wc_api_handler = get_class($handler['callback'][0]); 
  39.  
  40. switch($wc_api_handler) { 
  41. case 'WC_REST_Products_Controller': 
  42. new WC_POS_APIv2_Products(); 
  43. break; 
  44. case 'WC_REST_Orders_Controller': 
  45. new WC_POS_APIv2_Orders(); 
  46. break; 
  47. case 'WC_REST_Customers_Controller': 
  48. new WC_POS_APIv2_Customers(); 
  49. break; 
  50. case 'WC_REST_Coupons_Controller': 
  51. new WC_POS_APIv2_Coupons(); 
  52. break; 
  53.  
  54. return $response; 
  55.  
  56.  
  57. /** 
  58. * @param null $halt 
  59. * @param $request 
  60. * @param $route 
  61. * @param $handler 
  62. * @return mixed 
  63. */ 
  64. public function rest_dispatch_request( $halt, $request, $route, $handler ) { 
  65.  
  66. if( isset($request['filter']) ) { 
  67. $filter = $request['filter']; 
  68.  
  69. if( isset($filter['q']) ) { 
  70. $request->set_param('search', $filter['q']); 
  71.  
  72. if( isset($filter['limit']) ) { 
  73. $request->set_param('per_page', $filter['limit']); 
  74.  
  75. if( isset($filter['in']) ) { 
  76. $request->set_param('include', explode( ', ', $filter['in'] )); 
  77.  
  78. if( isset($filter['not_in']) ) { 
  79. $request->set_param('exclude', explode( ', ', $filter['not_in'] )); 
  80.  
  81. if( isset($filter['featured']) ) { 
  82. $request->set_param('featured', $filter['featured']); 
  83.  
  84. if( isset($filter['on_sale']) ) { 
  85. $request->set_param('on_sale', $filter['on_sale']); 
  86.  
  87.  
  88. return $halt; 
  89.  
  90.  
  91. /** 
  92. * Get all the ids for a given post_type 
  93. * @return json 
  94. */ 
  95. static public function get_all_ids() { 
  96. $entity = isset($_REQUEST['type']) ? $_REQUEST['type'] : false; 
  97. $updated_at_min = isset($_REQUEST['updated_at_min']) ? $_REQUEST['updated_at_min'] : false; 
  98. $class_name = 'WC_POS_APIv2_' . ucfirst( $entity ); 
  99. $handler = new $class_name(); 
  100.  
  101. if(method_exists($handler, 'get_ids')) { 
  102. $result = call_user_func(array($handler, 'get_ids'), $updated_at_min); 
  103. } else { 
  104. $result = new WP_Error( 
  105. 'woocommerce_pos_get_ids_error',  
  106. /** translators: woocommerce */ 
  107. sprintf( __( 'There was an error calling %s::%s', 'woocommerce' ), 'WC_POS_API', $entity ),  
  108. array( 'status' => 500 ) 
  109. ); 
  110.  
  111. WC_POS_Server::response($result); 
  112.