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