|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Persist Admin notices Dismissal |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (C) 2016 Agbonghama Collins <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 Agbonghama Collins |
|
23
|
|
|
* @author Andy Fragen |
|
24
|
|
|
* @license http://www.gnu.org/licenses GNU General Public License |
|
25
|
|
|
* @version 1.3.2 |
|
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()) return; |
|
56
|
|
|
|
|
57
|
|
|
wp_enqueue_script( |
|
58
|
|
|
'dismissible-notices', |
|
59
|
|
|
plugins_url( 'dismiss-notice.js', __FILE__ ), |
|
60
|
|
|
array( 'jquery', 'common' ), |
|
61
|
|
|
false, |
|
62
|
|
|
true |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
wp_localize_script( |
|
66
|
|
|
'dismissible-notices', |
|
67
|
|
|
'dismissible_notice', |
|
68
|
|
|
array( |
|
69
|
|
|
'nonce' => wp_create_nonce( 'dismissible-notice' ), |
|
70
|
|
|
) |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Handles Ajax request to persist notices dismissal. |
|
76
|
|
|
* Uses check_ajax_referer to verify nonce. |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function dismiss_admin_notice() { |
|
79
|
|
|
$option_name = sanitize_text_field( $_POST['option_name'] ); |
|
80
|
|
|
$dismissible_length = sanitize_text_field( $_POST['dismissible_length'] ); |
|
81
|
|
|
$transient = 0; |
|
82
|
|
|
|
|
83
|
|
|
if ( 'forever' != $dismissible_length ) { |
|
84
|
|
|
// If $dismissible_length is not an integer default to 1 |
|
85
|
|
|
$dismissible_length = ( 0 == absint( $dismissible_length ) ) ? 1 : $dismissible_length; |
|
86
|
|
|
$transient = absint( $dismissible_length ) * DAY_IN_SECONDS; |
|
87
|
|
|
$dismissible_length = strtotime( absint( $dismissible_length ) . ' days' ); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
check_ajax_referer( 'dismissible-notice', 'nonce' ); |
|
91
|
|
|
set_site_transient( $option_name, $dismissible_length, $transient ); |
|
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 = get_site_transient( $option_name ); |
|
107
|
|
|
|
|
108
|
|
|
if ( 'forever' == $db_record ) { |
|
109
|
|
|
return false; |
|
110
|
|
|
} elseif ( absint( $db_record ) >= time() ) { |
|
111
|
|
|
return false; |
|
112
|
|
|
} else { |
|
113
|
|
|
return true; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.