PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 19.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v19.0
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 / class-yoast-notifications.php

class-yoast-notifications.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 19.0, at admin/class-yoast-notifications.php

377 lines 8.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\Notifications
6 */
7
8 /**
9 * Class Yoast_Notifications.
10 */
11 class Yoast_Notifications {
12
13 /**
14 * Holds the admin page's ID.
15 *
16 * @var string
17 */
18 const ADMIN_PAGE = 'wpseo_dashboard';
19
20 /**
21 * Total notifications count.
22 *
23 * @var int
24 */
25 private static $notification_count = 0;
26
27 /**
28 * All error notifications.
29 *
30 * @var array
31 */
32 private static $errors = [];
33
34 /**
35 * Active errors.
36 *
37 * @var array
38 */
39 private static $active_errors = [];
40
41 /**
42 * Dismissed errors.
43 *
44 * @var array
45 */
46 private static $dismissed_errors = [];
47
48 /**
49 * All warning notifications.
50 *
51 * @var array
52 */
53 private static $warnings = [];
54
55 /**
56 * Active warnings.
57 *
58 * @var array
59 */
60 private static $active_warnings = [];
61
62 /**
63 * Dismissed warnings.
64 *
65 * @var array
66 */
67 private static $dismissed_warnings = [];
68
69 /**
70 * Yoast_Notifications constructor.
71 */
72 public function __construct() {
73
74 $this->add_hooks();
75 }
76
77 /**
78 * Add hooks
79 */
80 private function add_hooks() {
81
82 $page = filter_input( INPUT_GET, 'page' );
83 if ( $page === self::ADMIN_PAGE ) {
84 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
85 }
86
87 // Needed for adminbar and Notifications page.
88 add_action( 'admin_init', [ __CLASS__, 'collect_notifications' ], 99 );
89
90 // Add AJAX hooks.
91 add_action( 'wp_ajax_yoast_dismiss_notification', [ $this, 'ajax_dismiss_notification' ] );
92 add_action( 'wp_ajax_yoast_restore_notification', [ $this, 'ajax_restore_notification' ] );
93 }
94
95 /**
96 * Enqueue assets.
97 */
98 public function enqueue_assets() {
99
100 $asset_manager = new WPSEO_Admin_Asset_Manager();
101 $asset_manager->enqueue_style( 'notifications' );
102 }
103
104 /**
105 * Deprecated: Handle ajax request to dismiss a alert.
106 * Renamed to ajax_dismiss_notification
107 *
108 * @deprecated 14.0
109 *
110 * @codeCoverageIgnore
111 */
112 public function ajax_dismiss_alert() {
113 _deprecated_function( __METHOD__, 'WPSEO 14.0' );
114 }
115
116 /**
117 * Handle ajax request to dismiss a notification.
118 */
119 public function ajax_dismiss_notification() {
120
121 $notification = $this->get_notification_from_ajax_request();
122 if ( $notification ) {
123 $notification_center = Yoast_Notification_Center::get();
124 $notification_center->maybe_dismiss_notification( $notification );
125
126 $this->output_ajax_response( $notification->get_type() );
127 }
128
129 wp_die();
130 }
131
132 /**
133 * Deprecated: Handle ajax request to restore a notification.
134 * Renamed to ajax_restore_notification
135 *
136 * @deprecated 14.0
137 * @codeCoverageIgnore
138 *
139 * @return void
140 */
141 public function ajax_restore_alert() {
142 _deprecated_function( __METHOD__, 'WPSEO 14.0' );
143 }
144
145 /**
146 * Handle ajax request to restore a notification.
147 */
148 public function ajax_restore_notification() {
149
150 $notification = $this->get_notification_from_ajax_request();
151 if ( $notification ) {
152 $notification_center = Yoast_Notification_Center::get();
153 $notification_center->restore_notification( $notification );
154
155 $this->output_ajax_response( $notification->get_type() );
156 }
157
158 wp_die();
159 }
160
161 /**
162 * Create AJAX response data.
163 *
164 * @param string $type Notification type.
165 */
166 private function output_ajax_response( $type ) {
167
168 $html = $this->get_view_html( $type );
169 // phpcs:disable WordPress.Security.EscapeOutput -- Reason: WPSEO_Utils::format_json_encode is safe.
170 echo WPSEO_Utils::format_json_encode(
171 [
172 'html' => $html,
173 'total' => self::get_active_notification_count(),
174 ]
175 );
176 // phpcs:enable -- Reason: WPSEO_Utils::format_json_encode is safe.
177 }
178
179 /**
180 * Get the HTML to return in the AJAX request.
181 *
182 * @param string $type Notification type.
183 *
184 * @return bool|string
185 */
186 private function get_view_html( $type ) {
187
188 switch ( $type ) {
189 case 'error':
190 $view = 'errors';
191 break;
192
193 case 'warning':
194 default:
195 $view = 'warnings';
196 break;
197 }
198
199 // Re-collect notifications.
200 self::collect_notifications();
201
202 /**
203 * Stops PHPStorm from nagging about this variable being unused. The variable is used in the view.
204 *
205 * @noinspection PhpUnusedLocalVariableInspection
206 */
207 $notifications_data = self::get_template_variables();
208
209 ob_start();
210 include WPSEO_PATH . 'admin/views/partial-notifications-' . $view . '.php';
211 $html = ob_get_clean();
212
213 return $html;
214 }
215
216 /**
217 * Extract the Yoast Notification from the AJAX request.
218 *
219 * @return Yoast_Notification|null
220 */
221 private function get_notification_from_ajax_request() {
222
223 $notification_center = Yoast_Notification_Center::get();
224 $notification_id = filter_input( INPUT_POST, 'notification' );
225
226 return $notification_center->get_notification_by_id( $notification_id );
227 }
228
229 /**
230 * Collect the notifications and group them together.
231 */
232 public static function collect_notifications() {
233
234 $notification_center = Yoast_Notification_Center::get();
235
236 $notifications = $notification_center->get_sorted_notifications();
237 self::$notification_count = count( $notifications );
238
239 self::$errors = array_filter( $notifications, [ __CLASS__, 'filter_error_notifications' ] );
240 self::$dismissed_errors = array_filter( self::$errors, [ __CLASS__, 'filter_dismissed_notifications' ] );
241 self::$active_errors = array_diff( self::$errors, self::$dismissed_errors );
242
243 self::$warnings = array_filter( $notifications, [ __CLASS__, 'filter_warning_notifications' ] );
244 self::$dismissed_warnings = array_filter( self::$warnings, [ __CLASS__, 'filter_dismissed_notifications' ] );
245 self::$active_warnings = array_diff( self::$warnings, self::$dismissed_warnings );
246 }
247
248 /**
249 * Get the variables needed in the views.
250 *
251 * @return array
252 */
253 public static function get_template_variables() {
254
255 return [
256 'metrics' => [
257 'total' => self::$notification_count,
258 'active' => self::get_active_notification_count(),
259 'errors' => count( self::$errors ),
260 'warnings' => count( self::$warnings ),
261 ],
262 'errors' => [
263 'dismissed' => self::$dismissed_errors,
264 'active' => self::$active_errors,
265 ],
266 'warnings' => [
267 'dismissed' => self::$dismissed_warnings,
268 'active' => self::$active_warnings,
269 ],
270 ];
271 }
272
273 /**
274 * Deprecated: Get the number of active notifications.
275 * Renamed to get_active_notification_count
276 *
277 * @deprecated 14.0
278 * @codeCoverageIgnore
279 *
280 * @return int
281 */
282 public function get_active_alert_count() {
283 _deprecated_function( __METHOD__, 'WPSEO 14.0' );
284 return 0;
285 }
286
287 /**
288 * Get the number of active notifications.
289 *
290 * @return int
291 */
292 public static function get_active_notification_count() {
293
294 return ( count( self::$active_errors ) + count( self::$active_warnings ) );
295 }
296
297 /**
298 * Deprecated: Filter out any non-errors. Renamed to filter_error_notifications
299 *
300 * @deprecated 14.0
301 * @codeCoverageIgnore
302 *
303 * @param Yoast_Notification $notification Notification to test.
304 * @return bool
305 */
306 public function filter_error_alerts( Yoast_Notification $notification ) {
307 _deprecated_function( __METHOD__, 'WPSEO 14.0' );
308 return false;
309 }
310
311 /**
312 * Filter out any non-errors.
313 *
314 * @param Yoast_Notification $notification Notification to test.
315 *
316 * @return bool
317 */
318 private static function filter_error_notifications( Yoast_Notification $notification ) {
319
320 return $notification->get_type() === 'error';
321 }
322
323 /**
324 * Deprecated: Filter out any non-warnings. Renamed to filter_warning_notifications
325 *
326 * @deprecated 14.0
327 * @codeCoverageIgnore
328 *
329 * @param Yoast_Notification $notification Notification to test.
330 * @return bool
331 */
332 public function filter_warning_alerts( Yoast_Notification $notification ) {
333 _deprecated_function( __METHOD__, 'WPSEO 14.0' );
334 return false;
335 }
336
337 /**
338 * Filter out any non-warnings.
339 *
340 * @param Yoast_Notification $notification Notification to test.
341 *
342 * @return bool
343 */
344 private static function filter_warning_notifications( Yoast_Notification $notification ) {
345
346 return $notification->get_type() !== 'error';
347 }
348
349 /**
350 * Deprecated: Filter out any dismissed notifications. Renamed to filter_dismissed_alerts.
351 *
352 * @deprecated 14.0
353 * @codeCoverageIgnore
354 *
355 * @param Yoast_Notification $notification Notification to test.
356 * @return bool
357 */
358 public function filter_dismissed_alerts( Yoast_Notification $notification ) {
359 _deprecated_function( __METHOD__, 'WPSEO 14.0' );
360 return false;
361 }
362
363 /**
364 * Filter out any dismissed notifications.
365 *
366 * @param Yoast_Notification $notification Notification to test.
367 *
368 * @return bool
369 */
370 private static function filter_dismissed_notifications( Yoast_Notification $notification ) {
371
372 return Yoast_Notification_Center::is_notification_dismissed( $notification );
373 }
374 }
375
376 class_alias( Yoast_Notifications::class, 'Yoast_Alerts' );
377