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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. * @var WPSEO_Gutenberg_Compatibility
  19. */
  20. private $compatibility_checker;
  21. /**
  22. * @var Yoast_Notification_Center
  23. */
  24. private $notification_center;
  25. /**
  26. * WPSEO_Admin_Gutenberg_Compatibility_Notification constructor.
  27. */
  28. public function __construct() {
  29. $this->compatibility_checker = new WPSEO_Gutenberg_Compatibility();
  30. $this->notification_center = Yoast_Notification_Center::get();
  31. }
  32. /**
  33. * Registers all hooks to WordPress.
  34. *
  35. * @return void
  36. */
  37. public function register_hooks() {
  38. add_action( 'admin_init', array( $this, 'manage_notification' ) );
  39. }
  40. /**
  41. * Manages if the notification should be shown or removed.
  42. *
  43. * @return void
  44. */
  45. public function manage_notification() {
  46. if ( ! $this->compatibility_checker->is_installed() || $this->compatibility_checker->is_fully_compatible() ) {
  47. $this->notification_center->remove_notification_by_id( $this->notification_id );
  48. return;
  49. }
  50. $this->add_notification();
  51. }
  52. /**
  53. * Adds the notification to the notificaton center.
  54. *
  55. * @return void
  56. */
  57. private function add_notification() {
  58. $level = $this->compatibility_checker->is_below_minimum() ? Yoast_Notification::ERROR : Yoast_Notification::WARNING;
  59. $message = sprintf(
  60. /* translators: %1$s expands to Yoast SEO, %2$s expands to the installed version, %3$s expands to Gutenberg */
  61. __( '%1$s detected you are using version %2$s of %3$s, please update to the latest version to prevent compatibility issues.', 'wordpress-seo' ),
  62. 'Yoast SEO',
  63. $this->compatibility_checker->get_installed_version(),
  64. 'Gutenberg'
  65. );
  66. $notification = new Yoast_Notification(
  67. $message,
  68. array(
  69. 'id' => $this->notification_id,
  70. 'type' => $level,
  71. 'priority' => 1,
  72. )
  73. );
  74. $this->notification_center->add_notification( $notification );
  75. }
  76. }