class-link-content-processor.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Links
  6. */
  7. /**
  8. * Represents the content processor. It will extract links from the content and
  9. * saves them for the given post id.
  10. */
  11. class WPSEO_Link_Content_Processor {
  12. /**
  13. * Holds the link storage instance.
  14. *
  15. * @var WPSEO_Link_Storage
  16. */
  17. protected $storage;
  18. /**
  19. * Holds the meta storage instance.
  20. *
  21. * @var WPSEO_Meta_Storage
  22. */
  23. private $count_storage;
  24. /**
  25. * Sets an instance of a storage object.
  26. *
  27. * @param WPSEO_Link_Storage $storage The storage object to use.
  28. * @param WPSEO_Meta_Storage $count_storage The storage object for the link
  29. * counts.
  30. */
  31. public function __construct( WPSEO_Link_Storage $storage, WPSEO_Meta_Storage $count_storage ) {
  32. $this->storage = $storage;
  33. $this->count_storage = $count_storage;
  34. }
  35. /**
  36. * Process the content for the given post id.
  37. *
  38. * @param int $post_id The post id.
  39. * @param string $content The content to process.
  40. */
  41. public function process( $post_id, $content ) {
  42. $link_extractor = new WPSEO_Link_Extractor( $content );
  43. $link_processor = new WPSEO_Link_Factory(
  44. new WPSEO_Link_Type_Classifier( home_url() ),
  45. new WPSEO_Link_Internal_Lookup(),
  46. new WPSEO_Link_Filter( get_permalink( $post_id ) )
  47. );
  48. $extracted_links = $link_extractor->extract();
  49. $links = $link_processor->build( $extracted_links );
  50. $internal_links = array_filter( $links, [ $this, 'filter_internal_link' ] );
  51. $stored_links = $this->get_stored_internal_links( $post_id );
  52. $this->storage->cleanup( $post_id );
  53. $this->storage->save_links( $post_id, $links );
  54. $this->update_link_counts( $post_id, count( $internal_links ), array_merge( $stored_links, $internal_links ) );
  55. }
  56. /**
  57. * Updates the link counts for the post and referenced posts.
  58. *
  59. * @param int $post_id Post to update link counts for.
  60. * @param int|null $count Number of internal links.
  61. * @param array $links Links to process for incoming link count update.
  62. */
  63. public function update_link_counts( $post_id, $count, array $links ) {
  64. $this->store_internal_link_count( $post_id, $count );
  65. $this->update_incoming_links( $post_id, $links );
  66. }
  67. /**
  68. * Retrieves the stored internal links for the supplied post.
  69. *
  70. * @param int $post_id The post to fetch links for.
  71. *
  72. * @return WPSEO_Link[] List of internal links connected to the post.
  73. */
  74. public function get_stored_internal_links( $post_id ) {
  75. $links = $this->storage->get_links( $post_id );
  76. return array_filter( $links, [ $this, 'filter_internal_link' ] );
  77. }
  78. /**
  79. * Filters on INTERNAL links.
  80. *
  81. * @param WPSEO_Link $link Link to test type of.
  82. *
  83. * @return bool True for internal link, false for external link.
  84. */
  85. protected function filter_internal_link( WPSEO_Link $link ) {
  86. return $link->get_type() === WPSEO_Link::TYPE_INTERNAL;
  87. }
  88. /**
  89. * Stores the total links for the post.
  90. *
  91. * @param int $post_id The post id.
  92. * @param int $internal_link_count Total amount of links in the post.
  93. *
  94. * @return void
  95. */
  96. protected function store_internal_link_count( $post_id, $internal_link_count ) {
  97. $this->count_storage->save_meta_data( $post_id, [ 'internal_link_count' => $internal_link_count ] );
  98. }
  99. /**
  100. * Updates the incoming link count.
  101. *
  102. * @param int $post_id Post which is processed, this needs to be recalculated too.
  103. * @param WPSEO_Link[] $links Links to update the incoming link count of.
  104. *
  105. * @return void
  106. */
  107. protected function update_incoming_links( $post_id, $links ) {
  108. $post_ids = $this->get_internal_post_ids( $links );
  109. $post_ids = array_merge( [ $post_id ], $post_ids );
  110. $this->count_storage->update_incoming_link_count( $post_ids, $this->storage );
  111. }
  112. /**
  113. * Extract the post IDs from the links.
  114. *
  115. * @param WPSEO_Link[] $links Links to update the incoming link count of.
  116. *
  117. * @return int[] List of post IDs.
  118. */
  119. protected function get_internal_post_ids( $links ) {
  120. $post_ids = [];
  121. foreach ( $links as $link ) {
  122. $post_ids[] = $link->get_target_post_id();
  123. }
  124. return array_filter( $post_ids );
  125. }
  126. }