class-admin-gutenberg-compatibility-notification.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Handles the Gutenberg Compatibility notification showing and hiding.
  9. */
  10. class WPSEO_Admin_Gutenberg_Compatibility_Notification implements WPSEO_WordPress_Integration {
  11. /**
  12. * Notification ID to use.
  13. *
  14. * @var string
  15. */
  16. private $notification_id = 'wpseo-outdated-gutenberg-plugin';
  17. /**
  18. * Instance of gutenberg compatibility checker.
  19. *
  20. * @var WPSEO_Gutenberg_Compatibility
  21. */
  22. private $compatibility_checker;
  23. /**
  24. * Instance of Yoast Notification Center.
  25. *
  26. * @var Yoast_Notification_Center
  27. */
  28. private $notification_center;
  29. /**
  30. * WPSEO_Admin_Gutenberg_Compatibility_Notification constructor.
  31. */
  32. public function __construct() {
  33. $this->compatibility_checker = new WPSEO_Gutenberg_Compatibility();
  34. $this->notification_center = Yoast_Notification_Center::get();
  35. }
  36. /**
  37. * Registers all hooks to WordPress.
  38. *
  39. * @return void
  40. */
  41. public function register_hooks() {
  42. add_action( 'admin_init', [ $this, 'manage_notification' ] );
  43. }
  44. /**
  45. * Manages if the notification should be shown or removed.
  46. *
  47. * @return void
  48. */
  49. public function manage_notification() {
  50. if ( ! $this->compatibility_checker->is_installed() || $this->compatibility_checker->is_fully_compatible() ) {
  51. $this->notification_center->remove_notification_by_id( $this->notification_id );
  52. return;
  53. }
  54. $this->add_notification();
  55. }
  56. /**
  57. * Adds the notification to the notificaton center.
  58. *
  59. * @return void
  60. */
  61. private function add_notification() {
  62. $level = $this->compatibility_checker->is_below_minimum() ? Yoast_Notification::ERROR : Yoast_Notification::WARNING;
  63. $message = sprintf(
  64. /* translators: %1$s expands to Yoast SEO, %2$s expands to the installed version, %3$s expands to Gutenberg */
  65. __( '%1$s detected you are using version %2$s of %3$s, please update to the latest version to prevent compatibility issues.', 'wordpress-seo' ),
  66. 'Yoast SEO',
  67. $this->compatibility_checker->get_installed_version(),
  68. 'Gutenberg'
  69. );
  70. $notification = new Yoast_Notification(
  71. $message,
  72. [
  73. 'id' => $this->notification_id,
  74. 'type' => $level,
  75. 'priority' => 1,
  76. ]
  77. );
  78. $this->notification_center->add_notification( $notification );
  79. }
  80. }