WC_POS_Server

The WooCommerce POS WC POS Server class.

Defined (1)

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

/includes/class-wc-pos-server.php  
  1. class WC_POS_Server { 
  2.  
  3. /** 
  4. * The below functions closely resemble output from the WC REST API 
  5. * This keeps response handling in the POS somewhat consistent 
  6. * between API and AJAX calls 
  7. * Output the result 
  8. * @param $result 
  9. */ 
  10. static public function response($result) { 
  11.  
  12. header( 'Content-Type: application/json; charset=utf-8' ); 
  13.  
  14. if (is_wp_error($result)) { 
  15. $data = $result->get_error_data(); 
  16. if ( is_array( $data ) && isset( $data['status'] ) ) { 
  17. status_header( $data['status'] ); 
  18. $result = self::error_to_array( $result ); 
  19.  
  20. echo json_encode( $result ); 
  21. die(); 
  22.  
  23. /** 
  24. * Verifies the AJAX request 
  25. * @param string $id 
  26. */ 
  27. static public function check_ajax_referer( $id = 'security' ) { 
  28. $pass = check_ajax_referer( WC_POS_PLUGIN_NAME, $id, false ); 
  29. if(!$pass) { 
  30. $result = new WP_Error( 
  31. 'woocommerce_pos_invalid_nonce',  
  32. __( 'Invalid security nonce', 'woocommerce-pos' ),  
  33. array( 'status' => 401 ) 
  34. ); 
  35. self::response($result); 
  36.  
  37. /** 
  38. * Convert wp_error to array 
  39. * @param $error 
  40. * @return array 
  41. */ 
  42. static private function error_to_array( $error ) { 
  43. $errors = array(); 
  44. foreach ( (array) $error->errors as $code => $messages ) { 
  45. foreach ( (array) $messages as $message ) { 
  46. $errors[] = array( 'code' => $code, 'message' => $message ); 
  47. return array( 'errors' => $errors ); 
  48.  
  49. /** 
  50. * Raw payload 
  51. * @return array|mixed|string 
  52. */ 
  53. static public function get_raw_data() { 
  54. global $HTTP_RAW_POST_DATA; 
  55. if ( !isset( $HTTP_RAW_POST_DATA ) ) { 
  56. $HTTP_RAW_POST_DATA = trim(file_get_contents('php://input')); 
  57. return json_decode( $HTTP_RAW_POST_DATA, true); 
  58.