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

Yoast / wordpress-seo / 7254177976

18 Dec 2023 09:46PM UTC coverage: 49.372%. Remained the same
7254177976

push

github

web-flow
Merge pull request #20963 from Yoast/JRF/modernize-class-references

Modernize: use magic `::class` keyword for name resolution

11 of 26 new or added lines in 10 files covered. (42.31%)

2 existing lines in 1 file now uncovered.

15415 of 31222 relevant lines covered (49.37%)

4.07 hits per line

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

0.0
/admin/class-yoast-notifications.php
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
        public 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
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
82
                if ( isset( $_GET['page'] ) && is_string( $_GET['page'] ) ) {
×
83
                        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information.
84
                        $page = sanitize_text_field( wp_unslash( $_GET['page'] ) );
×
85
                        if ( $page === self::ADMIN_PAGE ) {
×
86
                                add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
×
87
                        }
88
                }
89

90
                // Needed for adminbar and Notifications page.
NEW
91
                add_action( 'admin_init', [ self::class, 'collect_notifications' ], 99 );
×
92

93
                // Add AJAX hooks.
94
                add_action( 'wp_ajax_yoast_dismiss_notification', [ $this, 'ajax_dismiss_notification' ] );
×
95
                add_action( 'wp_ajax_yoast_restore_notification', [ $this, 'ajax_restore_notification' ] );
×
96
        }
97

98
        /**
99
         * Enqueue assets.
100
         */
101
        public function enqueue_assets() {
×
102
                $asset_manager = new WPSEO_Admin_Asset_Manager();
×
103

104
                $asset_manager->enqueue_style( 'notifications' );
×
105
        }
106

107
        /**
108
         * Handle ajax request to dismiss a notification.
109
         */
110
        public function ajax_dismiss_notification() {
×
111

112
                $notification = $this->get_notification_from_ajax_request();
×
113
                if ( $notification ) {
×
114
                        $notification_center = Yoast_Notification_Center::get();
×
115
                        $notification_center->maybe_dismiss_notification( $notification );
×
116

117
                        $this->output_ajax_response( $notification->get_type() );
×
118
                }
119

120
                wp_die();
×
121
        }
122

123
        /**
124
         * Handle ajax request to restore a notification.
125
         */
126
        public function ajax_restore_notification() {
×
127

128
                $notification = $this->get_notification_from_ajax_request();
×
129
                if ( $notification ) {
×
130
                        $notification_center = Yoast_Notification_Center::get();
×
131
                        $notification_center->restore_notification( $notification );
×
132

133
                        $this->output_ajax_response( $notification->get_type() );
×
134
                }
135

136
                wp_die();
×
137
        }
138

139
        /**
140
         * Create AJAX response data.
141
         *
142
         * @param string $type Notification type.
143
         */
144
        private function output_ajax_response( $type ) {
×
145

146
                $html = $this->get_view_html( $type );
×
147
                // phpcs:disable WordPress.Security.EscapeOutput -- Reason: WPSEO_Utils::format_json_encode is safe.
148
                echo WPSEO_Utils::format_json_encode(
×
149
                        [
×
150
                                'html'  => $html,
×
151
                                'total' => self::get_active_notification_count(),
×
152
                        ]
×
153
                );
×
154
                // phpcs:enable -- Reason: WPSEO_Utils::format_json_encode is safe.
155
        }
156

157
        /**
158
         * Get the HTML to return in the AJAX request.
159
         *
160
         * @param string $type Notification type.
161
         *
162
         * @return bool|string
163
         */
164
        private function get_view_html( $type ) {
×
165

166
                switch ( $type ) {
167
                        case 'error':
×
168
                                $view = 'errors';
×
169
                                break;
×
170

171
                        case 'warning':
×
172
                        default:
173
                                $view = 'warnings';
×
174
                                break;
×
175
                }
176

177
                // Re-collect notifications.
178
                self::collect_notifications();
×
179

180
                /**
181
                 * Stops PHPStorm from nagging about this variable being unused. The variable is used in the view.
182
                 *
183
                 * @noinspection PhpUnusedLocalVariableInspection
184
                 */
185
                $notifications_data = self::get_template_variables();
×
186

187
                ob_start();
×
188
                include WPSEO_PATH . 'admin/views/partial-notifications-' . $view . '.php';
×
189
                $html = ob_get_clean();
×
190

191
                return $html;
×
192
        }
