class-yoast-alerts.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Notifications
  6. */
  7. /**
  8. * Class Yoast_Alerts
  9. */
  10. class Yoast_Alerts {
  11. const ADMIN_PAGE = 'wpseo_dashboard';
  12. /** @var int Total notifications count */
  13. private static $notification_count = 0;
  14. /** @var array All error notifications */
  15. private static $errors = array();
  16. /** @var array Active errors */
  17. private static $active_errors = array();
  18. /** @var array Dismissed errors */
  19. private static $dismissed_errors = array();
  20. /** @var array All warning notifications */
  21. private static $warnings = array();
  22. /** @var array Active warnings */
  23. private static $active_warnings = array();
  24. /** @var array Dismissed warnings */
  25. private static $dismissed_warnings = array();
  26. /**
  27. * Yoast_Alerts constructor.
  28. */
  29. public function __construct() {
  30. $this->add_hooks();
  31. }
  32. /**
  33. * Add hooks
  34. */
  35. private function add_hooks() {
  36. $page = filter_input( INPUT_GET, 'page' );
  37. if ( self::ADMIN_PAGE === $page ) {
  38. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
  39. }
  40. // Needed for adminbar and Alerts page.
  41. add_action( 'admin_init', array( __CLASS__, 'collect_alerts' ), 99 );
  42. // Add AJAX hooks.
  43. add_action( 'wp_ajax_yoast_dismiss_alert', array( $this, 'ajax_dismiss_alert' ) );
  44. add_action( 'wp_ajax_yoast_restore_alert', array( $this, 'ajax_restore_alert' ) );
  45. }
  46. /**
  47. * Enqueue assets
  48. */
  49. public function enqueue_assets() {
  50. $asset_manager = new WPSEO_Admin_Asset_Manager();
  51. $asset_manager->enqueue_style( 'alerts' );
  52. }
  53. /**
  54. * Handle ajax request to dismiss an alert
  55. */
  56. public function ajax_dismiss_alert() {
  57. $notification = $this->get_notification_from_ajax_request();
  58. if ( $notification ) {
  59. $notification_center = Yoast_Notification_Center::get();
  60. $notification_center->maybe_dismiss_notification( $notification );
  61. $this->output_ajax_response( $notification->get_type() );
  62. }
  63. wp_die();
  64. }
  65. /**
  66. * Handle ajax request to restore an alert
  67. */
  68. public function ajax_restore_alert() {
  69. $notification = $this->get_notification_from_ajax_request();
  70. if ( $notification ) {
  71. $notification_center = Yoast_Notification_Center::get();
  72. $notification_center->restore_notification( $notification );
  73. $this->output_ajax_response( $notification->get_type() );
  74. }
  75. wp_die();
  76. }
  77. /**
  78. * Create AJAX response data
  79. *
  80. * @param string $type Alert type.
  81. */
  82. private function output_ajax_response( $type ) {
  83. $html = $this->get_view_html( $type );
  84. echo wp_json_encode(
  85. array(
  86. 'html' => $html,
  87. 'total' => self::get_active_alert_count(),
  88. )
  89. );
  90. }
  91. /**
  92. * Get the HTML to return in the AJAX request
  93. *
  94. * @param string $type Alert type.
  95. *
  96. * @return bool|string
  97. */
  98. private function get_view_html( $type ) {
  99. switch ( $type ) {
  100. case 'error':
  101. $view = 'errors';
  102. break;
  103. case 'warning':
  104. default:
  105. $view = 'warnings';
  106. break;
  107. }
  108. // Re-collect alerts.
  109. self::collect_alerts();
  110. /** @noinspection PhpUnusedLocalVariableInspection */
  111. $alerts_data = self::get_template_variables();
  112. ob_start();
  113. include WPSEO_PATH . 'admin/views/partial-alerts-' . $view . '.php';
  114. $html = ob_get_clean();
  115. return $html;
  116. }
  117. /**
  118. * Extract the Yoast Notification from the AJAX request
  119. *
  120. * @return null|Yoast_Notification
  121. */
  122. private function get_notification_from_ajax_request() {
  123. $notification_center = Yoast_Notification_Center::get();
  124. $notification_id = filter_input( INPUT_POST, 'notification' );
  125. return $notification_center->get_notification_by_id( $notification_id );
  126. }
  127. /**
  128. * Show the alerts overview page
  129. */
  130. public static function show_overview_page() {
  131. /** @noinspection PhpUnusedLocalVariableInspection */
  132. $alerts_data = self::get_template_variables();
  133. include WPSEO_PATH . 'admin/views/alerts-dashboard.php';
  134. }
  135. /**
  136. * Collect the alerts and group them together
  137. */
  138. public static function collect_alerts() {
  139. $notification_center = Yoast_Notification_Center::get();
  140. $notifications = $notification_center->get_sorted_notifications();
  141. self::$notification_count = count( $notifications );
  142. self::$errors = array_filter( $notifications, array( __CLASS__, 'filter_error_alerts' ) );
  143. self::$dismissed_errors = array_filter( self::$errors, array( __CLASS__, 'filter_dismissed_alerts' ) );
  144. self::$active_errors = array_diff( self::$errors, self::$dismissed_errors );
  145. self::$warnings = array_filter( $notifications, array( __CLASS__, 'filter_warning_alerts' ) );
  146. self::$dismissed_warnings = array_filter( self::$warnings, array( __CLASS__, 'filter_dismissed_alerts' ) );
  147. self::$active_warnings = array_diff( self::$warnings, self::$dismissed_warnings );
  148. }
  149. /**
  150. * Get the variables needed in the views
  151. *
  152. * @return array
  153. */
  154. public static function get_template_variables() {
  155. return array(
  156. 'metrics' => array(
  157. 'total' => self::$notification_count,
  158. 'active' => self::get_active_alert_count(),
  159. 'errors' => count( self::$errors ),
  160. 'warnings' => count( self::$warnings ),
  161. ),
  162. 'errors' => array(
  163. 'dismissed' => self::$dismissed_errors,
  164. 'active' => self::$active_errors,
  165. ),
  166. 'warnings' => array(
  167. 'dismissed' => self::$dismissed_warnings,
  168. 'active' => self::$active_warnings,
  169. ),
  170. );
  171. }
  172. /**
  173. * Get the number of active alerts
  174. *
  175. * @return int
  176. */
  177. public static function get_active_alert_count() {
  178. return ( count( self::$active_errors ) + count( self::$active_warnings ) );
  179. }
  180. /**
  181. * Filter out any non-errors
  182. *
  183. * @param Yoast_Notification $notification Notification to test.
  184. *
  185. * @return bool
  186. */
  187. private static function filter_error_alerts( Yoast_Notification $notification ) {
  188. return $notification->get_type() === 'error';
  189. }
  190. /**
  191. * Filter out any non-warnings
  192. *
  193. * @param Yoast_Notification $notification Notification to test.
  194. *
  195. * @return bool
  196. */
  197. private static function filter_warning_alerts( Yoast_Notification $notification ) {
  198. return $notification->get_type() !== 'error';
  199. }
  200. /**
  201. * Filter out any dismissed notifications
  202. *
  203. * @param Yoast_Notification $notification Notification to test.
  204. *
  205. * @return bool
  206. */
  207. private static function filter_dismissed_alerts( Yoast_Notification $notification ) {
  208. return Yoast_Notification_Center::is_notification_dismissed( $notification );
  209. }
  210. }