-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathwpsc-admin-notifications.class.php
More file actions
174 lines (144 loc) · 4.21 KB
/
wpsc-admin-notifications.class.php
File metadata and controls
174 lines (144 loc) · 4.21 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
<?php
/**
* Show WPeC admin notifications, a.k.a. Nags
*
* an admin notifications class is defined here in core includes so that notifications
* can be queued by logic running on the user facing side that will be visisble to
* store administrators when they are on the admin pages
*
* @access public
*
* @since 3.8.14.1
*
*/
class WPSC_Admin_Notifications {
static $instance = null;
function __construct() {
if ( ! self::$instance ) {
self::$instance = $this;
if ( is_admin() ) {
add_action( 'admin_notices', array( $this, 'show_messages' ) );
add_action( 'wp_ajax_wpsc_dismiss_admin_msg', array( $this, 'dismiss_msg' ) );
}
}
}
/**
* add one or more new messages to be shown to the administrator
* @param string|array[string] $new_messages to show to the admin
*/
function new_message( $new_messages ) {
if ( empty ( $new_messages ) )
return;
if ( is_string( $new_messages ) ) {
$new_messages = array( $new_messages );
}
$messages = get_option( __CLASS__ , array() );
$save_admin_messages = false;
foreach ( $new_messages as $new_message ) {
// using the hash key is an easy way of preventing duplicate messages
$id = md5( $new_message );
if ( ! isset($messages[$id] ) ) {
$messages[$id] = $new_message;
$save_admin_messages = true;
}
}
// only save the admin messages if they have been updated
if ( $save_admin_messages ) {
update_option( __CLASS__ , $messages );
}
if ( did_action( 'admin_notices' ) ) {
$wpsc_admin_notifications = new WPSC_Admin_Notifications();
$wpsc_admin_notifications->show_messages();
}
}
/**
* display admin messages(nags)
*
* @since 3.8.14.1
*/
function show_messages() {
$messages = get_option( __CLASS__ , array() );
static $script_already_sent = false;
static $already_displayed = array();
// first time though this function and we should add the admin nag script to the page
if ( ! $script_already_sent ) {
?>
<script type="text/javascript">
// WPeC Admin nag handler
jQuery(document).ready(function ($) {
function wpsc_dismiss_admin_msg(id) {
jQuery( "#wpsc-admin-message-"+id ).hide();
jQuery.ajax({
type : "post",
dataType : "text",
url : "<?php echo admin_url( 'admin-ajax.php' );?>",
data : {action: "wpsc_dismiss_admin_msg", id : id},
success: function (response) {
},
error: function (response) {
;
},
});
}
jQuery(".wpsc-admin-message-dismiss").click(function(event) {
wpsc_dismiss_admin_msg(event.target.id);
return false;
});
});
</script>
<?php
$script_already_sent = true;
}
foreach ( $messages as $id => $message ) {
if ( in_array( $id, $already_displayed ) )
continue;
$already_displayed[] = $id;
?>
<div class="updated wpsc-admin-message" id="wpsc-admin-message-<?php echo esc_attr( $id );?>">
<div class="message-text">
<p>
<?php echo $message; ?>
</p>
</div>
<div class="wpsc-admin-message-action" style="width: 100%; text-align: right;">
<a class="wpsc-admin-message-dismiss" id="<?php echo esc_attr( $id );?>"><?php _e( 'Dismiss', 'wpec' )?></a>
</div>
</div>
<?php
}
}
/**
* Dismiss an admin message
*
* @param string $message_id the unqiue message id to be dismissed
*/
function dismiss_msg( $message_id = false ) {
if ( ! $message_id ) {
if ( isset( $_REQUEST['id'] ) ) {
$message_id = $_REQUEST['id'];
}
}
$messages = get_option( __CLASS__ , array() );
if ( isset($messages[$message_id] ) ) {
unset( $messages[$message_id] );
update_option( __CLASS__, $messages );
}
wp_send_json_success( true );
}
}
/**
* Show one or more admin notification(s) that must be acknowledged
*
* @param string|array[string] $messages admin the messages(nags) to show
*/
function wpsc_admin_nag( $messages ) {
static $wpsc_admin_notifications = null;
if ( empty( $wpsc_admin_notifications ) ) {
$wpsc_admin_notifications = new WPSC_Admin_Notifications();
}
$wpsc_admin_notifications->new_message( $messages );
}
// If we are showing an admin page we want to show the admin nags
if ( is_admin() ) {
$wpsc_admin_notifications = new WPSC_Admin_Notifications();
}