diff --git a/includes/admin/helper/class-wc-helper-api.php b/includes/admin/helper/class-wc-helper-api.php index f24775c2cc3..646c814c724 100644 --- a/includes/admin/helper/class-wc-helper-api.php +++ b/includes/admin/helper/class-wc-helper-api.php @@ -28,13 +28,15 @@ class WC_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 The response from wp_safe_remote_request() + * @return array|WP_Error The response from wp_safe_remote_request() */ public static function request( $endpoint, $args = array() ) { $url = self::url( $endpoint ); if ( ! empty( $args['authenticated'] ) ) { - self::_authenticate( $url, $args ); + if ( ! self::_authenticate( $url, $args ) ) { + return new WP_Error( 'authentication', 'Authentication failed.' ); + } } /** @@ -52,23 +54,26 @@ class WC_Helper_API { * * @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; + return false; } - $request_uri = parse_url( $url, PHP_URL_PATH ); + $request_uri = parse_url( $url, PHP_URL_PATH ); $query_string = parse_url( $url, PHP_URL_QUERY ); + if ( $query_string ) { $request_uri .= '?' . $query_string; } $data = array( - 'host' => parse_url( $url, PHP_URL_HOST ), + 'host' => parse_url( $url, PHP_URL_HOST ), 'request_uri' => $request_uri, - 'method' => ! empty( $args['method'] ) ? $args['method'] : 'GET', + 'method' => ! empty( $args['method'] ) ? $args['method'] : 'GET', ); if ( ! empty( $args['body'] ) ) { @@ -84,6 +89,8 @@ class WC_Helper_API { 'Authorization' => 'Bearer ' . $auth['access_token'], 'X-Woo-Signature' => $signature, ); + + return true; } /**