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