Geen omschrijving

class-wc-helper-api.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * WooCommerce Admin Helper API
  4. *
  5. * @package WooCommerce\Admin\Helper
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) {
  8. exit;
  9. }
  10. /**
  11. * WC_Helper_API Class
  12. *
  13. * Provides a communication interface with the WooCommerce.com Helper API.
  14. */
  15. class WC_Helper_API {
  16. /**
  17. * Base path for API routes.
  18. *
  19. * @var $api_base
  20. */
  21. public static $api_base;
  22. /**
  23. * Load
  24. *
  25. * Allow devs to point the API base to a local API development or staging server.
  26. * Note that sslverify will be turned off for the woocommerce.dev + WP_DEBUG combination.
  27. * The URL can be changed on plugins_loaded before priority 10.
  28. */
  29. public static function load() {
  30. self::$api_base = apply_filters( 'woocommerce_helper_api_base', 'https://woocommerce.com/wp-json/helper/1.0' );
  31. }
  32. /**
  33. * Perform an HTTP request to the Helper API.
  34. *
  35. * @param string $endpoint The endpoint to request.
  36. * @param array $args Additional data for the request. Set authenticated to a truthy value to enable auth.
  37. *
  38. * @return array|WP_Error The response from wp_safe_remote_request()
  39. */
  40. public static function request( $endpoint, $args = array() ) {
  41. $url = self::url( $endpoint );
  42. if ( ! empty( $args['authenticated'] ) ) {
  43. if ( ! self::_authenticate( $url, $args ) ) {
  44. return new WP_Error( 'authentication', 'Authentication failed.' );
  45. }
  46. }
  47. /**
  48. * Allow developers to filter the request args passed to wp_safe_remote_request().
  49. * Useful to remove sslverify when working on a local api dev environment.
  50. */
  51. $args = apply_filters( 'woocommerce_helper_api_request_args', $args, $endpoint );
  52. // TODO: Check response signatures on certain endpoints.
  53. return wp_safe_remote_request( $url, $args );
  54. }
  55. /**
  56. * Adds authentication headers to an HTTP request.
  57. *
  58. * @param string $url The request URI.
  59. * @param array $args By-ref, the args that will be passed to wp_remote_request().
  60. * @return bool Were the headers added?
  61. */
  62. private static function _authenticate( &$url, &$args ) {
  63. $auth = WC_Helper_Options::get( 'auth' );
  64. if ( empty( $auth['access_token'] ) || empty( $auth['access_token_secret'] ) ) {
  65. return false;
  66. }
  67. $request_uri = parse_url( $url, PHP_URL_PATH );
  68. $query_string = parse_url( $url, PHP_URL_QUERY );
  69. if ( is_string( $query_string ) ) {
  70. $request_uri .= '?' . $query_string;
  71. }
  72. $data = array(
  73. 'host' => parse_url( $url, PHP_URL_HOST ),
  74. 'request_uri' => $request_uri,
  75. 'method' => ! empty( $args['method'] ) ? $args['method'] : 'GET',
  76. );
  77. if ( ! empty( $args['body'] ) ) {
  78. $data['body'] = $args['body'];
  79. }
  80. $signature = hash_hmac( 'sha256', json_encode( $data ), $auth['access_token_secret'] );
  81. if ( empty( $args['headers'] ) ) {
  82. $args['headers'] = array();
  83. }
  84. $headers = array(
  85. 'Authorization' => 'Bearer ' . $auth['access_token'],
  86. 'X-Woo-Signature' => $signature,
  87. );
  88. $args['headers'] = wp_parse_args( $headers, $args['headers'] );
  89. $url = add_query_arg(
  90. array(
  91. 'token' => $auth['access_token'],
  92. 'signature' => $signature,
  93. ),
  94. $url
  95. );
  96. return true;
  97. }
  98. /**
  99. * Wrapper for self::request().
  100. *
  101. * @param string $endpoint The helper API endpoint to request.
  102. * @param array $args Arguments passed to wp_remote_request().
  103. *
  104. * @return array The response object from wp_safe_remote_request().
  105. */
  106. public static function get( $endpoint, $args = array() ) {
  107. $args['method'] = 'GET';
  108. return self::request( $endpoint, $args );
  109. }
  110. /**
  111. * Wrapper for self::request().
  112. *
  113. * @param string $endpoint The helper API endpoint to request.
  114. * @param array $args Arguments passed to wp_remote_request().
  115. *
  116. * @return array The response object from wp_safe_remote_request().
  117. */
  118. public static function post( $endpoint, $args = array() ) {
  119. $args['method'] = 'POST';
  120. return self::request( $endpoint, $args );
  121. }
  122. /**
  123. * Wrapper for self::request().
  124. *
  125. * @param string $endpoint The helper API endpoint to request.
  126. * @param array $args Arguments passed to wp_remote_request().
  127. *
  128. * @return array The response object from wp_safe_remote_request().
  129. */
  130. public static function put( $endpoint, $args = array() ) {
  131. $args['method'] = 'PUT';
  132. return self::request( $endpoint, $args );
  133. }
  134. /**
  135. * Using the API base, form a request URL from a given endpoint.
  136. *
  137. * @param string $endpoint The endpoint to request.
  138. *
  139. * @return string The absolute endpoint URL.
  140. */
  141. public static function url( $endpoint ) {
  142. $endpoint = ltrim( $endpoint, '/' );
  143. $endpoint = sprintf( '%s/%s', self::$api_base, $endpoint );
  144. $endpoint = esc_url_raw( $endpoint );
  145. return $endpoint;
  146. }
  147. }
  148. WC_Helper_API::load();