| @@ -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. |
| @@ -111,16 +114,20 @@ | ||
| 111 | 114 | |
| 112 | 115 | /** |
| 113 | 116 | * Retrieve the user to show the notification for. |
| 114 | 117 | * |
| 115 | - * @return WP_User The user to show this notification for. | |
| 118 | + * @deprecated 21.6 | |
| 119 | + * @codeCoverageIgnore | |
| 120 | + * | |
| 121 | + * @return WP_User|null The user to show this notification for. | |
| 116 | 122 | */ |
| 117 | 123 | public function get_user() { |
| 118 | - return $this->options['user']; | |
| 124 | + _deprecated_function( __METHOD__, 'Yoast SEO 21.6' ); | |
| 125 | + return null; | |
| 119 | 126 | } |
| 120 | 127 | |
| 121 | 128 | /** |
| 122 | - * Retrieve the is of the user to show the notification for. | |
| 129 | + * Retrieve the id of the user to show the notification for. | |
| 123 | 130 | * |
| 124 | 131 | * Returns the id of the current user if not user has been sent. |
| 125 | 132 | * |
| 126 | 133 | * @return int The user id |
| @@ -125,12 +132,9 @@ | ||
| 125 | 132 | * |
| 126 | 133 | * @return int The user id |
| 127 | 134 | */ |
| 128 | 135 | public function get_user_id() { |
| 129 | - if ( $this->get_user() !== null ) { | |
| 130 | - return $this->get_user()->ID; | |
| 131 | - } | |
| 132 | - return get_current_user_id(); | |
| 136 | + return ( $this->options['user_id'] ?? get_current_user_id() ); | |
| 133 | 137 | } |
| 134 | 138 | |
| 135 | 139 | /** |
| 136 | 140 | * Retrieve nonce identifier. |
| @@ -146,8 +150,10 @@ | ||
| 146 | 150 | } |
| 147 | 151 | |
| 148 | 152 | /** |
| 149 | 153 | * Make sure the nonce is up to date. |
| 154 | + * | |
| 155 | + * @return void | |
| 150 | 156 | */ |
| 151 | 157 | public function refresh_nonce() { |
| 152 | 158 | if ( $this->options['id'] ) { |
| 153 | 159 | $this->options['nonce'] = wp_create_nonce( $this->options['id'] ); |
| @@ -174,8 +180,17 @@ | ||
| 174 | 180 | return $this->options['priority']; |
| 175 | 181 | } |
| 176 | 182 | |
| 177 | 183 | /** |
| 184 | + * Get the nonce to resolve the alert. | |
| 185 | + * | |
| 186 | + * @return string | |
| 187 | + */ | |
| 188 | + public function get_resolve_nonce() { | |
| 189 | + return $this->options['resolve_nonce']; | |
| 190 | + } | |
| 191 | + | |
| 192 | + /** | |
| 178 | 193 | * Get the User Meta key to check for dismissal of notification. |
| 179 | 194 | * |
| 180 | 195 | * @return string User Meta Option key that registers dismissal. |
| 181 | 196 | */ |
| @@ -219,9 +234,9 @@ | ||
| 219 | 234 | * @return bool |
| 220 | 235 | */ |
| 221 | 236 | public function match_capabilities() { |
| 222 | 237 | // Super Admin can do anything. |
| 223 | - if ( is_multisite() && is_super_admin( $this->options['user']->ID ) ) { | |
| 238 | + if ( is_multisite() && is_super_admin( $this->options['user_id'] ) ) { | |
| 224 | 239 | return true; |
| 225 | 240 | } |
| 226 | 241 | |
| 227 | 242 | /** |
| @@ -279,9 +294,17 @@ | ||
| 279 | 294 | * |
| 280 | 295 | * @return bool |
| 281 | 296 | */ |
| 282 | 297 | private function has_capability( $capability ) { |
| 283 | - $user = $this->options['user']; | |
| 298 | + $user_id = $this->options['user_id']; | |
| 299 | + if ( ! is_numeric( $user_id ) ) { | |
| 300 | + return false; | |
| 301 | + } | |
| 302 | + $user = get_user_by( 'id', $user_id ); | |
| 303 | + if ( ! $user ) { | |
| 304 | + return false; | |
| 305 | + } | |
| 306 | + | |
| 284 | 307 | return $user->has_cap( $capability ); |
| 285 | 308 | } |
| 286 | 309 | |
| 287 | 310 | /** |
| @@ -335,11 +358,9 @@ | ||
| 335 | 358 | if ( $this->options['yoast_branding'] ) { |
| 336 | 359 | $message = $this->wrap_yoast_seo_icon( $this->message ); |
| 337 | 360 | } |
| 338 | 361 | |
| 339 | - if ( $message === null ) { | |
| 340 | - $message = wpautop( $this->message ); | |
| 341 | - } | |
| 362 | + $message ??= wpautop( $this->message ); | |
| 342 | 363 | |
| 343 | 364 | // Build the output DIV. |
| 344 | 365 | return '<div ' . implode( ' ', $attributes ) . '>' . $message . '</div>' . PHP_EOL; |
| 345 | 366 | } |
| @@ -344,8 +365,17 @@ | ||
| 344 | 365 | return '<div ' . implode( ' ', $attributes ) . '>' . $message . '</div>' . PHP_EOL; |
| 345 | 366 | } |
| 346 | 367 | |
| 347 | 368 | /** |
| 369 | + * Get the message for the notification. | |
| 370 | + * | |
| 371 | + * @return string The message. | |
| 372 | + */ | |
| 373 | + public function get_message() { | |
| 374 | + return wpautop( $this->message ); | |
| 375 | + } | |
| 376 | + | |
| 377 | + /** | |
| 348 | 378 | * Wraps the message with a Yoast SEO icon. |
| 349 | 379 | * |
| 350 | 380 | * @param string $message The message to wrap. |
| 351 | 381 | * |
| @@ -355,9 +385,9 @@ | ||
| 355 | 385 | $out = sprintf( |
| 356 | 386 | '<img src="%1$s" height="%2$d" width="%3$d" class="yoast-seo-icon" />', |
| 357 | 387 | esc_url( plugin_dir_url( WPSEO_FILE ) . 'packages/js/images/Yoast_SEO_Icon.svg' ), |
| 358 | 388 | 60, |
| 359 | - 60 | |
| 389 | + 60, | |
| 360 | 390 | ); |
| 361 | 391 | $out .= '<div class="yoast-seo-icon-wrap">'; |
| 362 | 392 | $out .= $message; |
| 363 | 393 | $out .= '</div>'; |
| @@ -367,9 +397,9 @@ | ||
| 367 | 397 | |
| 368 | 398 | /** |
| 369 | 399 | * Get the JSON if provided. |
| 370 | 400 | * |
| 371 | - * @return false|string | |
| 401 | + * @return string|false | |
| 372 | 402 | */ |
| 373 | 403 | public function get_json() { |
| 374 | 404 | if ( empty( $this->options['data_json'] ) ) { |
| 375 | 405 | return ''; |
| @@ -395,12 +425,10 @@ | ||
| 395 | 425 | if ( empty( $options['capabilities'] ) || $options['capabilities'] === [] ) { |
| 396 | 426 | $options['capabilities'] = [ 'wpseo_manage_options' ]; |
| 397 | 427 | } |
| 398 | 428 | |
| 399 | - // Set to the current user if not supplied. | |
| 400 | - if ( $options['user'] === null ) { | |
| 401 | - $options['user'] = wp_get_current_user(); | |
| 402 | - } | |
| 429 | + // Set to the id of the current user if not supplied. | |
| 430 | + $options['user_id'] ??= get_current_user_id(); | |
| 403 | 431 | |
| 404 | 432 | return $options; |
| 405 | 433 | } |
| 406 | 434 | |
| @@ -408,8 +436,10 @@ | ||
| 408 | 436 | * Format HTML element attributes. |
| 409 | 437 | * |
| 410 | 438 | * @param string $value Attribute value. |
| 411 | 439 | * @param string $key Attribute name. |
| 440 | + * | |
| 441 | + * @return void | |
| 412 | 442 | */ |
| 413 | 443 | private function parse_attributes( &$value, $key ) { |
| 414 | 444 | $value = sprintf( '%s="%s"', sanitize_key( $key ), esc_attr( $value ) ); |
| 415 | 445 | } |