193

194
        /**
195
         * Extract the Yoast Notification from the AJAX request.
196
         *
197
         * This function does not handle nonce verification.
198
         *
199
         * @return Yoast_Notification|null A Yoast_Notification on success, null on failure.
200
         */
201
        private function get_notification_from_ajax_request() {
×
202
                // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Reason: This function does not handle nonce verification.
203
                if ( ! isset( $_POST['notification'] ) || ! is_string( $_POST['notification'] ) ) {
×
204
                        return null;
×
205
                }
206
                // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Reason: This function does not handle nonce verification.
207
                $notification_id = sanitize_text_field( wp_unslash( $_POST['notification'] ) );
×
208

209
                if ( empty( $notification_id ) ) {
×
210
                        return null;
×
211
                }
212
                $notification_center = Yoast_Notification_Center::get();
×
213
                return $notification_center->get_notification_by_id( $notification_id );
×
214
        }
215

216
        /**
217
         * Collect the notifications and group them together.
218
         */
219
        public static function collect_notifications() {
×
220

221
                $notification_center = Yoast_Notification_Center::get();
×
222

223
                $notifications            = $notification_center->get_sorted_notifications();
×
224
                self::$notification_count = count( $notifications );
×
225

NEW
226
                self::$errors           = array_filter( $notifications, [ self::class, 'filter_error_notifications' ] );
×
NEW
227
                self::$dismissed_errors = array_filter( self::$errors, [ self::class, 'filter_dismissed_notifications' ] );
×
UNCOV
228
                self::$active_errors    = array_diff( self::$errors, self::$dismissed_errors );
×
229

NEW
230
                self::$warnings           = array_filter( $notifications, [ self::class, 'filter_warning_notifications' ] );
×
NEW
231
                self::$dismissed_warnings = array_filter( self::$warnings, [ self::class, 'filter_dismissed_notifications' ] );
×
UNCOV
232
                self::$active_warnings    = array_diff( self::$warnings, self::$dismissed_warnings );
×
233
        }
234

235
        /**
236
         * Get the variables needed in the views.
237
         *
238
         * @return array
239
         */
240
        public static function get_template_variables() {
×
241

242
                return [
×
243
                        'metrics'  => [
×
244
                                'total'    => self::$notification_count,
×
245
                                'active'   => self::get_active_notification_count(),
×
246
                                'errors'   => count( self::$errors ),
×
247
                                'warnings' => count( self::$warnings ),
×
248
                        ],
×
249
                        'errors'   => [
×
250
                                'dismissed' => self::$dismissed_errors,
×
251
                                'active'    => self::$active_errors,
×
252
                        ],
×
253
                        'warnings' => [
×
254
                                'dismissed' => self::$dismissed_warnings,
×
255
                                'active'    => self::$active_warnings,
×
256
                        ],
×
257
                ];
×
258
        }
259

260
        /**
261
         * Get the number of active notifications.
262
         *
263
         * @return int
264
         */
265
        public static function get_active_notification_count() {
×
266

267
                return ( count( self::$active_errors ) + count( self::$active_warnings ) );
×
268
        }
269

270
        /**
271
         * Filter out any non-errors.
272
         *
273
         * @param Yoast_Notification $notification Notification to test.
274
         *
275
         * @return bool
276
         */
277
        private static function filter_error_notifications( Yoast_Notification $notification ) {
×
278

279
                return $notification->get_type() === 'error';
×
280
        }
281

282
        /**
283
         * Filter out any non-warnings.
284
         *
285
         * @param Yoast_Notification $notification Notification to test.
286
         *
287
         * @return bool
288
         */
289
        private static function filter_warning_notifications( Yoast_Notification $notification ) {
×
290

291
                return $notification->get_type() !== 'error';
×
292
        }
293

294
        /**
295
         * Filter out any dismissed notifications.
296
         *
297
         * @param Yoast_Notification $notification Notification to test.
298
         *
299
         * @return bool
300
         */
301
        private static function filter_dismissed_notifications( Yoast_Notification $notification ) {
×
302

303
                return Yoast_Notification_Center::is_notification_dismissed( $notification );
×
304
        }
305
}
306

307
class_alias( Yoast_Notifications::class, 'Yoast_Alerts' );
×
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