PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 19.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v19.0
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 / admin / class-remote-request.php

class-remote-request.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 19.0, at admin/class-remote-request.php

157 lines 3.1 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\Admin
6 */
7
8 /**
9 * This class handles a post request being send to a given endpoint.
10 */
11 class WPSEO_Remote_Request {
12
13 /**
14 * Holds the post method.
15 *
16 * @var string
17 */
18 const METHOD_POST = 'post';
19
20 /**
21 * Holds the get method.
22 *
23 * @var string
24 */
25 const METHOD_GET = 'get';
26
27 /**
28 * Holds the endpoint to send the request to.
29 *
30 * @var string
31 */
32 protected $endpoint = '';
33
34 /**
35 * Holds the arguments to use in this request.
36 *
37 * @var array
38 */
39 protected $args = [
40 'blocking' => false,
41 'timeout' => 2,
42 ];
43
44 /**
45 * Holds the response error.
46 *
47 * @var WP_Error|null
48 */
49 protected $response_error;
50
51 /**
52 * Holds the response body.
53 *
54 * @var mixed
55 */
56 protected $response_body;
57
58 /**
59 * Sets the endpoint and arguments.
60 *
61 * @param string $endpoint The endpoint to send the request to.
62 * @param array $args The arguments to use in this request.
63 */
64 public function __construct( $endpoint, array $args = [] ) {
65 $this->endpoint = $endpoint;
66 $this->args = wp_parse_args( $this->args, $args );
67 }
68
69 /**
70 * Sets the request body.
71 *
72 * @param mixed $body The body to set.
73 */
74 public function set_body( $body ) {
75 $this->args['body'] = $body;
76 }
77
78 /**
79 * Sends the data to the given endpoint.
80 *
81 * @param string $method The type of request to send.
82 *
83 * @return bool True when sending data has been successful.
84 */
85 public function send( $method = self::METHOD_POST ) {
86 switch ( $method ) {
87 case self::METHOD_POST:
88 $response = $this->post();
89 break;
90 case self::METHOD_GET:
91 $response = $this->get();
92 break;
93 default:
94 /* translators: %1$s expands to the request method */
95 $response = new WP_Error( 1, sprintf( __( 'Request method %1$s is not valid.', 'wordpress-seo' ), $method ) );
96 break;
97 }
98
99 return $this->process_response( $response );
100 }
101
102 /**
103 * Returns the value of the response error.
104 *
105 * @return WP_Error|null The response error.
106 */
107 public function get_response_error() {
108 return $this->response_error;
109 }
110
111 /**
112 * Returns the response body.
113 *
114 * @return mixed The response body.
115 */
116 public function get_response_body() {
117 return $this->response_body;
118 }
119
120 /**
121 * Processes the given response.
122 *
123 * @param mixed $response The response to process.
124 *
125 * @return bool True when response is valid.
126 */
127 protected function process_response( $response ) {
128 if ( $response instanceof WP_Error ) {
129 $this->response_error = $response;
130
131 return false;
132 }
133
134 $this->response_body = wp_remote_retrieve_body( $response );
135
136 return ( wp_remote_retrieve_response_code( $response ) === 200 );
137 }
138
139 /**
140 * Performs a post request to the specified endpoint with set arguments.
141 *
142 * @return WP_Error|array The response or WP_Error on failure.
143 */
144 protected function post() {
145 return wp_remote_post( $this->endpoint, $this->args );
146 }
147
148 /**
149 * Performs a post request to the specified endpoint with set arguments.
150 *
151 * @return WP_Error|array The response or WP_Error on failure.
152 */
153 protected function get() {
154 return wp_remote_get( $this->endpoint, $this->args );
155 }
156 }
157