2017-04-21 12:05:44 +00:00
|
|
|
<?php
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2017-04-26 15:50:38 +00:00
|
|
|
/**
|
|
|
|
* WC_Helper_API Class
|
|
|
|
*
|
|
|
|
* Provides a communication interface with the WooCommerce.com Helper API.
|
|
|
|
*/
|
2017-04-21 12:05:44 +00:00
|
|
|
class WC_Helper_API {
|
|
|
|
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.
|
2018-03-05 18:59:17 +00:00
|
|
|
* @param array $args Additional data for the request. Set authenticated to a truthy value to enable auth.
|
2017-04-21 12:05:44 +00:00
|
|
|
*
|
2017-08-21 09:49:00 +00:00
|
|
|
* @return array|WP_Error The response from wp_safe_remote_request()
|
2017-04-21 12:05:44 +00:00
|
|
|
*/
|
|
|
|
public static function request( $endpoint, $args = array() ) {
|
|
|
|
$url = self::url( $endpoint );
|
|
|
|
|
|
|
|
if ( ! empty( $args['authenticated'] ) ) {
|
2017-08-21 09:49:00 +00:00
|
|
|
if ( ! self::_authenticate( $url, $args ) ) {
|
|
|
|
return new WP_Error( 'authentication', 'Authentication failed.' );
|
|
|
|
}
|
2017-04-21 12:05:44 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 10:45:43 +00:00
|
|
|
/**
|
|
|
|
* 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 );
|
2017-04-21 12:05:44 +00:00
|
|
|
|
|
|
|
// TODO: Check response signatures on certain endpoints.
|
2017-04-26 12:57:56 +00:00
|
|
|
return wp_safe_remote_request( $url, $args );
|
2017-04-21 12:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds authentication headers to an HTTP request.
|
|
|
|
*
|
|
|
|
* @param string $url The request URI.
|
2018-03-05 18:59:17 +00:00
|
|
|
* @param array $args By-ref, the args that will be passed to wp_remote_request().
|
2017-08-21 09:49:00 +00:00
|
|
|
* @return bool Were the headers added?
|
2017-04-21 12:05:44 +00:00
|
|
|
*/
|
|
|
|
private static function _authenticate( $url, &$args ) {
|
|
|
|
$auth = WC_Helper_Options::get( 'auth' );
|
2017-08-21 09:49:00 +00:00
|
|
|
|
2017-06-14 13:12:53 +00:00
|
|
|
if ( empty( $auth['access_token'] ) || empty( $auth['access_token_secret'] ) ) {
|
2017-08-21 09:49:00 +00:00
|
|
|
return false;
|
2017-06-14 13:12:53 +00:00
|
|
|
}
|
|
|
|
|
2017-08-21 09:49:00 +00:00
|
|
|
$request_uri = parse_url( $url, PHP_URL_PATH );
|
2017-04-21 12:05:44 +00:00
|
|
|
$query_string = parse_url( $url, PHP_URL_QUERY );
|
2017-08-21 09:49:00 +00:00
|
|
|
|
2017-08-23 13:40:34 +00:00
|
|
|
if ( is_string( $query_string ) ) {
|
2017-04-21 12:05:44 +00:00
|
|
|
$request_uri .= '?' . $query_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = array(
|
2017-08-21 09:49:00 +00:00
|
|
|
'host' => parse_url( $url, PHP_URL_HOST ),
|
2017-04-21 12:05:44 +00:00
|
|
|
'request_uri' => $request_uri,
|
2017-08-21 09:49:00 +00:00
|
|
|
'method' => ! empty( $args['method'] ) ? $args['method'] : 'GET',
|
2017-04-21 12:05:44 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if ( ! empty( $args['body'] ) ) {
|
|
|
|
$data['body'] = $args['body'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$signature = hash_hmac( 'sha256', json_encode( $data ), $auth['access_token_secret'] );
|
|
|
|
if ( empty( $args['headers'] ) ) {
|
|
|
|
$args['headers'] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$args['headers'] = array(
|
2018-03-05 18:59:17 +00:00
|
|
|
'Authorization' => 'Bearer ' . $auth['access_token'],
|
2017-04-21 12:05:44 +00:00
|
|
|
'X-Woo-Signature' => $signature,
|
|
|
|
);
|
2017-08-21 09:49:00 +00:00
|
|
|
|
|
|
|
return true;
|
2017-04-21 12:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper for self::request().
|
2017-05-03 10:33:39 +00:00
|
|
|
*
|
|
|
|
* @param string $endpoint The helper API endpoint to request.
|
2018-03-05 18:59:17 +00:00
|
|
|
* @param array $args Arguments passed to wp_remote_request().
|
2017-05-03 10:33:39 +00:00
|
|
|
*
|
|
|
|
* @return array The response object from wp_safe_remote_request().
|
2017-04-21 12:05:44 +00:00
|
|
|
*/
|
|
|
|
public static function get( $endpoint, $args = array() ) {
|
|
|
|
$args['method'] = 'GET';
|
|
|
|
return self::request( $endpoint, $args );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper for self::request().
|
2017-05-03 10:33:39 +00:00
|
|
|
*
|
|
|
|
* @param string $endpoint The helper API endpoint to request.
|
2018-03-05 18:59:17 +00:00
|
|
|
* @param array $args Arguments passed to wp_remote_request().
|
2017-05-03 10:33:39 +00:00
|
|
|
*
|
|
|
|
* @return array The response object from wp_safe_remote_request().
|
2017-04-21 12:05:44 +00:00
|
|
|
*/
|
|
|
|
public static function post( $endpoint, $args = array() ) {
|
|
|
|
$args['method'] = 'POST';
|
|
|
|
return self::request( $endpoint, $args );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Using the API base, form a request URL from a given endpoint.
|
|
|
|
*
|
|
|
|
* @param string $endpoint The endpoint to request.
|
|
|
|
*
|
|
|
|
* @return string The absolute endpoint URL.
|
|
|
|
*/
|
|
|
|
public static function url( $endpoint ) {
|
|
|
|
$endpoint = ltrim( $endpoint, '/' );
|
|
|
|
$endpoint = sprintf( '%s/%s', self::$api_base, $endpoint );
|
|
|
|
$endpoint = esc_url_raw( $endpoint );
|
|
|
|
return $endpoint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WC_Helper_API::load();
|