PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / 3.1.0
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization v3.1.0
4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 2.5.5 2.5.6 2.5.7 3.0.0 3.0.1 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.11.0 3.11.1 3.11.2 3.11.3 3.12.0 3.12.1 All 134 releases
optimole-wp / inc / api.php

api.php in Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization 3.1.0, at inc/api.php

373 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * The class defines way of connecting this user to the Optimole Dashboard.
5 *
6 * @codeCoverageIgnore
7 * @package \Optimole\Inc
8 * @author Optimole <friends@optimole.com>
9 */
10 final class Optml_Api {
11
12 /**
13 * Optimole root api url.
14 *
15 * @var string Api root.
16 */
17 private $api_root = 'https://dashboard.optimole.com/api/';
18 /**
19 * Optimole upload api root url.
20 *
21 * @var string Api root.
22 */
23 private $upload_api_root = 'https://generateurls-prod.i.optimole.com/upload';
24 /**
25 * Hold the user api key.
26 *
27 * @var string Api key.
28 */
29 private $api_key;
30 /**
31 * Optml_Api constructor.
32 */
33 public function __construct() {
34 $settings = new Optml_Settings();
35 $this->api_key = $settings->get( 'api_key' );
36 if ( defined( 'OPTIML_API_ROOT' ) && constant( 'OPTIML_API_ROOT' ) ) {
37 $this->api_root = constant( 'OPTIML_API_ROOT' );
38 }
39 if ( defined( 'OPTIML_UPLOAD_API_ROOT' ) && constant( 'OPTIML_UPLOAD_API_ROOT' ) ) {
40 $this->upload_api_root = constant( 'OPTIML_UPLOAD_API_ROOT' );
41 }
42 }
43
44 /**
45 * Connect to the service.
46 *
47 * @param string $api_key Api key.
48 *
49 * @return array|bool
50 */
51 public function connect( $api_key = '' ) {
52 if ( ! empty( $api_key ) ) {
53 $this->api_key = $api_key;
54 }
55
56 return $this->request( '/optml/v2/account/connect', 'POST' );
57 }
58
59 /**
60 * Get user data from service.
61 *
62 * @return array|bool User data.
63 */
64 public function get_user_data( $api_key = '', $application = '' ) {
65 if ( ! empty( $api_key ) ) {
66 $this->api_key = $api_key;
67 }
68
69 return $this->request( '/optml/v2/account/details', 'POST', [ 'application' => $application ] );
70 }
71
72 /**
73 * Get cache token from service.
74 *
75 * @return array|bool User data.
76 */
77 public function get_cache_token( $token = '', $api_key = '' ) {
78 if ( ! empty( $api_key ) ) {
79 $this->api_key = $api_key;
80 }
81 $lock = get_transient( 'optml_cache_lock' );
82 if ( $lock === 'yes' ) {
83 return new WP_Error( 'cache_throttle', __( 'You can clear cache only once per 5 minutes.', 'optimole-wp' ) );
84 }
85 return $this->request( '/optml/v1/cache/tokens', 'POST', [ 'token' => $token ] );
86 }
87
88 /**
89 * Request constructor.
90 *
91 * @param string $path The request url.
92 * @param string $method The request method type.
93 * @param array $params The request method type.
94 *
95 * @return array|boolean Api data.
96 */
97 private function request( $path, $method = 'GET', $params = [], $extra_headers = [] ) {
98
99 $headers = [
100 'Optml-Site' => get_home_url(),
101 ];
102 if ( ! empty( $this->api_key ) ) {
103 $headers['Authorization'] = 'Bearer ' . $this->api_key;
104 }
105 if ( ! empty( $headers ) && is_array( $headers ) ) {
106 $headers = array_merge( $headers, $extra_headers );
107 }
108 $url = trailingslashit( $this->api_root ) . ltrim( $path, '/' );
109 // If there is a extra, add that as a url var.
110 if ( 'GET' === $method && ! empty( $params ) ) {
111 foreach ( $params as $key => $val ) {
112 $url = add_query_arg( [ $key => $val ], $url );
113 }
114 }
115 $args = $this->build_args( $method, $url, $headers, $params );
116
117 $response = wp_remote_request( $url, $args );
118
119 if ( is_wp_error( $response ) ) {
120 return $response;
121 }
122
123 $response = wp_remote_retrieve_body( $response );
124
125 if ( empty( $response ) ) {
126 return false;
127 }
128 $response = json_decode( $response, true );
129
130 if ( isset( $response['id'] ) && is_numeric( $response['id'] ) ) {
131 return true;
132 }
133 if ( ! isset( $response['code'] ) ) {
134 return false;
135 }
136 if ( intval( $response['code'] ) !== 200 ) {
137 return false;
138 }
139
140 return $response['data'];
141
142 }
143
144 /**
145 * Builds Request arguments array.
146 *
147 * @param string $method Request method (GET | POST | PUT | UPDATE | DELETE).
148 * @param string $url Request URL.
149 * @param array $headers Headers Array.
150 * @param array|string $params Additional params for the Request.
151 *
152 * @return array
153 */
154 private function build_args( $method, $url, $headers, $params ) {
155 $args = [
156 'method' => $method,
157 'timeout' => 45,
158 'user-agent' => 'Optimle WP (v' . OPTML_VERSION . ') ',
159 'sslverify' => false,
160 'headers' => $headers,
161 ];
162 if ( $method !== 'GET' ) {
163 $args['body'] = $params;
164 }
165
166 return $args;
167 }
168
169 /**
170 * Upload image to our servers using the generated signed url.
171 *
172 * @param string $upload_url The signed to url to upload the image to.
173 * @param string $content_type Image mime type, it must match the actual mime type of the image.
174 * @param string $image Image data from file_get_contents.
175 * @return mixed
176 */
177 public function upload_image( $upload_url, $content_type, $image ) {
178 $args = $this->build_args( 'PUT', '', ['content-type' => $content_type], $image );
179 return wp_remote_request( $upload_url, $args );
180 }
181
182 /**
183 * Check if the optimized url is available.
184 *
185 * @param string $url The optimized url to check.
186 * @return bool Whether or not the url is valid.
187 */
188 public function check_optimized_url( $url ) {
189 $response = wp_remote_get( $url, ['timeout' => 30] );
190 if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) {
191 return false;
192 }
193 return true;
194 }
195
196 /**
197 * Get options for the signed urls api call.
198 *
199 * @param string $original_url Image original url.
200 * @param string $delete Whether to delete a bucket object or not(ie. generate signed upload url).
201 * @param string $table_id Remote id used on our servers.
202 * @param string $update_table False or success.
203 * @param string $get_url Whether to return a get url or not.
204 * @param string $width Original image width.
205 * @param string $height Original image height.
206 * @param int $file_size Original file size.
207 * @return array
208 */
209 public function call_upload_api( $original_url = '', $delete = 'false', $table_id = '', $update_table = 'false', $get_url = 'false', $width = 'auto', $height = 'auto', $file_size = 0 ) {
210 $body = [
211 'secret' => Optml_Config::$secret,
212 'userKey' => Optml_Config::$key,
213 'originalUrl' => $original_url,
214 'deleteUrl' => $delete,
215 'id' => $table_id,
216 'updateDynamo' => $update_table,
217 'getUrl' => $get_url,
218 'width' => $width,
219 'height' => $height,
220 'originalFileSize' => $file_size,
221 ];
222 $body = wp_json_encode( $body );
223
224 $options = [
225 'body' => $body,
226 'headers' => [
227 'Content-Type' => 'application/json',
228 ],
229 'timeout' => 60,
230 'blocking' => true,
231 'sslverify' => false,
232 'data_format' => 'body',
233 ];
234 return wp_remote_post( $this->upload_api_root, $options );
235 }
236 /**
237 * Register user remotely on optimole.com.
238 *
239 * @param string $email User email.
240 *
241 * @return array|bool Api response.
242 */
243 public function create_account( $email ) {
244 return $this->request(
245 'optml/v1/user/register-remote',
246 'POST',
247 [
248 'email' => $email,
249 'version' => OPTML_VERSION,
250 'site' => get_home_url(),
251 ]
252 );
253 }
254
255 /**
256 * Get the optimized images from API.
257 *
258 * @param string $api_key the api key.
259 *
260 * @return array|bool
261 */
262 public function get_optimized_images( $api_key = '' ) {
263 if ( ! empty( $api_key ) ) {
264 $this->api_key = $api_key;
265 }
266
267 return $this->request( '/optml/v1/stats/images' );
268 }
269
270 /**
271 * Get the watermarks from API.
272 *
273 * @param string $api_key The API key.
274 *
275 * @return array|bool
276 */
277 public function get_watermarks( $api_key = '' ) {
278 if ( ! empty( $api_key ) ) {
279 $this->api_key = $api_key;
280 }
281
282 return $this->request( '/optml/v1/settings/watermark' );
283 }
284
285 /**
286 * Remove the watermark from the API.
287 *
288 * @param integer $post_id The watermark post ID.
289 * @param string $api_key The API key.
290 *
291 * @return array|bool
292 */
293 public function remove_watermark( $post_id, $api_key = '' ) {
294 if ( ! empty( $api_key ) ) {
295 $this->api_key = $api_key;
296 }
297
298 return $this->request( '/optml/v1/settings/watermark', 'DELETE', [ 'watermark' => $post_id ] );
299 }
300
301 /**
302 * Add watermark.
303 *
304 * @param array $file The file to be uploaded.
305 *
306 * @return array|bool|mixed|object
307 */
308 public function add_watermark( $file ) {
309
310 $headers = [
311 'Content-Disposition' => 'attachment; filename=' . $file['file']['name'],
312 ];
313
314 $response = $this->request( 'wp/v2/media', 'POST', file_get_contents( $file['file']['tmp_name'] ), $headers );
315
316 if ( $response === false ) {
317 return false;
318 }
319
320 return $response;
321 }
322
323 /**
324 * Call the images endpoint.
325 *
326 * @param integer $page Page used to advance the search.
327 * @param array $domains Domains to filter by.
328 * @param string $search The string to search inside the originURL.
329 * @return mixed The decoded json response from the api.
330 */
331 public function get_cloud_images( $page = 0, $domains = [], $search = '' ) {
332
333 $params = ['key' => Optml_Config::$key ];
334 $params['page'] = $page;
335 $params['size'] = 40;
336 if ( $search !== '' ) {
337 $params['search'] = $search;
338 }
339
340 if ( ! empty( $domains ) ) {
341 $params['domains'] = implode( ',', $domains );
342 }
343 return $this->request( 'optml/v2/media/browser', 'GET', $params );
344 }
345
346 /**
347 * Throw error on object clone
348 *
349 * The whole idea of the singleton design pattern is that there is a single
350 * object therefore, we don't want the object to be cloned.
351 *
352 * @access public
353 * @return void
354 * @since 1.0.0
355 */
356 public function __clone() {
357 // Cloning instances of the class is forbidden.
358 _doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin&#8217; huh?', 'optimole-wp' ), '1.0.0' );
359 }
360
361 /**
362 * Disable unserializing of the class
363 *
364 * @access public
365 * @return void
366 * @since 1.0.0
367 */
368 public function __wakeup() {
369 // Unserializing instances of the class is forbidden.
370 _doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin&#8217; huh?', 'optimole-wp' ), '1.0.0' );
371 }
372 }
373