Yoast_Dismissable_Notice_Ajax

This class will catch the request to dismiss the target notice (set by notice_name) and will store the dismiss status as an user meta in the database.

Defined (1)

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

/admin/ajax/class-yoast-dismissable-notice.php  
  1. class Yoast_Dismissable_Notice_Ajax { 
  2.  
  3. const FOR_USER = 'user_meta'; 
  4. const FOR_NETWORK = 'site_option'; 
  5. const FOR_SITE = 'option'; 
  6.  
  7.  
  8. /** 
  9. * @var string Name of the notice that will be dismissed. 
  10. */ 
  11. private $notice_name; 
  12.  
  13. /** 
  14. * @var string 
  15. */ 
  16. private $notice_type; 
  17.  
  18. /** 
  19. * Initialize the hooks for the AJAX request 
  20. * @param string $notice_name The name for the hook to catch the notice. 
  21. * @param string $notice_type The notice type. 
  22. */ 
  23. public function __construct( $notice_name, $notice_type = self::FOR_USER ) { 
  24. $this->notice_name = $notice_name; 
  25. $this->notice_type = $notice_type; 
  26.  
  27. add_action( 'wp_ajax_wpseo_dismiss_' . $notice_name, array( $this, 'dismiss_notice' ) ); 
  28.  
  29. /** 
  30. * Handles the dismiss notice request 
  31. */ 
  32. public function dismiss_notice() { 
  33. check_ajax_referer( 'wpseo-dismiss-' . $this->notice_name ); 
  34.  
  35. $this->save_dismissed(); 
  36.  
  37. wp_die( 'true' ); 
  38.  
  39. /** 
  40. * Storing the dismissed value in the database. The target location is based on the set notification type. 
  41. */ 
  42. private function save_dismissed() { 
  43. if ( $this->notice_type === self::FOR_SITE ) { 
  44. update_option( 'wpseo_dismiss_' . $this->notice_name, 1 ); 
  45.  
  46. return; 
  47.  
  48. if ( $this->notice_type === self::FOR_NETWORK ) { 
  49. update_site_option( 'wpseo_dismiss_' . $this->notice_name, 1 ); 
  50.  
  51. return; 
  52.  
  53. update_user_meta( get_current_user_id(), 'wpseo_dismiss_' . $this->notice_name, 1 );