class-link-watcher.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Links
  6. */
  7. /**
  8. * Represents the link watcher. This class will watch for the save_post hook being called.
  9. */
  10. class WPSEO_Link_Watcher {
  11. /**
  12. * Represents the content processor. It will extract links from the content and saves them for the given post id.
  13. *
  14. * @var WPSEO_Link_Content_Processor
  15. */
  16. protected $content_processor;
  17. /**
  18. * WPSEO_Link_Watcher constructor.
  19. *
  20. * @param WPSEO_Link_Content_Processor $content_processor The processor to use.
  21. */
  22. public function __construct( WPSEO_Link_Content_Processor $content_processor ) {
  23. $this->content_processor = $content_processor;
  24. }
  25. /**
  26. * Registers the hooks.
  27. *
  28. * @returns void
  29. */
  30. public function register_hooks() {
  31. add_action( 'save_post', [ $this, 'save_post' ], 10, 2 );
  32. add_action( 'delete_post', [ $this, 'delete_post' ] );
  33. }
  34. /**
  35. * Saves the links that are used in the post.
  36. *
  37. * @param int $post_id The post id to.
  38. * @param WP_Post $post The post object.
  39. *
  40. * @return void
  41. */
  42. public function save_post( $post_id, WP_Post $post ) {
  43. /**
  44. * Filter: 'wpseo_should_index_links' - Allows disabling of Yoast's links indexation.
  45. *
  46. * @api bool To disable the indexation, return false.
  47. */
  48. if ( ! apply_filters( 'wpseo_should_index_links', true ) ) {
  49. return;
  50. }
  51. if ( ! WPSEO_Link_Table_Accessible::is_accessible() || ! WPSEO_Meta_Table_Accessible::is_accessible() ) {
  52. return;
  53. }
  54. // When the post is a revision.
  55. if ( wp_is_post_revision( $post->ID ) ) {
  56. return;
  57. }
  58. $post_statuses_to_skip = [ 'auto-draft', 'trash' ];
  59. if ( in_array( $post->post_status, $post_statuses_to_skip, true ) ) {
  60. return;
  61. }
  62. // When the post isn't processable, just remove the saved links.
  63. if ( ! $this->is_processable( $post_id ) ) {
  64. return;
  65. }
  66. $this->process( $post_id, $post->post_content );
  67. }
  68. /**
  69. * Removes the seo links when the post is deleted.
  70. *
  71. * @param int $post_id The post id.
  72. *
  73. * @return void
  74. */
  75. public function delete_post( $post_id ) {
  76. /** This filter is documented in admin/links/class-link-watcher.php */
  77. if ( ! apply_filters( 'wpseo_should_index_links', true ) ) {
  78. return;
  79. }
  80. if ( ! WPSEO_Link_Table_Accessible::is_accessible() || ! WPSEO_Meta_Table_Accessible::is_accessible() ) {
  81. return;
  82. }
  83. // Fetch links to update related linked objects.
  84. $links = $this->content_processor->get_stored_internal_links( $post_id );
  85. // Update the storage, remove all links for this post.
  86. $storage = new WPSEO_Link_Storage();
  87. $storage->cleanup( $post_id );
  88. // Update link counts for object and referenced links.
  89. $this->content_processor->update_link_counts( $post_id, 0, $links );
  90. }
  91. /**
  92. * Checks if the post is processable.
  93. *
  94. * @param int $post_id The post id.
  95. *
  96. * @return bool True when the post is processable.
  97. */
  98. protected function is_processable( $post_id ) {
  99. /*
  100. * Do not use the `wpseo_link_count_post_types` because we want to always count the links,
  101. * even if we don't show them.
  102. */
  103. $post_types = WPSEO_Post_Type::get_accessible_post_types();
  104. return isset( $post_types[ get_post_type( $post_id ) ] );
  105. }
  106. /**
  107. * Processes the content for the given post id.
  108. *
  109. * @param int $post_id The post id to process.
  110. * @param string $content The content to process.
  111. *
  112. * @return void
  113. */
  114. private function process( $post_id, $content ) {
  115. // Apply the filters to have the same content as shown on the frontend.
  116. $content = apply_filters( 'the_content', $content );
  117. $content = str_replace( ']]>', ']]&gt;', $content );
  118. $this->content_processor->process( $post_id, $content );
  119. }
  120. }