PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.8
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.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 18.8, at admin/notifiers/dismissible-notification.php

122 lines 2.6 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 The listener value.
94 */
95 protected function get_listener_value() {
96 return filter_input( INPUT_GET, 'yoast_dismiss' );
97 }
98
99 /**
100 * Dismisses the notification.
101 *
102 * @codeCoverageIgnore
103 *
104 * @return void
105 */
106 protected function set_dismissal_state() {
107 update_user_meta( get_current_user_id(), 'wpseo-remove-' . $this->notification_identifier, true );
108 }
109
110 /**
111 * Redirects the user back to the dashboard.
112 *
113 * @codeCoverageIgnore
114 *
115 * @return void
116 */
117 protected function redirect_to_dashboard() {
118 wp_safe_redirect( admin_url( 'admin.php?page=wpseo_dashboard' ) );
119 exit;
120 }
121 }
122