PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.2
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 18.2, at inc/class-my-yoast-api-request.php

208 lines 4.8 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 $this->response = $this->decode_response( $response );
70
71 return true;
72 }
73 catch ( WPSEO_MyYoast_Bad_Request_Exception $bad_request_exception ) {
74 $this->error_message = $bad_request_exception->getMessage();
75
76 return false;
77 }
78 }
79
80 /**
81 * Retrieves the error message.
82 *
83 * @return string The set error message.
84 */
85 public function get_error_message() {
86 return $this->error_message;
87 }
88
89 /**
90 * Retrieves the response.
91 *
92 * @return stdClass The response object.
93 */
94 public function get_response() {
95 return $this->response;
96 }
97
98 /**
99 * Performs the request using WordPress internals.
100 *
101 * @codeCoverageIgnore
102 *
103 * @param string $url The request URL.
104 * @param array $request_arguments The request arguments.
105 *
106 * @return string The retrieved body.
107 * @throws WPSEO_MyYoast_Bad_Request_Exception When request is invalid.
108 */
109 protected function do_request( $url, $request_arguments ) {
110 $request_arguments = $this->enrich_request_arguments( $request_arguments );
111 $response = wp_remote_request( $url, $request_arguments );
112
113 if ( is_wp_error( $response ) ) {
114 throw new WPSEO_MyYoast_Bad_Request_Exception( $response->get_error_message() );
115 }
116
117 $response_code = wp_remote_retrieve_response_code( $response );
118 $response_message = wp_remote_retrieve_response_message( $response );
119
120 // Do nothing, response code is okay.
121 if ( $response_code === 200 || strpos( $response_code, '200' ) !== false ) {
122 return wp_remote_retrieve_body( $response );
123 }
124
125 throw new WPSEO_MyYoast_Bad_Request_Exception( esc_html( $response_message ), (int) $response_code );
126 }
127
128 /**
129 * Decodes the JSON encoded response.
130 *
131 * @param string $response The response to decode.
132 *
133 * @return stdClass The json decoded response.
134 * @throws WPSEO_MyYoast_Invalid_JSON_Exception When decoded string is not a JSON object.
135 */
136 protected function decode_response( $response ) {
137 $response = json_decode( $response );
138
139 if ( ! is_object( $response ) ) {
140 throw new WPSEO_MyYoast_Invalid_JSON_Exception(
141 esc_html__( 'No JSON object was returned.', 'wordpress-seo' )
142 );
143 }
144
145 return $response;
146 }
147
148 /**
149 * Checks if MyYoast tokens are allowed and adds the token to the request body.
150 *
151 * When tokens are disallowed it will add the url to the request body.
152 *
153 * @param array $request_arguments The arguments to enrich.
154 *
155 * @return array The enriched arguments.
156 */
157 protected function enrich_request_arguments( array $request_arguments ) {
158 $request_arguments = wp_parse_args( $request_arguments, [ 'headers' => [] ] );
159 $addon_version_headers = $this->get_installed_addon_versions();
160
161 foreach ( $addon_version_headers as $addon => $version ) {
162 $request_arguments['headers'][ $addon . '-version' ] = $version;
163 }
164
165 $request_body = $this->get_request_body();
166 if ( $request_body !== [] ) {
167 $request_arguments['body'] = $request_body;
168 }
169
170 return $request_arguments;
171 }
172
173 /**
174 * Retrieves the request body based on URL or access token support.
175 *
176 * @codeCoverageIgnore
177 *
178 * @return array The request body.
179 */
180 public function get_request_body() {
181 return [ 'url' => WPSEO_Utils::get_home_url() ];
182 }
183
184 /**
185 * Wraps the get current user id function.
186 *
187 * @codeCoverageIgnore
188 *
189 * @return int The user id.
190 */
191 protected function get_current_user_id() {
192 return get_current_user_id();
193 }
194
195 /**
196 * Retrieves the installed addons as http headers.
197 *
198 * @codeCoverageIgnore
199 *
200 * @return array The installed addon versions.
201 */
202 protected function get_installed_addon_versions() {
203 $addon_manager = new WPSEO_Addon_Manager();
204
205 return $addon_manager->get_installed_addons_versions();
206 }
207 }
208