/wp-content/plugins/wordpress-seo/admin/links/class-link-notifier.php

https://bitbucket.org/carloskikea/helpet · PHP · 127 lines · 67 code · 16 blank · 44 comment · 7 complexity · 26f0d3c87b2ac31c2920c9b7c2539c5f MD5 · raw file

  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Represents the notifier for adding link indexing notification to the dashboard.
  9. */
  10. class WPSEO_Link_Notifier {
  11. const NOTIFICATION_ID = 'wpseo-reindex-links';
  12. /**
  13. * Registers all hooks to WordPress
  14. */
  15. public function register_hooks() {
  16. if ( filter_input( INPUT_GET, 'page' ) === 'wpseo_dashboard' ) {
  17. add_action( 'admin_init', array( $this, 'cleanup_notification' ) );
  18. }
  19. if ( ! wp_next_scheduled( self::NOTIFICATION_ID ) ) {
  20. wp_schedule_event( time(), 'daily', self::NOTIFICATION_ID );
  21. }
  22. add_action( self::NOTIFICATION_ID, array( $this, 'manage_notification' ) );
  23. }
  24. /**
  25. * Removes the notification when it is set and the amount of unindexed items is lower than the threshold.
  26. */
  27. public function cleanup_notification() {
  28. if ( ! $this->has_notification() || $this->requires_notification() ) {
  29. return;
  30. }
  31. $this->remove_notification( $this->get_notification() );
  32. }
  33. /**
  34. * Adds the notification when it isn't set already and the amount of unindexed items is greater than the set.
  35. * threshold.
  36. */
  37. public function manage_notification() {
  38. if ( $this->has_notification() || ! $this->requires_notification() ) {
  39. return;
  40. }
  41. $this->add_notification( $this->get_notification() );
  42. }
  43. /**
  44. * Checks if the notification has been set already.
  45. *
  46. * @return bool True when there is a notification.
  47. */
  48. public function has_notification() {
  49. $notification = Yoast_Notification_Center::get()->get_notification_by_id( self::NOTIFICATION_ID );
  50. return $notification instanceof Yoast_Notification;
  51. }
  52. /**
  53. * Adds a notification to the notification center.
  54. *
  55. * @param Yoast_Notification $notification The notification to add.
  56. */
  57. protected function add_notification( Yoast_Notification $notification ) {
  58. Yoast_Notification_Center::get()->add_notification( $notification );
  59. }
  60. /**
  61. * Removes the notification from the notification center.
  62. *
  63. * @param Yoast_Notification $notification The notification to remove.
  64. */
  65. protected function remove_notification( Yoast_Notification $notification ) {
  66. Yoast_Notification_Center::get()->remove_notification( $notification );
  67. }
  68. /**
  69. * Returns an instance of the notification.
  70. *
  71. * @return Yoast_Notification The notification to show.
  72. */
  73. protected function get_notification() {
  74. return new Yoast_Notification(
  75. sprintf(
  76. /* translators: 1: link to yoast.com post about internal linking suggestion. 2: is anchor closing. 3: button to the recalculation option. 4: closing button */
  77. __(
  78. 'To make sure all the links in your texts are counted, we need to analyze all your texts.
  79. All you have to do is press the following button and we\'ll go through all your texts for you.
  80. %3$sCount links%4$s
  81. The Text link counter feature provides insights in how many links are found in your text and how many links are referring to your text. This is very helpful when you are improving your %1$sinternal linking%2$s.',
  82. 'wordpress-seo'
  83. ),
  84. '<a href="' . WPSEO_Shortlinker::get( 'https://yoa.st/15m' ) . '" target="_blank">',
  85. '</a>',
  86. '<button type="button" id="noticeRunLinkIndex" class="button">',
  87. '</button>'
  88. ),
  89. array(
  90. 'type' => Yoast_Notification::WARNING,
  91. 'id' => self::NOTIFICATION_ID,
  92. 'capabilities' => 'wpseo_manage_options',
  93. 'priority' => 0.8,
  94. )
  95. );
  96. }
  97. /**
  98. * Checks if the unindexed threshold is exceeded.
  99. *
  100. * @return bool True when the threshold is exceeded.
  101. */
  102. protected function requires_notification() {
  103. $post_types = apply_filters( 'wpseo_link_count_post_types', WPSEO_Post_Type::get_accessible_post_types() );
  104. if ( ! is_array( $post_types ) ) {
  105. return false;
  106. }
  107. return WPSEO_Link_Query::has_unprocessed_posts( $post_types );
  108. }
  109. }