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

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