/wp-content/plugins/wordpress-seo/admin/links/class-link-reindex-post-service.php

https://bitbucket.org/carloskikea/helpet · PHP · 95 lines · 39 code · 14 blank · 42 comment · 4 complexity · 640cd2fadf8de0642a6254d1d30ff652 MD5 · raw file

  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Links\Reindex
  6. */
  7. /**
  8. * Class WPSEO_Link_Reindex_Post_Service
  9. */
  10. class WPSEO_Link_Reindex_Post_Service {
  11. /**
  12. * Reindexes the unprocessed posts by REST request.
  13. *
  14. * @return WP_REST_Response The response object.
  15. */
  16. public function reindex() {
  17. return new WP_REST_Response( $this->process_posts() );
  18. }
  19. /**
  20. * Returns the posts.
  21. *
  22. * @return int The total amount of unprocessed posts.
  23. */
  24. protected function process_posts() {
  25. if ( ! $this->is_processable() ) {
  26. return 0;
  27. }
  28. $posts = $this->get_unprocessed_posts();
  29. array_walk( $posts, array( $this, 'process_post' ) );
  30. return count( $posts );
  31. }
  32. /**
  33. * Returns all unprocessed posts.
  34. *
  35. * @return array The unprocessed posts.
  36. */
  37. protected function get_unprocessed_posts() {
  38. $post_types = apply_filters( 'wpseo_link_count_post_types', WPSEO_Post_Type::get_accessible_post_types() );
  39. if ( ! is_array( $post_types ) ) {
  40. return array();
  41. }
  42. return WPSEO_Link_Query::get_unprocessed_posts( $post_types );
  43. }
  44. /**
  45. * Checks if the required tables are accessible.
  46. *
  47. * @return bool True when the tables are accessible.
  48. */
  49. protected function is_processable() {
  50. return WPSEO_Link_Table_Accessible::is_accessible() && WPSEO_Meta_Table_Accessible::is_accessible();
  51. }
  52. /**
  53. * Processes the post.
  54. *
  55. * @param stdObject $post The post to process.
  56. *
  57. * @return void
  58. */
  59. protected function process_post( $post ) {
  60. // Some plugins might output data, so let's buffer this to prevent wrong responses.
  61. ob_start();
  62. // Apply the filters to have the same content as shown on the frontend.
  63. $content = apply_filters( 'the_content', $post->post_content );
  64. $content = str_replace( ']]>', ']]&gt;', $content );
  65. ob_end_clean();
  66. $content_processor = $this->get_content_processor();
  67. $content_processor->process( $post->ID, $content );
  68. }
  69. /**
  70. * Returns an instance of the content processor.
  71. *
  72. * @return WPSEO_Link_Content_Processor The instance of the link content processor.
  73. */
  74. protected function get_content_processor() {
  75. static $content_processor;
  76. if ( $content_processor === null ) {
  77. $content_processor = new WPSEO_Link_Content_Processor( new WPSEO_Link_Storage(), new WPSEO_Meta_Storage() );
  78. }
  79. return $content_processor;
  80. }
  81. }