PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / trunk
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization vtrunk
4.2.14 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 All 135 releases
← All changes | inc/api.php +39 -135 3.12.0 → trunk View file →
@@ -15,14 +15,8 @@
15 15 * @var string Api root.
16 16 */
17 17 private $api_root = 'https://dashboard.optimole.com/api/';
18 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 19 * Optimole onboard api root url.
26 20 *
27 21 * @var string Api root.
28 22 */
@@ -44,18 +38,15 @@
44 38 */
45 39 public function __construct() {
46 40 $settings = new Optml_Settings();
47 41 $this->api_key = $settings->get( 'api_key' );
48 - if ( defined( 'OPTIML_API_ROOT' ) && constant( 'OPTIML_API_ROOT' ) ) {
42 + if ( defined( 'OPTIML_API_ROOT' ) ) {
49 43 $this->api_root = constant( 'OPTIML_API_ROOT' );
50 44 }
51 - if ( defined( 'OPTIML_UPLOAD_API_ROOT' ) && constant( 'OPTIML_UPLOAD_API_ROOT' ) ) {
52 - $this->upload_api_root = constant( 'OPTIML_UPLOAD_API_ROOT' );
53 - }
54 - if ( defined( 'OPTIML_ONBOARD_API_ROOT' ) && constant( 'OPTIML_ONBOARD_API_ROOT' ) ) {
45 + if ( defined( 'OPTIML_ONBOARD_API_ROOT' ) ) {
55 46 $this->onboard_api_root = constant( 'OPTIML_ONBOARD_API_ROOT' );
56 47 }
57 - if ( defined( 'OPTIML_UPLOAD_CONFLICTS_API_ROOT' ) && constant( 'OPTIML_UPLOAD_CONFLICTS_API_ROOT' ) ) {
48 + if ( defined( 'OPTIML_UPLOAD_CONFLICTS_API_ROOT' ) ) {
58 49 $this->upload_conflicts_api = constant( 'OPTIML_UPLOAD_CONFLICTS_API_ROOT' );
59 50 }
60 51 }
61 52
@@ -135,11 +126,15 @@
135 126 public function get_cache_token( $token = '', $type = '', $api_key = '' ) {
136 127 if ( ! empty( $api_key ) ) {
137 128 $this->api_key = $api_key;
138 129 }
139 - $lock = get_transient( 'optml_cache_lock' );
140 - if ( ! empty( $type ) && $type === 'assets' ) {
130 + $lock = '';
131 + if ( empty( $type ) || $type === 'images' ) {
132 + $lock = get_transient( 'optml_cache_lock' );
133 + } elseif ( $type === 'assets' ) {
141 134 $lock = get_transient( 'optml_cache_lock_assets' );
135 + } else {
136 + $type = 'images';
142 137 }
143 138
144 139 if ( $lock === 'yes' ) {
145 140 return new WP_Error( 'cache_throttle', __( 'You can clear cache only once per 5 minutes.', 'optimole-wp' ) );
@@ -155,19 +150,18 @@
155 150 * @param array|string $params The request method type.
156 151 *
157 152 * @return array|string|boolean|WP_Error Api data.
158 153 */
159 - private function request( $path, $method = 'GET', $params = [], $extra_headers = [] ) {
154 + public function request( $path, $method = 'GET', $params = [], $extra_headers = [] ) {
160 155
161 156 $headers = [
162 157 'Optml-Site' => get_home_url(),
163 158 ];
159 + update_option( 'optimole_wp_logger_flag', 'yes' );
164 160 if ( ! empty( $this->api_key ) ) {
165 161 $headers['Authorization'] = 'Bearer ' . $this->api_key;
166 162 }
167 - if ( is_array( $headers ) ) {
168 - $headers = array_merge( $headers, $extra_headers );
169 - }
163 + $headers = array_merge( $headers, $extra_headers );
170 164 $url = trailingslashit( $this->api_root ) . ltrim( $path, '/' );
171 165 // If there is a extra, add that as a url var.
172 166 if ( 'GET' === $method && ! empty( $params ) ) {
173 167 foreach ( $params as $key => $val ) {
@@ -173,18 +167,18 @@
173 167 foreach ( $params as $key => $val ) {
174 168 $url = add_query_arg( [ $key => $val ], $url );
175 169 }
176 170 }
171 + $url = tsdk_translate_link( $url, 'query' );
172 +
177 173 $args = $this->build_args( $method, $url, $headers, $params );
178 174
179 175 $response = wp_remote_request( $url, $args );
180 -
181 176 if ( is_wp_error( $response ) ) {
182 177 return $response;
183 178 }
184 179
185 180 $response = wp_remote_retrieve_body( $response );
186 -
187 181 if ( empty( $response ) ) {
188 182 return false;
189 183 }
190 184 $response = json_decode( $response, true );
@@ -197,9 +191,9 @@
197 191 }
198 192
199 193 if ( intval( $response['code'] ) !== 200 ) {
200 194 if ( isset( $response['error'] ) && $response['error'] === 'domain_not_accessible' ) {
201 - return new WP_Error( 'domain_not_accessible', sprintf( __( 'It seems Optimole is having trouble reaching your website. This issue often occurs if your website is private, local, or protected by a firewall. But don\'t stress – it\'s an easy fix! Ensure your website is live and accessible to the public. If a firewall is in place, just tweak the settings to allow the %1$sOptimole(1.0)%2$s user agent access to your website. %3$sLearn More%4$s', 'optimole-wp' ), '<i>', '</i>', '<a href="https://docs.optimole.com/article/1976-resolving-optimole-access-to-your-website" target="_blank">', '</a>' ) );
195 + return new WP_Error( 'domain_not_accessible', sprintf( /* translators: 1 start of the italic tag, 2 is the end of italic tag, 3 is starting anchor tag, 4 is the ending anchor tag. */ __( 'It seems Optimole is having trouble reaching your website. This issue often occurs if your website is private, local, or protected by a firewall. But don\'t stress – it\'s an easy fix! Ensure your website is live and accessible to the public. If a firewall is in place, just tweak the settings to allow the %1$sOptimole(1.0)%2$s user agent access to your website. %3$sLearn More%4$s', 'optimole-wp' ), '<i>', '</i>', '<a href="https://docs.optimole.com/article/1976-resolving-optimole-access-to-your-website" target="_blank">', '</a>' ) );
202 196 }
203 197 if ( $path === 'optml/v2/account/complete_register_remote' && isset( $response['error'] ) ) {
204 198 if ( strpos( $response['error'], 'This email address is already registered.' ) !== false ) {
205 199 return 'email_registered';
@@ -213,13 +207,29 @@
213 207 if ( $path === '/optml/v2/account/details'
214 208 && isset( $response['code'] ) && $response['code'] === 'not_allowed' ) {
215 209 return 'disconnect';
216 210 }
217 - return false;
211 +
212 + if ( $path === '/optml/v2/account/details'
213 + && isset( $response['error'] ) && $response['error'] === 'whitelist_limit_reached' ) {
214 + return 'disconnect';
215 + }
216 +
217 + return isset( $response['error'] ) ? new WP_Error(
218 + 'api_error',
219 + wp_kses(
220 + $response['error'],
221 + [
222 + 'a' => [
223 + 'href' => [],
224 + 'target' => [],
225 + ],
226 + ]
227 + )
228 + ) : false;
218 229 }
219 230
220 231 return $response['data'];
221 -
222 232 }
223 233
224 234 /**
225 235 * Builds Request arguments array.
@@ -246,21 +256,8 @@
246 256 return $args;
247 257 }
248 258
249 259 /**
250 - * Upload image to our servers using the generated signed url.
251 - *
252 - * @param string $upload_url The signed to url to upload the image to.
253 - * @param string $content_type Image mime type, it must match the actual mime type of the image.
254 - * @param string $image Image data from file_get_contents.
255 - * @return mixed
256 - */
257 - public function upload_image( $upload_url, $content_type, $image ) {
258 - $args = $this->build_args( 'PUT', '', ['content-type' => $content_type], $image );
259 - return wp_remote_request( $upload_url, $args );
260 - }
261 -
262 - /**
263 260 * Check if the optimized url is available.
264 261 *
265 262 * @param string $url The optimized url to check.
266 263 * @return bool Whether or not the url is valid.
@@ -265,9 +262,9 @@
265 262 * @param string $url The optimized url to check.
266 263 * @return bool Whether or not the url is valid.
267 264 */
268 265 public function check_optimized_url( $url ) {
269 - $response = wp_remote_get( $url, ['timeout' => 30] );
266 + $response = wp_remote_get( $url, [ 'timeout' => 30 ] );
270 267
271 268 if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 || ! empty( wp_remote_retrieve_header( $response, 'x-not-found-o' ) ) ) {
272 269 $this->log_offload_error( $response );
273 270 return false;
@@ -275,49 +272,8 @@
275 272 return true;
276 273 }
277 274
278 275 /**
279 - * Get options for the signed urls api call.
280 - *
281 - * @param string $original_url Image original url.
282 - * @param string $delete Whether to delete a bucket object or not(ie. generate signed upload url).
283 - * @param string $table_id Remote id used on our servers.
284 - * @param string $update_table False or success.
285 - * @param string $get_url Whether to return a get url or not.
286 - * @param string $width Original image width.
287 - * @param string $height Original image height.
288 - * @param int $file_size Original file size.
289 - * @return array|WP_Error
290 - */
291 - public function call_upload_api( $original_url = '', $delete = 'false', $table_id = '', $update_table = 'false', $get_url = 'false', $width = 'auto', $height = 'auto', $file_size = 0 ) {
292 - $body = [
293 - 'secret' => Optml_Config::$secret,
294 - 'userKey' => Optml_Config::$key,
295 - 'originalUrl' => $original_url,
296 - 'deleteUrl' => $delete,
297 - 'id' => $table_id,
298 - 'updateDynamo' => $update_table,
299 - 'getUrl' => $get_url,
300 - 'width' => $width,
301 - 'height' => $height,
302 - 'originalFileSize' => $file_size,
303 - ];
304 - $body = wp_json_encode( $body );
305 -
306 - $options = [
307 - 'body' => $body,
308 - 'headers' => [
309 - 'Content-Type' => 'application/json',
310 - ],
311 - 'timeout' => 60,
312 - 'blocking' => true,
313 - 'sslverify' => false,
314 - 'data_format' => 'body',
315 - ];
316 - return wp_remote_post( $this->upload_api_root, $options );
317 - }
318 -
319 - /**
320 276 * Send a list of images to upload.
321 277 *
322 278 * @param array $images List of Images.
323 279 * @return array
@@ -354,9 +310,9 @@
354 310 * @param array $images List of images.
355 311 * @return array
356 312 */
357 313 public function call_data_enrich_api( $images = [] ) {
358 - return $this->request( 'optml/v2/media/add_data', 'POST', ['images' => $images, 'key' => Optml_Config::$key] );
314 + return $this->request( 'optml/v2/media/add_data', 'POST', [ 'images' => $images, 'key' => Optml_Config::$key ] );
359 315 }
360 316 /**
361 317 * Register user remotely on optimole.com.
362 318 *
@@ -372,8 +328,9 @@
372 328 'email' => $email,
373 329 'version' => OPTML_VERSION,
374 330 'sample_image' => $this->get_sample_image(),
375 331 'site' => get_home_url(),
332 + 'reference' => get_option( 'optimole_reference_key', '' ),
376 333 ]
377 334 );
378 335 }
379 336
@@ -393,65 +350,12 @@
393 350 $service_data = $settings->get( 'service_data' );
394 351 if ( isset( $service_data['cdn_key'] ) ) {
395 352 $app_key = $service_data['cdn_key'];
396 353 }
397 - return $this->request( '/optml/v1/stats/images', 'GET', [], ['application' => $app_key] );
354 + return $this->request( '/optml/v1/stats/images', 'GET', [], [ 'application' => $app_key ] );
398 355 }
399 356
400 357 /**
401 - * Get the watermarks from API.
402 - *
403 - * @param string $api_key The API key.
404 - *
405 - * @return array|bool|WP_Error
406 - */
407 - public function get_watermarks( $api_key = '' ) {
408 - if ( ! empty( $api_key ) ) {
409 - $this->api_key = $api_key;
410 - }
411 -
412 - return $this->request( '/optml/v1/settings/watermark' );
413 - }
414 -
415 - /**
416 - * Remove the watermark from the API.
417 - *
418 - * @param integer $post_id The watermark post ID.
419 - * @param string $api_key The API key.
420 - *
421 - * @return array|bool|WP_Error
422 - */
423 - public function remove_watermark( $post_id, $api_key = '' ) {
424 - if ( ! empty( $api_key ) ) {
425 - $this->api_key = $api_key;
426 - }
427 -
428 - return $this->request( '/optml/v1/settings/watermark', 'DELETE', [ 'watermark' => $post_id ] );
429 - }
430 -
431 - /**
432 - * Add watermark.
433 - *
434 - * @param array $file The file to be uploaded.
435 - *
436 - * @return array|bool|mixed|object
437 - */
438 - public function add_watermark( $file ) {
439 -
440 - $headers = [
441 - 'Content-Disposition' => 'attachment; filename=' . $file['file']['name'],
442 - ];
443 -
444 - $response = $this->request( 'wp/v2/media', 'POST', file_get_contents( $file['file']['tmp_name'] ), $headers );
445 -
446 - if ( $response === false ) {
447 - return false;
448 - }
449 -
450 - return $response;
451 - }
452 -
453 - /**
454 358 * Call the images endpoint.
455 359 *
456 360 * @param integer $page Page used to advance the search.
457 361 * @param array $domains Domains to filter by.
@@ -459,9 +363,9 @@
459 363 * @return mixed The decoded json response from the api.
460 364 */
461 365 public function get_cloud_images( $page = 0, $domains = [], $search = '' ) {
462 366
463 - $params = ['key' => Optml_Config::$key ];
367 + $params = [ 'key' => Optml_Config::$key ];
464 368 $params['page'] = $page;
465 369 $params['size'] = 40;
466 370 if ( $search !== '' ) {
467 371 $params['search'] = $search;
@@ -497,9 +401,9 @@
497 401 }
498 402 wp_remote_post(
499 403 $this->upload_conflicts_api,
500 404 [
501 - 'headers' => [ 'Content-Type' => 'application/json' ],
405 + 'headers' => [ 'Content-Type' => 'application/json' ],
502 406 'timeout' => 15,
503 407 'blocking' => true,
504 408 'sslverify' => false,
505 409 'data_format' => 'body',