config
9 years ago
css
9 years ago
data
9 years ago
images
9 years ago
js
9 years ago
vendor
9 years ago
views
9 years ago
class-tiny-compress-client.php
9 years ago
class-tiny-compress-fopen.php
9 years ago
class-tiny-compress.php
9 years ago
class-tiny-exception.php
9 years ago
class-tiny-image-size.php
9 years ago
class-tiny-image.php
9 years ago
class-tiny-notices.php
9 years ago
class-tiny-php.php
9 years ago
class-tiny-plugin.php
9 years ago
class-tiny-settings.php
9 years ago
class-tiny-wp-base.php
9 years ago
class-tiny-settings.php
719 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Tiny Compress Images - WordPress plugin. |
| 4 | * Copyright (C) 2015-2017 Voormedia 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 | public function __construct() { |
| 29 | parent::__construct(); |
| 30 | $this->notices = new Tiny_Notices(); |
| 31 | } |
| 32 | |
| 33 | private function init_compressor() { |
| 34 | $this->compressor = Tiny_Compress::create( |
| 35 | $this->get_api_key(), |
| 36 | $this->get_method( 'after_compress_callback' ) |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | public function get_absolute_url() { |
| 41 | return get_admin_url( null, 'options-media.php#' . self::NAME ); |
| 42 | } |
| 43 | |
| 44 | public function xmlrpc_init() { |
| 45 | try { |
| 46 | $this->init_compressor(); |
| 47 | } catch (Tiny_Exception $e) { |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | public function admin_init() { |
| 52 | if ( current_user_can( 'manage_options' ) ) { |
| 53 | if ( ! $this->get_api_key() ) { |
| 54 | $notice_class = 'error'; |
| 55 | $notice = esc_html__( |
| 56 | 'Please register or provide an API key to start compressing images', |
| 57 | 'tiny-compress-images' |
| 58 | ); |
| 59 | } elseif ( $this->get_api_key_pending() ) { |
| 60 | $notice_class = 'notice-warning'; |
| 61 | $notice = esc_html__( |
| 62 | 'Please activate your account to start compressing images', |
| 63 | 'tiny-compress-images' |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | if ( isset( $notice ) && $notice ) { |
| 68 | $link = sprintf( |
| 69 | '<a href="options-media.php#%s">%s</a>', self::NAME, $notice |
| 70 | ); |
| 71 | $this->notices->show( 'setting', $link, $notice_class, false ); |
| 72 | } |
| 73 | |
| 74 | if ( ! Tiny_PHP::client_supported() ) { |
| 75 | $details = 'PHP ' . PHP_VERSION; |
| 76 | if ( extension_loaded( 'curl' ) ) { |
| 77 | $curlinfo = curl_version(); |
| 78 | $details .= ' with curl ' . $curlinfo['version']; |
| 79 | } else { |
| 80 | $details .= ' without curl'; |
| 81 | } |
| 82 | $message = printf( |
| 83 | esc_html__( |
| 84 | 'You are using an outdated platform (%s) – some features are disabled', |
| 85 | 'tiny-compress-images' |
| 86 | ), $details |
| 87 | ); |
| 88 | $this->notices->show( 'deprecated', $message, 'notice-warning', false ); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | try { |
| 93 | $this->init_compressor(); |
| 94 | } catch (Tiny_Exception $e) { |
| 95 | $this->notices->show( |
| 96 | 'compressor_exception', |
| 97 | esc_html__( $e->getMessage(), 'tiny-compress-images' ), |
| 98 | 'error', false |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | $section = self::get_prefixed_name( 'settings' ); |
| 103 | add_settings_section( $section, |
| 104 | esc_html__( 'JPEG and PNG optimization', 'tiny-compress-images' ), |
| 105 | $this->get_method( 'render_section' ), |
| 106 | 'media' |
| 107 | ); |
| 108 | |
| 109 | $field = self::get_prefixed_name( 'api_key' ); |
| 110 | register_setting( 'media', $field ); |
| 111 | add_settings_field( $field, |
| 112 | esc_html__( 'TinyPNG account', 'tiny-compress-images' ), |
| 113 | $this->get_method( 'render_pending_status' ), |
| 114 | 'media', |
| 115 | $section |
| 116 | ); |
| 117 | |
| 118 | $field = self::get_prefixed_name( 'api_key_pending' ); |
| 119 | register_setting( 'media', $field ); |
| 120 | |
| 121 | $field = self::get_prefixed_name( 'sizes' ); |
| 122 | register_setting( 'media', $field ); |
| 123 | add_settings_field( $field, |
| 124 | esc_html__( 'File compression', 'tiny-compress-images' ), |
| 125 | $this->get_method( 'render_sizes' ), |
| 126 | 'media', |
| 127 | $section |
| 128 | ); |
| 129 | |
| 130 | $field = self::get_prefixed_name( 'resize_original' ); |
| 131 | register_setting( 'media', $field ); |
| 132 | add_settings_field( $field, |
| 133 | esc_html__( 'Original image', 'tiny-compress-images' ), |
| 134 | $this->get_method( 'render_resize' ), |
| 135 | 'media', |
| 136 | $section |
| 137 | ); |
| 138 | |
| 139 | $field = self::get_prefixed_name( 'preserve_data' ); |
| 140 | register_setting( 'media', $field ); |
| 141 | |
| 142 | add_settings_section( 'section_end', '', |
| 143 | $this->get_method( 'render_section_end' ), |
| 144 | 'media' |
| 145 | ); |
| 146 | |
| 147 | add_action( |
| 148 | 'wp_ajax_tiny_image_sizes_notice', |
| 149 | $this->get_method( 'image_sizes_notice' ) |
| 150 | ); |
| 151 | |
| 152 | add_action( |
| 153 | 'wp_ajax_tiny_account_status', |
| 154 | $this->get_method( 'account_status' ) |
| 155 | ); |
| 156 | |
| 157 | add_action( |
| 158 | 'wp_ajax_tiny_settings_create_api_key', |
| 159 | $this->get_method( 'create_api_key' ) |
| 160 | ); |
| 161 | |
| 162 | add_action( |
| 163 | 'wp_ajax_tiny_settings_update_api_key', |
| 164 | $this->get_method( 'update_api_key' ) |
| 165 | ); |
| 166 | } |
| 167 | |
| 168 | public function image_sizes_notice() { |
| 169 | $this->render_image_sizes_notice( |
| 170 | $_GET['image_sizes_selected'], |
| 171 | isset( $_GET['resize_original'] ), |
| 172 | isset( $_GET['compress_wr2x'] ) |
| 173 | ); |
| 174 | exit(); |
| 175 | } |
| 176 | |
| 177 | public function account_status() { |
| 178 | $this->render_account_status(); |
| 179 | exit(); |
| 180 | } |
| 181 | |
| 182 | public function get_compressor() { |
| 183 | return $this->compressor; |
| 184 | } |
| 185 | |
| 186 | public function set_compressor( $compressor ) { |
| 187 | $this->compressor = $compressor; |
| 188 | } |
| 189 | |
| 190 | public function get_status() { |
| 191 | return intval( get_option( self::get_prefixed_name( 'status' ) ) ); |
| 192 | } |
| 193 | |
| 194 | protected function get_api_key() { |
| 195 | if ( defined( 'TINY_API_KEY' ) ) { |
| 196 | return TINY_API_KEY; |
| 197 | } else { |
| 198 | return get_option( self::get_prefixed_name( 'api_key' ) ); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | protected function get_api_key_pending() { |
| 203 | if ( defined( 'TINY_API_KEY' ) ) { |
| 204 | return false; |
| 205 | } else { |
| 206 | return get_option( self::get_prefixed_name( 'api_key_pending' ) ); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | protected function clear_api_key_pending() { |
| 211 | delete_option( self::get_prefixed_name( 'api_key_pending' ) ); |
| 212 | } |
| 213 | |
| 214 | protected static function get_intermediate_size( $size ) { |
| 215 | /* Inspired by |
| 216 | http://codex.wordpress.org/Function_Reference/get_intermediate_image_sizes */ |
| 217 | global $_wp_additional_image_sizes; |
| 218 | |
| 219 | $width = get_option( $size . '_size_w' ); |
| 220 | $height = get_option( $size . '_size_h' ); |
| 221 | |
| 222 | /* Note: dimensions might be 0 to indicate no limit. */ |
| 223 | if ( isset( $width ) && isset( $height ) ) { |
| 224 | return array( $width, $height ); |
| 225 | } |
| 226 | |
| 227 | if ( isset( $_wp_additional_image_sizes[ $size ] ) ) { |
| 228 | $sizes = $_wp_additional_image_sizes[ $size ]; |
| 229 | return array( |
| 230 | isset( $sizes['width'] ) ? $sizes['width'] : null, |
| 231 | isset( $sizes['height'] ) ? $sizes['height'] : null, |
| 232 | ); |
| 233 | } |
| 234 | return array( null, null ); |
| 235 | } |
| 236 | |
| 237 | public function get_sizes() { |
| 238 | if ( is_array( $this->sizes ) ) { |
| 239 | return $this->sizes; |
| 240 | } |
| 241 | |
| 242 | $setting = get_option( self::get_prefixed_name( 'sizes' ) ); |
| 243 | |
| 244 | $size = Tiny_Image::ORIGINAL; |
| 245 | $this->sizes = array( |
| 246 | $size => array( |
| 247 | 'width' => null, |
| 248 | 'height' => null, |
| 249 | 'tinify' => ! is_array( $setting ) || |
| 250 | ( isset( $setting[ $size ] ) && 'on' === $setting[ $size ] ), |
| 251 | ), |
| 252 | ); |
| 253 | |
| 254 | foreach ( get_intermediate_image_sizes() as $size ) { |
| 255 | if ( self::DUMMY_SIZE === $size ) { |
| 256 | continue; |
| 257 | } |
| 258 | |
| 259 | list($width, $height) = self::get_intermediate_size( $size ); |
| 260 | if ( $width || $height ) { |
| 261 | $this->sizes[ $size ] = array( |
| 262 | 'width' => $width, |
| 263 | 'height' => $height, |
| 264 | 'tinify' => ! is_array( $setting ) || |
| 265 | ( isset( $setting[ $size ] ) && 'on' === $setting[ $size ] ), |
| 266 | ); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | return $this->sizes; |
| 271 | } |
| 272 | |
| 273 | public function get_active_tinify_sizes() { |
| 274 | if ( is_array( $this->tinify_sizes ) ) { |
| 275 | return $this->tinify_sizes; |
| 276 | } |
| 277 | |
| 278 | $this->tinify_sizes = array(); |
| 279 | foreach ( $this->get_sizes() as $size => $values ) { |
| 280 | if ( $values['tinify'] ) { |
| 281 | $this->tinify_sizes[] = $size; |
| 282 | } |
| 283 | } |
| 284 | return $this->tinify_sizes; |
| 285 | } |
| 286 | |
| 287 | public function get_resize_enabled() { |
| 288 | /* This only applies if the original is being resized. */ |
| 289 | $sizes = $this->get_sizes(); |
| 290 | if ( ! $sizes[ Tiny_Image::ORIGINAL ]['tinify'] ) { |
| 291 | return false; |
| 292 | } |
| 293 | |
| 294 | $setting = get_option( self::get_prefixed_name( 'resize_original' ) ); |
| 295 | return isset( $setting['enabled'] ) && 'on' === $setting['enabled']; |
| 296 | } |
| 297 | |
| 298 | public function get_preserve_enabled( $name ) { |
| 299 | $setting = get_option( self::get_prefixed_name( 'preserve_data' ) ); |
| 300 | return isset( $setting[ $name ] ) && 'on' === $setting[ $name ]; |
| 301 | } |
| 302 | |
| 303 | public function get_preserve_options( $size_name ) { |
| 304 | if ( ! Tiny_Image::is_original( $size_name ) ) { |
| 305 | return false; |
| 306 | } |
| 307 | $options = array(); |
| 308 | $settings = get_option( self::get_prefixed_name( 'preserve_data' ) ); |
| 309 | if ( $settings ) { |
| 310 | $keys = array_keys( $settings ); |
| 311 | foreach ( $keys as &$key ) { |
| 312 | if ( 'on' === $settings[ $key ] ) { |
| 313 | array_push( $options, $key ); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | return $options; |
| 318 | } |
| 319 | |
| 320 | public function get_resize_options( $size_name ) { |
| 321 | if ( ! Tiny_Image::is_original( $size_name ) ) { |
| 322 | return false; |
| 323 | } |
| 324 | if ( ! $this->get_resize_enabled() ) { |
| 325 | return false; |
| 326 | } |
| 327 | $setting = get_option( self::get_prefixed_name( 'resize_original' ) ); |
| 328 | $width = intval( $setting['width'] ); |
| 329 | $height = intval( $setting['height'] ); |
| 330 | $method = $width > 0 && $height > 0 ? 'fit' : 'scale'; |
| 331 | $options['method'] = $method; |
| 332 | if ( $width > 0 ) { |
| 333 | $options['width'] = $width; |
| 334 | } |
| 335 | if ( $height > 0 ) { |
| 336 | $options['height'] = $height; |
| 337 | } |
| 338 | return sizeof( $options ) >= 2 ? $options : false; |
| 339 | } |
| 340 | |
| 341 | public function render_section_end() { |
| 342 | echo '</div>'; |
| 343 | } |
| 344 | |
| 345 | public function render_section() { |
| 346 | echo '<div class="' . self::NAME . '">'; |
| 347 | echo '<span id="' . self::NAME . '"></span>'; |
| 348 | } |
| 349 | |
| 350 | public function render_sizes() { |
| 351 | echo '<p>'; |
| 352 | esc_html_e( |
| 353 | 'Choose sizes to compress. Remember each selected size counts as a compression.', |
| 354 | 'tiny-compress-images' |
| 355 | ); |
| 356 | echo '</p>'; |
| 357 | echo '<input type="hidden" name="' . |
| 358 | self::get_prefixed_name( 'sizes[' . self::DUMMY_SIZE . ']' ) . '" value="on"/>'; |
| 359 | |
| 360 | foreach ( $this->get_sizes() as $size => $option ) { |
| 361 | $this->render_size_checkbox( $size, $option ); |
| 362 | } |
| 363 | if ( self::wr2x_active() ) { |
| 364 | $this->render_size_checkbox( 'wr2x', $this->get_wr2x_option() ); |
| 365 | } |
| 366 | echo '<br>'; |
| 367 | echo '<div id="tiny-image-sizes-notice">'; |
| 368 | |
| 369 | $this->render_image_sizes_notice( |
| 370 | count( self::get_active_tinify_sizes() ), |
| 371 | self::get_resize_enabled(), |
| 372 | self::compress_wr2x_images() |
| 373 | ); |
| 374 | |
| 375 | echo '</div>'; |
| 376 | } |
| 377 | |
| 378 | private function render_size_checkbox( $size, $option ) { |
| 379 | $id = self::get_prefixed_name( "sizes_$size" ); |
| 380 | $name = self::get_prefixed_name( 'sizes[' . $size . ']' ); |
| 381 | $checked = ( $option['tinify'] ? ' checked="checked"' : '' ); |
| 382 | if ( Tiny_Image::is_original( $size ) ) { |
| 383 | $label = esc_html__( 'Original image', 'tiny-compress-images' ) . ' (' . |
| 384 | esc_html__( 'overwritten by compressed image', 'tiny-compress-images' ) . ')'; |
| 385 | } elseif ( Tiny_Image::is_retina( $size ) ) { |
| 386 | $label = esc_html__( 'WP Retina 2x sizes', 'tiny-compress-images' ); |
| 387 | } else { |
| 388 | $width = $option['width']; |
| 389 | if ( ! $width ) { |
| 390 | $width = '?'; |
| 391 | } |
| 392 | |
| 393 | $height = $option['height']; |
| 394 | if ( ! $height ) { |
| 395 | $height = '?'; |
| 396 | } |
| 397 | |
| 398 | $label = esc_html__( ucfirst( str_replace( '_', ' ', $size ) ) ) |
| 399 | . ' - ' . $width . 'x' . $height; |
| 400 | } |
| 401 | echo '<p>'; |
| 402 | echo '<input type="checkbox" id="' . $id . '" name="' . $name . |
| 403 | '" value="on" ' . $checked . '/>'; |
| 404 | echo '<label for="' . $id . '">' . $label . '</label>'; |
| 405 | echo '</p>'; |
| 406 | } |
| 407 | |
| 408 | public function render_image_sizes_notice( |
| 409 | $active_sizes_count, $resize_original_enabled, $compress_wr2x ) { |
| 410 | echo '<p>'; |
| 411 | if ( $resize_original_enabled ) { |
| 412 | $active_sizes_count++; |
| 413 | } |
| 414 | if ( $compress_wr2x ) { |
| 415 | $active_sizes_count *= 2; |
| 416 | } |
| 417 | |
| 418 | if ( $active_sizes_count < 1 ) { |
| 419 | esc_html_e( |
| 420 | 'With these settings no images will be compressed.', |
| 421 | 'tiny-compress-images' |
| 422 | ); |
| 423 | } else { |
| 424 | $free_images_per_month = floor( |
| 425 | Tiny_Config::MONTHLY_FREE_COMPRESSIONS / $active_sizes_count |
| 426 | ); |
| 427 | printf( wp_kses( __( |
| 428 | 'With these settings you can compress |
| 429 | <strong> at least %s images </strong> for free each month.', |
| 430 | 'tiny-compress-images' |
| 431 | ), array( 'strong' => array() ) ), $free_images_per_month ); |
| 432 | |
| 433 | if ( self::wr2x_active() ) { |
| 434 | echo '</p>'; |
| 435 | echo '<p>'; |
| 436 | esc_html_e( |
| 437 | 'If selected, retina sizes will be compressed when generated by WP Retina 2x', |
| 438 | 'tiny-compress-images' |
| 439 | ); |
| 440 | echo '<br>'; |
| 441 | esc_html_e( |
| 442 | 'Each retina size will count as an additional compression.', |
| 443 | 'tiny-compress-images' |
| 444 | ); |
| 445 | } |
| 446 | } |
| 447 | echo '</p>'; |
| 448 | } |
| 449 | |
| 450 | public function render_resize() { |
| 451 | echo '<p class="tiny-resize-unavailable" style="display: none">'; |
| 452 | esc_html_e( |
| 453 | 'Enable compression of the original image size for more options.', |
| 454 | 'tiny-compress-images' |
| 455 | ); |
| 456 | echo '</p>'; |
| 457 | |
| 458 | $id = self::get_prefixed_name( 'resize_original_enabled' ); |
| 459 | $name = self::get_prefixed_name( 'resize_original[enabled]' ); |
| 460 | $checked = ( $this->get_resize_enabled() ? ' checked="checked"' : '' ); |
| 461 | |
| 462 | $label = esc_html__( |
| 463 | 'Resize and compress the original image', |
| 464 | 'tiny-compress-images' |
| 465 | ); |
| 466 | |
| 467 | echo '<p class="tiny-resize-available">'; |
| 468 | echo '<input type="checkbox" id="' . $id . '" name="' . $name . |
| 469 | '" value="on" ' . $checked . '/>'; |
| 470 | echo '<label for="' . $id . '">' . $label . '</label>'; |
| 471 | echo '<br>'; |
| 472 | echo '</p>'; |
| 473 | |
| 474 | echo '<p class="tiny-resize-available tiny-resize-resolution">'; |
| 475 | printf( '%s ', esc_html__( 'Max Width' ) ); |
| 476 | $this->render_resize_input( 'width' ); |
| 477 | printf( '%s ', esc_html__( 'Max Height' ) ); |
| 478 | $this->render_resize_input( 'height' ); |
| 479 | echo '</p>'; |
| 480 | |
| 481 | echo '<p class="tiny-resize-available tiny-resize-resolution">'; |
| 482 | |
| 483 | esc_html_e( |
| 484 | 'Resizing takes 1 additional compression for each image that is larger.', |
| 485 | 'tiny-compress-images' |
| 486 | ); |
| 487 | |
| 488 | echo '</p><br>'; |
| 489 | |
| 490 | $this->render_preserve_input( |
| 491 | 'creation', |
| 492 | esc_html__( |
| 493 | 'Preserve creation date and time in the original image', |
| 494 | 'tiny-compress-images' |
| 495 | ) . ' ' . |
| 496 | esc_html__( '(JPEG only)', 'tiny-compress-images' ) |
| 497 | ); |
| 498 | |
| 499 | $this->render_preserve_input( |
| 500 | 'copyright', |
| 501 | esc_html__( |
| 502 | 'Preserve copyright information in the original image', |
| 503 | 'tiny-compress-images' |
| 504 | ) |
| 505 | ); |
| 506 | |
| 507 | $this->render_preserve_input( |
| 508 | 'location', |
| 509 | esc_html__( |
| 510 | 'Preserve GPS location in the original image', |
| 511 | 'tiny-compress-images' |
| 512 | ) . ' ' . |
| 513 | esc_html__( '(JPEG only)', 'tiny-compress-images' ) |
| 514 | ); |
| 515 | } |
| 516 | |
| 517 | public function render_preserve_input( $name, $description ) { |
| 518 | echo '<p class="tiny-preserve">'; |
| 519 | $id = sprintf( self::get_prefixed_name( 'preserve_data_%s' ), $name ); |
| 520 | $field = sprintf( self::get_prefixed_name( 'preserve_data[%s]' ), $name ); |
| 521 | $checked = ( $this->get_preserve_enabled( $name ) ? ' checked="checked"' : '' ); |
| 522 | $label = esc_html__( $description, 'tiny-compress-images' ); |
| 523 | echo '<input type="checkbox" id="' . $id . '" name="' . $field . |
| 524 | '" value="on" ' . $checked . '/>'; |
| 525 | echo '<label for="' . $id . '">' . $label . '</label>'; |
| 526 | echo '<br>'; |
| 527 | echo '</p>'; |
| 528 | } |
| 529 | |
| 530 | public function render_resize_input( $name ) { |
| 531 | $id = sprintf( self::get_prefixed_name( 'resize_original_%s' ), $name ); |
| 532 | $field = sprintf( self::get_prefixed_name( 'resize_original[%s]' ), $name ); |
| 533 | $settings = get_option( self::get_prefixed_name( 'resize_original' ) ); |
| 534 | $value = isset( $settings[ $name ] ) ? $settings[ $name ] : '2048'; |
| 535 | echo '<input type="number" id="' . $id . '" name="' . $field . |
| 536 | '" value="' . $value . '" size="5" />'; |
| 537 | } |
| 538 | |
| 539 | public function get_compression_count() { |
| 540 | $field = self::get_prefixed_name( 'status' ); |
| 541 | return get_option( $field ); |
| 542 | } |
| 543 | |
| 544 | public function after_compress_callback( $compressor ) { |
| 545 | if ( ! is_null( $count = $compressor->get_compression_count() ) ) { |
| 546 | $field = self::get_prefixed_name( 'status' ); |
| 547 | update_option( $field, $count ); |
| 548 | } |
| 549 | if ( $compressor->limit_reached() ) { |
| 550 | $link = '<a href="https://tinypng.com/developers" target="_blank">' . |
| 551 | esc_html__( 'TinyPNG API account', 'tiny-compress-images' ) . '</a>'; |
| 552 | |
| 553 | $this->notices->add('limit-reached', |
| 554 | sprintf( |
| 555 | esc_html__( |
| 556 | 'You have reached your limit of %s compressions this month.', |
| 557 | 'tiny-compress-images' |
| 558 | ), |
| 559 | $count |
| 560 | ) . |
| 561 | sprintf( |
| 562 | esc_html__( |
| 563 | 'Upgrade your %s if you like to compress more images.', |
| 564 | 'tiny-compress-images' |
| 565 | ), |
| 566 | $link |
| 567 | ) |
| 568 | ); |
| 569 | } else { |
| 570 | $this->notices->remove( 'limit-reached' ); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | public function render_account_status() { |
| 575 | $key = $this->get_api_key(); |
| 576 | if ( empty( $key ) ) { |
| 577 | $compressor = $this->get_compressor(); |
| 578 | if ( $compressor->can_create_key() ) { |
| 579 | include( dirname( __FILE__ ) . '/views/account-status-create-advanced.php' ); |
| 580 | } else { |
| 581 | include( dirname( __FILE__ ) . '/views/account-status-create-simple.php' ); |
| 582 | } |
| 583 | } else { |
| 584 | $status = $this->compressor->get_status(); |
| 585 | $status->pending = false; |
| 586 | |
| 587 | if ( $status->ok ) { |
| 588 | if ( $this->get_api_key_pending() ) { |
| 589 | $this->clear_api_key_pending(); |
| 590 | } |
| 591 | } else { |
| 592 | if ( $this->get_api_key_pending() ) { |
| 593 | $status->ok = true; |
| 594 | $status->pending = true; |
| 595 | $status->message = ( |
| 596 | 'An email has been sent with a link to activate your account' |
| 597 | ); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | include( dirname( __FILE__ ) . '/views/account-status-connected.php' ); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | public function render_pending_status() { |
| 606 | $key = $this->get_api_key(); |
| 607 | if ( empty( $key ) ) { |
| 608 | $compressor = $this->get_compressor(); |
| 609 | if ( $compressor->can_create_key() ) { |
| 610 | include( dirname( __FILE__ ) . '/views/account-status-create-advanced.php' ); |
| 611 | } else { |
| 612 | include( dirname( __FILE__ ) . '/views/account-status-create-simple.php' ); |
| 613 | } |
| 614 | } else { |
| 615 | include( dirname( __FILE__ ) . '/views/account-status-loading.php' ); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | public function create_api_key() { |
| 620 | $compressor = $this->get_compressor(); |
| 621 | if ( $compressor->can_create_key() ) { |
| 622 | if ( ! isset( $_POST['name'] ) || ! $_POST['name'] ) { |
| 623 | $status = (object) array( |
| 624 | 'ok' => false, |
| 625 | 'message' => __( |
| 626 | 'Please enter your name', 'tiny-compress-images' |
| 627 | ), |
| 628 | ); |
| 629 | echo json_encode( $status ); |
| 630 | exit(); |
| 631 | } |
| 632 | |
| 633 | if ( ! isset( $_POST['email'] ) || ! $_POST['email'] ) { |
| 634 | $status = (object) array( |
| 635 | 'ok' => false, |
| 636 | 'message' => __( |
| 637 | 'Please enter your email address', 'tiny-compress-images' |
| 638 | ), |
| 639 | ); |
| 640 | echo json_encode( $status ); |
| 641 | exit(); |
| 642 | } |
| 643 | |
| 644 | try { |
| 645 | $site = str_replace( array( 'http://', 'https://' ), '', get_bloginfo( 'url' ) ); |
| 646 | $identifier = 'WordPress plugin for ' . $site; |
| 647 | $link = $this->get_absolute_url(); |
| 648 | $compressor->create_key( $_POST['email'], array( |
| 649 | 'name' => $_POST['name'], |
| 650 | 'identifier' => $identifier, |
| 651 | 'link' => $link, |
| 652 | ) ); |
| 653 | |
| 654 | update_option( self::get_prefixed_name( 'api_key_pending' ), true ); |
| 655 | update_option( self::get_prefixed_name( 'api_key' ), $compressor->get_key() ); |
| 656 | update_option( self::get_prefixed_name( 'status' ), 0 ); |
| 657 | |
| 658 | $status = (object) array( |
| 659 | 'ok' => true, |
| 660 | 'message' => null, |
| 661 | ); |
| 662 | } catch (Tiny_Exception $err) { |
| 663 | list( $message ) = explode( ' (HTTP', $err->getMessage(), 2 ); |
| 664 | $status = (object) array( |
| 665 | 'ok' => false, |
| 666 | 'message' => $message, |
| 667 | ); |
| 668 | } |
| 669 | } else { |
| 670 | $status = (object) array( |
| 671 | 'ok' => false, |
| 672 | 'message' => 'This feature is not available on your platform', |
| 673 | ); |
| 674 | } |
| 675 | |
| 676 | $status->message = __( $status->message, 'tiny-compress-images' ); |
| 677 | echo json_encode( $status ); |
| 678 | exit(); |
| 679 | } |
| 680 | |
| 681 | public function update_api_key() { |
| 682 | $key = $_POST['key']; |
| 683 | if ( empty( $key ) ) { |
| 684 | /* Always save if key is blank, so the key can be deleted. */ |
| 685 | $status = (object) array( |
| 686 | 'ok' => true, |
| 687 | 'message' => null, |
| 688 | ); |
| 689 | } else { |
| 690 | $status = Tiny_Compress::create( $key )->get_status(); |
| 691 | } |
| 692 | if ( $status->ok ) { |
| 693 | update_option( self::get_prefixed_name( 'api_key_pending' ), false ); |
| 694 | update_option( self::get_prefixed_name( 'api_key' ), $key ); |
| 695 | } |
| 696 | $status->message = __( $status->message, 'tiny-compress-images' ); |
| 697 | echo json_encode( $status ); |
| 698 | exit(); |
| 699 | } |
| 700 | |
| 701 | public static function wr2x_active() { |
| 702 | return is_plugin_active( 'wp-retina-2x/wp-retina-2x.php' ); |
| 703 | } |
| 704 | |
| 705 | public function get_wr2x_option() { |
| 706 | $setting = get_option( self::get_prefixed_name( 'sizes' ) ); |
| 707 | return array( |
| 708 | 'width' => null, |
| 709 | 'height' => null, |
| 710 | 'tinify' => ( isset( $setting['wr2x'] ) && 'on' === $setting['wr2x'] ), |
| 711 | ); |
| 712 | } |
| 713 | |
| 714 | public function compress_wr2x_images() { |
| 715 | $option = $this->get_wr2x_option(); |
| 716 | return self::wr2x_active() && $option['tinify']; |
| 717 | } |
| 718 | } |
| 719 |