class-yoast-notification-center.php
3.56 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
<?php
/**
* @package WPSEO\Admin
*/
/**
* Handles notifications storage and display.
*/
class Yoast_Notification_Center {
const TRANSIENT_KEY = 'yoast_notifications';
/**
* The singleton instance of this object
*
* @var \Yoast_Notification_Center
*/
private static $instance = null;
/**
* @var array
*/
private $notifications = array();
/**
* Construct
*/
private function __construct() {
// Load the notifications from cookie.
$this->notifications = $this->get_notifications_from_transient();
// Clear the cookie.
if ( count( $this->notifications ) > 0 ) {
$this->remove_transient();
}
add_action( 'all_admin_notices', array( $this, 'display_notifications' ) );
add_action( 'shutdown', array( $this, 'set_transient' ) );
add_action( 'wp_ajax_yoast_get_notifications', array( $this, 'ajax_get_notifications' ) );
add_action( 'wpseo_deactivate', array( $this, 'deactivate_hook' ) );
}
/**
* Singleton getter
*
* @return Yoast_Notification_Center
*/
public static function get() {
if ( null == self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Get the notifications from cookie
*
* @return array
*/
private function get_notifications_from_transient() {
// The notifications array.
$notifications = array();
$transient_notifications = get_transient( self::TRANSIENT_KEY );
// Check if cookie is set.
if ( false !== $transient_notifications ) {
// Get json notifications from cookie.
$json_notifications = json_decode( $transient_notifications, true );
// Create Yoast_Notification objects.
if ( count( $json_notifications ) > 0 ) {
foreach ( $json_notifications as $json_notification ) {
$notifications[] = new Yoast_Notification( $json_notification['message'], $json_notification['options'] );
}
}
}
return $notifications;
}
/**
* Clear the cookie
*/
private function remove_transient() {
delete_transient( self::TRANSIENT_KEY );
}
/**
* Clear local stored notifications
*/
private function clear_notifications() {
$this->notifications = array();
}
/**
* Remove transient when the plugin is deactivated
*/
public function deactivate_hook() {
$this->clear_notifications();
}
/**
* Write the notifications to a cookie (hooked on shutdown)
*/
public function set_transient() {
// Count local stored notifications.
if ( count( $this->notifications ) > 0 ) {
// Create array with all notifications.
$arr_notifications = array();
// Add each notification as array to $arr_notifications.
foreach ( $this->notifications as $notification ) {
$arr_notifications[] = $notification->to_array();
}
// Set the cookie with notifications.
set_transient( self::TRANSIENT_KEY, json_encode( $arr_notifications ), ( MINUTE_IN_SECONDS * 10 ) );
}
}
/**
* Add notification to the cookie
*
* @param Yoast_Notification $notification
*/
public function add_notification( Yoast_Notification $notification ) {
$this->notifications[] = $notification;
}
/**
* Display the notifications
*/
public function display_notifications() {
$this->notifications = array_unique( $this->notifications );
// Display notifications.
if ( count( $this->notifications ) > 0 ) {
foreach ( $this->notifications as $notification ) {
echo $notification;
}
}
// Clear the local stored notifications.
$this->clear_notifications();
}
/**
* AJAX display notifications
*/
public function ajax_get_notifications() {
// Display the notices.
$this->display_notifications();
// AJAX die.
exit;
}
}