| @@ -14,9 +14,9 @@ | ||
| 14 | 14 | * Holds the admin page's ID. |
| 15 | 15 | * |
| 16 | 16 | * @var string |
| 17 | 17 | */ |
| 18 | - const ADMIN_PAGE = 'wpseo_dashboard'; | |
| 18 | + public const ADMIN_PAGE = 'wpseo_dashboard'; | |
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | 21 | * Total notifications count. |
| 22 | 22 | * |
| @@ -75,18 +75,23 @@ | ||
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | /** |
| 78 | 78 | * Add hooks |
| 79 | + * | |
| 80 | + * @return void | |
| 79 | 81 | */ |
| 80 | 82 | private function add_hooks() { |
| 81 | - | |
| 82 | - $page = filter_input( INPUT_GET, 'page' ); | |
| 83 | - if ( $page === self::ADMIN_PAGE ) { | |
| 84 | - add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] ); | |
| 83 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. | |
| 84 | + if ( isset( $_GET['page'] ) && is_string( $_GET['page'] ) ) { | |
| 85 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. | |
| 86 | + $page = sanitize_text_field( wp_unslash( $_GET['page'] ) ); | |
| 87 | + if ( $page === self::ADMIN_PAGE ) { | |
| 88 | + add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] ); | |
| 89 | + } | |
| 85 | 90 | } |
| 86 | 91 | |
| 87 | 92 | // Needed for adminbar and Notifications page. |
| 88 | - add_action( 'admin_init', [ __CLASS__, 'collect_notifications' ], 99 ); | |
| 93 | + add_action( 'admin_init', [ self::class, 'collect_notifications' ], 99 ); | |
| 89 | 94 | |
| 90 | 95 | // Add AJAX hooks. |
| 91 | 96 | add_action( 'wp_ajax_yoast_dismiss_notification', [ $this, 'ajax_dismiss_notification' ] ); |
| 92 | 97 | add_action( 'wp_ajax_yoast_restore_notification', [ $this, 'ajax_restore_notification' ] ); |
| @@ -93,30 +98,22 @@ | ||
| 93 | 98 | } |
| 94 | 99 | |
| 95 | 100 | /** |
| 96 | 101 | * Enqueue assets. |
| 102 | + * | |
| 103 | + * @return void | |
| 97 | 104 | */ |
| 98 | 105 | public function enqueue_assets() { |
| 106 | + $asset_manager = new WPSEO_Admin_Asset_Manager(); | |
| 99 | 107 | |
| 100 | - $asset_manager = new WPSEO_Admin_Asset_Manager(); | |
| 101 | 108 | $asset_manager->enqueue_style( 'notifications' ); |
| 102 | 109 | } |
| 103 | 110 | |
| 104 | 111 | /** |
| 105 | - * Deprecated: Handle ajax request to dismiss a alert. | |
| 106 | - * Renamed to ajax_dismiss_notification | |
| 112 | + * Handle ajax request to dismiss a notification. | |
| 107 | 113 | * |
| 108 | - * @deprecated 14.0 | |
| 109 | - * | |
| 110 | - * @codeCoverageIgnore | |
| 114 | + * @return void | |
| 111 | 115 | */ |
| 112 | - public function ajax_dismiss_alert() { | |
| 113 | - _deprecated_function( __METHOD__, 'WPSEO 14.0' ); | |
| 114 | - } | |
| 115 | - | |
| 116 | - /** | |
| 117 | - * Handle ajax request to dismiss a notification. | |
| 118 | - */ | |
| 119 | 116 | public function ajax_dismiss_notification() { |
| 120 | 117 | |
| 121 | 118 | $notification = $this->get_notification_from_ajax_request(); |
| 122 | 119 | if ( $notification ) { |
| @@ -129,23 +126,12 @@ | ||
| 129 | 126 | wp_die(); |
| 130 | 127 | } |
| 131 | 128 | |
| 132 | 129 | /** |
| 133 | - * Deprecated: Handle ajax request to restore a notification. | |
| 134 | - * Renamed to ajax_restore_notification | |
| 130 | + * Handle ajax request to restore a notification. | |
| 135 | 131 | * |
| 136 | - * @deprecated 14.0 | |
| 137 | - * @codeCoverageIgnore | |
| 138 | - * | |
| 139 | 132 | * @return void |
| 140 | 133 | */ |
| 141 | - public function ajax_restore_alert() { | |
| 142 | - _deprecated_function( __METHOD__, 'WPSEO 14.0' ); | |
| 143 | - } | |
| 144 | - | |
| 145 | - /** | |
| 146 | - * Handle ajax request to restore a notification. | |
| 147 | - */ | |
| 148 | 134 | public function ajax_restore_notification() { |
| 149 | 135 | |
| 150 | 136 | $notification = $this->get_notification_from_ajax_request(); |
| 151 | 137 | if ( $notification ) { |
| @@ -161,8 +147,10 @@ | ||
| 161 | 147 | /** |
| 162 | 148 | * Create AJAX response data. |
| 163 | 149 | * |
| 164 | 150 | * @param string $type Notification type. |
| 151 | + * | |
| 152 | + * @return void | |
| 165 | 153 | */ |
| 166 | 154 | private function output_ajax_response( $type ) { |
| 167 | 155 | |
| 168 | 156 | $html = $this->get_view_html( $type ); |
| @@ -170,9 +158,9 @@ | ||
| 170 | 158 | echo WPSEO_Utils::format_json_encode( |
| 171 | 159 | [ |
| 172 | 160 | 'html' => $html, |
| 173 | 161 | 'total' => self::get_active_notification_count(), |
| 174 | - ] | |
| 162 | + ], | |
| 175 | 163 | ); |
| 176 | 164 | // phpcs:enable -- Reason: WPSEO_Utils::format_json_encode is safe. |
| 177 | 165 | } |
| 178 | 166 | |
| @@ -215,20 +203,31 @@ | ||
| 215 | 203 | |
| 216 | 204 | /** |
| 217 | 205 | * Extract the Yoast Notification from the AJAX request. |
| 218 | 206 | * |
| 219 | - * @return Yoast_Notification|null | |
| 207 | + * This function does not handle nonce verification. | |
| 208 | + * | |
| 209 | + * @return Yoast_Notification|null A Yoast_Notification on success, null on failure. | |
| 220 | 210 | */ |
| 221 | 211 | private function get_notification_from_ajax_request() { |
| 212 | + // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Reason: This function does not handle nonce verification. | |
| 213 | + if ( ! isset( $_POST['notification'] ) || ! is_string( $_POST['notification'] ) ) { | |
| 214 | + return null; | |
| 215 | + } | |
| 216 | + // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Reason: This function does not handle nonce verification. | |
| 217 | + $notification_id = sanitize_text_field( wp_unslash( $_POST['notification'] ) ); | |
| 222 | 218 | |
| 219 | + if ( empty( $notification_id ) ) { | |
| 220 | + return null; | |
| 221 | + } | |
| 223 | 222 | $notification_center = Yoast_Notification_Center::get(); |
| 224 | - $notification_id = filter_input( INPUT_POST, 'notification' ); | |
| 225 | - | |
| 226 | 223 | return $notification_center->get_notification_by_id( $notification_id ); |
| 227 | 224 | } |
| 228 | 225 | |
| 229 | 226 | /** |
| 230 | 227 | * Collect the notifications and group them together. |
| 228 | + * | |
| 229 | + * @return void | |
| 231 | 230 | */ |
| 232 | 231 | public static function collect_notifications() { |
| 233 | 232 | |
| 234 | 233 | $notification_center = Yoast_Notification_Center::get(); |
| @@ -235,14 +234,14 @@ | ||
| 235 | 234 | |
| 236 | 235 | $notifications = $notification_center->get_sorted_notifications(); |
| 237 | 236 | self::$notification_count = count( $notifications ); |
| 238 | 237 | |
| 239 | - self::$errors = array_filter( $notifications, [ __CLASS__, 'filter_error_notifications' ] ); | |
| 240 | - self::$dismissed_errors = array_filter( self::$errors, [ __CLASS__, 'filter_dismissed_notifications' ] ); | |
| 238 | + self::$errors = array_filter( $notifications, [ self::class, 'filter_error_notifications' ] ); | |
| 239 | + self::$dismissed_errors = array_filter( self::$errors, [ self::class, 'filter_dismissed_notifications' ] ); | |
| 241 | 240 | self::$active_errors = array_diff( self::$errors, self::$dismissed_errors ); |
| 242 | 241 | |
| 243 | - self::$warnings = array_filter( $notifications, [ __CLASS__, 'filter_warning_notifications' ] ); | |
| 244 | - self::$dismissed_warnings = array_filter( self::$warnings, [ __CLASS__, 'filter_dismissed_notifications' ] ); | |
| 242 | + self::$warnings = array_filter( $notifications, [ self::class, 'filter_warning_notifications' ] ); | |
| 243 | + self::$dismissed_warnings = array_filter( self::$warnings, [ self::class, 'filter_dismissed_notifications' ] ); | |
| 245 | 244 | self::$active_warnings = array_diff( self::$warnings, self::$dismissed_warnings ); |
| 246 | 245 | } |
| 247 | 246 | |
| 248 | 247 | /** |
| @@ -270,22 +269,8 @@ | ||
| 270 | 269 | ]; |
| 271 | 270 | } |
| 272 | 271 | |
| 273 | 272 | /** |
| 274 | - * Deprecated: Get the number of active notifications. | |
| 275 | - * Renamed to get_active_notification_count | |
| 276 | - * | |
| 277 | - * @deprecated 14.0 | |
| 278 | - * @codeCoverageIgnore | |
| 279 | - * | |
| 280 | - * @return int | |
| 281 | - */ | |
| 282 | - public function get_active_alert_count() { | |
| 283 | - _deprecated_function( __METHOD__, 'WPSEO 14.0' ); | |
| 284 | - return 0; | |
| 285 | - } | |
| 286 | - | |
| 287 | - /** | |
| 288 | 273 | * Get the number of active notifications. |
| 289 | 274 | * |
| 290 | 275 | * @return int |
| 291 | 276 | */ |
| @@ -294,22 +279,8 @@ | ||
| 294 | 279 | return ( count( self::$active_errors ) + count( self::$active_warnings ) ); |
| 295 | 280 | } |
| 296 | 281 | |
| 297 | 282 | /** |
| 298 | - * Deprecated: Filter out any non-errors. Renamed to filter_error_notifications | |
| 299 | - * | |
| 300 | - * @deprecated 14.0 | |
| 301 | - * @codeCoverageIgnore | |
| 302 | - * | |
| 303 | - * @param Yoast_Notification $notification Notification to test. | |
| 304 | - * @return bool | |
| 305 | - */ | |
| 306 | - public function filter_error_alerts( Yoast_Notification $notification ) { | |
| 307 | - _deprecated_function( __METHOD__, 'WPSEO 14.0' ); | |
| 308 | - return false; | |
| 309 | - } | |
| 310 | - | |
| 311 | - /** | |
| 312 | 283 | * Filter out any non-errors. |
| 313 | 284 | * |
| 314 | 285 | * @param Yoast_Notification $notification Notification to test. |
| 315 | 286 | * |
| @@ -320,22 +291,8 @@ | ||
| 320 | 291 | return $notification->get_type() === 'error'; |
| 321 | 292 | } |
| 322 | 293 | |
| 323 | 294 | /** |
| 324 | - * Deprecated: Filter out any non-warnings. Renamed to filter_warning_notifications | |
| 325 | - * | |
| 326 | - * @deprecated 14.0 | |
| 327 | - * @codeCoverageIgnore | |
| 328 | - * | |
| 329 | - * @param Yoast_Notification $notification Notification to test. | |
| 330 | - * @return bool | |
| 331 | - */ | |
| 332 | - public function filter_warning_alerts( Yoast_Notification $notification ) { | |
| 333 | - _deprecated_function( __METHOD__, 'WPSEO 14.0' ); | |
| 334 | - return false; | |
| 335 | - } | |
| 336 | - | |
| 337 | - /** | |
| 338 | 295 | * Filter out any non-warnings. |
| 339 | 296 | * |
| 340 | 297 | * @param Yoast_Notification $notification Notification to test. |
| 341 | 298 | * |
| @@ -343,22 +300,8 @@ | ||
| 343 | 300 | */ |
| 344 | 301 | private static function filter_warning_notifications( Yoast_Notification $notification ) { |
| 345 | 302 | |
| 346 | 303 | return $notification->get_type() !== 'error'; |
| 347 | - } | |
| 348 | - | |
| 349 | - /** | |
| 350 | - * Deprecated: Filter out any dismissed notifications. Renamed to filter_dismissed_alerts. | |
| 351 | - * | |
| 352 | - * @deprecated 14.0 | |
| 353 | - * @codeCoverageIgnore | |
| 354 | - * | |
| 355 | - * @param Yoast_Notification $notification Notification to test. | |
| 356 | - * @return bool | |
| 357 | - */ | |
| 358 | - public function filter_dismissed_alerts( Yoast_Notification $notification ) { | |
| 359 | - _deprecated_function( __METHOD__, 'WPSEO 14.0' ); | |
| 360 | - return false; | |
| 361 | 304 | } |
| 362 | 305 | |
| 363 | 306 | /** |
| 364 | 307 | * Filter out any dismissed notifications. |