• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / d5c0395617268a4e44627c6847bf3faa0a6cc695

15 Apr 2025 07:12AM UTC coverage: 52.454% (-2.1%) from 54.595%
d5c0395617268a4e44627c6847bf3faa0a6cc695

push

github

web-flow
Merge pull request #22077 from Yoast/feature/drop-php-7.2-7.3

Drop compatibility with PHP 7.2 and 7.3

7826 of 13877 branches covered (56.4%)

Branch coverage included in aggregate %.

29028 of 56382 relevant lines covered (51.48%)

42273.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

56.86
/admin/class-yoast-notification.php
1
<?php
2
/**
3
 * WPSEO plugin file.
4
 *
5
 * @package WPSEO\Admin\Notifications
6
 * @since   1.5.3
7
 */
8

9
/**
10
 * Implements individual notification.
11
 */
12
class Yoast_Notification {
13

14
        /**
15
         * Type of capability check.
16
         *
17
         * @var string
18
         */
19
        public const MATCH_ALL = 'all';
20

21
        /**
22
         * Type of capability check.
23
         *
24
         * @var string
25
         */
26
        public const MATCH_ANY = 'any';
27

28
        /**
29
         * Notification type.
30
         *
31
         * @var string
32
         */
33
        public const ERROR = 'error';
34

35
        /**
36
         * Notification type.
37
         *
38
         * @var string
39
         */
40
        public const WARNING = 'warning';
41

42
        /**
43
         * Notification type.
44
         *
45
         * @var string
46
         */
47
        public const UPDATED = 'updated';
48

49
        /**
50
         * Options of this Notification.
51
         *
52
         * Contains optional arguments:
53
         *
54
         * -             type: The notification type, i.e. 'updated' or 'error'
55
         * -               id: The ID of the notification
56
         * -            nonce: Security nonce to use in case of dismissible notice.
57
         * -         priority: From 0 to 1, determines the order of Notifications.
58
         * -    dismissal_key: Option name to save dismissal information in, ID will be used if not supplied.
59
         * -     capabilities: Capabilities that a user must have for this Notification to show.
60
         * - capability_check: How to check capability pass: all or any.
61
         * -  wpseo_page_only: Only display on wpseo page or on every page.
62
         *
63
         * @var array
64
         */
65
        private $options = [];
66

67
        /**
68
         * Contains default values for the optional arguments.
69
         *
70
         * @var array
71
         */
72
        private $defaults = [
73
                'type'             => self::UPDATED,
74
                'id'               => '',
75
                'user_id'          => null,
76
                'nonce'            => null,
77
                'priority'         => 0.5,
78
                'data_json'        => [],
79
                'dismissal_key'    => null,
80
                'capabilities'     => [],
81
                'capability_check' => self::MATCH_ALL,
82
                'yoast_branding'   => false,
83
        ];
84

85
        /**
86
         * The message for the notification.
87
         *
88
         * @var string
89
         */
90
        private $message;
91

92
        /**
93
         * Notification class constructor.
94
         *
95
         * @param string $message Message string.
96
         * @param array  $options Set of options.
97
         */
98
        public function __construct( $message, $options = [] ) {
64✔
99
                $this->message = $message;
64✔
100
                $this->options = $this->normalize_options( $options );
64✔
101
        }
102

103
        /**
104
         * Retrieve notification ID string.
105
         *
106
         * @return string
107
         */
108
        public function get_id() {
28✔
109
                return $this->options['id'];
28✔
110
        }
111

112
        /**
113
         * Retrieve the user to show the notification for.
114
         *
115
         * @deprecated 21.6
116
         * @codeCoverageIgnore
117
         *
118
         * @return WP_User|null The user to show this notification for.
119
         */
120
        public function get_user() {
121
                _deprecated_function( __METHOD__, 'Yoast SEO 21.6' );
122
                return null;
123
        }
124

125
        /**
126
         * Retrieve the id of the user to show the notification for.
127
         *
128
         * Returns the id of the current user if not user has been sent.
129
         *
130
         * @return int The user id
131
         */
132
        public function get_user_id() {
×
133
                return ( $this->options['user_id'] ?? get_current_user_id() );
×
134
        }
135

136
        /**
137
         * Retrieve nonce identifier.
138
         *
139
         * @return string|null Nonce for this Notification.
140
         */
141
        public function get_nonce() {
8✔
142
                if ( $this->options['id'] && empty( $this->options['nonce'] ) ) {
8✔
143
                        $this->options['nonce'] = wp_create_nonce( $this->options['id'] );
4✔
144
                }
145

146
                return $this->options['nonce'];
8✔
147
        }
148

149
        /**
150
         * Make sure the nonce is up to date.
151
         *
152
         * @return void
153
         */
154
        public function refresh_nonce() {
×
155
                if ( $this->options['id'] ) {
×
156
                        $this->options['nonce'] = wp_create_nonce( $this->options['id'] );
×
157
                }
158
        }
159

160
        /**
161
         * Get the type of the notification.
162
         *
163
         * @return string
164
         */
165
        public function get_type() {
8✔
166
                return $this->options['type'];
8✔
167
        }
168

169
        /**
170
         * Priority of the notification.
171
         *
172
         * Relative to the type.
173
         *
174
         * @return float Returns the priority between 0 and 1.
175
         */
176
        public function get_priority() {
4✔
177
                return $this->options['priority'];
4✔
178
        }
179

180
        /**
181
         * Get the User Meta key to check for dismissal of notification.
182
         *
183
         * @return string User Meta Option key that registers dismissal.
184
         */
185
        public function get_dismissal_key() {
8✔
186
                if ( empty( $this->options['dismissal_key'] ) ) {
8✔
187
                        return $this->options['id'];
4✔
188
                }
189

190
                return $this->options['dismissal_key'];
4✔
191
        }
192

193
        /**
194
         * Is this Notification persistent.
195
         *
196
         * @return bool True if persistent, False if fire and forget.
197
         */
198
        public function is_persistent() {
28✔
199
                $id = $this->get_id();
28✔
200

201
                return ! empty( $id );
28✔
202
        }
203

204
        /**
205
         * Check if the notification is relevant for the current user.
206
         *
207
         * @return bool True if a user needs to see this notification, false if not.
208
         */
209
        public function display_for_current_user() {
24✔
210
                // If the notification is for the current page only, always show.
211
                if ( ! $this->is_persistent() ) {
24✔
212
                        return true;
4✔
213
                }
214

215
                // If the current user doesn't match capabilities.
216
                return $this->match_capabilities();
20✔
217
        }
218

219
        /**
220
         * Does the current user match required capabilities.
221
         *
222
         * @return bool
223
         */
224
        public function match_capabilities() {
20✔
225
                // Super Admin can do anything.
226
                if ( is_multisite() && is_super_admin( $this->options['user_id'] ) ) {
20✔
227
                        return true;
×
228
                }
229

230
                /**
231
                 * Filter capabilities that enable the displaying of this notification.
232
                 *
233
                 * @param array              $capabilities The capabilities that must be present for this notification.
234
                 * @param Yoast_Notification $notification The notification object.
235
                 *
236
                 * @return array Array of capabilities or empty for no restrictions.
237
                 *
238
                 * @since 3.2
239
                 */
240
                $capabilities = apply_filters( 'wpseo_notification_capabilities', $this->options['capabilities'], $this );
20✔
241

242
                // Should be an array.
243
                if ( ! is_array( $capabilities ) ) {
20✔
244
                        $capabilities = (array) $capabilities;
4✔
245
                }
246

247
                /**
248
                 * Filter capability check to enable all or any capabilities.
249
                 *
250
                 * @param string             $capability_check The type of check that will be used to determine if an capability is present.
251
                 * @param Yoast_Notification $notification     The notification object.
252
                 *
253
                 * @return string self::MATCH_ALL or self::MATCH_ANY.
254
                 *
255
                 * @since 3.2
256
                 */
257
                $capability_check = apply_filters( 'wpseo_notification_capability_check', $this->options['capability_check'], $this );
20✔
258

259
                if ( ! in_array( $capability_check, [ self::MATCH_ALL, self::MATCH_ANY ], true ) ) {
20✔
260
                        $capability_check = self::MATCH_ALL;
×
261
                }
262

263
                if ( ! empty( $capabilities ) ) {
20✔
264

265
                        $has_capabilities = array_filter( $capabilities, [ $this, 'has_capability' ] );
20✔
266

267
                        switch ( $capability_check ) {
268
                                case self::MATCH_ALL:
269
                                        return $has_capabilities === $capabilities;
12✔
270
                                case self::MATCH_ANY:
271
                                        return ! empty( $has_capabilities );
8✔
272
                        }
273
                }
274

275
                return true;
×
276
        }
277

278
        /**
279
         * Array filter function to find matched capabilities.
280
         *
281
         * @param string $capability Capability to test.
282
         *
283
         * @return bool
284
         */
285
        private function has_capability( $capability ) {
20✔
286
                $user_id = $this->options['user_id'];
20✔
287
                if ( ! is_numeric( $user_id ) ) {
20✔
288
                        return false;
×
289
                }
290
                $user = get_user_by( 'id', $user_id );
20✔
291
                if ( ! $user ) {
20✔
292
                        return false;
×
293
                }
294

295
                return $user->has_cap( $capability );
20✔
296
        }
297

298
        /**
299
         * Return the object properties as an array.
300
         *
301
         * @return array
302
         */
303
        public function to_array() {
8✔
304
                return [
8✔
305
                        'message' => $this->message,
8✔
306
                        'options' => $this->options,
8✔
307
                ];
8✔
308
        }
309

310
        /**
311
         * Adds string (view) behaviour to the notification.
312
         *
313
         * @return string
314
         */
315
        public function __toString() {
×
316
                return $this->render();
×
317
        }
318

319
        /**
320
         * Renders the notification as a string.
321
         *
322
         * @return string The rendered notification.
323
         */
324
        public function render() {
×
325
                $attributes = [];
×
326

327
                // Default notification classes.
328
                $classes = [
×
329
                        'yoast-notification',
×
330
                ];
×
331

332
                // Maintain WordPress visualisation of notifications when they are not persistent.
333
                if ( ! $this->is_persistent() ) {
×
334
                        $classes[] = 'notice';
×
335
                        $classes[] = $this->get_type();
×
336
                }
337

338
                if ( ! empty( $classes ) ) {
×
339
                        $attributes['class'] = implode( ' ', $classes );
×
340
                }
341

342
                // Combined attribute key and value into a string.
343
                array_walk( $attributes, [ $this, 'parse_attributes' ] );
×
344

345
                $message = null;
×
346
                if ( $this->options['yoast_branding'] ) {
×
347
                        $message = $this->wrap_yoast_seo_icon( $this->message );
×
348
                }
349

350
                if ( $message === null ) {
×
351
                        $message = wpautop( $this->message );
×
352
                }
353

354
                // Build the output DIV.
355
                return '<div ' . implode( ' ', $attributes ) . '>' . $message . '</div>' . PHP_EOL;
×
356
        }
357

358
        /**
359
         * Get the message for the notification.
360
         *
361
         * @return string The message.
362
         */
363
        public function get_message() {
×
364
                return wpautop( $this->message );
×
365
        }
366

367
        /**
368
         * Wraps the message with a Yoast SEO icon.
369
         *
370
         * @param string $message The message to wrap.
371
         *
372
         * @return string The wrapped message.
373
         */
374
        private function wrap_yoast_seo_icon( $message ) {
×
375
                $out  = sprintf(
×
376
                        '<img src="%1$s" height="%2$d" width="%3$d" class="yoast-seo-icon" />',
×
377
                        esc_url( plugin_dir_url( WPSEO_FILE ) . 'packages/js/images/Yoast_SEO_Icon.svg' ),
×
378
                        60,
×
379
                        60
×
380
                );
×
381
                $out .= '<div class="yoast-seo-icon-wrap">';
×
382
                $out .= $message;
×
383
                $out .= '</div>';
×
384

385
                return $out;
×
386
        }
387

388
        /**
389
         * Get the JSON if provided.
390
         *
391
         * @return string|false
392
         */
393
        public function get_json() {
4✔
394
                if ( empty( $this->options['data_json'] ) ) {
4✔
395
                        return '';
4✔
396
                }
397

398
                return WPSEO_Utils::format_json_encode( $this->options['data_json'] );
4✔
399
        }
400

401
        /**
402
         * Make sure we only have values that we can work with.
403
         *
404
         * @param array $options Options to normalize.
405
         *
406
         * @return array
407
         */
408
        private function normalize_options( $options ) {
64✔
409
                $options = wp_parse_args( $options, $this->defaults );
64✔
410

411
                // Should not exceed 0 or 1.
412
                $options['priority'] = min( 1, max( 0, $options['priority'] ) );
64✔
413

414
                // Set default capabilities when not supplied.
415
                if ( empty( $options['capabilities'] ) || $options['capabilities'] === [] ) {
64✔
416
                        $options['capabilities'] = [ 'wpseo_manage_options' ];
44✔
417
                }
418

419
                // Set to the id of the current user if not supplied.
420
                if ( $options['user_id'] === null ) {
64✔
421
                        $options['user_id'] = get_current_user_id();
64✔
422
                }
423

424
                return $options;
64✔
425
        }
426

427
        /**
428
         * Format HTML element attributes.
429
         *
430
         * @param string $value Attribute value.
431
         * @param string $key   Attribute name.
432
         *
433
         * @return void
434
         */
435
        private function parse_attributes( &$value, $key ) {
×
436
                $value = sprintf( '%s="%s"', sanitize_key( $key ), esc_attr( $value ) );
×
437
        }
438
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc