dismissible-notification.php
3.1 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
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Admin\Notifiers
*/
/**
* Abstract class representing a dismissible notification.
*/
abstract class WPSEO_Dismissible_Notification implements WPSEO_Listener, WPSEO_Notification_Handler {
/**
* The identifier for the notification.
*
* @var string
*/
protected $notification_identifier = '';
/**
* Retrieves instance of a notification.
*
* @return Yoast_Notification The notification.
*/
abstract protected function get_notification();
/**
* Listens to an argument in the request URL and triggers an action.
*
* @return void
*/
public function listen() {
if ( $this->get_listener_value() !== $this->notification_identifier ) {
return;
}
$this->dismiss();
}
/**
* Adds the notification if applicable, otherwise removes it.
*
* @param Yoast_Notification_Center $notification_center The notification center object.
*
* @return void
*/
public function handle( Yoast_Notification_Center $notification_center ) {
if ( $this->is_applicable() ) {
$notification = $this->get_notification();
$notification_center->add_notification( $notification );
return;
}
$notification_center->remove_notification_by_id( 'wpseo-' . $this->notification_identifier );
}
/**
* Listens to an argument in the request URL and triggers an action.
*
* @return void
*/
protected function dismiss() {
$this->set_dismissal_state();
$this->redirect_to_dashboard();
}
/**
* Checks if a notice is applicable.
*
* @return bool Whether a notice should be shown or not.
*/
protected function is_applicable() {
return $this->is_notice_dismissed() === false;
}
/**
* Checks whether the notification has been dismissed.
*
* @codeCoverageIgnore
*
* @return bool True when notification is dismissed.
*/
protected function is_notice_dismissed() {
return get_user_meta( get_current_user_id(), 'wpseo-remove-' . $this->notification_identifier, true ) === '1';
}
/**
* Retrieves the value where listener is listening for.
*
* @codeCoverageIgnore
*
* @return string|null The listener value or null if not set.
*/
protected function get_listener_value() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: Normally we would need to check for a nonce here but this class is not used anymore.
if ( isset( $_GET['yoast_dismiss'] ) && is_string( $_GET['yoast_dismiss'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: Normally we would need to check for a nonce here but this class is not used anymore.
return sanitize_text_field( wp_unslash( $_GET['yoast_dismiss'] ) );
}
return null;
}
/**
* Dismisses the notification.
*
* @codeCoverageIgnore
*
* @return void
*/
protected function set_dismissal_state() {
update_user_meta( get_current_user_id(), 'wpseo-remove-' . $this->notification_identifier, true );
}
/**
* Redirects the user back to the dashboard.
*
* @codeCoverageIgnore
*
* @return void
*/
protected function redirect_to_dashboard() {
wp_safe_redirect( admin_url( 'admin.php?page=wpseo_dashboard' ) );
exit;
}
}