class-admin-media-purge-notification.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Handles the media purge notification showing and hiding.
  9. */
  10. class WPSEO_Admin_Media_Purge_Notification implements WPSEO_WordPress_Integration {
  11. /**
  12. * Notification ID to use.
  13. *
  14. * @var string
  15. */
  16. private $notification_id = 'wpseo_media_purge';
  17. /**
  18. * Registers all hooks to WordPress.
  19. *
  20. * @return void
  21. */
  22. public function register_hooks() {
  23. add_action( 'admin_init', [ $this, 'manage_notification' ] );
  24. add_filter( 'wpseo_option_tab-metas_media', [ $this, 'output_hidden_setting' ] );
  25. // Dismissing is just setting the relevancy to false, which cancels out any functionality.
  26. if ( WPSEO_Utils::is_yoast_seo_page() && filter_input( INPUT_GET, 'dismiss' ) === $this->notification_id ) {
  27. WPSEO_Options::set( 'is-media-purge-relevant', false );
  28. }
  29. }
  30. /**
  31. * Adds a hidden setting to the media tab.
  32. *
  33. * To make sure the setting is not reverted to the default when -anything-
  34. * is saved on the entire page (not just the media tab).
  35. *
  36. * @param string|null $input Current filter value.
  37. *
  38. * @return string|null
  39. */
  40. public function output_hidden_setting( $input ) {
  41. $form = Yoast_Form::get_instance();
  42. $form->hidden( 'is-media-purge-relevant' );
  43. return $input;
  44. }
  45. /**
  46. * Manages if the notification should be shown or removed.
  47. *
  48. * @return void
  49. */
  50. public function manage_notification() {
  51. $this->remove_notification();
  52. }
  53. /**
  54. * Retrieves the notification that should be shown or removed.
  55. *
  56. * @return Yoast_Notification The notification to use.
  57. */
  58. private function get_notification() {
  59. $content = sprintf(
  60. /* translators: %1$s expands to the link to the article, %2$s closes the link tag. */
  61. __( 'Your site\'s settings currently allow attachment URLs on your site to exist. Please read %1$sthis post about a potential issue%2$s with attachment URLs and check whether you have the correct setting for your site.', 'wordpress-seo' ),
  62. '<a href="' . esc_url( WPSEO_Shortlinker::get( 'https://yoa.st/2r8' ) ) . '" rel="noopener noreferrer" target="_blank">',
  63. '</a>'
  64. );
  65. $content .= '<br><br>';
  66. $content .= sprintf(
  67. /* translators: %1$s dismiss link open tag, %2$s closes the link tag. */
  68. __( 'If you know what this means and you do not want to see this message anymore, you can %1$sdismiss this message%2$s.', 'wordpress-seo' ),
  69. '<a href="' . esc_url( admin_url( 'admin.php?page=wpseo_dashboard&dismiss=' . $this->notification_id ) ) . '">',
  70. '</a>'
  71. );
  72. return new Yoast_Notification(
  73. $content,
  74. [
  75. 'type' => Yoast_Notification::ERROR,
  76. 'id' => $this->notification_id,
  77. 'capabilities' => 'wpseo_manage_options',
  78. 'priority' => 1,
  79. ]
  80. );
  81. }
  82. /**
  83. * Adds the notification to the notificaton center.
  84. *
  85. * @return void
  86. */
  87. private function add_notification() {
  88. $notification_center = Yoast_Notification_Center::get();
  89. $notification_center->add_notification( $this->get_notification() );
  90. }
  91. /**
  92. * Removes the notification from the notification center.
  93. *
  94. * @return void
  95. */
  96. private function remove_notification() {
  97. $notification_center = Yoast_Notification_Center::get();
  98. $notification_center->remove_notification( $this->get_notification() );
  99. }
  100. }