1 <?php
 2 /**
 3  * WooCommerce API
 4  *
 5  * Handles parsing JSON request bodies and generating JSON responses
 6  *
 7  * @author      WooThemes
 8  * @category    API
 9  * @package     WooCommerce/API
10  * @since       2.1
11  */
12 
13 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
14 
15 class WC_API_JSON_Handler implements WC_API_Handler {
16 
17     /**
18      * Get the content type for the response
19      *
20      * @since 2.1
21      * @return string
22      */
23     public function get_content_type() {
24 
25         return 'application/json; charset=' . get_option( 'blog_charset' );
26     }
27 
28     /**
29      * Parse the raw request body entity
30      *
31      * @since 2.1
32      * @param string $body the raw request body
33      * @return array|mixed
34      */
35     public function parse_body( $body ) {
36 
37         return json_decode( $body, true );
38     }
39 
40     /**
41      * Generate a JSON response given an array of data
42      *
43      * @since 2.1
44      * @param array $data the response data
45      * @return string
46      */
47     public function generate_response( $data ) {
48 
49         if ( isset( $_GET['_jsonp'] ) ) {
50 
51             // JSONP enabled by default
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             // Check for invalid characters (only alphanumeric allowed)
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 
WooCommerce API documentation generated by ApiGen 2.8.0