Yoast_Alerts

Class Yoast_Alerts.

Defined (1)

The class is defined in the following location(s).

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