PluginProbe ʕ •ᴥ•ʔ
TinyPNG – JPEG, PNG & WebP image compression / 3.4
TinyPNG – JPEG, PNG & WebP image compression v3.4
3.7.0 3.6.14 trunk 1.0.0 1.1.0 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.4.0 1.5.0 1.6.0 1.7.0 1.7.1 1.7.2 2.0.0 2.0.1 2.0.2 2.1.0 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.0.0 3.0.1 3.1.0 3.2.0 3.2.1 3.3 3.4 3.4.1 3.4.2 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.5.2 3.6.0 3.6.1 3.6.10 3.6.11 3.6.12 3.6.13 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.6.9
tiny-compress-images / src / class-tiny-settings.php
tiny-compress-images / src Last commit date
compatibility 3 years ago config 3 years ago css 3 years ago data 3 years ago images 3 years ago js 3 years ago vendor 3 years ago views 3 years ago class-tiny-bulk-optimization.php 3 years ago class-tiny-compress-client.php 3 years ago class-tiny-compress-fopen.php 3 years ago class-tiny-compress.php 3 years ago class-tiny-exception.php 3 years ago class-tiny-image-size.php 3 years ago class-tiny-image.php 3 years ago class-tiny-notices.php 3 years ago class-tiny-php.php 3 years ago class-tiny-plugin.php 3 years ago class-tiny-settings.php 3 years ago class-tiny-wp-base.php 3 years ago
class-tiny-settings.php
969 lines
1 <?php
2 /*
3 * Tiny Compress Images - WordPress plugin.
4 * Copyright (C) 2015-2018 Tinify B.V.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc., 51
18 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20 class Tiny_Settings extends Tiny_WP_Base {
21 const DUMMY_SIZE = '_tiny_dummy';
22
23 private $sizes;
24 private $tinify_sizes;
25 private $compressor;
26 private $notices;
27
28 protected static $offload_s3_plugin = 'amazon-s3-and-cloudfront/wordpress-s3.php';
29
30 public function __construct() {
31 parent::__construct();
32 $this->notices = new Tiny_Notices();
33 }
34
35 private function init_compressor() {
36 $this->compressor = Tiny_Compress::create(
37 $this->get_api_key(),
38 $this->get_method( 'after_compress_callback' )
39 );
40 }
41
42 public function get_absolute_url() {
43 return get_admin_url( null, 'options-general.php?page=tinify' );
44 }
45
46 public function xmlrpc_init() {
47 try {
48 $this->init_compressor();
49 } catch ( Tiny_Exception $e ) {
50 }
51 }
52
53 public function ajax_init() {
54 try {
55 $this->init_compressor();
56 } catch ( Tiny_Exception $e ) {
57 }
58
59 add_action(
60 'wp_ajax_tiny_image_sizes_notice',
61 $this->get_method( 'image_sizes_notice' )
62 );
63
64 add_action(
65 'wp_ajax_tiny_account_status',
66 $this->get_method( 'account_status' )
67 );
68
69 add_action(
70 'wp_ajax_tiny_settings_create_api_key',
71 $this->get_method( 'create_api_key' )
72 );
73
74 add_action(
75 'wp_ajax_tiny_settings_update_api_key',
76 $this->get_method( 'update_api_key' )
77 );
78 }
79
80 public function admin_init() {
81 try {
82 $this->init_compressor();
83 } catch ( Tiny_Exception $e ) {
84 $this->notices->show(
85 'compressor_exception',
86 esc_html( $e->getMessage(), 'tiny-compress-images' ),
87 'error', false
88 );
89 }
90
91 if ( current_user_can( 'manage_options' ) ) {
92 $this->setup_incomplete_checks();
93 $this->offload_s3_checks();
94 }
95
96 $field = self::get_prefixed_name( 'api_key' );
97 register_setting( 'tinify', $field );
98
99 $field = self::get_prefixed_name( 'api_key_pending' );
100 register_setting( 'tinify', $field );
101
102 $field = self::get_prefixed_name( 'compression_timing' );
103 register_setting( 'tinify', $field );
104
105 $field = self::get_prefixed_name( 'sizes' );
106 register_setting( 'tinify', $field );
107
108 $field = self::get_prefixed_name( 'resize_original' );
109 register_setting( 'tinify', $field );
110
111 $field = self::get_prefixed_name( 'preserve_data' );
112 register_setting( 'tinify', $field );
113 }
114
115 public function admin_menu() {
116 /* Create link to new settings page from media settings page. */
117 add_settings_section( 'section_end', '',
118 $this->get_method( 'render_settings_moved' ),
119 'media'
120 );
121
122 add_options_page(
123 __( 'TinyPNG - JPEG, PNG & WebP image compression', 'tiny-compress-images' ),
124 esc_html__( 'TinyPNG - JPEG, PNG & WebP image compression', 'tiny-compress-images' ),
125 'manage_options',
126 'tinify',
127 array( $this, 'add_options_to_page' )
128 );
129 }
130
131 public function add_options_to_page() {
132 include( dirname( __FILE__ ) . '/views/settings.php' );
133 }
134
135 public function image_sizes_notice() {
136 if ( current_user_can( 'manage_options' ) ) {
137 $this->render_size_checkboxes_description(
138 $_GET['image_sizes_selected'],
139 isset( $_GET['resize_original'] ),
140 isset( $_GET['compress_wr2x'] )
141 );
142 }
143 exit();
144 }
145
146 public function account_status() {
147 if ( current_user_can( 'manage_options' ) ) {
148 $this->render_account_status();
149 }
150 exit();
151 }
152
153 public function get_compressor() {
154 return $this->compressor;
155 }
156
157 public function set_compressor( $compressor ) {
158 $this->compressor = $compressor;
159 }
160
161 public function get_status() {
162 return intval( get_option( self::get_prefixed_name( 'status' ) ) );
163 }
164
165 public function disabled_required_functions() {
166 $required_functions = array( 'curl_exec' );
167 $disabled_required_functions = array();
168 $disabled_functions = explode( ',', ini_get( 'disable_functions' ) );
169
170 foreach ( $required_functions as $required_function ) {
171 if ( in_array( $required_function, $disabled_functions ) ) {
172 array_push( $disabled_required_functions, $required_function );
173 }
174 }
175
176 return $disabled_required_functions;
177 }
178
179 protected function get_api_key() {
180 if ( defined( 'TINY_API_KEY' ) ) {
181 return TINY_API_KEY;
182 } else {
183 return get_option( self::get_prefixed_name( 'api_key' ) );
184 }
185 }
186
187 protected function get_api_key_pending() {
188 if ( defined( 'TINY_API_KEY' ) ) {
189 return false;
190 } else {
191 return get_option( self::get_prefixed_name( 'api_key_pending' ) );
192 }
193 }
194
195 protected function clear_api_key_pending() {
196 delete_option( self::get_prefixed_name( 'api_key_pending' ) );
197 }
198
199 protected static function get_intermediate_size( $size ) {
200 /* Inspired by
201 http://codex.wordpress.org/Function_Reference/get_intermediate_image_sizes */
202 global $_wp_additional_image_sizes;
203
204 $width = get_option( $size . '_size_w' );
205 $height = get_option( $size . '_size_h' );
206
207 /* Note: dimensions might be 0 to indicate no limit. */
208 if ( $width || $height ) {
209 return array( $width, $height );
210 }
211
212 if ( isset( $_wp_additional_image_sizes[ $size ] ) ) {
213 $sizes = $_wp_additional_image_sizes[ $size ];
214 return array(
215 isset( $sizes['width'] ) ? $sizes['width'] : null,
216 isset( $sizes['height'] ) ? $sizes['height'] : null,
217 );
218 }
219 return array( null, null );
220 }
221
222 public function get_sizes() {
223 if ( is_array( $this->sizes ) ) {
224 return $this->sizes;
225 }
226
227 $setting = get_option( self::get_prefixed_name( 'sizes' ) );
228
229 $size = Tiny_Image::ORIGINAL;
230 $this->sizes = array(
231 $size => array(
232 'width' => null,
233 'height' => null,
234 'tinify' => ! is_array( $setting ) ||
235 ( isset( $setting[ $size ] ) && 'on' === $setting[ $size ] ),
236 ),
237 );
238
239 foreach ( get_intermediate_image_sizes() as $size ) {
240 if ( self::DUMMY_SIZE === $size ) {
241 continue;
242 }
243
244 list($width, $height) = self::get_intermediate_size( $size );
245 if ( $width || $height ) {
246 $this->sizes[ $size ] = array(
247 'width' => $width,
248 'height' => $height,
249 'tinify' => ! is_array( $setting ) ||
250 ( isset( $setting[ $size ] ) && 'on' === $setting[ $size ] ),
251 );
252 }
253 }
254
255 return $this->sizes;
256 }
257
258 public function get_active_tinify_sizes() {
259 if ( is_array( $this->tinify_sizes ) ) {
260 return $this->tinify_sizes;
261 }
262
263 $this->tinify_sizes = array();
264 foreach ( $this->get_sizes() as $size => $values ) {
265 if ( $values['tinify'] ) {
266 $this->tinify_sizes[] = $size;
267 }
268 }
269 return $this->tinify_sizes;
270 }
271
272 public function new_plugin_install() {
273 /* We merely have to check whether a newly added setting is already stored. */
274 $compression_timing = get_option( self::get_prefixed_name( 'compression_timing' ) );
275 return ! $compression_timing;
276 }
277
278 public function get_resize_enabled() {
279 /* This only applies if the original is being resized. */
280 $sizes = $this->get_sizes();
281 if ( ! $sizes[ Tiny_Image::ORIGINAL ]['tinify'] ) {
282 return false;
283 }
284
285 $setting = get_option( self::get_prefixed_name( 'resize_original' ) );
286 return isset( $setting['enabled'] ) && 'on' === $setting['enabled'];
287 }
288
289 public function get_compression_timing() {
290 $setting = get_option( self::get_prefixed_name( 'compression_timing' ) );
291 if ( isset( $setting ) && $setting ) {
292 return $setting;
293 } elseif ( $this->new_plugin_install() ) {
294 update_option( self::get_prefixed_name( 'compression_timing' ), 'background' );
295 return 'background';
296 } else {
297 update_option( self::get_prefixed_name( 'compression_timing' ), 'auto' );
298 return 'auto';
299 }
300 }
301
302 public function auto_compress_enabled() {
303 return $this->get_compression_timing() === 'auto' ||
304 $this->get_compression_timing() === 'background';
305 }
306
307 public function background_compress_enabled() {
308 return $this->get_compression_timing() === 'background';
309 }
310
311 public function has_offload_s3_installed() {
312 if (
313 ! function_exists( 'is_plugin_active' ) ||
314 ! is_plugin_active( self::$offload_s3_plugin )
315 ) {
316 return false;
317 }
318 $setting = get_option( 'tantan_wordpress_s3' );
319 if ( ! is_array( $setting ) ) {
320 return false;
321 }
322
323 return true;
324 }
325
326 public function old_offload_s3_version_installed() {
327 if (
328 function_exists( 'is_plugin_active' ) &&
329 is_plugin_active( self::$offload_s3_plugin ) &&
330 function_exists( 'get_plugin_data' )
331 ) {
332 $metadata = get_plugin_data( WP_PLUGIN_DIR . '/' . self::$offload_s3_plugin );
333 $offload_s3_version_parts = explode( '.', $metadata['Version'] );
334 $major_version = intval( $offload_s3_version_parts[0] );
335 $minor_version = intval( $offload_s3_version_parts[1] );
336 if ( 0 === $major_version && $minor_version < 7 ) {
337 return true;
338 }
339 }
340
341 return false;
342 }
343
344 public function remove_local_files_setting_enabled() {
345 /* Check if Offload S3 plugin is installed. */
346 if (
347 ! function_exists( 'is_plugin_active' ) ||
348 ! is_plugin_active( self::$offload_s3_plugin )
349 ) {
350 return false;
351 }
352 $setting = get_option( 'tantan_wordpress_s3' );
353 if ( ! is_array( $setting ) ) {
354 return false;
355 }
356 /* Check if Offload S3 is configured to remove local files. */
357 return ( $this->has_offload_s3_installed() &&
358 array_key_exists( 'remove-local-file', $setting ) &&
359 '1' === $setting['remove-local-file'] );
360 }
361
362 public function get_preserve_enabled( $name ) {
363 $setting = get_option( self::get_prefixed_name( 'preserve_data' ) );
364 return isset( $setting[ $name ] ) && 'on' === $setting[ $name ];
365 }
366
367 public function get_preserve_options( $size_name ) {
368 if ( ! Tiny_Image::is_original( $size_name ) ) {
369 return false;
370 }
371 $options = array();
372 $settings = get_option( self::get_prefixed_name( 'preserve_data' ) );
373 if ( $settings ) {
374 $keys = array_keys( $settings );
375 foreach ( $keys as &$key ) {
376 if ( 'on' === $settings[ $key ] ) {
377 array_push( $options, $key );
378 }
379 }
380 }
381 return $options;
382 }
383
384 public function get_resize_options( $size_name ) {
385 if ( ! Tiny_Image::is_original( $size_name ) ) {
386 return false;
387 }
388 if ( ! $this->get_resize_enabled() ) {
389 return false;
390 }
391 $setting = get_option( self::get_prefixed_name( 'resize_original' ) );
392 $width = intval( $setting['width'] );
393 $height = intval( $setting['height'] );
394 $method = $width > 0 && $height > 0 ? 'fit' : 'scale';
395 $options['method'] = $method;
396 if ( $width > 0 ) {
397 $options['width'] = $width;
398 }
399 if ( $height > 0 ) {
400 $options['height'] = $height;
401 }
402 return sizeof( $options ) >= 2 ? $options : false;
403 }
404
405 private function setup_incomplete_checks() {
406 if ( ! $this->get_api_key() ) {
407 $this->notices->api_key_missing_notice();
408 } elseif ( $this->get_api_key_pending() ) {
409 $this->notices->get_api_key_pending_notice();
410 }
411 }
412
413 public function render_settings_moved() {
414 echo '<div class="tinify-settings"><h3>';
415 esc_html_e( 'TinyPNG - JPEG, PNG & WebP image compression', 'tiny-compress-images' );
416 echo '</h3>';
417 $url = admin_url( 'options-general.php?page=tinify' );
418 $link = "<a href='" . $url . "'>";
419 $link .= esc_html__( 'settings', 'tiny-compress-images' );
420 $link .= '</a>';
421 printf(
422 wp_kses(
423 /* translators: %s: link saying settings */
424 __( 'The %s have moved.', 'tiny-compress-images' ),
425 array(
426 'a' => array(
427 'href' => array(),
428 ),
429 )
430 ),
431 $link
432 );
433 echo '</div>';
434 }
435
436 private function offload_s3_checks() {
437 if ( $this->remove_local_files_setting_enabled() &&
438 'background' === $this->get_compression_timing() ) {
439 update_option( self::get_prefixed_name( 'compression_timing' ), 'auto' );
440 $this->notices->show_offload_s3_notice();
441 }
442 if ( $this->old_offload_s3_version_installed() &&
443 'background' === $this->get_compression_timing() ) {
444 update_option( self::get_prefixed_name( 'compression_timing' ), 'auto' );
445 $this->notices->old_offload_s3_version_notice();
446 }
447 }
448
449 public function render_compression_timing_settings() {
450 $heading = esc_html__(
451 'When should new images be compressed?',
452 'tiny-compress-images'
453 );
454 echo '<h4>' . $heading . '</h4>';
455 echo '<div class="optimization-options">';
456
457 $name = self::get_prefixed_name( 'compression_timing' );
458 $compression_timing = $this->get_compression_timing();
459
460 $id = self::get_prefixed_name( 'background_compress_enabled' );
461 $checked = ( 'background' === $compression_timing ? ' checked="checked"' : '' );
462
463 $label = esc_html__(
464 'Compress new images in the background (Recommended)',
465 'tiny-compress-images'
466 );
467 $description = esc_html__(
468 'This is the fastest method, but can cause issues with some image related plugins.',
469 'tiny-compress-images'
470 );
471
472 $this->render_compression_timing_radiobutton(
473 $name,
474 $label,
475 $description,
476 'background',
477 $checked,
478 $this->remove_local_files_setting_enabled() || $this->old_offload_s3_version_installed()
479 );
480
481 $id = self::get_prefixed_name( 'auto_compress_enabled' );
482 $checked = ( 'auto' === $compression_timing ? ' checked="checked"' : '' );
483
484 $label = esc_html__(
485 'Compress new images during upload',
486 'tiny-compress-images'
487 );
488 $description = esc_html__(
489 'Uploads will take longer, but provides higher compatibility with other plugins.',
490 'tiny-compress-images'
491 );
492
493 $this->render_compression_timing_radiobutton(
494 $name,
495 $label,
496 $description,
497 'auto',
498 $checked,
499 false
500 );
501
502 $id = self::get_prefixed_name( 'auto_compress_disabled' );
503 $checked = ( 'manual' === $compression_timing ? ' checked="checked"' : '' );
504
505 $label = esc_html__(
506 'Do not compress new images automatically',
507 'tiny-compress-images'
508 );
509 $description = esc_html__(
510 'Manually select the images you want to compress in the media library.',
511 'tiny-compress-images'
512 );
513
514 $this->render_compression_timing_radiobutton(
515 $name,
516 $label,
517 $description,
518 'manual',
519 $checked,
520 false
521 );
522
523 echo '</div>';
524 }
525
526 public function render_sizes() {
527 echo '<input type="hidden" name="' .
528 self::get_prefixed_name( 'sizes[' . self::DUMMY_SIZE . ']' ) . '" value="on"/>';
529
530 foreach ( $this->get_sizes() as $size => $option ) {
531 $this->render_size_checkboxes( $size, $option );
532 }
533 if ( self::wr2x_active() ) {
534 $this->render_size_checkboxes( 'wr2x', $this->get_wr2x_option() );
535 }
536 echo '<br>';
537 echo '<div id="tiny-image-sizes-notice">';
538
539 $this->render_size_checkboxes_description(
540 count( self::get_active_tinify_sizes() ),
541 self::get_resize_enabled(),
542 self::compress_wr2x_images()
543 );
544
545 echo '</div>';
546 }
547
548 private function render_size_checkboxes( $size, $option ) {
549 $id = self::get_prefixed_name( "sizes_$size" );
550 $name = self::get_prefixed_name( 'sizes[' . $size . ']' );
551 $checked = ( $option['tinify'] ? ' checked="checked"' : '' );
552 if ( Tiny_Image::is_original( $size ) ) {
553 $label = esc_html__( 'Original image', 'tiny-compress-images' ) . ' (' .
554 esc_html__(
555 'overwritten by compressed image',
556 'tiny-compress-images'
557 ) . ')';
558 } elseif ( Tiny_Image::is_retina( $size ) ) {
559 $label = esc_html__( 'WP Retina 2x sizes', 'tiny-compress-images' );
560 } else {
561 $width = $option['width'];
562 if ( ! $width ) {
563 $width = '?';
564 }
565
566 $height = $option['height'];
567 if ( ! $height ) {
568 $height = '?';
569 }
570
571 $label = esc_html( ucfirst( str_replace( '_', ' ', $size ) ) )
572 . ' - ' . $width . 'x' . $height;
573 }
574 echo '<p>';
575 echo '<input type="checkbox" id="' . $id . '" name="' . $name .
576 '" value="on" ' . $checked . '/>';
577 echo '<label for="' . $id . '">' . $label . '</label>';
578 echo '</p>';
579 }
580
581 public function render_size_checkboxes_description(
582 $active_sizes_count, $resize_original_enabled, $compress_wr2x ) {
583 echo '<p>';
584 esc_html_e(
585 'Remember each selected size counts as a compression.',
586 'tiny-compress-images'
587 );
588 echo '</p>';
589 echo '<p>';
590 if ( $resize_original_enabled ) {
591 $active_sizes_count++;
592 }
593 if ( $compress_wr2x ) {
594 $active_sizes_count *= 2;
595 }
596
597 if ( $active_sizes_count < 1 ) {
598 esc_html_e(
599 'With these settings no images will be compressed.',
600 'tiny-compress-images'
601 );
602 } else {
603 $free_images_per_month = floor(
604 Tiny_Config::MONTHLY_FREE_COMPRESSIONS / $active_sizes_count
605 );
606
607 $strong = array(
608 'strong' => array(),
609 );
610
611 /* translators: %1$s: number of images */
612 printf( wp_kses( __(
613 'With these settings you can compress <strong>at least %1$s images</strong> for free each month.', // WPCS: Needed for proper translation.
614 'tiny-compress-images'
615 ), $strong ), $free_images_per_month );
616
617 if ( self::wr2x_active() ) {
618 echo '</p>';
619 echo '<p>';
620 esc_html_e(
621 'If selected, retina sizes will be compressed when generated by WP Retina 2x',
622 'tiny-compress-images'
623 );
624 echo '<br>';
625 esc_html_e(
626 'Each retina size will count as an additional compression.',
627 'tiny-compress-images'
628 );
629 }
630 } // End if().
631 echo '</p>';
632 }
633
634 public function render_resize() {
635 $strong = array(
636 'strong' => array(),
637 );
638
639 echo '<div class="tiny-resize-unavailable" style="display: none">';
640 esc_html_e(
641 'Enable compression of the original image size for more options.',
642 'tiny-compress-images'
643 );
644 echo '</div>';
645
646 $id = self::get_prefixed_name( 'resize_original_enabled' );
647 $name = self::get_prefixed_name( 'resize_original[enabled]' );
648 $checked = ( $this->get_resize_enabled() ? ' checked="checked"' : '' );
649
650 $label = esc_html__(
651 'Resize the original image',
652 'tiny-compress-images'
653 );
654
655 echo '<div class="tiny-resize-available">';
656 echo '<input type="checkbox" id="' . $id . '" name="' . $name .
657 '" value="on" ' . $checked . '/>';
658 echo '<label for="' . $id . '">' . $label . '</label><br>';
659
660 echo '<div class="tiny-resize-available tiny-resize-resolution">';
661 echo '<span>';
662 echo wp_kses( __( '<strong>Save space</strong> by setting a maximum width and height for all images uploaded.', 'tiny-compress-images' ), $strong ); // WPCS: Needed for proper translation.
663 echo '<br>';
664 echo wp_kses( __( 'Resizing takes <strong>1 additional compression</strong> for each image that is larger.', 'tiny-compress-images' ), $strong ); // WPCS: Needed for proper translation.
665 echo '</span>';
666 echo '<div class="tiny-resize-inputs">';
667 printf( '%s: ', esc_html__( 'Max Width' ) );
668 $this->render_resize_input( 'width' );
669 printf( '%s: ', esc_html__( 'Max Height' ) );
670 $this->render_resize_input( 'height' );
671 echo '</div></div></div>';
672
673 $this->render_preserve_input(
674 'creation',
675 esc_html__(
676 'Preserve creation date and time in the original image',
677 'tiny-compress-images'
678 )
679 );
680
681 $this->render_preserve_input(
682 'copyright',
683 esc_html__(
684 'Preserve copyright information in the original image',
685 'tiny-compress-images'
686 )
687 );
688
689 $this->render_preserve_input(
690 'location',
691 esc_html__(
692 'Preserve GPS location in the original image',
693 'tiny-compress-images'
694 ) . ' ' .
695 esc_html__( '(JPEG only)', 'tiny-compress-images' )
696 );
697 }
698
699 public function render_compression_timing_radiobutton(
700 $name,
701 $label,
702 $desc,
703 $value,
704 $checked,
705 $disabled
706 ) {
707 if ( $disabled ) {
708 echo '<div class="notice notice-warning inline"><p>';
709 echo '<strong>' . esc_html__( 'Warning', 'tiny-compress-images' ) . '</strong> — ';
710 if ( $this->old_offload_s3_version_installed() ) {
711 $message = esc_html_e(
712 'Background compressions are not compatible with the version of WP Offload S3 you have installed. Please update to version 0.7.2 at least.', // WPCS: Needed for proper translation.
713 'tiny-compress-images'
714 );
715 } elseif ( $this->remove_local_files_setting_enabled() ) {
716 $message = esc_html_e(
717 'For background compression to work you will need to configure WP Offload S3 to keep a copy of the images on the server.', // WPCS: Needed for proper translation.
718 'tiny-compress-images'
719 );
720 }
721 echo '</p></div>';
722 echo '<p class="tiny-radio disabled">';
723 } else {
724 echo '<p class="tiny-radio">';
725 }
726 $id = sprintf( self::get_prefixed_name( 'compression_timing_%s' ), $value );
727 $label = esc_html( $label, 'tiny-compress-images' );
728 $desc = esc_html( $desc, 'tiny-compress-images' );
729 $disabled = ( $disabled ? ' disabled="disabled"' : '' );
730 echo '<input type="radio" id="' . $id . '" name="' . $name .
731 '" value="' . $value . '" ' . $checked . ' ' . $disabled . '/>';
732 echo '<label for="' . $id . '">' . $label . '</label>';
733 echo '<br>';
734 echo '<span class="description">' . $desc . '</span>';
735 echo '<br>';
736 echo '</p>';
737 }
738
739 public function render_preserve_input( $name, $description ) {
740 echo '<p class="tiny-preserve">';
741 $id = sprintf( self::get_prefixed_name( 'preserve_data_%s' ), $name );
742 $field = sprintf( self::get_prefixed_name( 'preserve_data[%s]' ), $name );
743 $checked = ( $this->get_preserve_enabled( $name ) ? ' checked="checked"' : '' );
744 $label = esc_html( $description, 'tiny-compress-images' );
745 echo '<input type="checkbox" id="' . $id . '" name="' . $field .
746 '" value="on" ' . $checked . '/>';
747 echo '<label for="' . $id . '">' . $label . '</label>';
748 echo '<br>';
749 echo '</p>';
750 }
751
752 public function render_resize_input( $name ) {
753 $id = sprintf( self::get_prefixed_name( 'resize_original_%s' ), $name );
754 $field = sprintf( self::get_prefixed_name( 'resize_original[%s]' ), $name );
755 $settings = get_option( self::get_prefixed_name( 'resize_original' ) );
756 $value = isset( $settings[ $name ] ) ? $settings[ $name ] : '2048';
757 echo '<input type="number" id="' . $id . '" name="' . $field .
758 '" value="' . $value . '" size="5" />';
759 }
760
761 public function get_compression_count() {
762 $field = self::get_prefixed_name( 'status' );
763 return get_option( $field );
764 }
765
766 public function limit_reached() {
767 $this->compressor->get_compression_count();
768 return $this->compressor->limit_reached();
769 }
770
771 public function get_remaining_credits() {
772 $field = self::get_prefixed_name( 'remaining_credits' );
773 return get_option( $field );
774 }
775
776 public function get_paying_state() {
777 $field = self::get_prefixed_name( 'paying_state' );
778 return get_option( $field );
779 }
780
781 public function is_on_free_plan() {
782 return self::get_paying_state() === 'free';
783 }
784
785 public function get_email_address() {
786 $field = self::get_prefixed_name( 'email_address' );
787 return get_option( $field );
788 }
789
790 public function after_compress_callback( $compressor ) {
791 $count = $compressor->get_compression_count();
792 if ( ! is_null( $count ) ) {
793 $field = self::get_prefixed_name( 'status' );
794 update_option( $field, $count );
795 }
796 $remaining_credits = $compressor->get_remaining_credits();
797 if ( ! is_null( $remaining_credits ) ) {
798 $field = self::get_prefixed_name( 'remaining_credits' );
799 update_option( $field, $remaining_credits );
800 }
801 $paying_state = $compressor->get_paying_state();
802 if ( ! is_null( $paying_state ) ) {
803 $field = self::get_prefixed_name( 'paying_state' );
804 update_option( $field, $paying_state );
805 }
806 $email_address = $compressor->get_email_address();
807 if ( ! is_null( $email_address ) ) {
808 $field = self::get_prefixed_name( 'email_address' );
809 update_option( $field, $email_address );
810 }
811 if ( $compressor->limit_reached() ) {
812 $this->notices->add_limit_reached_notice( $email_address );
813 } else {
814 $this->notices->remove( 'limit-reached' );
815 }
816 }
817
818 public function render_account_status() {
819 $key = $this->get_api_key();
820 if ( empty( $key ) ) {
821 $compressor = $this->get_compressor();
822 if ( $compressor->can_create_key() ) {
823 include( dirname( __FILE__ ) . '/views/account-status-create-advanced.php' );
824 } else {
825 include( dirname( __FILE__ ) . '/views/account-status-create-simple.php' );
826 }
827 } else {
828 $status = $this->compressor->get_status();
829 $status->pending = false;
830 if ( $status->ok ) {
831 if ( $this->get_api_key_pending() ) {
832 $this->clear_api_key_pending();
833 }
834 } else {
835 if ( $this->get_api_key_pending() ) {
836 $status->ok = true;
837 $status->pending = true;
838 $status->message = (
839 'An email has been sent to activate your account'
840 );
841 }
842 }
843 include( dirname( __FILE__ ) . '/views/account-status-connected.php' );
844 }
845 }
846
847 public function render_pending_status() {
848 $key = $this->get_api_key();
849 if ( empty( $key ) ) {
850 $compressor = $this->get_compressor();
851 if ( $compressor->can_create_key() ) {
852 include( dirname( __FILE__ ) . '/views/account-status-create-advanced.php' );
853 } else {
854 include( dirname( __FILE__ ) . '/views/account-status-create-simple.php' );
855 }
856 } else {
857 include( dirname( __FILE__ ) . '/views/account-status-loading.php' );
858 }
859 }
860
861 public function create_api_key() {
862 $compressor = $this->get_compressor();
863 if ( ! current_user_can( 'manage_options' ) ) {
864 $status = (object) array(
865 'ok' => false,
866 'message' => 'This feature requires certain user capabilities',
867 );
868 } elseif ( $compressor->can_create_key() ) {
869 if ( ! isset( $_POST['name'] ) || ! $_POST['name'] ) {
870 $status = (object) array(
871 'ok' => false,
872 'message' => __(
873 'Please enter your name', 'tiny-compress-images'
874 ),
875 );
876 echo json_encode( $status );
877 exit();
878 }
879
880 if ( ! isset( $_POST['email'] ) || ! $_POST['email'] ) {
881 $status = (object) array(
882 'ok' => false,
883 'message' => __(
884 'Please enter your email address', 'tiny-compress-images'
885 ),
886 );
887 echo json_encode( $status );
888 exit();
889 }
890
891 try {
892 $site = str_replace( array( 'http://', 'https://' ), '', get_bloginfo( 'url' ) );
893 $identifier = 'WordPress plugin for ' . $site;
894 $link = $this->get_absolute_url();
895 $compressor->create_key( $_POST['email'], array(
896 'name' => $_POST['name'],
897 'identifier' => $identifier,
898 'link' => $link,
899 ) );
900
901 update_option( self::get_prefixed_name( 'api_key_pending' ), true );
902 update_option( self::get_prefixed_name( 'api_key' ), $compressor->get_key() );
903 update_option( self::get_prefixed_name( 'status' ), 0 );
904
905 $status = (object) array(
906 'ok' => true,
907 'message' => null,
908 );
909 } catch ( Tiny_Exception $err ) {
910 list( $message ) = explode( ' (HTTP', $err->getMessage(), 2 );
911 $status = (object) array(
912 'ok' => false,
913 'message' => $message,
914 );
915 }
916 } else {
917 $status = (object) array(
918 'ok' => false,
919 'message' => 'This feature is not available on your platform',
920 );
921 }// End if().
922
923 echo json_encode( $status );
924 exit();
925 }
926
927 public function update_api_key() {
928 $key = $_POST['key'];
929 if ( ! current_user_can( 'manage_options' ) ) {
930 $status = (object) array(
931 'ok' => false,
932 'message' => 'This feature requires certain user capabilities',
933 );
934 } elseif ( empty( $key ) ) {
935 /* Always save if key is blank, so the key can be deleted. */
936 $status = (object) array(
937 'ok' => true,
938 'message' => null,
939 );
940 } else {
941 $status = Tiny_Compress::create( $key )->get_status();
942 }
943 if ( $status->ok ) {
944 update_option( self::get_prefixed_name( 'api_key_pending' ), false );
945 update_option( self::get_prefixed_name( 'api_key' ), $key );
946 }
947 echo json_encode( $status );
948 exit();
949 }
950
951 public static function wr2x_active() {
952 return function_exists( 'wr2x_get_retina' );
953 }
954
955 public function get_wr2x_option() {
956 $setting = get_option( self::get_prefixed_name( 'sizes' ) );
957 return array(
958 'width' => null,
959 'height' => null,
960 'tinify' => ( isset( $setting['wr2x'] ) && 'on' === $setting['wr2x'] ),
961 );
962 }
963
964 public function compress_wr2x_images() {
965 $option = $this->get_wr2x_option();
966 return self::wr2x_active() && $option['tinify'];
967 }
968 }
969