/var/www/html/wp-content/plugins/woocommerce/includes/admin/helper/class-wc-helper-api.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/**
 * WooCommerce Admin Helper API
 *
 * @package WooCommerce\Admin\Helper
 */

if ( ! defined'ABSPATH' ) ) {
    exit;
}

/**
 * WC_Helper_API Class
 *
 * Provides a communication interface with the WooCommerce.com Helper API.
 */
class WC_Helper_API {
    
/**
     * Base path for API routes.
     *
     * @var $api_base
     */
    
public static $api_base;

    
/**
     * Load
     *
     * Allow devs to point the API base to a local API development or staging server.
     * Note that sslverify will be turned off for the woocommerce.dev + WP_DEBUG combination.
     * The URL can be changed on plugins_loaded before priority 10.
     */
    
public static function load() {
        
self::$api_base apply_filters'woocommerce_helper_api_base''https://woocommerce.com/wp-json/helper/1.0' );
    }

    
/**
     * Perform an HTTP request to the Helper API.
     *
     * @param string $endpoint The endpoint to request.
     * @param array  $args Additional data for the request. Set authenticated to a truthy value to enable auth.
     *
     * @return array|WP_Error The response from wp_safe_remote_request()
     */
    
public static function request$endpoint$args = array() ) {
        if ( ! isset( 
$args['query_string'] ) ) {
            
$args['query_string'] = '';
        }
        
$url self::url$endpoint$args['query_string'] );

        if ( ! empty( 
$args['authenticated'] ) ) {
            if ( ! 
self::_authenticate$url$args ) ) {
                return new 
WP_Error'authentication''Authentication failed.' );
            }
        }

        if ( ! isset( 
$args['user-agent'] ) ) {
            
$args['user-agent'] = 'WooCommerce/' WC()->version '; ' get_bloginfo'url' );
        }

        
/**
         * Allow developers to filter the request args passed to wp_safe_remote_request().
         * Useful to remove sslverify when working on a local api dev environment.
         */
        
$args apply_filters'woocommerce_helper_api_request_args'$args$endpoint );

        
// TODO: Check response signatures on certain endpoints.
        
return wp_safe_remote_request$url$args );
    }

    
/**
     * Create signature for a request.
     *
     * @param string $access_token_secret The access token secret.
     * @param string $url The URL to add the access token and signature to.
     * @param string $method The request method.
     * @param array  $body The body of the request.
     * @return string The signature.
     */
    
private static function create_request_signaturestring $access_token_secretstring $urlstring $method$body null ): string {

        
$request_uri  wp_parse_url$urlPHP_URL_PATH );
        
$query_string wp_parse_url$urlPHP_URL_QUERY );

        if ( 
is_string$query_string ) ) {
            
$request_uri .= '?' $query_string;
        }

        
$data = array(
            
'host'        => wp_parse_url$urlPHP_URL_HOST ),
            
'request_uri' => $request_uri,
            
'method'      => $method,
        );

        if ( ! empty( 
$body ) ) {
            
$data['body'] = $body;
        }

        return 
hash_hmac'sha256'wp_json_encode$data ), $access_token_secret );
    }

    
/**
     * Add the access token and signature to the provided URL.
     *
     * @param string $url The URL to add the access token and signature to.
     * @return string
     */
    
public static function add_auth_parametersstring $url ): string {
        
$auth WC_Helper_Options::get'auth' );

        if ( empty( 
$auth['access_token'] ) || empty( $auth['access_token_secret'] ) ) {
            return 
false;
        }

        
$signature self::create_request_signature( (string) $auth['access_token_secret'], $url'GET' );

        return 
add_query_arg(
            array(
                
'token'     => $auth['access_token'],
                
'signature' => $signature,
            ),
            
$url
        
);
    }

    
/**
     * Adds authentication headers to an HTTP request.
     *
     * @param string $url The request URI.
     * @param array  $args By-ref, the args that will be passed to wp_remote_request().
     * @return bool Were the headers added?
     */
    
private static function _authenticate( &$url, &$args ) {
        
$auth WC_Helper_Options::get'auth' );

        if ( empty( 
$auth['access_token'] ) || empty( $auth['access_token_secret'] ) ) {
            return 
false;
        }

        
$signature self::create_request_signature(
            (string) 
$auth['access_token_secret'],
            
$url,
            ! empty( 
$args['method'] ) ? $args['method'] : 'GET',
            
$args['body'] ?? null
        
);

        if ( empty( 
$args['headers'] ) ) {
            
$args['headers'] = array();
        }

        
$headers         = array(
            
'Authorization'   => 'Bearer ' $auth['access_token'],
            
'X-Woo-Signature' => $signature,
        );
        
$args['headers'] = wp_parse_args$headers$args['headers'] );

        
$url add_query_arg(
            array(
                
'token'     => $auth['access_token'],
                
'signature' => $signature,
            ),
            
$url
        
);

        return 
true;
    }

    
/**
     * Wrapper for self::request().
     *
     * @param string $endpoint The helper API endpoint to request.
     * @param array  $args Arguments passed to wp_remote_request().
     *
     * @return array The response object from wp_safe_remote_request().
     */
    
public static function get$endpoint$args = array() ) {
        
$args['method'] = 'GET';
        return 
self::request$endpoint$args );
    }

    
/**
     * Wrapper for self::request().
     *
     * @param string $endpoint The helper API endpoint to request.
     * @param array  $args Arguments passed to wp_remote_request().
     *
     * @return array The response object from wp_safe_remote_request().
     */
    
public static function post$endpoint$args = array() ) {
        
$args['method'] = 'POST';
        return 
self::request$endpoint$args );
    }

    
/**
     * Wrapper for self::request().
     *
     * @param string $endpoint The helper API endpoint to request.
     * @param array  $args Arguments passed to wp_remote_request().
     *
     * @return array The response object from wp_safe_remote_request().
     */
    
public static function put$endpoint$args = array() ) {
        
$args['method'] = 'PUT';
        return 
self::request$endpoint$args );
    }

    
/**
     * Using the API base, form a request URL from a given endpoint.
     *
     * @param string $endpoint The endpoint to request.
     * @param string $query_string Optional query string to append to the URL.
     *
     * @return string The absolute endpoint URL.
     */
    
public static function url$endpoint$query_string '' ) {
        
$endpoint ltrim$endpoint'/' );
        
$endpoint sprintf'%s/%s/%s'self::$api_base$endpoint$query_string );
        
$endpoint esc_url_raw$endpoint );
        
$endpoint rtrim$endpoint'/' );
        return 
$endpoint;
    }
}

WC_Helper_API::load();