PluginProbe ʕ •ᴥ•ʔ
TinyPNG – JPEG, PNG & WebP image compression / 3.8.0
TinyPNG – JPEG, PNG & WebP image compression v3.8.0
3.8.0 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-notices.php
tiny-compress-images / src Last commit date
compatibility 2 months ago config 2 months ago css 7 months ago data 4 years ago images 4 years ago js 1 week ago vendor 6 months ago views 1 week ago class-tiny-apache-rewrite.php 2 months ago class-tiny-bulk-optimization.php 2 months ago class-tiny-cli.php 2 months ago class-tiny-compress-client.php 1 week ago class-tiny-compress-fopen.php 2 months ago class-tiny-compress.php 1 week ago class-tiny-conversion.php 4 months ago class-tiny-diagnostics.php 7 months ago class-tiny-exception.php 7 months ago class-tiny-helpers.php 2 months ago class-tiny-image-size.php 1 week ago class-tiny-image.php 1 week ago class-tiny-logger.php 2 months ago class-tiny-migrate.php 2 months ago class-tiny-notices.php 2 months ago class-tiny-php.php 2 months ago class-tiny-picture.php 2 months ago class-tiny-plugin.php 1 week ago class-tiny-settings.php 1 week ago class-tiny-source-base.php 1 week ago class-tiny-source-image.php 7 months ago class-tiny-source-picture.php 7 months ago class-tiny-wp-base.php 2 months ago
class-tiny-notices.php
351 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 ( ! check_ajax_referer( 'tiny-compress', '_nonce', false ) || empty( $_POST['name'] ) ) {
152 echo json_encode( false );
153 exit();
154 }
155 $this->load_dismissals();
156 $notice_name = sanitize_key( wp_unslash( $_POST['name'] ) );
157 $this->dismissals[ $notice_name ] = true;
158 $this->save_dismissals();
159 echo json_encode( true );
160 exit();
161 }
162
163 public function show( $name, $message, $klass = 'error', $dismissible = true ) {
164 $css = array( $klass, 'notice', 'tiny-notice' );
165 if ( ! $dismissible ) {
166 $add = '</p>';
167 } elseif ( self::check_wp_version( 4.2 ) ) {
168 $add = '</p>';
169 $css[] = 'is-dismissible';
170 } else {
171 $add = '&nbsp;<a href="#" class="tiny-dismiss">' .
172 esc_html__( 'Dismiss', 'tiny-compress-images' ) . '</a></p>';
173 }
174
175 $css = implode( ' ', $css );
176 $plugin_name = __(
177 'TinyPNG - JPEG, PNG & WebP image compression',
178 'tiny-compress-images'
179 );
180
181 add_action(
182 'admin_notices',
183 function () use ( $css, $name, $plugin_name, $message, $add ) {
184 echo '<div class="' . esc_attr( $css ) . '" data-name="' .
185 esc_attr( $name ) . '"><p>' . esc_html( $plugin_name ) .
186 ': ' . wp_kses_post( $message ) . wp_kses_post( $add ) . '</div>';
187 }
188 );
189 }
190
191 public function api_key_missing_notice() {
192 $notice_class = 'error';
193 $notice = esc_html__(
194 'Please register or provide an API key to start compressing images.',
195 'tiny-compress-images'
196 );
197 $link = sprintf(
198 '<a href="options-general.php?page=tinify">%s</a>',
199 $notice
200 );
201 $this->show( 'setting', $link, $notice_class, false );
202 }
203
204 public function get_api_key_pending_notice() {
205 $notice_class = 'notice-warning';
206 $notice = esc_html__(
207 'Please activate your account to start compressing images.',
208 'tiny-compress-images'
209 );
210 $link = sprintf(
211 '<a href="options-general.php?page=tinify">%s</a>',
212 $notice
213 );
214 $this->show( 'setting', $link, $notice_class, false );
215 }
216
217 public function add_limit_reached_notice( $email ) {
218 $encoded_email = str_replace( '%20', '%2B', rawurlencode( $email ) );
219 $url = 'https://tinypng.com/dashboard/api?type=upgrade&mail=' . $encoded_email;
220 $link = '<a href="' . $url . '" target="_blank">' .
221 esc_html__( 'TinyPNG API account', 'tiny-compress-images' ) . '</a>';
222
223 $this->add(
224 'limit-reached',
225 esc_html__(
226 'You have reached your free limit this month.',
227 'tiny-compress-images'
228 ) . ' ' .
229 sprintf(
230 /* translators: %s: link saying TinyPNG API account */
231 esc_html__(
232 'Upgrade your %s if you like to compress more images.',
233 'tiny-compress-images'
234 ),
235 $link
236 )
237 );
238 }
239
240 public function outdated_platform_notice() {
241 if ( ! Tiny_PHP::client_supported() ) {
242 if ( ! Tiny_PHP::has_fully_supported_php() ) {
243 $details = 'PHP ' . PHP_VERSION;
244 if ( Tiny_PHP::curl_available() ) {
245 $curlinfo = curl_version();
246 $details .= ' ' . sprintf(
247 /* translators: %s: curl version */
248 esc_html__( 'with curl %s', 'tiny-compress-images' ),
249 $curlinfo['version']
250 );
251 } else {
252 $details .= ' ' . esc_html__( 'without curl', 'tiny-compress-images' );
253 }
254 if ( Tiny_PHP::curl_exec_disabled() ) {
255 $details .= ' ' .
256 esc_html__( 'and curl_exec disabled', 'tiny-compress-images' );
257 }
258 $message = sprintf(
259 /* translators: %s: details of outdated platform */
260 esc_html__(
261 'You are using an outdated platform (%s).',
262 'tiny-compress-images'
263 ),
264 $details
265 );
266 } elseif ( ! Tiny_PHP::curl_available() ) {
267 $message = esc_html__(
268 // phpcs:ignore Generic.Files.LineLength
269 'We noticed that cURL is not available. For the best experience we recommend to make sure cURL is available.',
270 'tiny-compress-images'
271 );
272 } elseif ( Tiny_PHP::curl_exec_disabled() ) {
273 $message = esc_html__(
274 // phpcs:ignore Generic.Files.LineLength
275 'We noticed that curl_exec is disabled in your PHP configuration. Please update this setting for the best experience.',
276 'tiny-compress-images'
277 );
278 }
279 $this->show( 'deprecated', $message, 'notice-warning', false );
280 } // End if().
281 }
282
283 public function incompatible_plugins_notice() {
284 $incompatible_plugins = array_filter( self::$incompatible_plugins, 'is_plugin_active' );
285 if ( count( $incompatible_plugins ) > 0 ) {
286 $this->show_incompatible_plugins( $incompatible_plugins );
287 }
288 }
289
290 private function show_incompatible_plugins( $incompatible_plugins ) {
291 $notice = '<div class="error notice tiny-notice incompatible-plugins">';
292 $notice .= '<h3>';
293 $notice .= esc_html__(
294 'TinyPNG - JPEG, PNG & WebP image compression',
295 'tiny-compress-images'
296 );
297 $notice .= '</h3>';
298 $notice .= '<p>';
299 $notice .= esc_html__(
300 // phpcs:ignore Generic.Files.LineLength
301 'You have activated multiple image optimization plugins. This may lead to unexpected results. The following plugins were detected:',
302 'tiny-compress-images'
303 );
304 $notice .= '</p>';
305 $notice .= '<table>';
306 $notice .= '<tr><td class="bullet">•</td><td class="name">';
307 $notice .= esc_html__(
308 'TinyPNG - JPEG, PNG & WebP image compression',
309 'tiny-compress-images'
310 );
311 $notice .= '</td><td></td></tr>';
312 foreach ( $incompatible_plugins as $name => $file ) {
313 $notice .= '<tr><td class="bullet">•</td><td class="name">';
314 $notice .= $name;
315 $notice .= '</td><td>';
316 $nonce = wp_create_nonce( 'deactivate-plugin_' . $file );
317 $query_string = 'action=deactivate&plugin=' . $file . '&_wpnonce=' . $nonce;
318 $url = admin_url( 'plugins.php?' . $query_string );
319 $notice .= '<a class="button button-primary" href="' . $url . '">';
320 $notice .= esc_html__( 'Deactivate', 'tiny-compress-images' );
321 $notice .= '</a></td></tr>';
322 }
323 $notice .= '</table>';
324 $notice .= '</div>';
325
326 add_action(
327 'admin_notices',
328 function () use ( $notice ) {
329 echo wp_kses_post( $notice );
330 }
331 );
332 }
333
334 /**
335 * Checks if the feedback notice should be displayed and hooks it to admin_notices.
336 *
337 * @return void
338 */
339 private function feedback_notice() {
340 if ( ! isset( $this->dismissals['feedback'] ) &&
341 $this->settings->get_compression_count() > $this->compressions_for_feedback
342 ) {
343 add_action( 'admin_notices', array( $this, 'feedback_notice_show' ) );
344 }
345 }
346
347 public function feedback_notice_show() {
348 include __DIR__ . '/views/notice-feedback.php';
349 }
350 }
351