PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / 3.0.1
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization v3.0.1
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.0.1, at inc/api.php

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