PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 19.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v19.0
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / config / semrush-client.php

semrush-client.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 19.0, at src/config/semrush-client.php

87 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 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(
33 Options_Helper $options_helper,
34 WP_Remote_Handler $wp_remote_handler
35 ) {
36 $provider = new GenericProvider(
37 [
38 'clientId' => 'yoast',
39 'clientSecret' => 'YdqNsWwnP4vE54WO1ugThKEjGMxMAHJt',
40 'redirectUri' => 'https://oauth.semrush.com/oauth2/yoast/success',
41 'urlAuthorize' => 'https://oauth.semrush.com/oauth2/authorize',
42 'urlAccessToken' => 'https://oauth.semrush.com/oauth2/access_token',
43 'urlResourceOwnerDetails' => 'https://oauth.semrush.com/oauth2/resource',
44 ],
45 [
46 'httpClient' => new Client( [ 'handler' => $wp_remote_handler ] ),
47 ]
48 );
49
50 parent::__construct(
51 self::TOKEN_OPTION,
52 $provider,
53 $options_helper
54 );
55 }
56
57 /**
58 * Performs the specified request.
59 *
60 * @codeCoverageIgnore
61 *
62 * @param string $method The HTTP method to use.
63 * @param string $url The URL to send the request to.
64 * @param array $options The options to pass along to the request.
65 *
66 * @return mixed The parsed API response.
67 *
68 * @throws IdentityProviderException Exception thrown if there's something wrong with the identifying data.
69 * @throws Authentication_Failed_Exception Exception thrown if authentication has failed.
70 * @throws Empty_Token_Exception Exception thrown if the token is empty.
71 */
72 public function do_request( $method, $url, array $options ) {
73 // Add the access token to the GET parameters as well since this is what
74 // the SEMRush API expects.
75 $options = \array_merge_recursive(
76 $options,
77 [
78 'params' => [
79 'access_token' => $this->get_tokens()->access_token,
80 ],
81 ]
82 );
83
84 return parent::do_request( $method, $url, $options );
85 }
86 }
87