| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Config; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Exceptions\OAuth\Authentication_Failed_Exception; |
| 6 |
use Yoast\WP\SEO\Exceptions\OAuth\Tokens\Empty_Property_Exception; |
| 7 |
use Yoast\WP\SEO\Exceptions\OAuth\Tokens\Empty_Token_Exception; |
| 8 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 9 |
use Yoast\WP\SEO\Wrappers\WP_Remote_Handler; |
| 10 |
use YoastSEO_Vendor\GuzzleHttp\Client; |
| 11 |
use YoastSEO_Vendor\League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
| 12 |
use YoastSEO_Vendor\League\OAuth2\Client\Provider\GenericProvider; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class SEMrush_Client |
| 16 |
*/ |
| 17 |
class SEMrush_Client extends OAuth_Client { |
| 18 |
|
| 19 |
/** |
| 20 |
* The option's key. |
| 21 |
*/ |
| 22 |
public const TOKEN_OPTION = 'semrush_tokens'; |
| 23 |
|
| 24 |
/** |
| 25 |
* SEMrush_Client constructor. |
| 26 |
* |
| 27 |
* @param Options_Helper $options_helper The Options_Helper instance. |
| 28 |
* @param WP_Remote_Handler $wp_remote_handler The request handler. |
| 29 |
* |
| 30 |
* @throws Empty_Property_Exception Throws when one of the required properties is empty. |
| 31 |
*/ |
| 32 |
public function __construct( Options_Helper $options_helper, WP_Remote_Handler $wp_remote_handler ) { |
| 33 |
$provider = new GenericProvider( |
| 34 |
[ |
| 35 |
'clientId' => 'yoast', |
| 36 |
'clientSecret' => 'YdqNsWwnP4vE54WO1ugThKEjGMxMAHJt', |
| 37 |
'redirectUri' => 'https://oauth.semrush.com/oauth2/yoast/success', |
| 38 |
'urlAuthorize' => 'https://oauth.semrush.com/oauth2/authorize', |
| 39 |
'urlAccessToken' => 'https://oauth.semrush.com/oauth2/access_token', |
| 40 |
'urlResourceOwnerDetails' => 'https://oauth.semrush.com/oauth2/resource', |
| 41 |
], |
| 42 |
[ |
| 43 |
'httpClient' => new Client( [ 'handler' => $wp_remote_handler ] ), |
| 44 |
], |
| 45 |
); |
| 46 |
|
| 47 |
parent::__construct( |
| 48 |
self::TOKEN_OPTION, |
| 49 |
$provider, |
| 50 |
$options_helper, |
| 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 ); |
| 82 |
} |
| 83 |
} |
| 84 |
|