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

Yoast / wordpress-seo / 7004843404

27 Nov 2023 11:48AM UTC coverage: 49.206% (-0.03%) from 49.232%
7004843404

push

github

web-flow
Merge pull request #20858 from Yoast/improve-copy-in-the-ftc-57

15305 of 31104 relevant lines covered (49.21%)

4.03 hits per line

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

60.61
/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
        const MATCH_ALL = 'all';
20

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

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

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

42
        /**
43
         * Notification type.
44
         *
45
         * @var string
46
         */
47
        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
        }
32✔
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 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
        public function refresh_nonce() {
×
153
                if ( $this->options['id'] ) {
×
154
                        $this->options['nonce'] = wp_create_nonce( $this->options['id'] );
×
155
                }
156
        }
157

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

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

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

188
                return $this->options['dismissal_key'];
4✔
189
        }
190

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

199
                return ! empty( $id );
28✔
200
        }
201

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

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

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

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

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

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

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

261
                if ( ! empty( $capabilities ) ) {
20✔
262

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

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

273
                return true;
×
274
        }
275

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

293
                return $user->has_cap( $capability );
20✔
294
        }
295

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

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

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

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

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

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

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

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

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

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

356
        /**
357
         * Wraps the message with a Yoast SEO icon.
358
         *
359
         * @param string $message The message to wrap.
360
         *
361
         * @return string The wrapped message.
362
         */
363
        private function wrap_yoast_seo_icon( $message ) {
×
364
                $out  = sprintf(
×
365
                        '<img src="%1$s" height="%2$d" width="%3$d" class="yoast-seo-icon" />',
×
366
                        esc_url( plugin_dir_url( WPSEO_FILE ) . 'packages/js/images/Yoast_SEO_Icon.svg' ),
×
367
                        60,
×
368
                        60
×
369
                );
370
                $out .= '<div class="yoast-seo-icon-wrap">';
×
371
                $out .= $message;
×
372
                $out .= '</div>';
×
373

374
                return $out;
×
375
        }
376

377
        /**
378
         * Get the JSON if provided.
379
         *
380
         * @return false|string
381
         */
382
        public function get_json() {
4✔
383
                if ( empty( $this->options['data_json'] ) ) {
4✔
384
                        return '';
4✔
385
                }
386

387
                return WPSEO_Utils::format_json_encode( $this->options['data_json'] );
4✔
388
        }
389

390
        /**
391
         * Make sure we only have values that we can work with.
392
         *
393
         * @param array $options Options to normalize.
394
         *
395
         * @return array
396
         */
397
        private function normalize_options( $options ) {
64✔
398
                $options = wp_parse_args( $options, $this->defaults );
64✔
399

400
                // Should not exceed 0 or 1.
401
                $options['priority'] = min( 1, max( 0, $options['priority'] ) );
64✔
402

403
                // Set default capabilities when not supplied.
404
                if ( empty( $options['capabilities'] ) || $options['capabilities'] === [] ) {
64✔
405
                        $options['capabilities'] = [ 'wpseo_manage_options' ];
44✔
406
                }
407

408
                // Set to the id of the current user if not supplied.
409
                if ( $options['user_id'] === null ) {
64✔
410
                        $options['user_id'] = get_current_user_id();
64✔
411
                }
412

413
                return $options;
64✔
414
        }
415

416
        /**
417
         * Format HTML element attributes.
418
         *
419
         * @param string $value Attribute value.
420
         * @param string $key   Attribute name.
421
         */
422
        private function parse_attributes( &$value, $key ) {
×
423
                $value = sprintf( '%s="%s"', sanitize_key( $key ), esc_attr( $value ) );
×
424
        }
425
}
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