forked from w3guy/persist-admin-notices-dismissal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersist-admin-notices-dismissal.php
187 lines (165 loc) · 4.83 KB
/
persist-admin-notices-dismissal.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
<?php
/**
* Persist Admin notices Dismissal
*
* Copyright (C) 2016 Collins Agbonghama <https://w3guy.com>
*
* 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/>.
*
* @package Persist Admin notices Dismissal
* @author Collins Agbonghama, Andy Fragen
* @license http://www.gnu.org/licenses GNU General Public License
*/
/**
* Exit if called directly.
*/
if ( ! defined( 'ABSPATH' ) ) {
die;
}
if ( ! class_exists( 'PAnD' ) ) {
/**
* Class PAnD
*/
class PAnD {
/**
* Init hooks.
*/
public static function init() {
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'load_script' ) );
add_action( 'wp_ajax_dismiss_admin_notice', array( __CLASS__, 'dismiss_admin_notice' ) );
/**
* Filter to activate another filter providing a simpler use case.
*
* @since 1.4.3
*
* @param bool
*/
if ( apply_filters( 'pand_theme_loader', false ) ) {
add_filter(
'pand_dismiss_notice_js_url',
function( $js_url, $composer_path ) {
return get_stylesheet_directory_uri() . $composer_path;
},
10,
2
);
}
}
/**
* Enqueue javascript and variables.
*/
public static function load_script() {
if ( is_customize_preview() ) {
return;
}
$js_url = plugins_url( 'dismiss-notice.js', __FILE__ );
$composer_path = '/vendor/collizo4sky/persist-admin-notices-dismissal/dismiss-notice.js';
/**
* Filter dismiss-notice.js URL.
*
* @since 1.4.3
*
* @param string $js_url URL to the Javascript file.
* @param string $composer_path Relative path of Javascript file from composer install.
*/
$js_url = apply_filters( 'pand_dismiss_notice_js_url', $js_url, $composer_path );
wp_enqueue_script(
'dismissible-notices',
$js_url,
array( 'jquery', 'common' ),
'1.4.5',
true
);
wp_localize_script(
'dismissible-notices',
'dismissible_notice',
array(
'nonce' => wp_create_nonce( 'dismissible-notice' ),
)
);
}
/**
* Handles Ajax request to persist notices dismissal.
* Uses check_ajax_referer to verify nonce.
*/
public static function dismiss_admin_notice() {
$option_name = isset( $_POST['option_name'] ) ? sanitize_text_field( wp_unslash( $_POST['option_name'] ) ) : '';
$dismissible_length = isset( $_POST['dismissible_length'] ) ? sanitize_text_field( wp_unslash( $_POST['dismissible_length'] ) ) : 0;
if ( 'forever' !== $dismissible_length ) {
// If $dismissible_length is not an integer default to 1.
$dismissible_length = ( 0 === absint( $dismissible_length ) ) ? 1 : $dismissible_length;
$dismissible_length = strtotime( absint( $dismissible_length ) . ' days' );
}
check_ajax_referer( 'dismissible-notice', 'nonce' );
self::set_admin_notice_cache( $option_name, $dismissible_length );
wp_die();
}
/**
* Is admin notice active?
*
* @param string $arg data-dismissible content of notice.
*
* @return bool
*/
public static function is_admin_notice_active( $arg ) {
$array = explode( '-', $arg );
$length = array_pop( $array );
$option_name = implode( '-', $array );
$db_record = self::get_admin_notice_cache( $option_name );
if ( 'forever' === $db_record ) {
return false;
} elseif ( absint( $db_record ) >= time() ) {
return false;
} else {
return true;
}
}
/**
* Returns admin notice cached timeout.
*
* @access public
*
* @param string|bool $id admin notice name or false.
*
* @return array|bool The timeout. False if expired.
*/
public static function get_admin_notice_cache( $id = false ) {
if ( ! $id ) {
return false;
}
$cache_key = 'pand-' . md5( $id );
$timeout = get_site_option( $cache_key );
$timeout = 'forever' === $timeout ? time() + 60 : $timeout;
if ( empty( $timeout ) || time() > $timeout ) {
return false;
}
return $timeout;
}
/**
* Sets admin notice timeout in site option.
*
* @access public
*
* @param string $id Data Identifier.
* @param string|bool $timeout Timeout for admin notice.
*
* @return bool
*/
public static function set_admin_notice_cache( $id, $timeout ) {
$cache_key = 'pand-' . md5( $id );
update_site_option( $cache_key, $timeout );
return true;
}
}
}