PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.8
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.8
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 / routes / semrush-route.php

semrush-route.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.8, at src/routes/semrush-route.php

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