1 <?php
2 3 4 5 6 7 8 9 10 11
12
13 if ( ! defined( 'ABSPATH' ) ) exit;
14
15 class WC_API_JSON_Handler implements WC_API_Handler {
16
17 18 19 20 21 22
23 public function get_content_type() {
24
25 return 'application/json; charset=' . get_option( 'blog_charset' );
26 }
27
28 29 30 31 32 33 34
35 public function parse_body( $body ) {
36
37 return json_decode( $body, true );
38 }
39
40 41 42 43 44 45 46
47 public function generate_response( $data ) {
48
49 if ( isset( $_GET['_jsonp'] ) ) {
50
51
52 if ( ! apply_filters( 'woocommerce_api_jsonp_enabled', true ) ) {
53
54 WC()->api->server->send_status( 400 );
55
56 $data = array( array( 'code' => 'woocommerce_api_jsonp_disabled', 'message' => __( 'JSONP support is disabled on this site', 'woocommerce' ) ) );
57 }
58
59
60 if ( preg_match( '/\W/', $_GET['_jsonp'] ) ) {
61
62 WC()->api->server->send_status( 400 );
63
64 $data = array( array( 'code' => 'woocommerce_api_jsonp_callback_invalid', __( 'The JSONP callback function is invalid', 'woocommerce' ) ) );
65 }
66
67 return $_GET['_jsonp'] . '(' . json_encode( $data ) . ')';
68 }
69
70 return json_encode( $data );
71 }
72
73 }
74