class-link-watcher.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. /** @var WPSEO_Link_Content_Processor */
  12. protected $content_processor;
  13. /**
  14. * WPSEO_Link_Watcher constructor.
  15. *
  16. * @param WPSEO_Link_Content_Processor $content_processor The processor to use.
  17. */
  18. public function __construct( WPSEO_Link_Content_Processor $content_processor ) {
  19. $this->content_processor = $content_processor;
  20. }
  21. /**
  22. * Registers the hooks.
  23. *
  24. * @returns void
  25. */
  26. public function register_hooks() {
  27. add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
  28. add_action( 'delete_post', array( $this, 'delete_post' ) );
  29. }
  30. /**
  31. * Saves the links that are used in the post.
  32. *
  33. * @param int $post_id The post id to.
  34. * @param WP_Post $post The post object.
  35. *
  36. * @return void
  37. */
  38. public function save_post( $post_id, WP_Post $post ) {
  39. if ( ! WPSEO_Link_Table_Accessible::is_accessible() || ! WPSEO_Meta_Table_Accessible::is_accessible() ) {
  40. return;
  41. }
  42. // When the post is a revision.
  43. if ( wp_is_post_revision( $post->ID ) ) {
  44. return;
  45. }
  46. $post_statuses_to_skip = array( 'auto-draft', 'trash' );
  47. if ( in_array( $post->post_status, $post_statuses_to_skip, true ) ) {
  48. return;
  49. }
  50. // When the post isn't processable, just remove the saved links.
  51. if ( ! $this->is_processable( $post_id ) ) {
  52. return;
  53. }
  54. $this->process( $post_id, $post->post_content );
  55. }
  56. /**
  57. * Removes the seo links when the post is deleted.
  58. *
  59. * @param int $post_id The post id.
  60. *
  61. * @return void
  62. */
  63. public function delete_post( $post_id ) {
  64. if ( ! WPSEO_Link_Table_Accessible::is_accessible() || ! WPSEO_Meta_Table_Accessible::is_accessible() ) {
  65. return;
  66. }
  67. // Fetch links to update related linked objects.
  68. $links = $this->content_processor->get_stored_internal_links( $post_id );
  69. // Update the storage, remove all links for this post.
  70. $storage = new WPSEO_Link_Storage();
  71. $storage->cleanup( $post_id );
  72. // Update link counts for object and referenced links.
  73. $this->content_processor->update_link_counts( $post_id, 0, $links );
  74. }
  75. /**
  76. * Checks if the post is processable.
  77. *
  78. * @param int $post_id The post id.
  79. *
  80. * @return bool True when the post is processable.
  81. */
  82. protected function is_processable( $post_id ) {
  83. /*
  84. * Do not use the `wpseo_link_count_post_types` because we want to always count the links,
  85. * even if we don't show them.
  86. */
  87. $post_types = WPSEO_Post_Type::get_accessible_post_types();
  88. return isset( $post_types[ get_post_type( $post_id ) ] );
  89. }
  90. /**
  91. * Processes the content for the given post id.
  92. *
  93. * @param int $post_id The post id to process.
  94. * @param string $content The content to process.
  95. *
  96. * @return void
  97. */
  98. private function process( $post_id, $content ) {
  99. // Apply the filters to have the same content as shown on the frontend.
  100. $content = apply_filters( 'the_content', $content );
  101. $content = str_replace( ']]>', ']]&gt;', $content );
  102. $this->content_processor->process( $post_id, $content );
  103. }
  104. }