config
11 years ago
languages
11 years ago
scripts
11 years ago
styles
11 years ago
cacert.pem
11 years ago
class-tiny-compress-curl.php
11 years ago
class-tiny-compress-fopen.php
11 years ago
class-tiny-compress.php
11 years ago
class-tiny-compressor-status.php
11 years ago
class-tiny-exception.php
11 years ago
class-tiny-metadata.php
11 years ago
class-tiny-php.php
11 years ago
class-tiny-plugin.php
11 years ago
class-tiny-settings.php
11 years ago
class-tiny-wp-base.php
11 years ago
class-tiny-settings.php
233 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Tiny Compress Images - WordPress plugin. |
| 4 | * Copyright (C) 2015 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 | |
| 21 | class Tiny_Settings extends Tiny_WP_Base { |
| 22 | const DUMMY_SIZE = '_tiny_dummy'; |
| 23 | |
| 24 | private $sizes; |
| 25 | private $tinify_sizes; |
| 26 | private $compressor; |
| 27 | |
| 28 | public function admin_init() { |
| 29 | parent::admin_init(); |
| 30 | |
| 31 | try { |
| 32 | $this->compressor = Tiny_Compress::get_compressor($this->get_api_key(), $this->get_method('after_compress_callback')); |
| 33 | } catch (Tiny_Exception $e) { |
| 34 | $this->add_admin_notice('compressor_exception', self::translate_escape($e->getMessage()), true); |
| 35 | } |
| 36 | |
| 37 | $section = self::get_prefixed_name('settings'); |
| 38 | add_settings_section($section, self::translate('PNG and JPEG compression'), $this->get_method('render_section'), 'media'); |
| 39 | |
| 40 | $field = self::get_prefixed_name('api_key'); |
| 41 | register_setting('media', $field); |
| 42 | add_settings_field($field, self::translate('TinyPNG API key'), $this->get_method('render_api_key'), 'media', $section, array('label_for' => $field)); |
| 43 | |
| 44 | $field = self::get_prefixed_name('sizes'); |
| 45 | register_setting('media', $field); |
| 46 | add_settings_field($field, self::translate('File compression'), $this->get_method('render_sizes'), 'media', $section); |
| 47 | |
| 48 | $field = self::get_prefixed_name('status'); |
| 49 | register_setting('media', $field); |
| 50 | add_settings_field($field, self::translate('Connection status'), $this->get_method('render_pending_status'), 'media', $section); |
| 51 | |
| 52 | add_action('wp_ajax_tiny_compress_status', $this->get_method('get_status')); |
| 53 | } |
| 54 | |
| 55 | public function get_status() { |
| 56 | $this->render_status(); |
| 57 | exit(); |
| 58 | } |
| 59 | |
| 60 | public function get_compressor() { |
| 61 | return $this->compressor; |
| 62 | } |
| 63 | |
| 64 | public function set_compressor($compressor) { |
| 65 | $this->compressor = $compressor; |
| 66 | } |
| 67 | |
| 68 | protected function get_api_key() { |
| 69 | if (defined('TINY_API_KEY')) { |
| 70 | return TINY_API_KEY; |
| 71 | } else { |
| 72 | return get_option(self::get_prefixed_name('api_key')); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | protected static function get_intermediate_size($size) { |
| 77 | # Inspired by http://codex.wordpress.org/Function_Reference/get_intermediate_image_sizes |
| 78 | global $_wp_additional_image_sizes; |
| 79 | |
| 80 | $width = get_option($size . '_size_w'); |
| 81 | $height = get_option($size . '_size_h'); |
| 82 | if ($width && $height) { |
| 83 | return array($width, $height); |
| 84 | } |
| 85 | |
| 86 | if (isset($_wp_additional_image_sizes[$size])) { |
| 87 | $sizes = $_wp_additional_image_sizes[$size]; |
| 88 | return array( |
| 89 | isset($sizes['width']) ? $sizes['width'] : null, |
| 90 | isset($sizes['height']) ? $sizes['height'] : null, |
| 91 | ); |
| 92 | } |
| 93 | return array(null, null); |
| 94 | } |
| 95 | |
| 96 | public function get_sizes() { |
| 97 | if (is_array($this->sizes)) { |
| 98 | return $this->sizes; |
| 99 | } |
| 100 | |
| 101 | $setting = get_option(self::get_prefixed_name('sizes')); |
| 102 | |
| 103 | $size = Tiny_Metadata::ORIGINAL; |
| 104 | $this->sizes = array($size => array( |
| 105 | 'width' => null, 'height' => null, |
| 106 | 'tinify' => !is_array($setting) || (isset($setting[$size]) && $setting[$size] === 'on'), |
| 107 | )); |
| 108 | |
| 109 | foreach (get_intermediate_image_sizes() as $size) { |
| 110 | if ($size === self::DUMMY_SIZE) { |
| 111 | continue; |
| 112 | } |
| 113 | list($width, $height) = self::get_intermediate_size($size); |
| 114 | if ($width && $height) { |
| 115 | $this->sizes[$size] = array( |
| 116 | 'width' => $width, 'height' => $height, |
| 117 | 'tinify' => !is_array($setting) || (isset($setting[$size]) && $setting[$size] === 'on'), |
| 118 | ); |
| 119 | } |
| 120 | } |
| 121 | return $this->sizes; |
| 122 | } |
| 123 | |
| 124 | public function get_tinify_sizes() { |
| 125 | if (is_array($this->tinify_sizes)) { |
| 126 | return $this->tinify_sizes; |
| 127 | } |
| 128 | |
| 129 | $this->tinify_sizes = array(); |
| 130 | foreach ($this->get_sizes() as $size => $values) { |
| 131 | if ($values['tinify']) { |
| 132 | $this->tinify_sizes[] = $size; |
| 133 | } |
| 134 | } |
| 135 | return $this->tinify_sizes; |
| 136 | } |
| 137 | |
| 138 | public function render_section() { |
| 139 | } |
| 140 | |
| 141 | public function render_api_key() { |
| 142 | $field = self::get_prefixed_name('api_key'); |
| 143 | $key = $this->get_api_key(); |
| 144 | |
| 145 | if (defined('TINY_API_KEY')) { |
| 146 | echo '<p>' . sprintf(self::translate('The API key has been configured in %s'), 'wp-config.php') . '.</p>'; |
| 147 | } else { |
| 148 | echo '<input type="text" id="' . $field . '" name="' . $field . '" value="' . htmlspecialchars($key) . '" size="40" />'; |
| 149 | } |
| 150 | echo '<p>'; |
| 151 | $link = '<a href="https://tinypng.com/developers" target="_blank">' . self::translate_escape('TinyPNG Developer section') . '</a>'; |
| 152 | if (empty($key)) { |
| 153 | printf(self::translate_escape('Visit %s to get an API key') . '.', $link); |
| 154 | } else { |
| 155 | printf(self::translate_escape('Visit %s to view your usage or upgrade your account') . '.', $link); |
| 156 | } |
| 157 | echo '</p>'; |
| 158 | } |
| 159 | |
| 160 | public function render_sizes() { |
| 161 | echo '<p>' . self::translate_escape('You can choose to compress different image sizes created by WordPress here') . '.<br/>'; |
| 162 | echo self::translate_escape('Remember each additional image size will affect your TinyPNG monthly usage') . "!";?> |
| 163 | <input type="hidden" name="<?php echo self::get_prefixed_name('sizes[' . self::DUMMY_SIZE .']'); ?>" value="on"/></p> |
| 164 | <?php |
| 165 | foreach ($this->get_sizes() as $size => $option) { |
| 166 | $this->render_size_checkbox($size, $option); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | private function render_size_checkbox($size, $option) { |
| 171 | $id = self::get_prefixed_name("sizes_$size"); |
| 172 | $field = self::get_prefixed_name("sizes[$size]"); |
| 173 | if ($size === Tiny_Metadata::ORIGINAL) { |
| 174 | $label = self::translate_escape("original"); |
| 175 | } else { |
| 176 | $label = $size . " - ${option['width']}x${option['height']}"; |
| 177 | }?> |
| 178 | <p><input type="checkbox" id="<?php echo $id; ?>" name="<?php echo $field ?>" value="on" <?php if ($option['tinify']) { echo ' checked="checked"'; } ?>/> |
| 179 | <label for="<?php echo $id; ?>"><?php echo $label; ?></label></p> |
| 180 | <?php |
| 181 | } |
| 182 | |
| 183 | public function get_compression_count() { |
| 184 | $field = self::get_prefixed_name('status'); |
| 185 | return get_option($field); |
| 186 | } |
| 187 | |
| 188 | public function after_compress_callback($details, $headers) { |
| 189 | if(isset($headers["Compression-Count"])) { |
| 190 | $field = self::get_prefixed_name('status'); |
| 191 | update_option($field, $headers["Compression-Count"]); |
| 192 | |
| 193 | if (isset($details['error']) && $details['error'] == 'TooManyRequests') { |
| 194 | $link = '<a href="https://tinypng.com/developers" target="_blank">' . self::translate_escape('subscription') . '</a>'; |
| 195 | $this->add_admin_notice('limit_reached', sprintf(self::translate_escape('you have reached your limit of %s compressions this month. Upgrade your %s if you like to compress more images') . '.', $headers["Compression-Count"], $link)); |
| 196 | } else { |
| 197 | $this->remove_admin_notice('limit_reached'); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | public function render_status() { |
| 203 | switch ($this->compressor->get_status()) { |
| 204 | case Tiny_Compressor_Status::Green: |
| 205 | echo '<p><img src="images/yes.png"> ' . self::translate_escape('API connection successful') . '</p>'; |
| 206 | break; |
| 207 | case Tiny_Compressor_Status::Yellow: |
| 208 | echo '<p>' . self::translate_escape('API status could not be checked, enable cURL for more information') . '.</p>'; |
| 209 | return; |
| 210 | case Tiny_Compressor_Status::Red: |
| 211 | echo '<p><img src="images/no.png"> ' . self::translate_escape('API connection unsuccessful') . '</p>'; |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | $compressions = self::get_compression_count(); |
| 216 | echo '<p>'; |
| 217 | // We currently have no way to check if a user is free or flexible. |
| 218 | if ($compressions == 500) { |
| 219 | $link = '<a href="https://tinypng.com/developers" target="_blank">' . self::translate_escape('TinyPNG API subscription') . '</a>'; |
| 220 | printf(self::translate_escape('You have reached your limit of %s compressions this month') . '.', $compressions); |
| 221 | echo '<br>'; |
| 222 | printf(self::translate_escape('If you need to compress more images you can change your %s') . '.', $link); |
| 223 | } else { |
| 224 | printf(self::translate_escape('You have made %s compressions this month') . '.', self::get_compression_count()); |
| 225 | } |
| 226 | echo '</p>'; |
| 227 | } |
| 228 | |
| 229 | public function render_pending_status() { |
| 230 | echo '<div id="tiny-compress-status"><div class="spinner"></div></div>'; |
| 231 | } |
| 232 | } |
| 233 |