PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / 3.12.0
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization v3.12.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 / rest.php

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

883 lines 24.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Optimole Rest related actions.
4 *
5 * @package Optimole/Inc
6 * @copyright Copyright (c) 2017, Marius Cristea
7 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
8 */
9
10 /**
11 * Class Optml_Rest
12 *
13 * @codeCoverageIgnore
14 */
15 class Optml_Rest {
16
17 /**
18 * Rest api namespace.
19 *
20 * @var string Namespace.
21 */
22 private $namespace;
23
24 /**
25 * Upload conflicts api.
26 *
27 * @var array upload_conflicts_api.
28 */
29 public static $rest_routes = [
30 'service_routes' => ['update_option' => 'POST', 'request_update' => 'GET', 'check_redirects' => 'POST_PUT_PATCH',
31 'connect' => [ 'POST', 'args' => [
32 'api_key' => [
33 'type' => 'string',
34 'required' => true,
35 ],
36 ],
37 ],
38 'select_application' => [ 'POST', 'args' => [
39 'api_key' => [
40 'type' => 'string',
41 'required' => true,
42 ],
43 'application' => [
44 'type' => 'string',
45 'required' => true,
46 ],
47 ],
48 ],
49 'register_service' => [ 'POST', 'args' => [
50 'email' => [
51 'type' => 'string',
52 'required' => true,
53 ],
54 ],
55
56 ],
57 'disconnect' => 'GET',
58 ],
59 'image_routes' => [
60 'poll_optimized_images' => 'GET',
61 'get_sample_rate' => 'POST',
62 'upload_onboard_images' => [ 'POST', 'args' => [
63 'offset' => [
64 'type' => 'number',
65 'required' => false,
66 'default' => 0,
67 ],
68 ],
69 ],
70 ],
71 'media_cloud_routes' => [
72 'number_of_images_and_pages' => 'POST',
73 'get_offload_conflicts' => 'GET',
74 ],
75 'watermark_routes' => [
76 'poll_watermarks' => 'GET',
77 'add_watermark' => 'POST',
78 'remove_watermark' => 'POST',
79 ],
80 'conflict_routes' => [
81 'poll_conflicts' => 'GET',
82 'dismiss_conflict' => 'POST',
83 ],
84 'cache_routes' => [
85 'clear_cache_request' => 'POST',
86 ],
87 'dam_routes' => [
88 'insert_images' => [
89 'POST',
90 'args' => [
91 'images' => [
92 'type' => 'array',
93 'required' => true,
94 ],
95 ],
96 'permission_callback' => 'upload_files',
97 ],
98 ],
99 ];
100
101 /**
102 * Optml_Rest constructor.
103 */
104 public function __construct() {
105 $this->namespace = OPTML_NAMESPACE . '/v1';
106
107 add_action( 'rest_api_init', [ $this, 'register' ] );
108 }
109
110 /**
111 * Method to register a specific rest route.
112 *
113 * @param string $route The route name.
114 * @param string $method The route access method GET, POST, POST_PUT_PATCH.
115 * @param array $args Optional argument to include required args.
116 * @param string $permission_callback Optional permission callback.
117 */
118 private function reqister_route( $route, $method = 'GET', $args = [], $permission_callback = 'manage_options' ) {
119 $wp_method_constant = false;
120 if ( $method === 'GET' ) {
121 $wp_method_constant = \WP_REST_Server::READABLE;
122 }
123 if ( $method === 'POST' ) {
124 $wp_method_constant = \WP_REST_Server::CREATABLE;
125 }
126 if ( $method === 'POST_PUT_PATCH' ) {
127 $wp_method_constant = \WP_REST_Server::EDITABLE;
128 }
129 if ( $wp_method_constant !== false ) {
130 $params = [
131 'methods' => $wp_method_constant,
132 'permission_callback' => function () use ( $permission_callback ) {
133 return current_user_can( $permission_callback );
134 },
135 'callback' => [ $this, $route ],
136 ];
137 if ( ! empty( $args ) ) {
138 $params['args'] = $args;
139 }
140 register_rest_route(
141 $this->namespace,
142 '/' . $route,
143 [
144 $params,
145 ]
146 );
147 }
148
149 }
150
151 /**
152 * Register rest routes.
153 */
154 public function register() {
155
156 $this->register_service_routes();
157
158 $this->register_image_routes();
159 $this->register_watermark_routes();
160 $this->register_conflict_routes();
161 $this->register_cache_routes();
162 $this->register_media_offload_routes();
163 $this->register_dam_routes();
164 }
165
166 /**
167 * Method to register service specific routes.
168 */
169 public function register_service_routes() {
170 foreach ( self::$rest_routes['service_routes'] as $route => $details ) {
171 if ( is_array( $details ) ) {
172 $this->reqister_route( $route, $details[0], $details['args'] );
173 } else {
174 $this->reqister_route( $route, $details );
175 }
176 }
177 }
178
179 /**
180 * Method to register image specific routes.
181 */
182 public function register_image_routes() {
183 foreach ( self::$rest_routes['image_routes'] as $route => $details ) {
184 if ( is_array( $details ) ) {
185 $this->reqister_route( $route, $details[0], $details['args'] );
186 } else {
187 $this->reqister_route( $route, $details );
188 }
189 }
190
191 }
192 /**
193 * Method to register media offload specific routes.
194 */
195 public function register_media_offload_routes() {
196 foreach ( self::$rest_routes['media_cloud_routes'] as $route => $details ) {
197 $this->reqister_route( $route, $details );
198 }
199 }
200
201 /**
202 * Method to register watermark specific routes.
203 */
204 public function register_watermark_routes() {
205 foreach ( self::$rest_routes['watermark_routes'] as $route => $details ) {
206 $this->reqister_route( $route, $details );
207 }
208 }
209
210 /**
211 * Method to register conflicts specific routes.
212 */
213 public function register_conflict_routes() {
214 foreach ( self::$rest_routes['conflict_routes'] as $route => $details ) {
215 $this->reqister_route( $route, $details );
216 }
217 }
218
219 /**
220 * Method to register cache specific routes.
221 */
222 public function register_cache_routes() {
223 foreach ( self::$rest_routes['cache_routes'] as $route => $details ) {
224 $this->reqister_route( $route, $details );
225 }
226 }
227
228 /**
229 * Register DAM routes.
230 *
231 * @return void
232 */
233 public function register_dam_routes() {
234 foreach ( self::$rest_routes['dam_routes'] as $route => $details ) {
235 $permission = isset( $details['permission_callback'] ) ? $details['permission_callback'] : 'manage_options';
236 $args = isset( $details['args'] ) ? $details['args'] : [];
237 $this->reqister_route( $route, $details[0], $args, $permission );
238 }
239 }
240
241 /**
242 * Clear Cache request.
243 *
244 * @param WP_REST_Request $request clear cache rest request.
245 *
246 * @return WP_Error|WP_REST_Response
247 */
248 public function clear_cache_request( WP_REST_Request $request ) {
249 $settings = new Optml_Settings();
250 $type = $request->get_param( 'type' );
251 $response = $settings->clear_cache( $type );
252
253 if ( is_wp_error( $response ) ) {
254 wp_send_json_error( $response->get_error_message() );
255 }
256
257 return $this->response( $response, '200' );
258 }
259
260 /**
261 * Connect to optimole service.
262 *
263 * @param WP_REST_Request $request connect rest request.
264 *
265 * @return WP_Error|WP_REST_Response
266 */
267 public function connect( WP_REST_Request $request ) {
268 $api_key = $request->get_param( 'api_key' );
269 $original_request = $request;
270 $request = new Optml_Api();
271 $data = $request->connect( $api_key );
272
273 if ( $data === false || is_wp_error( $data ) ) {
274 if ( $data->get_error_code() === 'domain_not_accessible' ) {
275 return $this->response( $data->get_error_message(), 400 );
276 }
277 $extra = '';
278 if ( is_wp_error( $data ) ) {
279 /**
280 * Error from api.
281 *
282 * @var WP_Error $data Error object.
283 */
284 $extra = sprintf( __( '. ERROR details: %s', 'optimole-wp' ), $data->get_error_message() );
285 }
286 return $this->response( __( 'Can not connect to Optimole service', 'optimole-wp' ) . $extra, 400 );
287 }
288 $settings = new Optml_Settings();
289 $settings->update( 'api_key', $api_key );
290
291 if ( isset( $data['extra_visits'] ) ) {
292 $settings->update_frontend_banner_from_remote( $data['extra_visits'] );
293 }
294
295 if ( $data['app_count'] === 1 ) {
296 return $this->select_application( $original_request );
297 }
298 return $this->response( $data );
299 }
300
301 /**
302 * Select application.
303 *
304 * @param WP_REST_Request $request Rest request.
305 *
306 * @return WP_REST_Response
307 */
308 public function select_application( WP_REST_Request $request ) {
309 $api_key = $request->get_param( 'api_key' );
310 $application = $request->get_param( 'application' );
311 $request = new Optml_Api();
312 $data = $request->get_user_data( $api_key, $application );
313 if ( $data === false || is_wp_error( $data ) ) {
314 $extra = '';
315 if ( is_wp_error( $data ) ) {
316 /**
317 * Error from api.
318 *
319 * @var WP_Error $data Error object.
320 */
321 $extra = sprintf( __( '. ERROR details: %s', 'optimole-wp' ), $data->get_error_message() );
322 }
323 wp_send_json_error( __( 'Can not connect to Optimole service', 'optimole-wp' ) . $extra );
324 }
325 $settings = new Optml_Settings();
326 $settings->update( 'service_data', $data );
327 return $this->response( $data );
328 }
329
330 /**
331 * Wrapper for api response.
332 *
333 * @param mixed $data data from api.
334 *
335 * @return WP_REST_Response
336 */
337 private function response( $data, $code = 'success' ) {
338 return new WP_REST_Response( [ 'data' => $data, 'code' => $code ], 200 );
339 }
340
341 /**
342 * Connect to optimole service.
343 *
344 * @param WP_REST_Request $request connect rest request.
345 *
346 * @return WP_Error|WP_REST_Response
347 */
348 public function register_service( WP_REST_Request $request ) {
349 $email = $request->get_param( 'email' );
350 $api = new Optml_Api();
351
352 $user = $api->create_account( $email );
353
354 $auto_connect = $request->get_param( 'auto_connect' );
355
356 if ( ! empty( $auto_connect ) && $auto_connect === 'true' ) {
357 delete_option( Optml_Settings::OPTML_USER_EMAIL );
358 }
359 if ( $user === false || is_wp_error( $user ) ) {
360 if ( $user->get_error_code() === 'domain_not_accessible' ) {
361 return new WP_REST_Response(
362 [
363 'data' => null,
364 'message' => $user->get_error_message(),
365 'code' => 'domain_not_accessible',
366 ],
367 200
368 );
369 }
370 return new WP_REST_Response(
371 [
372 'data' => null,
373 'message' => __( 'Error creating account.', 'optimole-wp' ),
374 'code' => 'error',
375 ],
376 200
377 );
378 }
379 if ( $user === 'email_registered' ) {
380 return new WP_REST_Response(
381 [
382 'data' => null,
383 'message' => __( 'Error: This email is already registered. Please choose another one.', 'optimole-wp' ),
384 'code' => 'email_registered',
385 ],
386 200
387 );
388
389 }
390
391 if ( $user === 'site_exists' ) {
392 return new WP_REST_Response(
393 [
394 'data' => null,
395 'message' => sprintf( __( 'Error: This site has been previously registered. You can login to your account from %1$shere%2$s ', 'optimole-wp' ), '<a href="https://dashboard.optimole.com/login" target="_blank"> ', '</a>' ),
396 'code' => 'site_exists',
397 ],
398 200
399 );
400 }
401
402 $user_data = $user['res'];
403
404 $settings = new Optml_Settings();
405 $settings->update( 'api_key', $user_data['api_key'] );
406
407 if ( $user_data['app_count'] === 1 ) {
408 $settings->update( 'service_data', $user_data );
409 }
410
411 if ( isset( $user_data['extra_visits'] ) ) {
412 $settings->update_frontend_banner_from_remote( $user_data['extra_visits'] );
413 }
414
415 return $this->response( $user_data );
416 }
417
418 /**
419 * Return image samples.
420 *
421 * @param WP_REST_Request $request Rest request.
422 *
423 * @return WP_REST_Response Image urls.
424 */
425 public function get_sample_rate( WP_REST_Request $request ) {
426
427 add_filter( 'optml_dont_replace_url', '__return_true' );
428 $image_sample = get_transient( 'optimole_sample_image' );
429 if ( $image_sample === false || $request->get_param( 'force' ) === 'yes' ) {
430 $image_sample = $this->fetch_sample_image();
431 set_transient( 'optimole_sample_image', $image_sample );
432 }
433 $image = [ 'id' => $image_sample['id'] ];
434
435 $image['original'] = $image_sample['url'];
436
437 remove_filter( 'optml_dont_replace_url', '__return_true' );
438
439 $image['optimized'] = apply_filters(
440 'optml_replace_image',
441 $image['original'],
442 [
443 'width' => $image_sample['width'],
444 'height' => $image_sample['height'],
445 'quality' => $request->get_param( 'quality' ),
446 ]
447 );
448
449 $optimized = wp_remote_get(
450 $image['optimized'],
451 [
452 'timeout' => 10,
453 'headers' => [
454 'Accept' => 'text/html,application/xhtml+xml,image/webp,image/apng ',
455 ],
456 ]
457 );
458
459 $original = wp_remote_get( $image['original'] );
460
461 $image['optimized_size'] = (int) wp_remote_retrieve_header( $optimized, 'content-length' );
462 $image['original_size'] = (int) wp_remote_retrieve_header( $original, 'content-length' );
463
464 return $this->response( $image );
465 }
466
467 /**
468 * Crawl & upload initial load.
469 *
470 * @return WP_REST_Response If there are more posts left to receive.
471 */
472 public function upload_onboard_images( WP_REST_Request $request ) {
473 $offset = absint( $request->get_param( 'offset' ) );
474
475 // Arguments for get_posts function
476 $args = [
477 'post_type' => 'attachment',
478 'post_mime_type' => 'image',
479 'post_status' => 'inherit',
480 'posts_per_page' => 100,
481 'offset' => $offset,
482 'fields' => 'ids',
483 'no_found_rows' => true,
484 'update_post_meta_cache' => false,
485 'update_post_term_cache' => false,
486 'orderby' => [
487 'parent' => 'DESC',
488 ],
489 ];
490
491 // Initialize an array to store the image URLs
492 $image_urls = [];
493
494 add_filter( 'optml_dont_replace_url', '__return_true' );
495
496 // If site has a logo, get the logo url if offset is 0, ie first request
497 if ( $offset === 0 ) {
498 $custom_logo_id = get_theme_mod( 'custom_logo' );
499
500 if ( $custom_logo_id ) {
501 $image_urls[] = wp_get_attachment_url( $custom_logo_id );
502 }
503 }
504
505 $query = new \WP_Query( $args );
506
507 if ( $query->have_posts() ) {
508 $image_ids = $query->get_posts();
509 $image_urls = array_map( 'wp_get_attachment_url', $image_ids );
510 }
511 remove_filter( 'optml_dont_replace_url', '__return_true' );
512
513 if ( count( $image_urls ) === 0 ) {
514 return $this->response( true );
515 }
516
517 $api = new Optml_Api();
518 $api->call_onboard_api( $image_urls );
519
520 // Check if image_url array has at least 100 items, if not, we have reached the end of the images
521 return $this->response( count( $image_urls ) < 100 ? true : false );
522 }
523
524 /**
525 * Return sample image data.
526 *
527 * @return array Image data.
528 */
529 private function fetch_sample_image() {
530 $accepted_mimes = [ 'image/jpeg' ];
531 $args = [
532 'post_type' => 'attachment',
533 'post_status' => 'any',
534 'number' => '5',
535 'no_found_rows' => true,
536 'fields' => 'ids',
537 'post_mime_type' => $accepted_mimes,
538 'post_parent__not_in' => [ 0 ],
539 ];
540 $image_result = new WP_Query( $args );
541 if ( empty( $image_result->posts ) ) {
542 $rand_id = rand( 1, 3 );
543 $original_image_url = OPTML_URL . 'assets/img/' . $rand_id . '.jpg';
544
545 return [
546 'url' => $original_image_url,
547 'width' => '700',
548 'height' => '465',
549 'id' => - 1,
550 ];
551 }
552 $attachment_id = $image_result->posts[ array_rand( $image_result->posts, 1 ) ];
553
554 $original_image_url = wp_get_attachment_image_url( $attachment_id, 'full' );
555
556 $metadata = wp_get_attachment_metadata( $attachment_id );
557
558 $width = 'auto';
559 $height = 'auto';
560 $size = 'full';
561 if ( isset( $metadata['sizes'] ) && isset( $metadata['sizes'][ $size ] ) ) {
562 $width = $metadata['sizes'][ $size ]['width'];
563 $height = $metadata['sizes'][ $size ]['height'];
564 }
565
566 return [
567 'url' => $original_image_url,
568 'id' => $attachment_id,
569 'width' => $width,
570 'height' => $height,
571 ];
572 }
573
574 /**
575 * Disconnect from optimole service.
576 *
577 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
578 * @param WP_REST_Request $request disconnect rest request.
579 */
580 public function disconnect( WP_REST_Request $request ) {
581 $settings = new Optml_Settings();
582 $settings->reset();
583 wp_send_json_success( 'Disconnected' );
584 }
585
586 /**
587 * Get optimized images from API.
588 *
589 * @param WP_REST_Request $request rest request.
590 *
591 * @return WP_REST_Response
592 */
593 public function poll_optimized_images( WP_REST_Request $request ) {
594 $api_key = $request->get_param( 'api_key' );
595 $request = new Optml_Api();
596 $images = $request->get_optimized_images( $api_key );
597 if ( ! isset( $images['list'] ) || empty( $images['list'] ) ) {
598 return $this->response( [] );
599 }
600
601 $final_images = array_splice( $images['list'], 0, 10 );
602
603 foreach ( $final_images as $index => $value ) {
604 $final_images[ $index ]['url'] = Optml_Media_Offload::instance()->get_media_optimized_url(
605 $value['url'],
606 $value['key'],
607 140,
608 140,
609 [
610 'type' => Optml_Resize::RESIZE_FILL,
611 'enlarge' => false,
612 'gravity' => Optml_Resize::GRAVITY_CENTER,
613 ]
614 );
615 unset( $final_images[ $index ]['key'] );
616 }
617
618 return $this->response( $final_images );
619 }
620
621 /**
622 * Get watermarks from API.
623 *
624 * @param WP_REST_Request $request rest request.
625 *
626 * @return WP_REST_Response
627 */
628 public function poll_watermarks( WP_REST_Request $request ) {
629 $api_key = $request->get_param( 'api_key' );
630 $request = new Optml_Api();
631 $watermarks = $request->get_watermarks( $api_key );
632 if ( ! isset( $watermarks['watermarks'] ) || empty( $watermarks['watermarks'] ) ) {
633 return $this->response( [] );
634 }
635 $final_images = array_splice( $watermarks['watermarks'], 0, 10 );
636
637 return $this->response( $final_images );
638 }
639
640 /**
641 * Add watermark.
642 *
643 * @param WP_REST_Request $request rest request.
644 *
645 * @return WP_REST_Response
646 */
647 public function add_watermark( WP_REST_Request $request ) {
648 $file = $request->get_file_params();
649 $request = new Optml_Api();
650 $response = $request->add_watermark( $file );
651 if ( $response === false ) {
652 return $this->response( __( 'Error uploading image. Please try again.', 'optimole-wp' ), 'error' );
653 }
654
655 return $this->response( __( 'Watermark image uploaded succesfully ! ', 'optimole-wp' ) );
656 }
657
658 /**
659 * Remove watermark.
660 *
661 * @param WP_REST_Request $request rest request.
662 *
663 * @return WP_REST_Response
664 */
665 public function remove_watermark( WP_REST_Request $request ) {
666 $post_id = $request->get_param( 'postID' );
667 $api_key = $request->get_param( 'api_key' );
668 $request = new Optml_Api();
669
670 return $this->response( $request->remove_watermark( $post_id, $api_key ) );
671 }
672
673 /**
674 * Get conflicts from API.
675 *
676 * @param WP_REST_Request $request rest request.
677 *
678 * @return WP_REST_Response
679 */
680 public function poll_conflicts( WP_REST_Request $request ) {
681 $conflicts_to_register = apply_filters( 'optml_register_conflicts', [] );
682 $manager = new Optml_Conflict_Manager( $conflicts_to_register );
683
684 return $this->response(
685 [
686 'count' => $manager->get_conflict_count(),
687 'conflicts' => $manager->get_conflict_list(),
688 ]
689 );
690 }
691
692 /**
693 * Dismiss conflict.
694 *
695 * @param WP_REST_Request $request rest request.
696 *
697 * @return WP_REST_Response
698 */
699 public function dismiss_conflict( WP_REST_Request $request ) {
700 $conflict_id = $request->get_param( 'conflictID' );
701 $conflicts_to_register = apply_filters( 'optml_register_conflicts', [] );
702 $manager = new Optml_Conflict_Manager( $conflicts_to_register );
703 $manager->dismiss_conflict( $conflict_id );
704
705 return $this->response(
706 [
707 'count' => $manager->get_conflict_count(),
708 'conflicts' => $manager->get_conflict_list(),
709 ]
710 );
711 }
712
713 /**
714 * Request stats update for app.
715 *
716 * @param WP_REST_Request $request rest request.
717 *
718 * @return WP_REST_Response
719 */
720 public function request_update( WP_REST_Request $request ) {
721 do_action( 'optml_daily_sync' );
722 $settings = new Optml_Settings();
723 $service_data = $settings->get( 'service_data' );
724 if ( empty( $service_data ) ) {
725 return $this->response( '', 'disconnected' );
726 }
727 return $this->response( $service_data );
728 }
729
730 /**
731 * Update options method.
732 *
733 * @param WP_REST_Request $request option update rest request.
734 *
735 * @return WP_REST_Response
736 */
737 public function update_option( WP_REST_Request $request ) {
738 $new_settings = $request->get_param( 'settings' );
739
740 if ( empty( $new_settings ) ) {
741 wp_send_json_error( 'No option key set.' );
742 }
743
744 $settings = new Optml_Settings();
745 $sanitized = $settings->parse_settings( $new_settings );
746
747 return $this->response( $sanitized );
748 }
749
750 /**
751 * Update options method.
752 *
753 * @param WP_REST_Request $request option update rest request.
754 *
755 * @return WP_REST_Response
756 */
757 public function check_redirects( WP_REST_Request $request ) {
758 if ( empty( $request->get_param( 'images' ) ) ) {
759 return $this->response( __( 'No images available on the current page.' ), 'noImagesFound' );
760 }
761 // 'ok' if no issues found, 'log' is there are issues we need to notify, 'deactivated' if the user's account is disabled
762 $status = 'ok';
763 $result = '';
764 foreach ( $request->get_param( 'images' ) as $domain => $value ) {
765 $args = [
766 'method' => 'GET',
767 'redirection' => 0,
768 ];
769 $processed_images = 0;
770 if ( isset( $value['src'] ) ) {
771 $processed_images = count( $value['src'] );
772 }
773 if ( isset( $value['ignoredUrls'] ) && $value['ignoredUrls'] > $processed_images ) {
774 $result .= '<li>❌ ' . sprintf( __( 'The images from: %1$s are not optimized by Optimole. If you would like to do so, you can follow this: %2$sWhy Optimole does not optimize all the images from my site?%3$s.', 'optimole-wp' ), $domain, '<a target="_blank" href="https://docs.optimole.com/article/1290-how-to-optimize-images-using-optimole-from-my-domain">', '</a>' ) . '</li>';
775 $status = 'log';
776 continue;
777 }
778
779 if ( $processed_images > 0 ) {
780 $response = wp_remote_get( $value['src'][ rand( 0, $processed_images - 1 ) ], $args );
781 if ( ! is_wp_error( $response ) && is_array( $response ) ) {
782 $headers = $response['headers']; // array of http header lines
783 $status_code = $response['response']['code'];
784 if ( $status_code === 301 ) {
785 $status = 'deactivated';
786 $result = '<li>❌ ' . sprintf( __( 'Your account is currently disabled due to exceeding quota and Optimole is no longer able to optimize the images. In order to fix this you will need to %1$supgrade%2$s.', 'optimole-wp' ), '<a target="_blank" href="https://optimole.com/pricing">', '</a>' ) . '</li>';
787 break;
788 }
789 if ( $status_code === 302 ) {
790 if ( isset( $headers['x-redirect-o'] ) ) {
791 $optimole_code = (int) $headers['x-redirect-o'];
792 if ( $optimole_code === 1 ) {
793 $status = 'log';
794 $result .= '<li>❌ ' . sprintf( __( 'The domain: %1$s is not allowed to optimize images using your Optimole account. You can add this to the allowed list %2$shere%3$s.', 'optimole-wp' ), '<b>' . $domain . '</b>', '<a target="_blank" href="https://dashboard.optimole.com/whitelist">', '</a>' ) . '</li>';
795 }
796 if ( $optimole_code === 4 ) {
797 $status = 'log';
798 $result .= '<li>❌ ' . sprintf( __( 'We are not able to download the images from %1$s. Please check %2$sthis%3$s document for a more advanced guide on how to solve this. ', 'optimole-wp' ), '<b>' . $domain . '</b>', '<a target="_blank" href="https://docs.optimole.com/article/1291-why-optimole-is-not-able-to-download-the-images-from-my-site">', '</a>' ) . '<br />' . '</li>';
799 }
800 }
801 }
802 }
803 }
804 }
805 if ( $result === '' ) {
806 $result = __( 'No issues detected, everything is running smoothly.', 'optimole-wp' );
807 }
808
809 return $this->response( '<ul>' . $result . '</ul>', $status );
810 }
811
812 /**
813 * Get total number of images.
814 *
815 * @param WP_REST_Request $request rest request object.
816 *
817 * @return WP_REST_Response
818 */
819 public function number_of_images_and_pages( WP_REST_Request $request ) {
820 $action = 'offload_images';
821 $refresh = false;
822 $images = [];
823
824 if ( ! empty( $request->get_param( 'action' ) ) ) {
825 $action = $request->get_param( 'action' );
826 }
827
828 if ( ! empty( $request->get_param( 'refresh' ) ) ) {
829 $refresh = $request->get_param( 'refresh' );
830 }
831
832 if ( ! empty( $request->get_param( 'images' ) ) ) {
833 $images = $request->get_param( 'images' );
834 }
835
836 return $this->response( Optml_Media_Offload::get_image_count( $action, $refresh, $images ) );
837 }
838
839 /**
840 * Get conflicts list.
841 *
842 * @param WP_REST_Request $request rest request object.
843 *
844 * @return WP_REST_Response
845 */
846 public function get_offload_conflicts( WP_REST_Request $request ) {
847 $request = new Optml_Api();
848 $decoded_list = $request->get_offload_conflicts();
849 $active_conflicts = [];
850 if ( isset( $decoded_list['plugins'] ) ) {
851 foreach ( $decoded_list['plugins'] as $slug ) {
852 if ( is_plugin_active( $slug ) ) {
853 $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $slug );
854 if ( ! empty( $plugin_data['Name'] ) ) {
855 $active_conflicts[] = $plugin_data['Name'];
856 }
857 }
858 }
859 }
860
861 return $this->response( $active_conflicts );
862 }
863
864 /**
865 * Insert images request.
866 *
867 * @param WP_REST_Request $request insert images rest request.
868 *
869 * @return WP_REST_Response
870 */
871 public function insert_images( WP_REST_Request $request ) {
872 $images = $request->get_param( 'images' );
873
874 if ( ! is_array( $images ) || empty( $images ) ) {
875 return $this->response( [ 'error' => 'No images found' ] );
876 }
877
878 $insert = Optml_Main::instance()->dam->insert_attachments( $images );
879
880 return $this->response( $insert );
881 }
882 }
883