PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.9
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.9
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 / inc / class-my-yoast-api-request.php

class-my-yoast-api-request.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.9, at inc/class-my-yoast-api-request.php

225 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO\Inc
6 */
7
8 /**
9 * Handles requests to MyYoast.
10 */
11 class WPSEO_MyYoast_Api_Request {
12
13 /**
14 * The Request URL.
15 *
16 * @var string
17 */
18 protected $url;
19
20 /**
21 * The request parameters.
22 *
23 * @var array
24 */
25 protected $args = [
26 'method' => 'GET',
27 'timeout' => 5,
28 'headers' => [
29 'Accept-Encoding' => '*',
30 'Expect' => '',
31 ],
32 ];
33
34 /**
35 * Contains the fetched response.
36 *
37 * @var stdClass
38 */
39 protected $response;
40
41 /**
42 * Contains the error message when request went wrong.
43 *
44 * @var string
45 */
46 protected $error_message = '';
47
48 /**
49 * Constructor.
50 *
51 * @codeCoverageIgnore
52 *
53 * @param string $url The request url.
54 * @param array $args The request arguments.
55 */
56 public function __construct( $url, array $args = [] ) {
57 $this->url = 'https://my.yoast.com/api/' . $url;
58 $this->args = wp_parse_args( $args, $this->args );
59 }
60
61 /**
62 * Fires the request.
63 *
64 * @return bool True when request is successful.
65 */
66 public function fire() {
67 try {
68 $response = $this->do_request( $this->url, $this->args );
69 $response = $this->decode_response( $response );
70 $this->response = $this->validate_response( $response );
71 return true;
72 } catch ( WPSEO_MyYoast_Bad_Request_Exception $bad_request_exception ) {
73 $this->error_message = $bad_request_exception->getMessage();
74
75 return false;
76 }
77 }
78
79 /**
80 * Retrieves the error message.
81 *
82 * @return string The set error message.
83 */
84 public function get_error_message() {
85 return $this->error_message;
86 }
87
88 /**
89 * Retrieves the response.
90 *
91 * @return stdClass The response object.
92 */
93 public function get_response() {
94 return $this->response;
95 }
96
97 /**
98 * Performs the request using WordPress internals.
99 *
100 * @codeCoverageIgnore
101 *
102 * @param string $url The request URL.
103 * @param array $request_arguments The request arguments.
104 *
105 * @return string The retrieved body.
106 * @throws WPSEO_MyYoast_Bad_Request_Exception When request is invalid.
107 */
108 protected function do_request( $url, $request_arguments ) {
109 $request_arguments = $this->enrich_request_arguments( $request_arguments );
110 $response = wp_remote_request( $url, $request_arguments );
111
112 if ( is_wp_error( $response ) ) {
113 throw new WPSEO_MyYoast_Bad_Request_Exception( $response->get_error_message() );
114 }
115
116 $response_code = wp_remote_retrieve_response_code( $response );
117 $response_message = wp_remote_retrieve_response_message( $response );
118
119 // Do nothing, response code is okay.
120 if ( $response_code === 200 ) {
121 return wp_remote_retrieve_body( $response );
122 }
123
124 throw new WPSEO_MyYoast_Bad_Request_Exception( esc_html( $response_message ), (int) $response_code );
125 }
126
127 /**
128 * Decodes the JSON encoded response.
129 *
130 * @param string $response The response to decode.
131 *
132 * @return stdClass The json decoded response.
133 * @throws WPSEO_MyYoast_Invalid_JSON_Exception When decoded string is not a JSON object.
134 */
135 protected function decode_response( $response ) {
136 $response = json_decode( $response );
137
138 if ( ! is_object( $response ) ) {
139 throw new WPSEO_MyYoast_Invalid_JSON_Exception(
140 esc_html__( 'No JSON object was returned.', 'wordpress-seo' ),
141 );
142 }
143
144 return $response;
145 }
146
147 /**
148 * Validates that all the needed fields are in de decoded response.
149 *
150 * @param stdClass $response The response to validate.
151 *
152 * @return stdClass The json decoded response.
153 * @throws WPSEO_MyYoast_Invalid_JSON_Exception When not all needed fields are found.
154 */
155 private function validate_response( $response ) {
156 if ( isset( $response->url, $response->subscriptions ) && is_array( $response->subscriptions ) ) {
157 return $response;
158 }
159
160 throw new WPSEO_MyYoast_Invalid_JSON_Exception(
161 esc_html__( 'Not all needed fields are present.', 'wordpress-seo' ),
162 );
163 }
164
165 /**
166 * Checks if MyYoast tokens are allowed and adds the token to the request body.
167 *
168 * When tokens are disallowed it will add the url to the request body.
169 *
170 * @param array $request_arguments The arguments to enrich.
171 *
172 * @return array The enriched arguments.
173 */
174 protected function enrich_request_arguments( array $request_arguments ) {
175 $request_arguments = wp_parse_args( $request_arguments, [ 'headers' => [] ] );
176 $addon_version_headers = $this->get_installed_addon_versions();
177
178 foreach ( $addon_version_headers as $addon => $version ) {
179 $request_arguments['headers'][ $addon . '-version' ] = $version;
180 }
181
182 $request_body = $this->get_request_body();
183 if ( $request_body !== [] ) {
184 $request_arguments['body'] = $request_body;
185 }
186
187 return $request_arguments;
188 }
189
190 /**
191 * Retrieves the request body based on URL or access token support.
192 *
193 * @codeCoverageIgnore
194 *
195 * @return array The request body.
196 */
197 public function get_request_body() {
198 return [ 'url' => WPSEO_Utils::get_home_url() ];
199 }
200
201 /**
202 * Wraps the get current user id function.
203 *
204 * @codeCoverageIgnore
205 *
206 * @return int The user id.
207 */
208 protected function get_current_user_id() {
209 return get_current_user_id();
210 }
211
212 /**
213 * Retrieves the installed addons as http headers.
214 *
215 * @codeCoverageIgnore
216 *
217 * @return array The installed addon versions.
218 */
219 protected function get_installed_addon_versions() {
220 $addon_manager = new WPSEO_Addon_Manager();
221
222 return $addon_manager->get_installed_addons_versions();
223 }
224 }
225