-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathclass-tiny-notices.php
More file actions
303 lines (272 loc) · 9.55 KB
/
class-tiny-notices.php
File metadata and controls
303 lines (272 loc) · 9.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
/*
* Tiny Compress Images - WordPress plugin.
* Copyright (C) 2015-2018 Tinify B.V.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
class Tiny_Notices extends Tiny_WP_Base {
private $notices;
private $dismissals;
protected static $incompatible_plugins = array(
'CheetahO Image Optimizer' => 'cheetaho-image-optimizer/cheetaho.php',
'EWWW Image Optimizer' => 'ewww-image-optimizer/ewww-image-optimizer.php',
'Imagify' => 'imagify/imagify.php',
'Kraken Image Optimizer' => 'kraken-image-optimizer/kraken.php',
'ShortPixel Image Optimizer' => 'shortpixel-image-optimiser/wp-shortpixel.php',
'WP Smush' => 'wp-smushit/wp-smush.php',
'WP Smush Pro' => 'wp-smush-pro/wp-smush.php',
);
private static function get_option_key() {
return self::get_prefixed_name( 'admin_notices' );
}
private static function get_user_meta_key() {
return self::get_prefixed_name( 'admin_notice_dismissals' );
}
public function ajax_init() {
add_action( 'wp_ajax_tiny_dismiss_notice', $this->get_method( 'dismiss' ) );
}
public function admin_init() {
if ( current_user_can( 'manage_options' ) ) {
$this->show_stored();
$this->show_notices();
}
}
private function load_notices() {
if ( is_array( $this->notices ) ) {
return;
}
$option = get_option( self::get_option_key() );
$this->notices = is_array( $option ) ? $option : array();
}
private function save_notices() {
update_option( self::get_option_key(), $this->notices );
}
private function load() {
$this->load_notices();
$this->load_dismissals();
}
private function load_dismissals() {
if ( is_array( $this->dismissals ) ) {
return;
}
$meta = get_user_meta(
$this->get_user_id(),
$this->get_user_meta_key(),
true
);
$this->dismissals = is_array( $meta ) ? $meta : array();
}
private function save_dismissals() {
update_user_meta(
$this->get_user_id(),
$this->get_user_meta_key(),
$this->dismissals
);
}
private function show_stored() {
$this->load();
foreach ( $this->notices as $name => $message ) {
if ( empty( $this->dismissals[ $name ] ) ) {
$this->show( $name, $message );
}
}
}
public function show_notices() {
$this->incompatible_plugins_notice();
$this->outdated_platform_notice();
}
public function add( $name, $message ) {
$this->load_notices();
$this->notices[ $name ] = $message;
$this->save_notices();
}
public function remove( $name ) {
$this->load();
if ( isset( $this->notices[ $name ] ) ) {
unset( $this->notices[ $name ] );
$this->save_notices();
}
if ( isset( $this->dismissals[ $name ] ) ) {
unset( $this->dismissals[ $name ] );
$this->save_dismissals();
}
}
public function dismiss() {
if ( empty( $_POST['name'] ) || ! $this->check_ajax_referer() ) {
echo json_encode( false );
exit();
}
$this->load_dismissals();
$this->dismissals[ $_POST['name'] ] = true;
$this->save_dismissals();
echo json_encode( true );
exit();
}
public function show( $name, $message, $klass = 'error', $dismissible = true ) {
$css = array( $klass, 'notice', 'tiny-notice' );
if ( ! $dismissible ) {
$add = '</p>';
} elseif ( self::check_wp_version( 4.2 ) ) {
$add = '</p>';
$css[] = 'is-dismissible';
} else {
$add = ' <a href="#" class="tiny-dismiss">' .
esc_html__( 'Dismiss', 'tiny-compress-images' ) . '</a></p>';
}
$css = implode( ' ', $css );
$plugin_name = esc_html__( 'TinyPNG - JPEG, PNG & WebP image compression', 'tiny-compress-images' );
add_action( 'admin_notices',
function() use ( $css, $name, $plugin_name, $message, $add ) {
echo '<div class="' . $css . '" data-name="' . $name . '"><p>' .
$plugin_name . ': ' . $message . $add . '</div>';
}
);
}
public function api_key_missing_notice() {
$notice_class = 'error';
$notice = esc_html__(
'Please register or provide an API key to start compressing images.',
'tiny-compress-images'
);
$link = sprintf(
'<a href="options-general.php?page=tinify">%s</a>', $notice
);
$this->show( 'setting', $link, $notice_class, false );
}
public function get_api_key_pending_notice() {
$notice_class = 'notice-warning';
$notice = esc_html__(
'Please activate your account to start compressing images.',
'tiny-compress-images'
);
$link = sprintf(
'<a href="options-general.php?page=tinify">%s</a>', $notice
);
$this->show( 'setting', $link, $notice_class, false );
}
public function add_limit_reached_notice( $email ) {
$encoded_email = str_replace( '%20', '%2B', rawurlencode( $email ) );
$url = 'https://tinypng.com/dashboard/api?type=upgrade&mail=' . $encoded_email . '&utm_source=installation&utm_medium=wordpress-plugin';
$link = '<a href="' . $url . '" target="_blank">' .
esc_html__( 'TinyPNG API account', 'tiny-compress-images' ) . '</a>';
$this->add('limit-reached',
esc_html__(
'You have reached your free limit this month.',
'tiny-compress-images'
) . ' ' .
sprintf(
/* translators: %s: link saying TinyPNG API account */
esc_html__(
'Upgrade your %s if you like to compress more images.',
'tiny-compress-images'
),
$link
)
);
}
public function outdated_platform_notice() {
if ( ! Tiny_PHP::client_supported() ) {
if ( ! Tiny_PHP::has_fully_supported_php() ) {
$details = 'PHP ' . PHP_VERSION;
if ( Tiny_PHP::curl_available() ) {
$curlinfo = curl_version();
$details .= ' ' . sprintf(
/* translators: %s: curl version */
esc_html__( 'with curl %s', 'tiny-compress-images' ), $curlinfo['version']
);
} else {
$details .= ' ' . esc_html__( 'without curl', 'tiny-compress-images' );
}
if ( Tiny_PHP::curl_exec_disabled() ) {
$details .= ' ' .
esc_html__( 'and curl_exec disabled', 'tiny-compress-images' );
}
$message = sprintf(
/* translators: %s: details of outdated platform */
esc_html__(
'You are using an outdated platform (%s).',
'tiny-compress-images'
), $details
);
} elseif ( ! Tiny_PHP::curl_available() ) {
$message = esc_html__(
'We noticed that cURL is not available. For the best experience we recommend to make sure cURL is available.', // WPCS: Needed for proper translation.
'tiny-compress-images'
);
} elseif ( Tiny_PHP::curl_exec_disabled() ) {
$message = esc_html__(
'We noticed that curl_exec is disabled in your PHP configuration. Please update this setting for the best experience.', // WPCS: Needed for proper translation.
'tiny-compress-images'
);
}
$this->show( 'deprecated', $message, 'notice-warning', false );
} // End if().
}
public function show_offload_s3_notice() {
$message = esc_html__(
'Removing files from the server is incompatible with background compressions. Images will still be automatically compressed, but no longer in the background.', // WPCS: Needed for proper translation.
'tiny-compress-images'
);
$this->show( 'offload-s3', $message, 'notice-error', false );
}
public function old_offload_s3_version_notice() {
$message = esc_html__(
'Background compressions are not compatible with the version of WP Offload S3 you have installed. Please update to version 0.7.2 at least.', // WPCS: Needed for proper translation.
'tiny-compress-images'
);
$this->show( 'old-offload-s3-version', $message, 'notice-error', false );
}
public function incompatible_plugins_notice() {
$incompatible_plugins = array_filter( self::$incompatible_plugins, 'is_plugin_active' );
if ( count( $incompatible_plugins ) > 0 ) {
$this->show_incompatible_plugins( $incompatible_plugins );
}
}
private function show_incompatible_plugins( $incompatible_plugins ) {
$notice = '<div class="error notice tiny-notice incompatible-plugins">';
$notice .= '<h3>';
$notice .= esc_html__( 'TinyPNG - JPEG, PNG & WebP image compression', 'tiny-compress-images' );
$notice .= '</h3>';
$notice .= '<p>';
$notice .= esc_html__(
'You have activated multiple image optimization plugins. This may lead to unexpected results. The following plugins were detected:', // WPCS: Needed for proper translation.
'tiny-compress-images'
);
$notice .= '</p>';
$notice .= '<table>';
$notice .= '<tr><td class="bullet">•</td><td class="name">';
$notice .= esc_html__( 'TinyPNG - JPEG, PNG & WebP image compression', 'tiny-compress-images' );
$notice .= '</td><td></td></tr>';
foreach ( $incompatible_plugins as $name => $file ) {
$notice .= '<tr><td class="bullet">•</td><td class="name">';
$notice .= $name;
$notice .= '</td><td>';
$nonce = wp_create_nonce( 'deactivate-plugin_' . $file );
$query_string = 'action=deactivate&plugin=' . $file . '&_wpnonce=' . $nonce;
$url = admin_url( 'plugins.php?' . $query_string );
$notice .= '<a class="button button-primary" href="' . $url . '">';
$notice .= esc_html__( 'Deactivate' );
$notice .= '</a></td></tr>';
}
$notice .= '</table>';
$notice .= '</div>';
add_action( 'admin_notices',
function() use ( $notice ) {
echo $notice;
}
);
}
}