WC_POS_API

WC REST API Class.

Defined (1)

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

/includes/class-wc-pos-api.php  
  1. class WC_POS_API { 
  2.  
  3.  
  4. /** 
  5. */ 
  6. public function __construct() { 
  7. if( ! is_pos() ) 
  8. return; 
  9.  
  10. // remove wc api authentication 
  11. // - relies on ->api and ->authentication being publicly accessible 
  12. if( isset( WC()->api ) && isset( WC()->api->authentication ) ) { 
  13. remove_filter( 'woocommerce_api_check_authentication', array( WC()->api->authentication, 'authenticate' ), 0 ); 
  14.  
  15. // support for X-HTTP-Method-Override for WC < 2.4 
  16. if( version_compare( WC()->version, '2.4', '<' ) && isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']) ) { 
  17. $_GET['_method'] = $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']; 
  18.  
  19. add_filter( 'woocommerce_api_check_authentication', array( $this, 'wc_api_authentication' ), 10, 0 ); 
  20. add_filter( 'woocommerce_api_dispatch_args', array( $this, 'dispatch_args'), 10, 2 ); 
  21. add_filter( 'woocommerce_api_query_args', array( $this, 'woocommerce_api_query_args' ), 10, 2 ); 
  22.  
  23. /** 
  24. * Bypass authentication for WC REST API 
  25. * @return WP_User object 
  26. */ 
  27. public function wc_api_authentication() { 
  28. global $current_user; 
  29. $user = $current_user; 
  30.  
  31. if( ! user_can( $user->ID, 'access_woocommerce_pos' ) ) 
  32. $user = new WP_Error( 
  33. 'woocommerce_pos_authentication_error',  
  34. __( 'User not authorized to access WooCommerce POS', 'woocommerce-pos' ),  
  35. array( 'status' => 401 ) 
  36. ); 
  37.  
  38. return $user; 
  39.  
  40. /** 
  41. * @param $args 
  42. * @param $callback 
  43. * @return mixed 
  44. */ 
  45. public function dispatch_args($args, $callback) { 
  46. $wc_api_handler = get_class($callback[0]); 
  47.  
  48. $has_data = in_array( $args['_method'], array(2, 4, 8) ) && isset( $args['data'] ) && is_array( $args['data'] ); 
  49. if( $has_data ) { 
  50. // remove status 
  51. if( array_key_exists('status', $args['data']) ) { 
  52. unset($args['data']['status']); 
  53.  
  54. switch($wc_api_handler) { 
  55. case 'WC_API_Products': 
  56. new WC_POS_API_Products(); 
  57. break; 
  58. case 'WC_API_Orders': 
  59. if( $has_data && !isset( $args['data']['order'] ) ) { 
  60. $data = $args['data']; 
  61. unset( $args['data'] ); 
  62. $args['data']['order'] = $data; 
  63. new WC_POS_API_Orders(); 
  64. break; 
  65. case 'WC_API_Customers': 
  66. new WC_POS_API_Customers(); 
  67. break; 
  68. case 'WC_API_Coupons': 
  69. new WC_POS_API_Coupons(); 
  70. break; 
  71.  
  72. return $args; 
  73.  
  74. /** 
  75. * - this filter was introduced in WC 2.3 
  76. * @param $args 
  77. * @param $request_args 
  78. * @return mixed 
  79. */ 
  80. public function woocommerce_api_query_args($args, $request_args) { 
  81.  
  82. // required for compatibility WC < 2.3.5 
  83. if ( ! empty( $request_args['in'] ) ) { 
  84. $args['post__in'] = explode(', ', $request_args['in']); 
  85. unset( $request_args['in'] ); 
  86.  
  87. // required for compatibility WC < 2.4 
  88. if ( ! empty( $request_args['not_in'] ) ) { 
  89. $args['post__not_in'] = explode(', ', $request_args['not_in']); 
  90. unset( $request_args['not_in'] ); 
  91.  
  92. return $args; 
  93.  
  94. /** 
  95. * Get all the ids for a given post_type 
  96. * @return json 
  97. */ 
  98. static public function get_all_ids() { 
  99. $entity = isset($_REQUEST['type']) ? $_REQUEST['type'] : false; 
  100. $updated_at_min = isset($_REQUEST['updated_at_min']) ? $_REQUEST['updated_at_min'] : false; 
  101. $class_name = 'WC_POS_API_' . ucfirst( $entity ); 
  102. $handler = new $class_name(); 
  103.  
  104. if(method_exists($handler, 'get_ids')) { 
  105. $result = call_user_func(array($handler, 'get_ids'), $updated_at_min); 
  106. } else { 
  107. $result = new WP_Error( 
  108. 'woocommerce_pos_get_ids_error',  
  109. /** translators: woocommerce */ 
  110. sprintf( __( 'There was an error calling %s::%s', 'woocommerce' ), 'WC_POS_API', $entity ),  
  111. array( 'status' => 500 ) 
  112. ); 
  113.  
  114. WC_POS_Server::response($result); 
  115.