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-notices.php
349 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 | |
| 21 | class Tiny_Notices extends Tiny_WP_Base { |
| 22 | private $notices; |
| 23 | private $dismissals; |
| 24 | |
| 25 | /** |
| 26 | * Tiny_Settings instance. |
| 27 | * |
| 28 | * @var Tiny_Settings |
| 29 | */ |
| 30 | private $settings; |
| 31 | |
| 32 | /** |
| 33 | * The number of compressions required before showing the feedback notice. |
| 34 | * |
| 35 | * @var int |
| 36 | */ |
| 37 | private $compressions_for_feedback = 10; |
| 38 | |
| 39 | /** |
| 40 | * Tiny_Notices constructor. |
| 41 | * |
| 42 | * @param Tiny_Settings $settings The settings instance. |
| 43 | */ |
| 44 | public function __construct( $settings ) { |
| 45 | parent::__construct(); |
| 46 | $this->settings = $settings; |
| 47 | } |
| 48 | |
| 49 | protected static $incompatible_plugins = array( |
| 50 | 'CheetahO Image Optimizer' => 'cheetaho-image-optimizer/cheetaho.php', |
| 51 | 'EWWW Image Optimizer' => 'ewww-image-optimizer/ewww-image-optimizer.php', |
| 52 | 'Imagify' => 'imagify/imagify.php', |
| 53 | 'Kraken Image Optimizer' => 'kraken-image-optimizer/kraken.php', |
| 54 | 'ShortPixel Image Optimizer' => 'shortpixel-image-optimiser/wp-shortpixel.php', |
| 55 | 'WP Smush' => 'wp-smushit/wp-smush.php', |
| 56 | 'WP Smush Pro' => 'wp-smush-pro/wp-smush.php', |
| 57 | ); |
| 58 | |
| 59 | private static function get_option_key() { |
| 60 | return self::get_prefixed_name( 'admin_notices' ); |
| 61 | } |
| 62 | |
| 63 | private static function get_user_meta_key() { |
| 64 | return self::get_prefixed_name( 'admin_notice_dismissals' ); |
| 65 | } |
| 66 | |
| 67 | public function ajax_init() { |
| 68 | add_action( 'wp_ajax_tiny_dismiss_notice', $this->get_method( 'dismiss' ) ); |
| 69 | } |
| 70 | |
| 71 | public function admin_init() { |
| 72 | if ( current_user_can( 'manage_options' ) ) { |
| 73 | $this->show_stored(); |
| 74 | $this->show_notices(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | private function load_notices() { |
| 79 | if ( is_array( $this->notices ) ) { |
| 80 | return; |
| 81 | } |
| 82 | $option = get_option( self::get_option_key() ); |
| 83 | $this->notices = is_array( $option ) ? $option : array(); |
| 84 | } |
| 85 | |
| 86 | private function save_notices() { |
| 87 | update_option( self::get_option_key(), $this->notices ); |
| 88 | } |
| 89 | |
| 90 | private function load() { |
| 91 | $this->load_notices(); |
| 92 | $this->load_dismissals(); |
| 93 | } |
| 94 | |
| 95 | private function load_dismissals() { |
| 96 | if ( is_array( $this->dismissals ) ) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | $meta = get_user_meta( |
| 101 | $this->get_user_id(), |
| 102 | $this->get_user_meta_key(), |
| 103 | true |
| 104 | ); |
| 105 | |
| 106 | $this->dismissals = is_array( $meta ) ? $meta : array(); |
| 107 | } |
| 108 | |
| 109 | private function save_dismissals() { |
| 110 | update_user_meta( |
| 111 | $this->get_user_id(), |
| 112 | $this->get_user_meta_key(), |
| 113 | $this->dismissals |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | private function show_stored() { |
| 118 | $this->load(); |
| 119 | foreach ( $this->notices as $name => $message ) { |
| 120 | if ( empty( $this->dismissals[ $name ] ) ) { |
| 121 | $this->show( $name, $message ); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | public function show_notices() { |
| 127 | $this->incompatible_plugins_notice(); |
| 128 | $this->outdated_platform_notice(); |
| 129 | $this->feedback_notice(); |
| 130 | } |
| 131 | |
| 132 | public function add( $name, $message ) { |
| 133 | $this->load_notices(); |
| 134 | $this->notices[ $name ] = $message; |
| 135 | $this->save_notices(); |
| 136 | } |
| 137 | |
| 138 | public function remove( $name ) { |
| 139 | $this->load(); |
| 140 | if ( isset( $this->notices[ $name ] ) ) { |
| 141 | unset( $this->notices[ $name ] ); |
| 142 | $this->save_notices(); |
| 143 | } |
| 144 | if ( isset( $this->dismissals[ $name ] ) ) { |
| 145 | unset( $this->dismissals[ $name ] ); |
| 146 | $this->save_dismissals(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | public function dismiss() { |
| 151 | if ( empty( $_POST['name'] ) || ! $this->check_ajax_referer() ) { |
| 152 | echo json_encode( false ); |
| 153 | exit(); |
| 154 | } |
| 155 | $this->load_dismissals(); |
| 156 | $this->dismissals[ $_POST['name'] ] = true; |
| 157 | $this->save_dismissals(); |
| 158 | echo json_encode( true ); |
| 159 | exit(); |
| 160 | } |
| 161 | |
| 162 | public function show( $name, $message, $klass = 'error', $dismissible = true ) { |
| 163 | $css = array( $klass, 'notice', 'tiny-notice' ); |
| 164 | if ( ! $dismissible ) { |
| 165 | $add = '</p>'; |
| 166 | } elseif ( self::check_wp_version( 4.2 ) ) { |
| 167 | $add = '</p>'; |
| 168 | $css[] = 'is-dismissible'; |
| 169 | } else { |
| 170 | $add = ' <a href="#" class="tiny-dismiss">' . |
| 171 | esc_html__( 'Dismiss', 'tiny-compress-images' ) . '</a></p>'; |
| 172 | } |
| 173 | |
| 174 | $css = implode( ' ', $css ); |
| 175 | $plugin_name = esc_html__( |
| 176 | 'TinyPNG - JPEG, PNG & WebP image compression', |
| 177 | 'tiny-compress-images' |
| 178 | ); |
| 179 | |
| 180 | add_action( |
| 181 | 'admin_notices', |
| 182 | function () use ( $css, $name, $plugin_name, $message, $add ) { |
| 183 | echo '<div class="' . $css . '" data-name="' . $name . '"><p>' . |
| 184 | $plugin_name . ': ' . $message . $add . '</div>'; |
| 185 | } |
| 186 | ); |
| 187 | } |
| 188 | |
| 189 | public function api_key_missing_notice() { |
| 190 | $notice_class = 'error'; |
| 191 | $notice = esc_html__( |
| 192 | 'Please register or provide an API key to start compressing images.', |
| 193 | 'tiny-compress-images' |
| 194 | ); |
| 195 | $link = sprintf( |
| 196 | '<a href="options-general.php?page=tinify">%s</a>', |
| 197 | $notice |
| 198 | ); |
| 199 | $this->show( 'setting', $link, $notice_class, false ); |
| 200 | } |
| 201 | |
| 202 | public function get_api_key_pending_notice() { |
| 203 | $notice_class = 'notice-warning'; |
| 204 | $notice = esc_html__( |
| 205 | 'Please activate your account to start compressing images.', |
| 206 | 'tiny-compress-images' |
| 207 | ); |
| 208 | $link = sprintf( |
| 209 | '<a href="options-general.php?page=tinify">%s</a>', |
| 210 | $notice |
| 211 | ); |
| 212 | $this->show( 'setting', $link, $notice_class, false ); |
| 213 | } |
| 214 | |
| 215 | public function add_limit_reached_notice( $email ) { |
| 216 | $encoded_email = str_replace( '%20', '%2B', rawurlencode( $email ) ); |
| 217 | $url = 'https://tinypng.com/dashboard/api?type=upgrade&mail=' . $encoded_email; |
| 218 | $link = '<a href="' . $url . '" target="_blank">' . |
| 219 | esc_html__( 'TinyPNG API account', 'tiny-compress-images' ) . '</a>'; |
| 220 | |
| 221 | $this->add( |
| 222 | 'limit-reached', |
| 223 | esc_html__( |
| 224 | 'You have reached your free limit this month.', |
| 225 | 'tiny-compress-images' |
| 226 | ) . ' ' . |
| 227 | sprintf( |
| 228 | /* translators: %s: link saying TinyPNG API account */ |
| 229 | esc_html__( |
| 230 | 'Upgrade your %s if you like to compress more images.', |
| 231 | 'tiny-compress-images' |
| 232 | ), |
| 233 | $link |
| 234 | ) |
| 235 | ); |
| 236 | } |
| 237 | |
| 238 | public function outdated_platform_notice() { |
| 239 | if ( ! Tiny_PHP::client_supported() ) { |
| 240 | if ( ! Tiny_PHP::has_fully_supported_php() ) { |
| 241 | $details = 'PHP ' . PHP_VERSION; |
| 242 | if ( Tiny_PHP::curl_available() ) { |
| 243 | $curlinfo = curl_version(); |
| 244 | $details .= ' ' . sprintf( |
| 245 | /* translators: %s: curl version */ |
| 246 | esc_html__( 'with curl %s', 'tiny-compress-images' ), |
| 247 | $curlinfo['version'] |
| 248 | ); |
| 249 | } else { |
| 250 | $details .= ' ' . esc_html__( 'without curl', 'tiny-compress-images' ); |
| 251 | } |
| 252 | if ( Tiny_PHP::curl_exec_disabled() ) { |
| 253 | $details .= ' ' . |
| 254 | esc_html__( 'and curl_exec disabled', 'tiny-compress-images' ); |
| 255 | } |
| 256 | $message = sprintf( |
| 257 | /* translators: %s: details of outdated platform */ |
| 258 | esc_html__( |
| 259 | 'You are using an outdated platform (%s).', |
| 260 | 'tiny-compress-images' |
| 261 | ), |
| 262 | $details |
| 263 | ); |
| 264 | } elseif ( ! Tiny_PHP::curl_available() ) { |
| 265 | $message = esc_html__( |
| 266 | // phpcs:ignore Generic.Files.LineLength |
| 267 | 'We noticed that cURL is not available. For the best experience we recommend to make sure cURL is available.', |
| 268 | 'tiny-compress-images' |
| 269 | ); |
| 270 | } elseif ( Tiny_PHP::curl_exec_disabled() ) { |
| 271 | $message = esc_html__( |
| 272 | // phpcs:ignore Generic.Files.LineLength |
| 273 | 'We noticed that curl_exec is disabled in your PHP configuration. Please update this setting for the best experience.', |
| 274 | 'tiny-compress-images' |
| 275 | ); |
| 276 | } |
| 277 | $this->show( 'deprecated', $message, 'notice-warning', false ); |
| 278 | } // End if(). |
| 279 | } |
| 280 | |
| 281 | public function incompatible_plugins_notice() { |
| 282 | $incompatible_plugins = array_filter( self::$incompatible_plugins, 'is_plugin_active' ); |
| 283 | if ( count( $incompatible_plugins ) > 0 ) { |
| 284 | $this->show_incompatible_plugins( $incompatible_plugins ); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | private function show_incompatible_plugins( $incompatible_plugins ) { |
| 289 | $notice = '<div class="error notice tiny-notice incompatible-plugins">'; |
| 290 | $notice .= '<h3>'; |
| 291 | $notice .= esc_html__( |
| 292 | 'TinyPNG - JPEG, PNG & WebP image compression', |
| 293 | 'tiny-compress-images' |
| 294 | ); |
| 295 | $notice .= '</h3>'; |
| 296 | $notice .= '<p>'; |
| 297 | $notice .= esc_html__( |
| 298 | // phpcs:ignore Generic.Files.LineLength |
| 299 | 'You have activated multiple image optimization plugins. This may lead to unexpected results. The following plugins were detected:', |
| 300 | 'tiny-compress-images' |
| 301 | ); |
| 302 | $notice .= '</p>'; |
| 303 | $notice .= '<table>'; |
| 304 | $notice .= '<tr><td class="bullet">•</td><td class="name">'; |
| 305 | $notice .= esc_html__( |
| 306 | 'TinyPNG - JPEG, PNG & WebP image compression', |
| 307 | 'tiny-compress-images' |
| 308 | ); |
| 309 | $notice .= '</td><td></td></tr>'; |
| 310 | foreach ( $incompatible_plugins as $name => $file ) { |
| 311 | $notice .= '<tr><td class="bullet">•</td><td class="name">'; |
| 312 | $notice .= $name; |
| 313 | $notice .= '</td><td>'; |
| 314 | $nonce = wp_create_nonce( 'deactivate-plugin_' . $file ); |
| 315 | $query_string = 'action=deactivate&plugin=' . $file . '&_wpnonce=' . $nonce; |
| 316 | $url = admin_url( 'plugins.php?' . $query_string ); |
| 317 | $notice .= '<a class="button button-primary" href="' . $url . '">'; |
| 318 | $notice .= esc_html__( 'Deactivate', 'tiny-compress-images' ); |
| 319 | $notice .= '</a></td></tr>'; |
| 320 | } |
| 321 | $notice .= '</table>'; |
| 322 | $notice .= '</div>'; |
| 323 | |
| 324 | add_action( |
| 325 | 'admin_notices', |
| 326 | function () use ( $notice ) { |
| 327 | echo $notice; |
| 328 | } |
| 329 | ); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Checks if the feedback notice should be displayed and hooks it to admin_notices. |
| 334 | * |
| 335 | * @return void |
| 336 | */ |
| 337 | private function feedback_notice() { |
| 338 | if ( ! isset( $this->dismissals['feedback'] ) && |
| 339 | $this->settings->get_compression_count() > $this->compressions_for_feedback |
| 340 | ) { |
| 341 | add_action( 'admin_notices', array( $this, 'feedback_notice_show' ) ); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | public function feedback_notice_show() { |
| 346 | include __DIR__ . '/views/notice-feedback.php'; |
| 347 | } |
| 348 | } |
| 349 |