Edit File by line
/home/barbar84/www/wp-conte.../plugins/wordpres.../src/routes
File: semrush-route.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\SEO\Routes;
[2] Fix | Delete
[3] Fix | Delete
use WP_REST_Request;
[4] Fix | Delete
use WP_REST_Response;
[5] Fix | Delete
use Yoast\WP\SEO\Actions\SEMrush\SEMrush_Login_Action;
[6] Fix | Delete
use Yoast\WP\SEO\Actions\SEMrush\SEMrush_Options_Action;
[7] Fix | Delete
use Yoast\WP\SEO\Actions\SEMrush\SEMrush_Phrases_Action;
[8] Fix | Delete
use Yoast\WP\SEO\Conditionals\SEMrush_Enabled_Conditional;
[9] Fix | Delete
use Yoast\WP\SEO\Main;
[10] Fix | Delete
[11] Fix | Delete
/**
[12] Fix | Delete
* SEMrush_Route class.
[13] Fix | Delete
*/
[14] Fix | Delete
class SEMrush_Route implements Route_Interface {
[15] Fix | Delete
[16] Fix | Delete
/**
[17] Fix | Delete
* The SEMrush route prefix.
[18] Fix | Delete
*
[19] Fix | Delete
* @var string
[20] Fix | Delete
*/
[21] Fix | Delete
const ROUTE_PREFIX = 'semrush';
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* The authenticate route constant.
[25] Fix | Delete
*
[26] Fix | Delete
* @var string
[27] Fix | Delete
*/
[28] Fix | Delete
const AUTHENTICATION_ROUTE = self::ROUTE_PREFIX . '/authenticate';
[29] Fix | Delete
[30] Fix | Delete
/**
[31] Fix | Delete
* The country code option route constant.
[32] Fix | Delete
*
[33] Fix | Delete
* @var string
[34] Fix | Delete
*/
[35] Fix | Delete
const COUNTRY_CODE_OPTION_ROUTE = self::ROUTE_PREFIX . '/country_code';
[36] Fix | Delete
[37] Fix | Delete
/**
[38] Fix | Delete
* The request related keyphrases route constant.
[39] Fix | Delete
*
[40] Fix | Delete
* @var string
[41] Fix | Delete
*/
[42] Fix | Delete
const RELATED_KEYPHRASES_ROUTE = self::ROUTE_PREFIX . '/related_keyphrases';
[43] Fix | Delete
[44] Fix | Delete
/**
[45] Fix | Delete
* The full login route constant.
[46] Fix | Delete
*
[47] Fix | Delete
* @var string
[48] Fix | Delete
*/
[49] Fix | Delete
const FULL_AUTHENTICATION_ROUTE = Main::API_V1_NAMESPACE . '/' . self::AUTHENTICATION_ROUTE;
[50] Fix | Delete
[51] Fix | Delete
/**
[52] Fix | Delete
* The full country code option route constant.
[53] Fix | Delete
*
[54] Fix | Delete
* @var string
[55] Fix | Delete
*/
[56] Fix | Delete
const FULL_COUNTRY_CODE_OPTION_ROUTE = Main::API_V1_NAMESPACE . '/' . self::COUNTRY_CODE_OPTION_ROUTE;
[57] Fix | Delete
[58] Fix | Delete
/**
[59] Fix | Delete
* The login action.
[60] Fix | Delete
*
[61] Fix | Delete
* @var SEMrush_Login_Action
[62] Fix | Delete
*/
[63] Fix | Delete
private $login_action;
[64] Fix | Delete
[65] Fix | Delete
/**
[66] Fix | Delete
* The options action.
[67] Fix | Delete
*
[68] Fix | Delete
* @var SEMrush_Options_Action
[69] Fix | Delete
*/
[70] Fix | Delete
private $options_action;
[71] Fix | Delete
[72] Fix | Delete
/**
[73] Fix | Delete
* The phrases action.
[74] Fix | Delete
*
[75] Fix | Delete
* @var SEMrush_Phrases_Action
[76] Fix | Delete
*/
[77] Fix | Delete
private $phrases_action;
[78] Fix | Delete
[79] Fix | Delete
/**
[80] Fix | Delete
* Returns the conditionals based in which this loadable should be active.
[81] Fix | Delete
*
[82] Fix | Delete
* @return array
[83] Fix | Delete
*/
[84] Fix | Delete
public static function get_conditionals() {
[85] Fix | Delete
return [ SEMrush_Enabled_Conditional::class ];
[86] Fix | Delete
}
[87] Fix | Delete
[88] Fix | Delete
/**
[89] Fix | Delete
* SEMrush_Route constructor.
[90] Fix | Delete
*
[91] Fix | Delete
* @param SEMrush_Login_Action $login_action The login action.
[92] Fix | Delete
* @param SEMrush_Options_Action $options_action The options action.
[93] Fix | Delete
* @param SEMrush_Phrases_Action $phrases_action The phrases action.
[94] Fix | Delete
*/
[95] Fix | Delete
public function __construct(
[96] Fix | Delete
SEMrush_Login_Action $login_action,
[97] Fix | Delete
SEMrush_Options_Action $options_action,
[98] Fix | Delete
SEMrush_Phrases_Action $phrases_action
[99] Fix | Delete
) {
[100] Fix | Delete
$this->login_action = $login_action;
[101] Fix | Delete
$this->options_action = $options_action;
[102] Fix | Delete
$this->phrases_action = $phrases_action;
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
/**
[106] Fix | Delete
* Registers routes with WordPress.
[107] Fix | Delete
*
[108] Fix | Delete
* @return void
[109] Fix | Delete
*/
[110] Fix | Delete
public function register_routes() {
[111] Fix | Delete
$authentication_route_args = [
[112] Fix | Delete
'methods' => 'POST',
[113] Fix | Delete
'callback' => [ $this, 'authenticate' ],
[114] Fix | Delete
'permission_callback' => [ $this, 'can_use_semrush' ],
[115] Fix | Delete
'args' => [
[116] Fix | Delete
'code' => [
[117] Fix | Delete
'validate_callback' => [ $this, 'has_valid_code' ],
[118] Fix | Delete
'required' => true,
[119] Fix | Delete
],
[120] Fix | Delete
],
[121] Fix | Delete
];
[122] Fix | Delete
[123] Fix | Delete
\register_rest_route( Main::API_V1_NAMESPACE, self::AUTHENTICATION_ROUTE, $authentication_route_args );
[124] Fix | Delete
[125] Fix | Delete
$set_country_code_option_route_args = [
[126] Fix | Delete
'methods' => 'POST',
[127] Fix | Delete
'callback' => [ $this, 'set_country_code_option' ],
[128] Fix | Delete
'permission_callback' => [ $this, 'can_use_semrush' ],
[129] Fix | Delete
'args' => [
[130] Fix | Delete
'country_code' => [
[131] Fix | Delete
'validate_callback' => [ $this, 'has_valid_country_code' ],
[132] Fix | Delete
'required' => true,
[133] Fix | Delete
],
[134] Fix | Delete
],
[135] Fix | Delete
];
[136] Fix | Delete
[137] Fix | Delete
\register_rest_route( Main::API_V1_NAMESPACE, self::COUNTRY_CODE_OPTION_ROUTE, $set_country_code_option_route_args );
[138] Fix | Delete
[139] Fix | Delete
$related_keyphrases_route_args = [
[140] Fix | Delete
'methods' => 'GET',
[141] Fix | Delete
'callback' => [ $this, 'get_related_keyphrases' ],
[142] Fix | Delete
'permission_callback' => [ $this, 'can_use_semrush' ],
[143] Fix | Delete
'args' => [
[144] Fix | Delete
'keyphrase' => [
[145] Fix | Delete
'validate_callback' => [ $this, 'has_valid_keyphrase' ],
[146] Fix | Delete
'required' => true,
[147] Fix | Delete
],
[148] Fix | Delete
'country_code' => [
[149] Fix | Delete
'required' => true,
[150] Fix | Delete
],
[151] Fix | Delete
],
[152] Fix | Delete
];
[153] Fix | Delete
[154] Fix | Delete
\register_rest_route( Main::API_V1_NAMESPACE, self::RELATED_KEYPHRASES_ROUTE, $related_keyphrases_route_args );
[155] Fix | Delete
}
[156] Fix | Delete
[157] Fix | Delete
/**
[158] Fix | Delete
* Authenticates with SEMrush.
[159] Fix | Delete
*
[160] Fix | Delete
* @param WP_REST_Request $request The request. This request should have a code param set.
[161] Fix | Delete
*
[162] Fix | Delete
* @return WP_REST_Response The response.
[163] Fix | Delete
*/
[164] Fix | Delete
public function authenticate( WP_REST_Request $request ) {
[165] Fix | Delete
$data = $this
[166] Fix | Delete
->login_action
[167] Fix | Delete
->authenticate( $request['code'] );
[168] Fix | Delete
[169] Fix | Delete
return new WP_REST_Response( $data, $data->status );
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
/**
[173] Fix | Delete
* Sets the SEMrush country code option.
[174] Fix | Delete
*
[175] Fix | Delete
* @param WP_REST_Request $request The request. This request should have a country code param set.
[176] Fix | Delete
*
[177] Fix | Delete
* @return WP_REST_Response The response.
[178] Fix | Delete
*/
[179] Fix | Delete
public function set_country_code_option( WP_REST_Request $request ) {
[180] Fix | Delete
$data = $this
[181] Fix | Delete
->options_action
[182] Fix | Delete
->set_country_code( $request['country_code'] );
[183] Fix | Delete
[184] Fix | Delete
return new WP_REST_Response( $data, $data->status );
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
/**
[188] Fix | Delete
* Checks if a valid code was returned.
[189] Fix | Delete
*
[190] Fix | Delete
* @param string $code The code to check.
[191] Fix | Delete
*
[192] Fix | Delete
* @return bool Whether or not the code is valid.
[193] Fix | Delete
*/
[194] Fix | Delete
public function has_valid_code( $code ) {
[195] Fix | Delete
return $code !== '';
[196] Fix | Delete
}
[197] Fix | Delete
[198] Fix | Delete
/**
[199] Fix | Delete
* Checks if a valid keyphrase is provided.
[200] Fix | Delete
*
[201] Fix | Delete
* @param string $keyphrase The keyphrase to check.
[202] Fix | Delete
*
[203] Fix | Delete
* @return bool Whether or not the keyphrase is valid.
[204] Fix | Delete
*/
[205] Fix | Delete
public function has_valid_keyphrase( $keyphrase ) {
[206] Fix | Delete
return \trim( $keyphrase ) !== '';
[207] Fix | Delete
}
[208] Fix | Delete
[209] Fix | Delete
/**
[210] Fix | Delete
* Gets the related keyphrases based on the passed keyphrase and database code.
[211] Fix | Delete
*
[212] Fix | Delete
* @param WP_REST_Request $request The request. This request should have a keyphrase and country_code param set.
[213] Fix | Delete
*
[214] Fix | Delete
* @return WP_REST_Response The response.
[215] Fix | Delete
*/
[216] Fix | Delete
public function get_related_keyphrases( WP_REST_Request $request ) {
[217] Fix | Delete
$data = $this
[218] Fix | Delete
->phrases_action
[219] Fix | Delete
->get_related_keyphrases(
[220] Fix | Delete
$request['keyphrase'],
[221] Fix | Delete
$request['country_code']
[222] Fix | Delete
);
[223] Fix | Delete
[224] Fix | Delete
return new WP_REST_Response( $data, $data->status );
[225] Fix | Delete
}
[226] Fix | Delete
[227] Fix | Delete
/**
[228] Fix | Delete
* Checks if a valid country code was submitted.
[229] Fix | Delete
*
[230] Fix | Delete
* @param string $country_code The country code to check.
[231] Fix | Delete
*
[232] Fix | Delete
* @return bool Whether or not the country code is valid.
[233] Fix | Delete
*/
[234] Fix | Delete
public function has_valid_country_code( $country_code ) {
[235] Fix | Delete
return ( $country_code !== '' && \preg_match( '/^[a-z]{2}$/', $country_code ) === 1 );
[236] Fix | Delete
}
[237] Fix | Delete
[238] Fix | Delete
/**
[239] Fix | Delete
* Whether or not the current user is allowed to edit post/pages and thus use the SEMrush integration.
[240] Fix | Delete
*
[241] Fix | Delete
* @return bool Whether or not the current user is allowed to use SEMrush.
[242] Fix | Delete
*/
[243] Fix | Delete
public function can_use_semrush() {
[244] Fix | Delete
return \current_user_can( 'edit_posts' ) || \current_user_can( 'edit_pages' );
[245] Fix | Delete
}
[246] Fix | Delete
}
[247] Fix | Delete
[248] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function