dismissible-notification.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Notifiers
  6. */
  7. /**
  8. * Abstract class representing a dismissible notification.
  9. */
  10. abstract class WPSEO_Dismissible_Notification implements WPSEO_Listener, WPSEO_Notification_Handler {
  11. /**
  12. * The identifier for the notification.
  13. *
  14. * @var string
  15. */
  16. protected $notification_identifier = '';
  17. /**
  18. * Retrieves instance of a notification.
  19. *
  20. * @return Yoast_Notification The notification.
  21. */
  22. abstract protected function get_notification();
  23. /**
  24. * Listens to an argument in the request URL and triggers an action.
  25. *
  26. * @return void
  27. */
  28. public function listen() {
  29. if ( $this->get_listener_value() !== $this->notification_identifier ) {
  30. return;
  31. }
  32. $this->dismiss();
  33. }
  34. /**
  35. * Adds the notification if applicable, otherwise removes it.
  36. *
  37. * @param Yoast_Notification_Center $notification_center The notification center object.
  38. *
  39. * @return void
  40. */
  41. public function handle( Yoast_Notification_Center $notification_center ) {
  42. if ( $this->is_applicable() ) {
  43. $notification = $this->get_notification();
  44. $notification_center->add_notification( $notification );
  45. return;
  46. }
  47. $notification_center->remove_notification_by_id( 'wpseo-' . $this->notification_identifier );
  48. }
  49. /**
  50. * Listens to an argument in the request URL and triggers an action.
  51. *
  52. * @return void
  53. */
  54. protected function dismiss() {
  55. $this->set_dismissal_state();
  56. $this->redirect_to_dashboard();
  57. }
  58. /**
  59. * Checks if a notice is applicable.
  60. *
  61. * @return bool Whether a notice should be shown or not.
  62. */
  63. protected function is_applicable() {
  64. return $this->is_notice_dismissed() === false;
  65. }
  66. /**
  67. * Checks whether the notification has been dismissed.
  68. *
  69. * @return bool True when notification is dismissed.
  70. *
  71. * @codeCoverageIgnore
  72. */
  73. protected function is_notice_dismissed() {
  74. return get_user_meta( get_current_user_id(), 'wpseo-remove-' . $this->notification_identifier, true ) === '1';
  75. }
  76. /**
  77. * Retrieves the value where listener is listening for.
  78. *
  79. * @return string The listener value.
  80. *
  81. * @codeCoverageIgnore
  82. */
  83. protected function get_listener_value() {
  84. return filter_input( INPUT_GET, 'yoast_dismiss' );
  85. }
  86. /**
  87. * Dismisses the notification.
  88. *
  89. * @return void
  90. *
  91. * @codeCoverageIgnore
  92. */
  93. protected function set_dismissal_state() {
  94. update_user_meta( get_current_user_id(), 'wpseo-remove-' . $this->notification_identifier, true );
  95. }
  96. /**
  97. * Redirects the user back to the dashboard.
  98. *
  99. * @return void
  100. *
  101. * @codeCoverageIgnore
  102. */
  103. protected function redirect_to_dashboard() {
  104. wp_safe_redirect( admin_url( 'admin.php?page=wpseo_dashboard' ) );
  105. exit;
  106. }
  107. }