123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- <?php
- /**
- * WPSEO plugin file.
- *
- * @package WPSEO\Admin\Notifications
- */
- /**
- * Class Yoast_Alerts.
- */
- class Yoast_Alerts {
- /**
- * Holds the admin page's ID.
- *
- * @var string
- */
- const ADMIN_PAGE = 'wpseo_dashboard';
- /**
- * Total notifications count.
- *
- * @var int
- */
- private static $notification_count = 0;
- /**
- * All error notifications.
- *
- * @var array
- */
- private static $errors = [];
- /**
- * Active errors.
- *
- * @var array
- */
- private static $active_errors = [];
- /**
- * Dismissed errors.
- *
- * @var array
- */
- private static $dismissed_errors = [];
- /**
- * All warning notifications.
- *
- * @var array
- */
- private static $warnings = [];
- /**
- * Active warnings.
- *
- * @var array
- */
- private static $active_warnings = [];
- /**
- * Dismissed warnings.
- *
- * @var array
- */
- private static $dismissed_warnings = [];
- /**
- * Yoast_Alerts constructor.
- */
- public function __construct() {
- $this->add_hooks();
- }
- /**
- * Add hooks
- */
- private function add_hooks() {
- $page = filter_input( INPUT_GET, 'page' );
- if ( self::ADMIN_PAGE === $page ) {
- add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
- }
- // Needed for adminbar and Alerts page.
- add_action( 'admin_init', [ __CLASS__, 'collect_alerts' ], 99 );
- // Add AJAX hooks.
- add_action( 'wp_ajax_yoast_dismiss_alert', [ $this, 'ajax_dismiss_alert' ] );
- add_action( 'wp_ajax_yoast_restore_alert', [ $this, 'ajax_restore_alert' ] );
- }
- /**
- * Enqueue assets.
- */
- public function enqueue_assets() {
- $asset_manager = new WPSEO_Admin_Asset_Manager();
- $asset_manager->enqueue_style( 'alerts' );
- }
- /**
- * Handle ajax request to dismiss an alert.
- */
- public function ajax_dismiss_alert() {
- $notification = $this->get_notification_from_ajax_request();
- if ( $notification ) {
- $notification_center = Yoast_Notification_Center::get();
- $notification_center->maybe_dismiss_notification( $notification );
- $this->output_ajax_response( $notification->get_type() );
- }
- wp_die();
- }
- /**
- * Handle ajax request to restore an alert.
- */
- public function ajax_restore_alert() {
- $notification = $this->get_notification_from_ajax_request();
- if ( $notification ) {
- $notification_center = Yoast_Notification_Center::get();
- $notification_center->restore_notification( $notification );
- $this->output_ajax_response( $notification->get_type() );
- }
- wp_die();
- }
- /**
- * Create AJAX response data.
- *
- * @param string $type Alert type.
- */
- private function output_ajax_response( $type ) {
- $html = $this->get_view_html( $type );
- // phpcs:disable WordPress.Security.EscapeOutput -- Reason: WPSEO_Utils::format_json_encode is safe.
- echo WPSEO_Utils::format_json_encode(
- [
- 'html' => $html,
- 'total' => self::get_active_alert_count(),
- ]
- );
- // phpcs:enable -- Reason: WPSEO_Utils::format_json_encode is safe.
- }
- /**
- * Get the HTML to return in the AJAX request.
- *
- * @param string $type Alert type.
- *
- * @return bool|string
- */
- private function get_view_html( $type ) {
- switch ( $type ) {
- case 'error':
- $view = 'errors';
- break;
- case 'warning':
- default:
- $view = 'warnings';
- break;
- }
- // Re-collect alerts.
- self::collect_alerts();
- /**
- * Stops PHPStorm from nagging about this variable being unused. The variable is used in the view.
- *
- * @noinspection PhpUnusedLocalVariableInspection
- */
- $alerts_data = self::get_template_variables();
- ob_start();
- include WPSEO_PATH . 'admin/views/partial-alerts-' . $view . '.php';
- $html = ob_get_clean();
- return $html;
- }
- /**
- * Extract the Yoast Notification from the AJAX request.
- *
- * @return null|Yoast_Notification
- */
- private function get_notification_from_ajax_request() {
- $notification_center = Yoast_Notification_Center::get();
- $notification_id = filter_input( INPUT_POST, 'notification' );
- return $notification_center->get_notification_by_id( $notification_id );
- }
- /**
- * Show the alerts overview page.
- */
- public static function show_overview_page() {
- /**
- * Stops PHPStorm from nagging about this variable being unused. The variable is used in the view.
- *
- * @noinspection PhpUnusedLocalVariableInspection
- */
- $alerts_data = self::get_template_variables();
- include WPSEO_PATH . 'admin/views/alerts-dashboard.php';
- }
- /**
- * Collect the alerts and group them together.
- */
- public static function collect_alerts() {
- $notification_center = Yoast_Notification_Center::get();
- $notifications = $notification_center->get_sorted_notifications();
- self::$notification_count = count( $notifications );
- self::$errors = array_filter( $notifications, [ __CLASS__, 'filter_error_alerts' ] );
- self::$dismissed_errors = array_filter( self::$errors, [ __CLASS__, 'filter_dismissed_alerts' ] );
- self::$active_errors = array_diff( self::$errors, self::$dismissed_errors );
- self::$warnings = array_filter( $notifications, [ __CLASS__, 'filter_warning_alerts' ] );
- self::$dismissed_warnings = array_filter( self::$warnings, [ __CLASS__, 'filter_dismissed_alerts' ] );
- self::$active_warnings = array_diff( self::$warnings, self::$dismissed_warnings );
- }
- /**
- * Get the variables needed in the views.
- *
- * @return array
- */
- public static function get_template_variables() {
- return [
- 'metrics' => [
- 'total' => self::$notification_count,
- 'active' => self::get_active_alert_count(),
- 'errors' => count( self::$errors ),
- 'warnings' => count( self::$warnings ),
- ],
- 'errors' => [
- 'dismissed' => self::$dismissed_errors,
- 'active' => self::$active_errors,
- ],
- 'warnings' => [
- 'dismissed' => self::$dismissed_warnings,
- 'active' => self::$active_warnings,
- ],
- ];
- }
- /**
- * Get the number of active alerts.
- *
- * @return int
- */
- public static function get_active_alert_count() {
- return ( count( self::$active_errors ) + count( self::$active_warnings ) );
- }
- /**
- * Filter out any non-errors.
- *
- * @param Yoast_Notification $notification Notification to test.
- *
- * @return bool
- */
- private static function filter_error_alerts( Yoast_Notification $notification ) {
- return $notification->get_type() === 'error';
- }
- /**
- * Filter out any non-warnings.
- *
- * @param Yoast_Notification $notification Notification to test.
- *
- * @return bool
- */
- private static function filter_warning_alerts( Yoast_Notification $notification ) {
- return $notification->get_type() !== 'error';
- }
- /**
- * Filter out any dismissed notifications.
- *
- * @param Yoast_Notification $notification Notification to test.
- *
- * @return bool
- */
- private static function filter_dismissed_alerts( Yoast_Notification $notification ) {
- return Yoast_Notification_Center::is_notification_dismissed( $notification );
- }
- }
|