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 -661 3.1.31.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 - return $this->response( __( 'Can not connect to Optimole service', 'optimole-wp' ) . $extra, 400 );
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,29 +154,11 @@
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 - );
158 + return new WP_Error( 'error', 'Error creating account.' );
461 159 }
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 160
472 - }
473 -
474 161 return $this->response( $user );
475 162 }
476 163
477 164 /**
@@ -483,101 +170,42 @@
483 170 */
484 171 public function get_sample_rate( WP_REST_Request $request ) {
485 172
486 173 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 174
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 - ]
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,
506 183 );
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 184 $image_result = new WP_Query( $args );
543 185 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 - ];
186 + return $this->response( array() );
553 187 }
554 - $attachment_id = $image_result->posts[ array_rand( $image_result->posts, 1 ) ];
555 188
556 - $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' );
557 193
558 - $metadata = wp_get_attachment_metadata( $attachment_id );
194 + remove_filter( 'optml_dont_replace_url', '__return_true' );
559 195
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 - }
196 + $image['optimized'] = apply_filters( 'optml_replace_image', $image['original'], array(
197 + 'width' => 'auto',
198 + 'height' => 'auto',
199 + 'quality' => $request->get_param( 'quality' )
200 + ) );
567 201
568 - return [
569 - 'url' => $original_image_url,
570 - 'id' => $attachment_id,
571 - 'width' => $width,
572 - 'height' => $height,
573 - ];
202 + return $this->response( $image );
574 203 }
575 204
576 205 /**
577 206 * Disconnect from optimole service.
578 207 *
579 - * @SuppressWarnings(PHPMD.UnusedFormalParameter)
580 208 * @param WP_REST_Request $request disconnect rest request.
581 209 */
582 210 public function disconnect( WP_REST_Request $request ) {
583 211 $settings = new Optml_Settings();
@@ -595,250 +223,58 @@
595 223 public function poll_optimized_images( WP_REST_Request $request ) {
596 224 $api_key = $request->get_param( 'api_key' );
597 225 $request = new Optml_Api();
598 226 $images = $request->get_optimized_images( $api_key );
599 - if ( ! isset( $images['list'] ) || empty( $images['list'] ) ) {
600 - return $this->response( [] );
227 + if ( ! isset ( $images['list'] ) || empty( $images['list'] ) ) {
228 + return $this->response( array() );
601 229 }
602 230
603 231 $final_images = array_splice( $images['list'], 0, 10 );
604 - $media = new Optml_Media_Offload();
605 232
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 233 return $this->response( $final_images );
612 234 }
613 235
614 236 /**
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 237 * Update options method.
722 238 *
723 239 * @param WP_REST_Request $request option update rest request.
724 - *
725 - * @return WP_REST_Response
726 240 */
727 241 public function update_option( WP_REST_Request $request ) {
728 - $new_settings = $request->get_param( 'settings' );
729 -
730 - 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 ) ) {
731 246 wp_send_json_error( 'No option key set.' );
732 247 }
248 + if ( empty( $option_type ) ) {
249 + wp_send_json_error( 'No option type set.' );
250 + }
733 251
734 - $settings = new Optml_Settings();
735 - $sanitized = $settings->parse_settings( $new_settings );
252 + $accepted_types = array( 'toggle', 'enum' );
736 253
737 - return $this->response( $sanitized );
738 - }
254 + if ( ! in_array( $option_type, $accepted_types ) ) {
255 + wp_send_json_error( 'Invalid option type.' );
256 + }
739 257
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 - }
258 + $settings = new Optml_Settings();
768 259
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 - }
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.' );
792 269 }
793 - }
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;
794 277 }
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 278 }
801 279
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 280 }