forked from litespeedtech/wp-dashboard-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dash-notifier.php
executable file
·448 lines (378 loc) · 10.7 KB
/
dash-notifier.php
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<?php
/**
* Plugin Name: Dash Notifier
* Plugin URI: https://github.com/litespeedtech/wp-dashboard-notifier
* Description: WordPress dashboard notifier
* Version: 1.2
* Author: LiteSpeed Technologies
* License: GPLv3
* License URI: http://www.gnu.org/licenses/gpl.html
* Text Domain: dash-notifier
*
* Copyright (C) 2015-2017 LiteSpeed Technologies, Inc.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
defined( 'WPINC' ) || exit ;
// define( 'DASH_NOTIFIER_MSG', json_encode( array( 'msg' => 'This is a message from your hosting provider. We have recently increased the server speed by installing LiteSpeed Web Server with the LSCache module. We recommend installing the LiteSpeed Cache plugin. This plugin includes a nice collection of optimization features, and also works with the server-side cache module to maximize your WordPress performance.', 'plugin' => 'litespeed-cache' ) ) ) ;
if ( defined( 'DASH_NOTIFIER_V' ) ) {
return ;
}
define( 'DASH_NOTIFIER_V', '1.2' ) ;
// Storage hook
add_action( 'setup_theme', 'dash_notifier_save_msg' ) ;
if ( defined( 'SHORTINIT' ) ) {
dash_notifier_save_msg() ;
}
// Display hook
add_action( 'admin_print_styles', 'dash_notifier_new_msg' ) ;
// Dismiss/install/uninstall hook
add_action( 'admin_init', 'dash_notifier_admin_init' ) ;
/**
* Admin init actions
*
* @since 1.0
*/
function dash_notifier_admin_init()
{
// Dismiss hook
if ( empty( $_GET[ 'dash_notifier_action' ] ) || empty( $_GET[ 'nonce' ] ) ) {
return ;
}
if ( ! wp_verify_nonce( $_GET[ 'nonce' ], $_GET[ 'dash_notifier_action' ] ) ) {
return ;
}
if ( ! dash_notifier_can_operate() ) {
return ;
}
switch ( $_GET[ 'dash_notifier_action' ] ) {
case 'uninstall':
dash_notifier_uninstall() ;
break;
case 'activate':
dash_notifier_install_3rd() ;
break;
case 'dismiss':
default:
dash_notifier_dismiss() ;
break;
}
wp_redirect( 'index.php' ) ;
exit ;
}
/**
* If current user can operate notifier related options
*
* @since 1.0
*/
function dash_notifier_can_operate()
{
if ( ! current_user_can( 'manage_options' ) ) {
return false ;
}
return true ;
}
/**
* Detect if the plugin is active or not
*
* @since 1.0
*/
function dash_notifier_is_plugin_active( $plugin )
{
include_once( ABSPATH . 'wp-admin/includes/plugin.php' ) ;
$plugin_path = $plugin . '/' . $plugin . '.php' ;
return is_plugin_active( $plugin_path ) ;
}
/**
* Detect if the plugin is installed or not
*
* @since 1.0
*/
function dash_notifier_is_plugin_installed( $plugin )
{
include_once( ABSPATH . 'wp-admin/includes/plugin.php' ) ;
$plugin_path = $plugin . '/' . $plugin . '.php' ;
$valid = validate_plugin( $plugin_path ) ;
return ! is_wp_error( $valid ) ;
}
/**
* Grab a plugin info from WordPress
*
* @since 1.0
*/
function dash_notifier_get_plugin_info( $slug )
{
include_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ) ;
$result = plugins_api( 'plugin_information', array( 'slug' => $slug ) ) ;
if ( is_wp_error( $result ) ) {
return false ;
}
return $result ;
}
/**
* Uninstall dash notifier
*
* From developer of dash notifier: We miss you though!
*
* @since 1.0
*/
function dash_notifier_uninstall()
{
include_once( ABSPATH . 'wp-admin/includes/plugin.php' ) ;
file_put_contents( ABSPATH . '.dash_notifier_bypass', date( 'Y-m-d H:i:s' ) ) ;
delete_option( 'dash_notifier.msg' ) ;
dash_notifier_is_plugin_active( 'dash-notifier' ) && deactivate_plugins( 'dash-notifier/dash-notifier.php' ) ;
delete_plugins( array( 'dash-notifier/dash-notifier.php' ) ) ;
dash_notifier_version_check( 'uninstall' ) ;
}
/**
* Install the 3rd party plugin
*
* @since 1.0
*/
function dash_notifier_install_3rd()
{
$msg = dash_notifier_get_msg() ;
if ( empty( $msg[ 'plugin' ] ) ) {
return ;
}
// Check if plugin is installed already
if ( dash_notifier_is_plugin_active( $msg[ 'plugin' ] ) ) {
return ;
}
/**
* @see wp-admin/update.php
*/
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ;
include_once ABSPATH . 'wp-admin/includes/file.php' ;
include_once ABSPATH . 'wp-admin/includes/misc.php' ;
$plugin_path = $msg[ 'plugin' ] . '/' . $msg[ 'plugin' ] . '.php' ;
if ( ! dash_notifier_is_plugin_installed( $msg[ 'plugin' ] ) ) {
$plugin_info = dash_notifier_get_plugin_info( $msg[ 'plugin' ] ) ;
if ( ! $plugin_info ) {
return ;
}
// Try to install plugin
try {
ob_start() ;
$skin = new \Automatic_Upgrader_Skin() ;
$upgrader = new \Plugin_Upgrader( $skin ) ;
$result = $upgrader->install( $plugin_info->download_link ) ;
ob_end_clean() ;
} catch ( \Exception $e ) {
return ;
}
}
if ( ! is_plugin_active( $plugin_path ) ) {
activate_plugin( $plugin_path ) ;
}
}
/**
* Dash notifier native installation process
*
* @since 1.2
*/
function dash_notifier_hook_activate()
{
dash_notifier_version_check( 'install_' . ( defined( 'DASH_NOTIFIER_REF' ) ? DASH_NOTIFIER_REF : '' ), $msg[ 'plugin' ] ) ;
}
/**
* Dash notifier native uninstallation process
*
* @since 1.2
*/
function dash_notifier_hook_uninstall()
{
dash_notifier_version_check( 'uninstall_' . ( defined( 'DASH_NOTIFIER_REF' ) ? DASH_NOTIFIER_REF : '' ) ) ;
}
/**
* Check latest version
*
* @since 1.2
*/
function dash_notifier_version_check( $src = false, $plugin = false )
{
// Check latest stable version allowed to upgrade
$url = 'https://wp.api.litespeedtech.com/latest_v.dash_notifier?v=' . DASH_NOTIFIER_V . '&plugin=' . $plugin . '&src=' . $src ;
if ( defined( 'DASH_NOTIFIER_ERR' ) ) {
$url .= '&err=' . base64_encode( ! is_string( DASH_NOTIFIER_ERR ) ? json_encode( DASH_NOTIFIER_ERR ) : DASH_NOTIFIER_ERR ) ;
}
$response = wp_remote_get( $url, array( 'timeout' => 15 ) ) ;
if ( ! is_array( $response ) || empty( $response[ 'body' ] ) ) {
return false ;
}
return $response[ 'body' ] ;
}
/**
* Receive and store dashboard msg
*
* @since 1.0
*/
function dash_notifier_save_msg()
{
if ( ! defined( 'DASH_NOTIFIER_MSG' ) ) {
return ;
}
$msg = json_decode( DASH_NOTIFIER_MSG, true ) ;
if ( ! $msg || empty( $msg[ 'msg' ] ) ) {
return ;
}
$existing_msg = dash_notifier_get_msg() ;
$plugin = $plugin_name = '' ;
if ( ! empty( $msg[ 'plugin' ] ) ) {
$plugin = $msg[ 'plugin' ] ;
if ( ! empty( $msg[ 'plugin_name' ] ) ) {
$plugin_name = $msg[ 'plugin_name' ] ;
}
// Query plugin name
else {
$data = dash_notifier_get_plugin_info( $plugin ) ;
if ( ! $data ) {
return ;
}
$plugin_name = $data->name ;
}
}
// Append msg
$existing_msg[ 'msg' ] = $msg[ 'msg' ] ;
$existing_msg[ 'msg_md5' ] = md5( $msg[ 'msg' ] ) ;
$existing_msg[ 'plugin' ] = $plugin ;
$existing_msg[ 'plugin_name' ] = $plugin_name ;
update_option( 'dash_notifier.msg', $existing_msg ) ;
}
/**
* Read current msg
*
* @since 1.0
*/
function dash_notifier_get_msg()
{
$existing_msg = get_option( 'dash_notifier.msg', array() ) ;
if ( ! is_array( $existing_msg ) ) {
$existing_msg = array(
'msg' => '',
'msg_md5' => '',
'msg_md5_previous' => '',
) ;
}
return $existing_msg ;
}
/**
* Check if can print dashboard message or not
*
* @since 1.0
*/
function dash_notifier_new_msg()
{
$screen = get_current_screen() ;
$screen = $screen ? $screen->id : false ;
if ( $screen != 'dashboard' ) {
return ;
}
if ( ! dash_notifier_can_operate() ) {
return ;
}
$msg = dash_notifier_get_msg() ;
if ( ! $msg || empty( $msg[ 'msg' ] ) ) {
return ;
}
if ( ! empty( $msg[ 'msg_md5_previous' ] ) && $msg[ 'msg_md5' ] == $msg[ 'msg_md5_previous' ] ) {
return;
}
if ( ! empty( $msg[ 'plugin' ] ) ) {
// Check if plugin is installed already
if ( dash_notifier_is_plugin_active( $msg[ 'plugin' ] ) ) {
return ;
}
}
add_action( 'admin_notices', 'dash_notifier_show_msg' ) ;
}
/**
* Print dashboard message
*
* @since 1.0
*/
function dash_notifier_show_msg()
{
$msg = dash_notifier_get_msg() ;
$dismiss_txt = __( 'Dismiss' ) ;
$install_link = '' ;
if ( ! empty( $msg[ 'plugin' ] ) && ! empty( $msg[ 'plugin_name' ] ) ) {
// If plugin installed, no need to show msg
if ( dash_notifier_is_plugin_active( $msg[ 'plugin' ] ) ) {
return ;
}
// Check if plugin is installed but not activated
if ( dash_notifier_is_plugin_installed( $msg[ 'plugin' ] ) ) {
$install_link = '<a href="?dash_notifier_action=activate&nonce=' . wp_create_nonce( 'activate' ) . '" class="install-now button button-primary button-small">' . sprintf( _x( 'Activate %s', 'plugin' ), $msg[ 'plugin_name' ] ) . '</a>' ;
}
else {
$install_link = '<a href="?dash_notifier_action=activate&nonce=' . wp_create_nonce( 'activate' ) . '" class="install-now button button-primary button-small">' . sprintf( __( 'Install %s now' ), $msg[ 'plugin_name' ] ) . '</a>' ;
}
}
$dont_show_link = '<a href="?dash_notifier_action=uninstall&nonce=' . wp_create_nonce( 'uninstall' ) . '" class="button button-small dash-notifier-uninstall">' . __( 'Never Notify Me Again', 'dash-notifier' ) . '</a>' ;
$nonce_dismiss = wp_create_nonce( 'dismiss' ) ;
$msg_con = $msg[ 'msg' ] ;
echo <<<eot
<style>
div.dash-notifier-msg {
overflow: hidden;
position: relative;
border-left-color: #000099!important;
}
a.dash-notifier-close {
position: static;
float: right;
top: 0;
right: 0;
padding: 0 15px 10px 28px;
margin-top: -10px;
font-size: 13px;
line-height: 1.23076923;
text-decoration: none;
}
a.dash-notifier-close:before {
position: relative;
top: 18px;
left: -20px;
-webkit-transition: all .1s ease-in-out;
transition: all .1s ease-in-out;
}
a.button.dash-notifier-uninstall {
margin-left:auto;
margin-top:auto;
}
</style>
<div class="updated dash-notifier-msg">
<a class="dash-notifier-close notice-dismiss" href="?dash_notifier_action=dismiss&nonce=$nonce_dismiss">$dismiss_txt</a>
<p>$msg_con</p>
<p style='display:flex;'>
$install_link
$dont_show_link
</p>
</div>
eot;
}
/**
* Dismiss current dashboard message
*
* @since 1.0
*/
function dash_notifier_dismiss()
{
$msg = dash_notifier_get_msg() ;
$msg[ 'msg_md5_previous' ] = $msg[ 'msg_md5' ] ;
delete_option( 'dash_notifier.msg' ) ;
update_option( 'dash_notifier.msg', $msg ) ;
}