1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Persist Admin notices Dismissal |
5
|
|
|
* |
6
|
|
|
* Copyright (C) 2016 Collins Agbonghama <http://w3guy.com> |
7
|
|
|
* |
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU General Public License as published by |
10
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
* |
21
|
|
|
* @package Persist Admin notices Dismissal |
22
|
|
|
* @author Collins Agbonghama |
23
|
|
|
* @author Andy Fragen |
24
|
|
|
* @license http://www.gnu.org/licenses GNU General Public License |
25
|
|
|
* @version 1.4.1 |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Exit if called directly. |
30
|
|
|
*/ |
31
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
32
|
|
|
die; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if ( ! class_exists( 'PAnD' ) ) { |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Class PAnD |
39
|
|
|
*/ |
40
|
|
|
class PAnD { |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Init hooks. |
44
|
|
|
*/ |
45
|
|
|
public static function init() { |
46
|
|
|
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'load_script' ) ); |
47
|
|
|
add_action( 'wp_ajax_dismiss_admin_notice', array( __CLASS__, 'dismiss_admin_notice' ) ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Enqueue javascript and variables. |
52
|
|
|
*/ |
53
|
|
|
public static function load_script() { |
54
|
|
|
|
55
|
|
|
if ( is_customize_preview() ) { |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
wp_enqueue_script( |
60
|
|
|
'dismissible-notices', |
61
|
|
|
plugins_url( 'dismiss-notice.js', __FILE__ ), |
62
|
|
|
array( 'jquery', 'common' ), |
63
|
|
|
false, |
64
|
|
|
true |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
wp_localize_script( |
68
|
|
|
'dismissible-notices', |
69
|
|
|
'dismissible_notice', |
70
|
|
|
array( |
71
|
|
|
'nonce' => wp_create_nonce( 'dismissible-notice' ), |
72
|
|
|
) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Handles Ajax request to persist notices dismissal. |
78
|
|
|
* Uses check_ajax_referer to verify nonce. |
79
|
|
|
*/ |
80
|
|
|
public static function dismiss_admin_notice() { |
81
|
|
|
$option_name = sanitize_text_field( $_POST['option_name'] ); |
82
|
|
|
$dismissible_length = sanitize_text_field( $_POST['dismissible_length'] ); |
83
|
|
|
|
84
|
|
|
if ( 'forever' != $dismissible_length ) { |
85
|
|
|
// If $dismissible_length is not an integer default to 1 |
86
|
|
|
$dismissible_length = ( 0 == absint( $dismissible_length ) ) ? 1 : $dismissible_length; |
87
|
|
|
$dismissible_length = strtotime( absint( $dismissible_length ) . ' days' ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
check_ajax_referer( 'dismissible-notice', 'nonce' ); |
91
|
|
|
self::set_admin_notice_cache( $option_name, $dismissible_length ); |
92
|
|
|
wp_die(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Is admin notice active? |
97
|
|
|
* |
98
|
|
|
* @param string $arg data-dismissible content of notice. |
99
|
|
|
* |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
public static function is_admin_notice_active( $arg ) { |
103
|
|
|
$array = explode( '-', $arg ); |
104
|
|
|
$length = array_pop( $array ); |
|
|
|
|
105
|
|
|
$option_name = implode( '-', $array ); |
106
|
|
|
$db_record = self::get_admin_notice_cache( $option_name ); |
107
|
|
|
if ( 'forever' == $db_record ) { |
108
|
|
|
return false; |
109
|
|
|
} elseif ( absint( $db_record ) >= time() ) { |
110
|
|
|
return false; |
111
|
|
|
} else { |
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns admin notice cached timeout. |
118
|
|
|
* |
119
|
|
|
* @access public |
120
|
|
|
* |
121
|
|
|
* @param string|bool $id admin notice name or false. |
122
|
|
|
* |
123
|
|
|
* @return array|bool The timeout. False if expired. |
124
|
|
|
*/ |
125
|
|
|
public static function get_admin_notice_cache( $id = false ) { |
126
|
|
|
if ( ! $id ) { |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
$cache_key = 'pand-' . md5( $id ); |
130
|
|
|
$timeout = get_site_option( $cache_key ); |
131
|
|
|
$timeout = 'forever' === $timeout ? time() + 60 : $timeout; |
132
|
|
|
|
133
|
|
|
if ( empty( $timeout ) || time() > $timeout ) { |
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $timeout; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Sets admin notice timeout in site option. |
142
|
|
|
* |
143
|
|
|
* @access public |
144
|
|
|
* |
145
|
|
|
* @param string $id Data Identifier. |
146
|
|
|
* @param string|bool $timeout Timeout for admin notice. |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public static function set_admin_notice_cache( $id, $timeout ) { |
151
|
|
|
$cache_key = 'pand-' . md5( $id ); |
152
|
|
|
update_site_option( $cache_key, $timeout ); |
153
|
|
|
|
154
|
|
|
return true; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.