PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
28.6 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 All 130 releases
← All changes | admin/class-yoast-notification.php +48 -31 18.4 → trunk View file →
@@ -15,9 +15,9 @@
15 15 * Type of capability check.
16 16 *
17 17 * @var string
18 18 */
19 - const MATCH_ALL = 'all';
19 + public const MATCH_ALL = 'all';
20 20
21 21 /**
22 22 * Type of capability check.
23 23 *
@@ -22,9 +22,9 @@
22 22 * Type of capability check.
23 23 *
24 24 * @var string
25 25 */
26 - const MATCH_ANY = 'any';
26 + public const MATCH_ANY = 'any';
27 27
28 28 /**
29 29 * Notification type.
30 30 *
@@ -29,9 +29,9 @@
29 29 * Notification type.
30 30 *
31 31 * @var string
32 32 */
33 - const ERROR = 'error';
33 + public const ERROR = 'error';
34 34
35 35 /**
36 36 * Notification type.
37 37 *
@@ -36,9 +36,9 @@
36 36 * Notification type.
37 37 *
38 38 * @var string
39 39 */
40 - const WARNING = 'warning';
40 + public const WARNING = 'warning';
41 41
42 42 /**
43 43 * Notification type.
44 44 *
@@ -43,9 +43,9 @@
43 43 * Notification type.
44 44 *
45 45 * @var string
46 46 */
47 - const UPDATED = 'updated';
47 + public const UPDATED = 'updated';
48 48
49 49 /**
50 50 * Options of this Notification.
51 51 *
@@ -58,8 +58,10 @@
58 58 * - dismissal_key: Option name to save dismissal information in, ID will be used if not supplied.
59 59 * - capabilities: Capabilities that a user must have for this Notification to show.
60 60 * - capability_check: How to check capability pass: all or any.
61 61 * - wpseo_page_only: Only display on wpseo page or on every page.
62 + * - yoast_branding: Whether to show the Yoast SEO branding in the notification.
63 + * - resolve_nonce: Security nonce to use in case of resolving the notification.
62 64 *
63 65 * @var array
64 66 */
65 67 private $options = [];
@@ -71,9 +73,9 @@
71 73 */
72 74 private $defaults = [
73 75 'type' => self::UPDATED,
74 76 'id' => '',
75 - 'user' => null,
77 + 'user_id' => null,
76 78 'nonce' => null,
77 79 'priority' => 0.5,
78 80 'data_json' => [],
79 81 'dismissal_key' => null,
@@ -79,8 +81,9 @@
79 81 'dismissal_key' => null,
80 82 'capabilities' => [],
81 83 'capability_check' => self::MATCH_ALL,
82 84 'yoast_branding' => false,
85 + 'resolve_nonce' => '',
83 86 ];
84 87
85 88 /**
86 89 * The message for the notification.
@@ -109,28 +112,16 @@
109 112 return $this->options['id'];
110 113 }
111 114
112 115 /**
113 - * Retrieve the user to show the notification for.
116 + * Retrieve the id of the user to show the notification for.
114 117 *
115 - * @return WP_User The user to show this notification for.
116 - */
117 - public function get_user() {
118 - return $this->options['user'];
119 - }
120 -
121 - /**
122 - * Retrieve the is of the user to show the notification for.
123 - *
124 118 * Returns the id of the current user if not user has been sent.
125 119 *
126 120 * @return int The user id
127 121 */
128 122 public function get_user_id() {
129 - if ( $this->get_user() !== null ) {
130 - return $this->get_user()->ID;
131 - }
132 - return get_current_user_id();
123 + return ( $this->options['user_id'] ?? get_current_user_id() );
133 124 }
134 125
135 126 /**
136 127 * Retrieve nonce identifier.
@@ -146,8 +137,10 @@
146 137 }
147 138
148 139 /**
149 140 * Make sure the nonce is up to date.
141 + *
142 + * @return void
150 143 */
151 144 public function refresh_nonce() {
152 145 if ( $this->options['id'] ) {
153 146 $this->options['nonce'] = wp_create_nonce( $this->options['id'] );
@@ -174,8 +167,17 @@
174 167 return $this->options['priority'];
175 168 }
176 169
177 170 /**
171 + * Get the nonce to resolve the alert.
172 + *
173 + * @return string
174 + */
175 + public function get_resolve_nonce() {
176 + return $this->options['resolve_nonce'];
177 + }
178 +
179 + /**
178 180 * Get the User Meta key to check for dismissal of notification.
179 181 *
180 182 * @return string User Meta Option key that registers dismissal.
181 183 */
@@ -219,9 +221,9 @@
219 221 * @return bool
220 222 */
221 223 public function match_capabilities() {
222 224 // Super Admin can do anything.
223 - if ( is_multisite() && is_super_admin( $this->options['user']->ID ) ) {
225 + if ( is_multisite() && is_super_admin( $this->options['user_id'] ) ) {
224 226 return true;
225 227 }
226 228
227 229 /**
@@ -279,9 +281,17 @@
279 281 *
280 282 * @return bool
281 283 */
282 284 private function has_capability( $capability ) {
283 - $user = $this->options['user'];
285 + $user_id = $this->options['user_id'];
286 + if ( ! is_numeric( $user_id ) ) {
287 + return false;
288 + }
289 + $user = get_user_by( 'id', $user_id );
290 + if ( ! $user ) {
291 + return false;
292 + }
293 +
284 294 return $user->has_cap( $capability );
285 295 }
286 296
287 297 /**
@@ -335,11 +345,9 @@
335 345 if ( $this->options['yoast_branding'] ) {
336 346 $message = $this->wrap_yoast_seo_icon( $this->message );
337 347 }
338 348
339 - if ( $message === null ) {
340 - $message = wpautop( $this->message );
341 - }
349 + $message ??= wpautop( $this->message );
342 350
343 351 // Build the output DIV.
344 352 return '<div ' . implode( ' ', $attributes ) . '>' . $message . '</div>' . PHP_EOL;
345 353 }
@@ -344,8 +352,17 @@
344 352 return '<div ' . implode( ' ', $attributes ) . '>' . $message . '</div>' . PHP_EOL;
345 353 }
346 354
347 355 /**
356 + * Get the message for the notification.
357 + *
358 + * @return string The message.
359 + */
360 + public function get_message() {
361 + return wpautop( $this->message );
362 + }
363 +
364 + /**
348 365 * Wraps the message with a Yoast SEO icon.
349 366 *
350 367 * @param string $message The message to wrap.
351 368 *
@@ -355,9 +372,9 @@
355 372 $out = sprintf(
356 373 '<img src="%1$s" height="%2$d" width="%3$d" class="yoast-seo-icon" />',
357 374 esc_url( plugin_dir_url( WPSEO_FILE ) . 'packages/js/images/Yoast_SEO_Icon.svg' ),
358 375 60,
359 - 60
376 + 60,
360 377 );
361 378 $out .= '<div class="yoast-seo-icon-wrap">';
362 379 $out .= $message;
363 380 $out .= '</div>';
@@ -367,9 +384,9 @@
367 384
368 385 /**
369 386 * Get the JSON if provided.
370 387 *
371 - * @return false|string
388 + * @return string|false
372 389 */
373 390 public function get_json() {
374 391 if ( empty( $this->options['data_json'] ) ) {
375 392 return '';
@@ -395,12 +412,10 @@
395 412 if ( empty( $options['capabilities'] ) || $options['capabilities'] === [] ) {
396 413 $options['capabilities'] = [ 'wpseo_manage_options' ];
397 414 }
398 415
399 - // Set to the current user if not supplied.
400 - if ( $options['user'] === null ) {
401 - $options['user'] = wp_get_current_user();
402 - }
416 + // Set to the id of the current user if not supplied.
417 + $options['user_id'] ??= get_current_user_id();
403 418
404 419 return $options;
405 420 }
406 421
@@ -408,8 +423,10 @@
408 423 * Format HTML element attributes.
409 424 *
410 425 * @param string $value Attribute value.
411 426 * @param string $key Attribute name.
427 + *
428 + * @return void
412 429 */
413 430 private function parse_attributes( &$value, $key ) {
414 431 $value = sprintf( '%s="%s"', sanitize_key( $key ), esc_attr( $value ) );
415 432 }