PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.8
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.8
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / admin / notifiers / dismissible-notification.php

dismissible-notification.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.8, at admin/notifiers/dismissible-notification.php

127 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO\Admin\Notifiers
6 */
7
8 /**
9 * Abstract class representing a dismissible notification.
10 */
11 abstract class WPSEO_Dismissible_Notification implements WPSEO_Listener, WPSEO_Notification_Handler {
12
13 /**
14 * The identifier for the notification.
15 *
16 * @var string
17 */
18 protected $notification_identifier = '';
19
20 /**
21 * Retrieves instance of a notification.
22 *
23 * @return Yoast_Notification The notification.
24 */
25 abstract protected function get_notification();
26
27 /**
28 * Listens to an argument in the request URL and triggers an action.
29 *
30 * @return void
31 */
32 public function listen() {
33 if ( $this->get_listener_value() !== $this->notification_identifier ) {
34 return;
35 }
36
37 $this->dismiss();
38 }
39
40 /**
41 * Adds the notification if applicable, otherwise removes it.
42 *
43 * @param Yoast_Notification_Center $notification_center The notification center object.
44 *
45 * @return void
46 */
47 public function handle( Yoast_Notification_Center $notification_center ) {
48 if ( $this->is_applicable() ) {
49 $notification = $this->get_notification();
50 $notification_center->add_notification( $notification );
51
52 return;
53 }
54
55 $notification_center->remove_notification_by_id( 'wpseo-' . $this->notification_identifier );
56 }
57
58 /**
59 * Listens to an argument in the request URL and triggers an action.
60 *
61 * @return void
62 */
63 protected function dismiss() {
64 $this->set_dismissal_state();
65 $this->redirect_to_dashboard();
66 }
67
68 /**
69 * Checks if a notice is applicable.
70 *
71 * @return bool Whether a notice should be shown or not.
72 */
73 protected function is_applicable() {
74 return $this->is_notice_dismissed() === false;
75 }
76
77 /**
78 * Checks whether the notification has been dismissed.
79 *
80 * @codeCoverageIgnore
81 *
82 * @return bool True when notification is dismissed.
83 */
84 protected function is_notice_dismissed() {
85 return get_user_meta( get_current_user_id(), 'wpseo-remove-' . $this->notification_identifier, true ) === '1';
86 }
87
88 /**
89 * Retrieves the value where listener is listening for.
90 *
91 * @codeCoverageIgnore
92 *
93 * @return string|null The listener value or null if not set.
94 */
95 protected function get_listener_value() {
96 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: Normally we would need to check for a nonce here but this class is not used anymore.
97 if ( isset( $_GET['yoast_dismiss'] ) && is_string( $_GET['yoast_dismiss'] ) ) {
98 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: Normally we would need to check for a nonce here but this class is not used anymore.
99 return sanitize_text_field( wp_unslash( $_GET['yoast_dismiss'] ) );
100 }
101 return null;
102 }
103
104 /**
105 * Dismisses the notification.
106 *
107 * @codeCoverageIgnore
108 *
109 * @return void
110 */
111 protected function set_dismissal_state() {
112 update_user_meta( get_current_user_id(), 'wpseo-remove-' . $this->notification_identifier, true );
113 }
114
115 /**
116 * Redirects the user back to the dashboard.
117 *
118 * @codeCoverageIgnore
119 *
120 * @return void
121 */
122 protected function redirect_to_dashboard() {
123 wp_safe_redirect( admin_url( 'admin.php?page=wpseo_dashboard' ) );
124 exit();
125 }
126 }
127