PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.3.1
Defender Security – Malware Scanner, Login Security & Firewall v5.3.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / extra / hub-connector / inc / class-request.php

class-request.php in Defender Security – Malware Scanner, Login Security & Firewall 5.3.1, at extra/hub-connector/inc/class-request.php

242 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * API request class
4 *
5 * Handles all the API requests to Hub.
6 *
7 * @link http://wpmudev.com
8 * @since 1.0.0
9 * @author Joel James <[email protected]>
10 * @package WPMUDEV\Hub\Connector
11 */
12
13 namespace WPMUDEV\Hub\Connector;
14
15 defined( 'WPINC' ) || die;
16
17 use Exception;
18 use WP_Error;
19
20 /**
21 * Class Request.
22 */
23 class Request {
24
25 /**
26 * Request max timeout.
27 *
28 * @since 1.0.0
29 *
30 * @var int
31 */
32 private $timeout = 15;
33
34 /**
35 * Header arguments.
36 *
37 * @since 1.0.0
38 *
39 * @var array
40 */
41 private $headers = array();
42
43 /**
44 * POST arguments.
45 *
46 * @since 1.0.0
47 *
48 * @var array
49 */
50 private $post_args = array();
51
52 /**
53 * GET arguments.
54 *
55 * @since 1.0.0
56 *
57 * @var array
58 */
59 private $get_args = array();
60
61 /**
62 * Request constructor.
63 *
64 * @since 1.0.0
65 */
66 public function __construct() {
67 }
68
69 /**
70 * Set request timeout.
71 *
72 * @since 1.0.0
73 *
74 * @param int $timeout Request timeout (seconds).
75 */
76 public function set_timeout( $timeout ) {
77 $this->timeout = $timeout;
78 }
79
80 /**
81 * Add a new request argument for POST requests.
82 *
83 * @since 1.0.0
84 *
85 * @param string $name Argument name.
86 * @param string $value Argument value.
87 */
88 public function add_post_argument( $name, $value ) {
89 $this->post_args[ $name ] = $value;
90 }
91
92 /**
93 * Add a new request argument for GET requests.
94 *
95 * @since 1.0.0
96 *
97 * @param string $name Argument name.
98 * @param string $value Argument value.
99 */
100 public function add_get_argument( $name, $value ) {
101 $this->get_args[ $name ] = $value;
102 }
103
104 /**
105 * Add a new request argument for GET requests.
106 *
107 * @since 1.0.0
108 *
109 * @param string $name Argument name.
110 * @param string $value Argument value.
111 */
112 public function add_header_argument( $name, $value ) {
113 $this->headers[ $name ] = $value;
114 }
115
116 /**
117 * Make a POST request.
118 *
119 * @since 1.0.0
120 *
121 * @param string $path Endpoint route.
122 * @param bool $auth Should attach API key?.
123 * @param array $data Data array.
124 *
125 * @return mixed|WP_Error
126 */
127 public function post( $path, $auth = false, $data = array() ) {
128 try {
129 return $this->request( $path, $auth, $data );
130 } catch ( Exception $e ) {
131 return new WP_Error( $e->getCode(), $e->getMessage() );
132 }
133 }
134
135 /**
136 * Make a GET request.
137 *
138 * @since 1.0.0
139 *
140 * @param string $path Endpoint route.
141 * @param bool $auth Should attach API key?.
142 * @param array $data Data array.
143 *
144 * @return mixed|WP_Error
145 */
146 public function get( $path, $auth = false, $data = array() ) {
147 try {
148 return $this->request( $path, $auth, $data, 'get' );
149 } catch ( Exception $e ) {
150 return new WP_Error( $e->getCode(), $e->getMessage() );
151 }
152 }
153
154 /**
155 * Add API key for authorization to request.
156 *
157 * Optionally if hub site id is found, include that too.
158 *
159 * @since 1.0.0
160 *
161 * @param string $url URL of request.
162 * @param string $method Request method (post or get).
163 *
164 * @return string
165 */
166 private function set_api_key( $url, $method = 'post' ) {
167 $key = API::get()->get_api_key();
168 $site_id = Data::get()->hub_site_id();
169
170 if ( ! empty( $key ) ) {
171 if ( 'post' === $method ) {
172 $this->add_post_argument( 'api_key', $key );
173 } else {
174 // Set API key if not already set.
175 if ( false === strpos( $url, '/' . $key ) ) {
176 $url .= '/' . $key;
177 }
178 if ( ! empty( $site_id ) ) {
179 $this->add_get_argument( 'site_id', $site_id );
180 }
181 }
182 }
183
184 return $url;
185 }
186
187 /**
188 * Make an HTTP request using WP.
189 *
190 * @since 1.0.0
191 *
192 * @param string $path API endpoint route.
193 * @param bool $auth Should attach API key?.
194 * @param array $data Data array.
195 * @param string $method Request method.
196 *
197 * @return array|WP_Error
198 */
199 private function request( $path, $auth = false, $data = array(), $method = 'post' ) {
200 $url = API::get()->rest_url( $path );
201
202 if ( $auth ) {
203 $url = $this->set_api_key( $url, $method );
204 }
205
206 // Add URL params.
207 $url = add_query_arg( $this->get_args, $url );
208
209 // Default request options.
210 $args = array(
211 'user-agent' => 'WPMUDEV Hub Connector Client/' . \WPMUDEV_HUB_CONNECTOR_VERSION . ' (+' . network_site_url() . ')',
212 'headers' => $this->headers,
213 'sslverify' => defined( '\WPMUDEV_API_SSLVERIFY' ) ? \WPMUDEV_API_SSLVERIFY : false,
214 'method' => strtoupper( $method ),
215 'timeout' => $this->timeout,
216 );
217
218 switch ( strtolower( $method ) ) {
219 case 'post':
220 if ( is_array( $data ) ) {
221 $data = array_merge( $data, $this->post_args );
222 }
223
224 $args['body'] = $data;
225
226 $response = wp_remote_post( $url, $args );
227 break;
228 case 'get':
229 // If data is set for get request add it to URL.
230 $url = add_query_arg( $data, $url );
231
232 $response = wp_remote_get( $url, $args );
233 break;
234 default:
235 $response = wp_remote_request( $url, $args );
236 break;
237 }
238
239 return $response;
240 }
241 }
242