class-link-content-processor.php 4.0 KB

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