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 / rest.php

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

835 lines 22.4 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 wp_send_json_error( __( 'Can not connect to Optimole service', 'optimole-wp' ) . $extra );
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 }
463
464 return $this->response( $user );
465 }
466
467 /**
468 * Return image samples.
469 *
470 * @param WP_REST_Request $request Rest request.
471 *
472 * @return WP_REST_Response Image urls.
473 */
474 public function get_sample_rate( WP_REST_Request $request ) {
475
476 add_filter( 'optml_dont_replace_url', '__return_true' );
477 $image_sample = get_transient( 'optimole_sample_image' );
478 if ( $image_sample === false || $request->get_param( 'force' ) === 'yes' ) {
479 $image_sample = $this->fetch_sample_image();
480 set_transient( 'optimole_sample_image', $image_sample );
481 }
482 $image = [ 'id' => $image_sample['id'] ];
483
484 $image['original'] = $image_sample['url'];
485
486 remove_filter( 'optml_dont_replace_url', '__return_true' );
487
488 $image['optimized'] = apply_filters(
489 'optml_replace_image',
490 $image['original'],
491 [
492 'width' => $image_sample['width'],
493 'height' => $image_sample['height'],
494 'quality' => $request->get_param( 'quality' ),
495 ]
496 );
497
498 $optimized = wp_remote_get(
499 $image['optimized'],
500 [
501 'timeout' => 10,
502 'headers' => [
503 'Accept' => 'text/html,application/xhtml+xml,image/webp,image/apng ',
504 ],
505 ]
506 );
507
508 $original = wp_remote_get( $image['original'] );
509
510 $image['optimized_size'] = (int) wp_remote_retrieve_header( $optimized, 'content-length' );
511 $image['original_size'] = (int) wp_remote_retrieve_header( $original, 'content-length' );
512
513 return $this->response( $image );
514 }
515
516 /**
517 * Return sample image data.
518 *
519 * @return array Image data.
520 */
521 private function fetch_sample_image() {
522 $accepted_mimes = [ 'image/jpeg' ];
523 $args = [
524 'post_type' => 'attachment',
525 'post_status' => 'any',
526 'number' => '5',
527 'no_found_rows' => true,
528 'fields' => 'ids',
529 'post_mime_type' => $accepted_mimes,
530 'post_parent__not_in' => [ 0 ],
531 ];
532 $image_result = new WP_Query( $args );
533 if ( empty( $image_result->posts ) ) {
534 $rand_id = rand( 1, 3 );
535 $original_image_url = OPTML_URL . 'assets/img/' . $rand_id . '.jpg';
536
537 return [
538 'url' => $original_image_url,
539 'width' => '700',
540 'height' => '465',
541 'id' => - 1,
542 ];
543 }
544 $attachment_id = $image_result->posts[ array_rand( $image_result->posts, 1 ) ];
545
546 $original_image_url = wp_get_attachment_image_url( $attachment_id, 'full' );
547
548 $metadata = wp_get_attachment_metadata( $attachment_id );
549
550 $width = 'auto';
551 $height = 'auto';
552 $size = 'full';
553 if ( isset( $metadata['sizes'] ) && isset( $metadata['sizes'][ $size ] ) ) {
554 $width = $metadata['sizes'][ $size ]['width'];
555 $height = $metadata['sizes'][ $size ]['height'];
556 }
557
558 return [
559 'url' => $original_image_url,
560 'id' => $attachment_id,
561 'width' => $width,
562 'height' => $height,
563 ];
564 }
565
566 /**
567 * Disconnect from optimole service.
568 *
569 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
570 * @param WP_REST_Request $request disconnect rest request.
571 */
572 public function disconnect( WP_REST_Request $request ) {
573 $settings = new Optml_Settings();
574 $settings->reset();
575 wp_send_json_success( 'Disconnected' );
576 }
577
578 /**
579 * Get optimized images from API.
580 *
581 * @param WP_REST_Request $request rest request.
582 *
583 * @return WP_REST_Response
584 */
585 public function poll_optimized_images( WP_REST_Request $request ) {
586 $api_key = $request->get_param( 'api_key' );
587 $request = new Optml_Api();
588 $images = $request->get_optimized_images( $api_key );
589 if ( ! isset( $images['list'] ) || empty( $images['list'] ) ) {
590 return $this->response( [] );
591 }
592
593 $final_images = array_splice( $images['list'], 0, 10 );
594 $media = new Optml_Media_Offload();
595
596 foreach ( $final_images as $index => $value ) {
597 $final_images[ $index ]['url'] = $media->get_media_optimized_url( $value['url'], $value['key'] );
598 unset( $final_images[ $index ]['key'] );
599 }
600
601 return $this->response( $final_images );
602 }
603
604 /**
605 * Get watermarks from API.
606 *
607 * @param WP_REST_Request $request rest request.
608 *
609 * @return WP_REST_Response
610 */
611 public function poll_watermarks( WP_REST_Request $request ) {
612 $api_key = $request->get_param( 'api_key' );
613 $request = new Optml_Api();
614 $watermarks = $request->get_watermarks( $api_key );
615 if ( ! isset( $watermarks['watermarks'] ) || empty( $watermarks['watermarks'] ) ) {
616 return $this->response( [] );
617 }
618 $final_images = array_splice( $watermarks['watermarks'], 0, 10 );
619
620 return $this->response( $final_images );
621 }
622
623 /**
624 * Add watermark.
625 *
626 * @param WP_REST_Request $request rest request.
627 *
628 * @return WP_REST_Response
629 */
630 public function add_watermark( WP_REST_Request $request ) {
631 $file = $request->get_file_params();
632 $request = new Optml_Api();
633 $response = $request->add_watermark( $file );
634 if ( $response === false ) {
635 return $this->response( __( 'Error uploading image. Please try again.', 'optimole-wp' ), 'error' );
636 }
637
638 return $this->response( __( 'Watermark image uploaded succesfully ! ', 'optimole-wp' ) );
639 }
640
641 /**
642 * Remove watermark.
643 *
644 * @param WP_REST_Request $request rest request.
645 *
646 * @return WP_REST_Response
647 */
648 public function remove_watermark( WP_REST_Request $request ) {
649 $post_id = $request->get_param( 'postID' );
650 $api_key = $request->get_param( 'api_key' );
651 $request = new Optml_Api();
652
653 return $this->response( $request->remove_watermark( $post_id, $api_key ) );
654 }
655
656 /**
657 * Get conflicts from API.
658 *
659 * @param WP_REST_Request $request rest request.
660 *
661 * @return WP_REST_Response
662 */
663 public function poll_conflicts( WP_REST_Request $request ) {
664 $conflicts_to_register = apply_filters( 'optml_register_conflicts', [] );
665 $manager = new Optml_Conflict_Manager( $conflicts_to_register );
666
667 return $this->response(
668 [
669 'count' => $manager->get_conflict_count(),
670 'conflicts' => $manager->get_conflict_list(),
671 ]
672 );
673 }
674
675 /**
676 * Dismiss conflict.
677 *
678 * @param WP_REST_Request $request rest request.
679 *
680 * @return WP_REST_Response
681 */
682 public function dismiss_conflict( WP_REST_Request $request ) {
683 $conflict_id = $request->get_param( 'conflictID' );
684 $conflicts_to_register = apply_filters( 'optml_register_conflicts', [] );
685 $manager = new Optml_Conflict_Manager( $conflicts_to_register );
686 $manager->dismiss_conflict( $conflict_id );
687
688 return $this->response(
689 [
690 'count' => $manager->get_conflict_count(),
691 'conflicts' => $manager->get_conflict_list(),
692 ]
693 );
694 }
695
696 /**
697 * Request stats update for app.
698 *
699 * @param WP_REST_Request $request rest request.
700 *
701 * @return WP_REST_Response
702 */
703 public function request_update( WP_REST_Request $request ) {
704 do_action( 'optml_daily_sync' );
705 $settings = new Optml_Settings();
706
707 return $this->response( $settings->get( 'service_data' ) );
708 }
709
710 /**
711 * Update options method.
712 *
713 * @param WP_REST_Request $request option update rest request.
714 *
715 * @return WP_REST_Response
716 */
717 public function update_option( WP_REST_Request $request ) {
718 $new_settings = $request->get_param( 'settings' );
719
720 if ( empty( $new_settings ) ) {
721 wp_send_json_error( 'No option key set.' );
722 }
723
724 $settings = new Optml_Settings();
725 $sanitized = $settings->parse_settings( $new_settings );
726
727 return $this->response( $sanitized );
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 check_redirects( WP_REST_Request $request ) {
738 if ( empty( $request->get_param( 'images' ) ) ) {
739 return $this->response( __( 'No images available on the current page.' ), 'noImagesFound' );
740 }
741 // 'ok' if no issues found, 'log' is there are issues we need to notify, 'deactivated' if the user's account is disabled
742 $status = 'ok';
743 $result = '';
744 foreach ( $request->get_param( 'images' ) as $domain => $value ) {
745 $args = [
746 'method' => 'GET',
747 'redirection' => 0,
748 ];
749 $processed_images = 0;
750 if ( isset( $value['src'] ) ) {
751 $processed_images = count( $value['src'] );
752 }
753 if ( isset( $value['ignoredUrls'] ) && $value['ignoredUrls'] > $processed_images ) {
754 $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>';
755 $status = 'log';
756 continue;
757 }
758
759 if ( $processed_images > 0 ) {
760 $response = wp_remote_get( $value['src'][ rand( 0, $processed_images - 1 ) ], $args );
761 if ( is_array( $response ) && ! is_wp_error( $response ) ) {
762 $headers = $response['headers']; // array of http header lines
763 $status_code = $response['response']['code'];
764 if ( $status_code === 301 ) {
765 $status = 'deactivated';
766 $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>';
767 break;
768 }
769 if ( $status_code === 302 ) {
770 if ( isset( $headers['x-redirect-o'] ) ) {
771 $optimole_code = (int) $headers['x-redirect-o'];
772 if ( $optimole_code === 1 ) {
773 $status = 'log';
774 $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>';
775 }
776 if ( $optimole_code === 4 ) {
777 $status = 'log';
778 $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>';
779 }
780 }
781 }
782 }
783 }
784 }
785 if ( $result === '' ) {
786 $result = __( 'No issues detected, everything is running smoothly.', 'optimole-wp' );
787 }
788
789 return $this->response( '<ul>' . $result . '</ul>', $status );
790 }
791
792 /**
793 * Push existing image our servers.
794 *
795 * @param WP_REST_Request $request rest request object.
796 *
797 * @return WP_REST_Response
798 */
799 public function offload_images( WP_REST_Request $request ) {
800 $batch = 300;
801 if ( ! empty( $request->get_param( 'batch' ) ) ) {
802 $batch = $request->get_param( 'batch' );
803 }
804 return $this->response( Optml_Media_Offload::upload_images( $batch ) );
805 }
806 /**
807 * Rollback images to media library.
808 *
809 * @param WP_REST_Request $request rest request object.
810 *
811 * @return WP_REST_Response
812 */
813 public function rollback_images( WP_REST_Request $request ) {
814 $batch = 300;
815 if ( ! empty( $request->get_param( 'batch' ) ) ) {
816 $batch = $request->get_param( 'batch' );
817 }
818 return $this->response( Optml_Media_Offload::rollback_images( $batch ) );
819 }
820 /**
821 * Get total number of images.
822 *
823 * @param WP_REST_Request $request rest request object.
824 *
825 * @return WP_REST_Response
826 */
827 public function number_of_library_images( WP_REST_Request $request ) {
828 $action = 'offload_images';
829 if ( ! empty( $request->get_param( 'action' ) ) ) {
830 $action = $request->get_param( 'action' );
831 }
832 return $this->response( Optml_Media_Offload::number_of_library_images( $action ) );
833 }
834 }
835