PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / 1.0.4
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization v1.0.4
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
← All changes | inc/rest.php +97 -651 3.1.01.0.4 View file →
@@ -8,10 +8,8 @@
8 8 */
9 9
10 10 /**
11 11 * Class Optml_Rest
12 - *
13 - * @codeCoverageIgnore
14 12 */
15 13 class Optml_Rest {
16 14 /**
17 15 * Rest api namespace.
@@ -24,9 +22,9 @@
24 22 * Optml_Rest constructor.
25 23 */
26 24 public function __construct() {
27 25 $this->namespace = OPTML_NAMESPACE . '/v1';
28 - add_action( 'rest_api_init', [ $this, 'register' ] );
26 + add_action( 'rest_api_init', array( $this, 'register' ) );
29 27 }
30 28
31 29 /**
32 30 * Register rest routes.
@@ -32,342 +30,89 @@
32 30 * Register rest routes.
33 31 */
34 32 public function register() {
35 33
36 - $this->register_service_routes();
37 -
38 34 register_rest_route(
39 - $this->namespace,
40 - '/update_option',
41 - [
42 - [
35 + $this->namespace, '/connect', array(
36 + array(
43 37 'methods' => \WP_REST_Server::CREATABLE,
44 38 'permission_callback' => function () {
45 39 return current_user_can( 'manage_options' );
46 40 },
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' => [
41 + 'callback' => array( $this, 'connect' ),
42 + 'args' => array(
43 + 'api_key' => array(
102 44 'type' => 'string',
103 45 'required' => true,
104 - ],
105 - ],
106 - ],
107 - ]
46 + ),
47 + ),
48 + ),
49 + )
108 50 );
109 51 register_rest_route(
110 - $this->namespace,
111 - '/select',
112 - [
113 - [
52 + $this->namespace, '/register', array(
53 + array(
114 54 'methods' => \WP_REST_Server::CREATABLE,
115 55 'permission_callback' => function () {
116 56 return current_user_can( 'manage_options' );
117 57 },
118 - 'callback' => [ $this, 'select_application' ],
119 - 'args' => [
120 - 'api_key' => [
58 + 'callback' => array( $this, 'register_service' ),
59 + 'args' => array(
60 + 'email' => array(
121 61 'type' => 'string',
122 62 'required' => true,
123 - ],
124 - 'application' => [
125 - 'type' => 'string',
126 - 'required' => true,
127 - ],
128 - ],
129 - ],
130 - ]
63 + ),
64 + ),
65 + ),
66 + )
131 67 );
132 68 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 - [
69 + $this->namespace, '/disconnect', array(
70 + array(
156 71 'methods' => \WP_REST_Server::READABLE,
157 72 'permission_callback' => function () {
158 73 return current_user_can( 'manage_options' );
159 74 },
160 - 'callback' => [ $this, 'disconnect' ],
161 - ],
162 - ]
75 + 'callback' => array( $this, 'disconnect' ),
76 + ),
77 + )
163 78 );
164 - }
165 -
166 - /**
167 - * Method to register image specific routes.
168 - */
169 - public function register_image_routes() {
170 79 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 - [
80 + $this->namespace, '/update_option', array(
81 + array(
188 82 'methods' => \WP_REST_Server::CREATABLE,
189 83 'permission_callback' => function () {
190 84 return current_user_can( 'manage_options' );
191 85 },
192 - 'callback' => [ $this, 'get_sample_rate' ],
193 - ],
194 - ]
86 + 'callback' => array( $this, 'update_option' ),
87 + ),
88 + )
195 89 );
196 - }
197 - /**
198 - * Method to register media offload specific routes.
199 - */
200 - public function register_media_offload_routes() {
201 90 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 - [
91 + $this->namespace, '/poll_optimized_images', array(
92 + array(
251 93 'methods' => \WP_REST_Server::READABLE,
252 94 'permission_callback' => function () {
253 95 return current_user_can( 'manage_options' );
254 96 },
255 - 'callback' => [ $this, 'poll_watermarks' ],
256 - ],
257 - ]
97 + 'callback' => array( $this, 'poll_optimized_images' ),
98 + ),
99 + )
258 100 );
259 101 register_rest_route(
260 - $this->namespace,
261 - '/add_watermark',
262 - [
263 - [
102 + $this->namespace, '/images-sample-rate', array(
103 + array(
264 104 'methods' => \WP_REST_Server::CREATABLE,
265 105 'permission_callback' => function () {
266 106 return current_user_can( 'manage_options' );
267 107 },
268 - 'callback' => [ $this, 'add_watermark' ],
269 - ],
270 - ]
108 + 'callback' => array( $this, 'get_sample_rate' ),
109 + ),
110 + )
271 111 );
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 112 }
286 113
287 114 /**
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 115 * Connect to optimole service.
371 116 *
372 117 * @param WP_REST_Request $request connect rest request.
373 118 *
@@ -374,57 +119,17 @@
374 119 * @return WP_Error|WP_REST_Response
375 120 */
376 121 public function connect( WP_REST_Request $request ) {
377 122 $api_key = $request->get_param( 'api_key' );
378 - $original_request = $request;
379 123 $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 );
124 + $data = $request->get_user_data( $api_key );
125 + if ( $data === false ) {
126 + wp_send_json_error( __( 'Can not connect to optimole service', 'optimole-wp' ) );
392 127 }
393 128 $settings = new Optml_Settings();
129 + $settings->update( 'service_data', $data );
394 130 $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 131
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 132 return $this->response( $data );
428 133 }
429 134
430 135 /**
@@ -429,14 +134,14 @@
429 134
430 135 /**
431 136 * Wrapper for api response.
432 137 *
433 - * @param mixed $data data from api.
138 + * @param array $data data from api.
434 139 *
435 140 * @return WP_REST_Response
436 141 */
437 - private function response( $data, $code = 'success' ) {
438 - return new WP_REST_Response( [ 'data' => $data, 'code' => $code ], 200 );
142 + private function response( $data ) {
143 + return new WP_REST_Response( array( 'data' => $data, 'code' => 'success' ), 200 );
439 144 }
440 145
441 146 /**
442 147 * Connect to optimole service.
@@ -449,17 +154,9 @@
449 154 $email = $request->get_param( 'email' );
450 155 $api = new Optml_Api();
451 156 $user = $api->create_account( $email );
452 157 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 -
158 + return new WP_Error( 'error', 'Error creating account.' );
462 159 }
463 160
464 161 return $this->response( $user );
465 162 }
@@ -473,101 +170,42 @@
473 170 */
474 171 public function get_sample_rate( WP_REST_Request $request ) {
475 172
476 173 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 174
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 - ]
175 + $accepted_mimes = array( 'image/jpeg', 'image/png' );
176 + $args = array(
177 + 'post_type' => 'attachment',
178 + 'post_status' => 'any',
179 + 'number' => '5',
180 + 'no_found_rows' => true,
181 + 'fields' => 'ids',
182 + 'post_mime_type' => $accepted_mimes,
496 183 );
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 184 $image_result = new WP_Query( $args );
533 185 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 - ];
186 + return $this->response( array() );
543 187 }
544 - $attachment_id = $image_result->posts[ array_rand( $image_result->posts, 1 ) ];
545 188
546 - $original_image_url = wp_get_attachment_image_url( $attachment_id, 'full' );
189 + $image = array(
190 + 'id' => $image_result->posts [ array_rand( $image_result->posts, 1 ) ],
191 + );
192 + $image['original'] = wp_get_attachment_image_url( $image['id'], 'full' );
547 193
548 - $metadata = wp_get_attachment_metadata( $attachment_id );
194 + remove_filter( 'optml_dont_replace_url', '__return_true' );
549 195
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 - }
196 + $image['optimized'] = apply_filters( 'optml_replace_image', $image['original'], array(
197 + 'width' => 'auto',
198 + 'height' => 'auto',
199 + 'quality' => $request->get_param( 'quality' )
200 + ) );
557 201
558 - return [
559 - 'url' => $original_image_url,
560 - 'id' => $attachment_id,
561 - 'width' => $width,
562 - 'height' => $height,
563 - ];
202 + return $this->response( $image );
564 203 }
565 204
566 205 /**
567 206 * Disconnect from optimole service.
568 207 *
569 - * @SuppressWarnings(PHPMD.UnusedFormalParameter)
570 208 * @param WP_REST_Request $request disconnect rest request.
571 209 */
572 210 public function disconnect( WP_REST_Request $request ) {
573 211 $settings = new Optml_Settings();
@@ -585,250 +223,58 @@
585 223 public function poll_optimized_images( WP_REST_Request $request ) {
586 224 $api_key = $request->get_param( 'api_key' );
587 225 $request = new Optml_Api();
588 226 $images = $request->get_optimized_images( $api_key );
589 - if ( ! isset( $images['list'] ) || empty( $images['list'] ) ) {
590 - return $this->response( [] );
227 + if ( ! isset ( $images['list'] ) || empty( $images['list'] ) ) {
228 + return $this->response( array() );
591 229 }
592 230
593 231 $final_images = array_splice( $images['list'], 0, 10 );
594 - $media = new Optml_Media_Offload();
595 232
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 233 return $this->response( $final_images );
602 234 }
603 235
604 236 /**
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 237 * Update options method.
712 238 *
713 239 * @param WP_REST_Request $request option update rest request.
714 - *
715 - * @return WP_REST_Response
716 240 */
717 241 public function update_option( WP_REST_Request $request ) {
718 - $new_settings = $request->get_param( 'settings' );
719 -
720 - if ( empty( $new_settings ) ) {
242 + $option_key = $request->get_param( 'option_key' );
243 + $option_type = $request->get_param( 'type' );
244 + $option_value = $request->get_param( 'option_value' );
245 + if ( empty( $option_key ) ) {
721 246 wp_send_json_error( 'No option key set.' );
722 247 }
248 + if ( empty( $option_type ) ) {
249 + wp_send_json_error( 'No option type set.' );
250 + }
723 251
724 - $settings = new Optml_Settings();
725 - $sanitized = $settings->parse_settings( $new_settings );
252 + $accepted_types = array( 'toggle', 'enum' );
726 253
727 - return $this->response( $sanitized );
728 - }
254 + if ( ! in_array( $option_type, $accepted_types ) ) {
255 + wp_send_json_error( 'Invalid option type.' );
256 + }
729 257
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 - }
258 + $settings = new Optml_Settings();
758 259
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 - }
260 + switch ( $option_type ) {
261 + case 'toggle':
262 + $status = $settings->get( $option_key );
263 + if ( $status === 'enabled' ) {
264 + $settings->update( $option_key, 'disabled' );
265 + wp_send_json_success( $option_key . ' disabled.' );
266 + } else {
267 + $settings->update( $option_key, 'enabled' );
268 + wp_send_json_success( $option_key . ' enabled.' );
782 269 }
783 - }
270 + break;
271 + case 'enum':
272 + $settings->update( $option_key, $option_value );
273 + wp_send_json_success( $option_key . ' saved to ' . $option_value );
274 + break;
275 + default:
276 + break;
784 277 }
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 278 }
791 279
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 280 }