| @@ -1,12 +1,15 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Yoast\WP\SEO\Config; |
| 4 | 4 | |
| 5 | +use Yoast\WP\SEO\Exceptions\OAuth\Authentication_Failed_Exception; | |
| 5 | 6 | use Yoast\WP\SEO\Exceptions\OAuth\Tokens\Empty_Property_Exception; |
| 7 | +use Yoast\WP\SEO\Exceptions\OAuth\Tokens\Empty_Token_Exception; | |
| 6 | 8 | use Yoast\WP\SEO\Helpers\Options_Helper; |
| 7 | 9 | use Yoast\WP\SEO\Wrappers\WP_Remote_Handler; |
| 8 | 10 | use YoastSEO_Vendor\GuzzleHttp\Client; |
| 11 | +use YoastSEO_Vendor\League\OAuth2\Client\Provider\Exception\IdentityProviderException; | |
| 9 | 12 | use YoastSEO_Vendor\League\OAuth2\Client\Provider\GenericProvider; |
| 10 | 13 | |
| 11 | 14 | /** |
| 12 | 15 | * Class SEMrush_Client |
| @@ -15,9 +18,9 @@ | ||
| 15 | 18 | |
| 16 | 19 | /** |
| 17 | 20 | * The option's key. |
| 18 | 21 | */ |
| 19 | - const TOKEN_OPTION = 'semrush_tokens'; | |
| 22 | + public const TOKEN_OPTION = 'semrush_tokens'; | |
| 20 | 23 | |
| 21 | 24 | /** |
| 22 | 25 | * SEMrush_Client constructor. |
| 23 | 26 | * |
| @@ -25,12 +28,9 @@ | ||
| 25 | 28 | * @param WP_Remote_Handler $wp_remote_handler The request handler. |
| 26 | 29 | * |
| 27 | 30 | * @throws Empty_Property_Exception Throws when one of the required properties is empty. |
| 28 | 31 | */ |
| 29 | - public function __construct( | |
| 30 | - Options_Helper $options_helper, | |
| 31 | - WP_Remote_Handler $wp_remote_handler | |
| 32 | - ) { | |
| 32 | + public function __construct( Options_Helper $options_helper, WP_Remote_Handler $wp_remote_handler ) { | |
| 33 | 33 | $provider = new GenericProvider( |
| 34 | 34 | [ |
| 35 | 35 | 'clientId' => 'yoast', |
| 36 | 36 | 'clientSecret' => 'YdqNsWwnP4vE54WO1ugThKEjGMxMAHJt', |
| @@ -40,14 +40,44 @@ | ||
| 40 | 40 | 'urlResourceOwnerDetails' => 'https://oauth.semrush.com/oauth2/resource', |
| 41 | 41 | ], |
| 42 | 42 | [ |
| 43 | 43 | 'httpClient' => new Client( [ 'handler' => $wp_remote_handler ] ), |
| 44 | - ] | |
| 44 | + ], | |
| 45 | 45 | ); |
| 46 | 46 | |
| 47 | 47 | parent::__construct( |
| 48 | 48 | self::TOKEN_OPTION, |
| 49 | 49 | $provider, |
| 50 | - $options_helper | |
| 50 | + $options_helper, | |
| 51 | 51 | ); |
| 52 | + } | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * Performs the specified request. | |
| 56 | + * | |
| 57 | + * @codeCoverageIgnore | |
| 58 | + * | |
| 59 | + * @param string $method The HTTP method to use. | |
| 60 | + * @param string $url The URL to send the request to. | |
| 61 | + * @param array $options The options to pass along to the request. | |
| 62 | + * | |
| 63 | + * @return mixed The parsed API response. | |
| 64 | + * | |
| 65 | + * @throws IdentityProviderException Exception thrown if there's something wrong with the identifying data. | |
| 66 | + * @throws Authentication_Failed_Exception Exception thrown if authentication has failed. | |
| 67 | + * @throws Empty_Token_Exception Exception thrown if the token is empty. | |
| 68 | + */ | |
| 69 | + public function do_request( $method, $url, array $options ) { | |
| 70 | + // Add the access token to the GET parameters as well since this is what | |
| 71 | + // the SEMRush API expects. | |
| 72 | + $options = \array_merge_recursive( | |
| 73 | + $options, | |
| 74 | + [ | |
| 75 | + 'params' => [ | |
| 76 | + 'access_token' => $this->get_tokens()->access_token, | |
| 77 | + ], | |
| 78 | + ], | |
| 79 | + ); | |
| 80 | + | |
| 81 | + return parent::do_request( $method, $url, $options ); | |
| 52 | 82 | } |
| 53 | 83 | } |