Edit File by line
/home/barbar84/www/wp-conte.../plugins/wordpres.../src/config
File: semrush-client.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\SEO\Config;
[2] Fix | Delete
[3] Fix | Delete
use Exception;
[4] Fix | Delete
use Yoast\WP\SEO\Exceptions\OAuth\Authentication_Failed_Exception;
[5] Fix | Delete
use Yoast\WP\SEO\Exceptions\SEMrush\Tokens\Empty_Property_Exception;
[6] Fix | Delete
use Yoast\WP\SEO\Exceptions\SEMrush\Tokens\Empty_Token_Exception;
[7] Fix | Delete
use Yoast\WP\SEO\Exceptions\SEMrush\Tokens\Failed_Storage_Exception;
[8] Fix | Delete
use Yoast\WP\SEO\Helpers\Options_Helper;
[9] Fix | Delete
use Yoast\WP\SEO\Values\SEMrush\SEMrush_Token;
[10] Fix | Delete
use Yoast\WP\SEO\Wrappers\WP_Remote_Handler;
[11] Fix | Delete
use YoastSEO_Vendor\GuzzleHttp\Client;
[12] Fix | Delete
use YoastSEO_Vendor\League\OAuth2\Client\Provider\Exception\IdentityProviderException;
[13] Fix | Delete
use YoastSEO_Vendor\League\OAuth2\Client\Provider\GenericProvider;
[14] Fix | Delete
[15] Fix | Delete
/**
[16] Fix | Delete
* Class SEMrush_Client
[17] Fix | Delete
*/
[18] Fix | Delete
class SEMrush_Client {
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* The option's key.
[22] Fix | Delete
*/
[23] Fix | Delete
const TOKEN_OPTION = 'semrush_tokens';
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* The provider.
[27] Fix | Delete
*
[28] Fix | Delete
* @var GenericProvider
[29] Fix | Delete
*/
[30] Fix | Delete
protected $provider;
[31] Fix | Delete
[32] Fix | Delete
/**
[33] Fix | Delete
* The options helper.
[34] Fix | Delete
*
[35] Fix | Delete
* @var Options_Helper
[36] Fix | Delete
*/
[37] Fix | Delete
protected $options_helper;
[38] Fix | Delete
[39] Fix | Delete
/**
[40] Fix | Delete
* The token.
[41] Fix | Delete
*
[42] Fix | Delete
* @var SEMrush_Token|null
[43] Fix | Delete
*/
[44] Fix | Delete
protected $token;
[45] Fix | Delete
[46] Fix | Delete
/**
[47] Fix | Delete
* SEMrush_Client constructor.
[48] Fix | Delete
*
[49] Fix | Delete
* @param Options_Helper $options_helper The Options_Helper instance.
[50] Fix | Delete
* @param WP_Remote_Handler $wp_remote_handler The request handler.
[51] Fix | Delete
*
[52] Fix | Delete
* @throws Empty_Property_Exception Exception thrown if a token property is empty.
[53] Fix | Delete
*/
[54] Fix | Delete
public function __construct(
[55] Fix | Delete
Options_Helper $options_helper,
[56] Fix | Delete
WP_Remote_Handler $wp_remote_handler
[57] Fix | Delete
) {
[58] Fix | Delete
$this->provider = new GenericProvider(
[59] Fix | Delete
[
[60] Fix | Delete
'clientId' => 'yoast',
[61] Fix | Delete
'clientSecret' => 'YdqNsWwnP4vE54WO1ugThKEjGMxMAHJt',
[62] Fix | Delete
'redirectUri' => 'https://oauth.semrush.com/oauth2/yoast/success',
[63] Fix | Delete
'urlAuthorize' => 'https://oauth.semrush.com/oauth2/authorize',
[64] Fix | Delete
'urlAccessToken' => 'https://oauth.semrush.com/oauth2/access_token',
[65] Fix | Delete
'urlResourceOwnerDetails' => 'https://oauth.semrush.com/oauth2/resource',
[66] Fix | Delete
],
[67] Fix | Delete
[
[68] Fix | Delete
'httpClient' => new Client( [ 'handler' => $wp_remote_handler ] ),
[69] Fix | Delete
]
[70] Fix | Delete
);
[71] Fix | Delete
[72] Fix | Delete
$this->options_helper = $options_helper;
[73] Fix | Delete
$this->token = $this->get_token_from_storage();
[74] Fix | Delete
}
[75] Fix | Delete
[76] Fix | Delete
/**
[77] Fix | Delete
* Requests the access token and refresh token based on the passed code.
[78] Fix | Delete
*
[79] Fix | Delete
* @param string $code The code to send.
[80] Fix | Delete
*
[81] Fix | Delete
* @return SEMrush_Token The requested tokens.
[82] Fix | Delete
*
[83] Fix | Delete
* @throws Authentication_Failed_Exception Exception thrown if authentication has failed.
[84] Fix | Delete
*/
[85] Fix | Delete
public function request_tokens( $code ) {
[86] Fix | Delete
try {
[87] Fix | Delete
$response = $this->provider
[88] Fix | Delete
->getAccessToken(
[89] Fix | Delete
'authorization_code',
[90] Fix | Delete
[
[91] Fix | Delete
'code' => $code,
[92] Fix | Delete
]
[93] Fix | Delete
);
[94] Fix | Delete
[95] Fix | Delete
$token = SEMrush_Token::from_response( $response );
[96] Fix | Delete
[97] Fix | Delete
return $this->store_token( $token );
[98] Fix | Delete
} catch ( Exception $exception ) {
[99] Fix | Delete
throw new Authentication_Failed_Exception( $exception );
[100] Fix | Delete
}
[101] Fix | Delete
}
[102] Fix | Delete
[103] Fix | Delete
/**
[104] Fix | Delete
* Performs an authenticated GET request to the desired URL.
[105] Fix | Delete
*
[106] Fix | Delete
* @param string $url The URL to send the request to.
[107] Fix | Delete
* @param array $options The options to pass along to the request.
[108] Fix | Delete
*
[109] Fix | Delete
* @return mixed The parsed API response.
[110] Fix | Delete
*
[111] Fix | Delete
* @throws IdentityProviderException Exception thrown if there's something wrong with the identifying data.
[112] Fix | Delete
* @throws Authentication_Failed_Exception Exception thrown if authentication has failed.
[113] Fix | Delete
* @throws Empty_Token_Exception Exception thrown if the token is empty.
[114] Fix | Delete
*/
[115] Fix | Delete
public function get( $url, $options = [] ) {
[116] Fix | Delete
return $this->do_request( 'GET', $url, $options );
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
/**
[120] Fix | Delete
* Performs an authenticated POST request to the desired URL.
[121] Fix | Delete
*
[122] Fix | Delete
* @param string $url The URL to send the request to.
[123] Fix | Delete
* @param mixed $body The data to send along in the request's body.
[124] Fix | Delete
* @param array $options The options to pass along to the request.
[125] Fix | Delete
*
[126] Fix | Delete
* @return mixed The parsed API response.
[127] Fix | Delete
*
[128] Fix | Delete
* @throws IdentityProviderException Exception thrown if there's something wrong with the identifying data.
[129] Fix | Delete
* @throws Authentication_Failed_Exception Exception thrown if authentication has failed.
[130] Fix | Delete
* @throws Empty_Token_Exception Exception thrown if the token is empty.
[131] Fix | Delete
*/
[132] Fix | Delete
public function post( $url, $body, $options = [] ) {
[133] Fix | Delete
$options['body'] = $body;
[134] Fix | Delete
[135] Fix | Delete
return $this->do_request( 'POST', $url, $options );
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
/**
[139] Fix | Delete
* Determines whether or not there are valid tokens available.
[140] Fix | Delete
*
[141] Fix | Delete
* @return bool Whether or not there are valid tokens.
[142] Fix | Delete
*/
[143] Fix | Delete
public function has_valid_tokens() {
[144] Fix | Delete
return ! empty( $this->token ) && $this->token->has_expired() === false;
[145] Fix | Delete
}
[146] Fix | Delete
[147] Fix | Delete
/**
[148] Fix | Delete
* Gets the stored tokens and refreshes them if they've expired.
[149] Fix | Delete
*
[150] Fix | Delete
* @return SEMrush_Token The stored tokens.
[151] Fix | Delete
*
[152] Fix | Delete
* @throws Empty_Token_Exception Exception thrown if the token is empty.
[153] Fix | Delete
*/
[154] Fix | Delete
public function get_tokens() {
[155] Fix | Delete
if ( empty( $this->token ) ) {
[156] Fix | Delete
throw new Empty_Token_Exception();
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
if ( $this->token->has_expired() ) {
[160] Fix | Delete
$this->token = $this->refresh_tokens( $this->token );
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
return $this->token;
[164] Fix | Delete
}
[165] Fix | Delete
[166] Fix | Delete
/**
[167] Fix | Delete
* Retrieves the token from storage.
[168] Fix | Delete
*
[169] Fix | Delete
* @return SEMrush_Token|null The token object. Returns null if none exists.
[170] Fix | Delete
*
[171] Fix | Delete
* @throws Empty_Property_Exception Exception thrown if a token property is empty.
[172] Fix | Delete
*/
[173] Fix | Delete
public function get_token_from_storage() {
[174] Fix | Delete
$tokens = $this->options_helper->get( self::TOKEN_OPTION );
[175] Fix | Delete
[176] Fix | Delete
if ( empty( $tokens ) ) {
[177] Fix | Delete
return null;
[178] Fix | Delete
}
[179] Fix | Delete
[180] Fix | Delete
return new SEMrush_Token(
[181] Fix | Delete
$tokens['access_token'],
[182] Fix | Delete
$tokens['refresh_token'],
[183] Fix | Delete
$tokens['expires'],
[184] Fix | Delete
$tokens['has_expired'],
[185] Fix | Delete
$tokens['created_at']
[186] Fix | Delete
);
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
/**
[190] Fix | Delete
* Stores the passed token.
[191] Fix | Delete
*
[192] Fix | Delete
* @param SEMrush_Token $token The token to store.
[193] Fix | Delete
*
[194] Fix | Delete
* @return SEMrush_Token The stored token.
[195] Fix | Delete
*
[196] Fix | Delete
* @throws Failed_Storage_Exception Exception thrown if storing of the token fails.
[197] Fix | Delete
*/
[198] Fix | Delete
public function store_token( SEMrush_Token $token ) {
[199] Fix | Delete
$saved = $this->options_helper->set( self::TOKEN_OPTION, $token->to_array() );
[200] Fix | Delete
[201] Fix | Delete
if ( $saved === false ) {
[202] Fix | Delete
throw new Failed_Storage_Exception();
[203] Fix | Delete
}
[204] Fix | Delete
[205] Fix | Delete
return $token;
[206] Fix | Delete
}
[207] Fix | Delete
[208] Fix | Delete
/**
[209] Fix | Delete
* Performs the specified request.
[210] Fix | Delete
*
[211] Fix | Delete
* @param string $method The HTTP method to use.
[212] Fix | Delete
* @param string $url The URL to send the request to.
[213] Fix | Delete
* @param array $options The options to pass along to the request.
[214] Fix | Delete
*
[215] Fix | Delete
* @return mixed The parsed API response.
[216] Fix | Delete
*
[217] Fix | Delete
* @throws IdentityProviderException Exception thrown if there's something wrong with the identifying data.
[218] Fix | Delete
* @throws Authentication_Failed_Exception Exception thrown if authentication has failed.
[219] Fix | Delete
* @throws Empty_Token_Exception Exception thrown if the token is empty.
[220] Fix | Delete
*/
[221] Fix | Delete
protected function do_request( $method, $url, array $options ) {
[222] Fix | Delete
$defaults = [
[223] Fix | Delete
'headers' => $this->provider->getHeaders(),
[224] Fix | Delete
'params' => [
[225] Fix | Delete
'access_token' => $this->get_tokens()->access_token,
[226] Fix | Delete
],
[227] Fix | Delete
];
[228] Fix | Delete
[229] Fix | Delete
$options = \array_merge_recursive( $defaults, $options );
[230] Fix | Delete
[231] Fix | Delete
if ( \array_key_exists( 'params', $options ) ) {
[232] Fix | Delete
$url .= '?' . \http_build_query( $options['params'] );
[233] Fix | Delete
unset( $options['params'] );
[234] Fix | Delete
}
[235] Fix | Delete
[236] Fix | Delete
$request = $this->provider
[237] Fix | Delete
->getAuthenticatedRequest( $method, $url, null, $options );
[238] Fix | Delete
[239] Fix | Delete
return $this->provider->getParsedResponse( $request );
[240] Fix | Delete
}
[241] Fix | Delete
[242] Fix | Delete
/**
[243] Fix | Delete
* Refreshes the outdated tokens.
[244] Fix | Delete
*
[245] Fix | Delete
* @param SEMrush_Token $tokens The outdated tokens.
[246] Fix | Delete
*
[247] Fix | Delete
* @return SEMrush_Token The refreshed tokens.
[248] Fix | Delete
*
[249] Fix | Delete
* @throws Authentication_Failed_Exception Exception thrown if authentication has failed.
[250] Fix | Delete
*/
[251] Fix | Delete
protected function refresh_tokens( SEMrush_Token $tokens ) {
[252] Fix | Delete
try {
[253] Fix | Delete
$new_tokens = $this->provider->getAccessToken(
[254] Fix | Delete
'refresh_token',
[255] Fix | Delete
[
[256] Fix | Delete
'refresh_token' => $tokens->refresh_token,
[257] Fix | Delete
]
[258] Fix | Delete
);
[259] Fix | Delete
[260] Fix | Delete
$token = SEMrush_Token::from_response( $new_tokens );
[261] Fix | Delete
[262] Fix | Delete
return $this->store_token( $token );
[263] Fix | Delete
} catch ( Exception $exception ) {
[264] Fix | Delete
throw new Authentication_Failed_Exception( $exception );
[265] Fix | Delete
}
[266] Fix | Delete
}
[267] Fix | Delete
}
[268] Fix | Delete
[269] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function