WC_API_JSON_Handler

The WooCommerce WC API JSON Handler class.

Defined (3)

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

/includes/api/legacy/v1/class-wc-api-json-handler.php  
  1. class WC_API_JSON_Handler implements WC_API_Handler { 
  2.  
  3. /** 
  4. * Get the content type for the response 
  5. * @since 2.1 
  6. * @return string 
  7. */ 
  8. public function get_content_type() { 
  9.  
  10. return sprintf( '%s; charset=%s', isset( $_GET['_jsonp'] ) ? 'application/javascript' : 'application/json', get_option( 'blog_charset' ) ); 
  11.  
  12. /** 
  13. * Parse the raw request body entity 
  14. * @since 2.1 
  15. * @param string $body the raw request body 
  16. * @return array|mixed 
  17. */ 
  18. public function parse_body( $body ) { 
  19.  
  20. return json_decode( $body, true ); 
  21.  
  22. /** 
  23. * Generate a JSON response given an array of data 
  24. * @since 2.1 
  25. * @param array $data the response data 
  26. * @return string 
  27. */ 
  28. public function generate_response( $data ) { 
  29.  
  30. if ( isset( $_GET['_jsonp'] ) ) { 
  31.  
  32. // JSONP enabled by default 
  33. if ( ! apply_filters( 'woocommerce_api_jsonp_enabled', true ) ) { 
  34.  
  35. WC()->api->server->send_status( 400 ); 
  36.  
  37. $data = array( array( 'code' => 'woocommerce_api_jsonp_disabled', 'message' => __( 'JSONP support is disabled on this site', 'woocommerce' ) ) ); 
  38.  
  39. // Check for invalid characters (only alphanumeric allowed) 
  40. if ( preg_match( '/\W/', $_GET['_jsonp'] ) ) { 
  41.  
  42. WC()->api->server->send_status( 400 ); 
  43.  
  44. $data = array( array( 'code' => 'woocommerce_api_jsonp_callback_invalid', __( 'The JSONP callback function is invalid', 'woocommerce' ) ) ); 
  45.  
  46. // see http://miki.it/blog/2014/7/8/abusing-jsonp-with-rosetta-flash/ 
  47. WC()->api->server->header( 'X-Content-Type-Options', 'nosniff' ); 
  48.  
  49. // Prepend '/**/' to mitigate possible JSONP Flash attacks 
  50. return '/**/' . $_GET['_jsonp'] . '(' . json_encode( $data ) . ')'; 
  51.  
  52. return json_encode( $data ); 
/includes/api/legacy/v2/class-wc-api-json-handler.php  
  1. class WC_API_JSON_Handler implements WC_API_Handler { 
  2.  
  3. /** 
  4. * Get the content type for the response 
  5. * @since 2.1 
  6. * @return string 
  7. */ 
  8. public function get_content_type() { 
  9.  
  10. return sprintf( '%s; charset=%s', isset( $_GET['_jsonp'] ) ? 'application/javascript' : 'application/json', get_option( 'blog_charset' ) ); 
  11.  
  12. /** 
  13. * Parse the raw request body entity 
  14. * @since 2.1 
  15. * @param string $body the raw request body 
  16. * @return array|mixed 
  17. */ 
  18. public function parse_body( $body ) { 
  19.  
  20. return json_decode( $body, true ); 
  21.  
  22. /** 
  23. * Generate a JSON response given an array of data 
  24. * @since 2.1 
  25. * @param array $data the response data 
  26. * @return string 
  27. */ 
  28. public function generate_response( $data ) { 
  29.  
  30. if ( isset( $_GET['_jsonp'] ) ) { 
  31.  
  32. // JSONP enabled by default 
  33. if ( ! apply_filters( 'woocommerce_api_jsonp_enabled', true ) ) { 
  34.  
  35. WC()->api->server->send_status( 400 ); 
  36.  
  37. $data = array( array( 'code' => 'woocommerce_api_jsonp_disabled', 'message' => __( 'JSONP support is disabled on this site', 'woocommerce' ) ) ); 
  38.  
  39. // Check for invalid characters (only alphanumeric allowed) 
  40. if ( preg_match( '/\W/', $_GET['_jsonp'] ) ) { 
  41.  
  42. WC()->api->server->send_status( 400 ); 
  43.  
  44. $data = array( array( 'code' => 'woocommerce_api_jsonp_callback_invalid', __( 'The JSONP callback function is invalid', 'woocommerce' ) ) ); 
  45.  
  46. // see http://miki.it/blog/2014/7/8/abusing-jsonp-with-rosetta-flash/ 
  47. WC()->api->server->header( 'X-Content-Type-Options', 'nosniff' ); 
  48.  
  49. // Prepend '/**/' to mitigate possible JSONP Flash attacks 
  50. return '/**/' . $_GET['_jsonp'] . '(' . json_encode( $data ) . ')'; 
  51.  
  52. return json_encode( $data ); 
/includes/api/legacy/v3/class-wc-api-json-handler.php  
  1. class WC_API_JSON_Handler implements WC_API_Handler { 
  2.  
  3. /** 
  4. * Get the content type for the response 
  5. * @since 2.1 
  6. * @return string 
  7. */ 
  8. public function get_content_type() { 
  9.  
  10. return sprintf( '%s; charset=%s', isset( $_GET['_jsonp'] ) ? 'application/javascript' : 'application/json', get_option( 'blog_charset' ) ); 
  11.  
  12. /** 
  13. * Parse the raw request body entity 
  14. * @since 2.1 
  15. * @param string $body the raw request body 
  16. * @return array|mixed 
  17. */ 
  18. public function parse_body( $body ) { 
  19.  
  20. return json_decode( $body, true ); 
  21.  
  22. /** 
  23. * Generate a JSON response given an array of data 
  24. * @since 2.1 
  25. * @param array $data the response data 
  26. * @return string 
  27. */ 
  28. public function generate_response( $data ) { 
  29.  
  30. if ( isset( $_GET['_jsonp'] ) ) { 
  31.  
  32. // JSONP enabled by default 
  33. if ( ! apply_filters( 'woocommerce_api_jsonp_enabled', true ) ) { 
  34.  
  35. WC()->api->server->send_status( 400 ); 
  36.  
  37. $data = array( array( 'code' => 'woocommerce_api_jsonp_disabled', 'message' => __( 'JSONP support is disabled on this site', 'woocommerce' ) ) ); 
  38.  
  39. // Check for invalid characters (only alphanumeric allowed) 
  40. if ( preg_match( '/\W/', $_GET['_jsonp'] ) ) { 
  41.  
  42. WC()->api->server->send_status( 400 ); 
  43.  
  44. $data = array( array( 'code' => 'woocommerce_api_jsonp_callback_invalid', __( 'The JSONP callback function is invalid', 'woocommerce' ) ) ); 
  45.  
  46. // see http://miki.it/blog/2014/7/8/abusing-jsonp-with-rosetta-flash/ 
  47. WC()->api->server->header( 'X-Content-Type-Options', 'nosniff' ); 
  48.  
  49. // Prepend '/**/' to mitigate possible JSONP Flash attacks 
  50. return '/**/' . $_GET['_jsonp'] . '(' . json_encode( $data ) . ')'; 
  51.  
  52. return json_encode( $data );