class-recalculate-posts.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * This class handles the calculation of the SEO score for all posts with a filled focus keyword.
  9. */
  10. class WPSEO_Recalculate_Posts extends WPSEO_Recalculate {
  11. /**
  12. * Save the scores.
  13. *
  14. * @param array $scores The scores for the posts.
  15. */
  16. public function save_scores( array $scores ) {
  17. foreach ( $scores as $score ) {
  18. $this->save_score( $score );
  19. }
  20. }
  21. /**
  22. * Save the score.
  23. *
  24. * @param array $score The score to save.
  25. */
  26. protected function save_score( array $score ) {
  27. WPSEO_Meta::set_value( 'linkdex', $score['score'], $score['item_id'] );
  28. }
  29. /**
  30. * Get the posts from the database by doing a WP_Query.
  31. *
  32. * @param integer $paged The page.
  33. *
  34. * @return string
  35. */
  36. protected function get_items( $paged ) {
  37. $items_per_page = max( 1, $this->items_per_page );
  38. $post_query = new WP_Query(
  39. [
  40. 'post_type' => 'any',
  41. 'meta_key' => '_yoast_wpseo_focuskw',
  42. 'posts_per_page' => $items_per_page,
  43. 'paged' => $paged,
  44. ]
  45. );
  46. return $post_query->get_posts();
  47. }
  48. /**
  49. * Map the posts to a response array.
  50. *
  51. * @param WP_Post $item The post for which to build the analyzer data.
  52. *
  53. * @return array
  54. */
  55. protected function item_to_response( $item ) {
  56. $focus_keyword = WPSEO_Meta::get_value( 'focuskw', $item->ID );
  57. $content = $item->post_content;
  58. // Check if there's a featured image.
  59. $content .= $this->add_featured_image( $item );
  60. /**
  61. * Filter the post content for use in the SEO score recalculation.
  62. *
  63. * @param string $content Content of the post. Modify to reflect front-end content.
  64. * @param WP_Post $item The Post object the content comes from.
  65. */
  66. $content = apply_filters( 'wpseo_post_content_for_recalculation', $content, $item );
  67. // Apply shortcodes.
  68. $content = do_shortcode( $content );
  69. return [
  70. 'post_id' => $item->ID,
  71. 'text' => $content,
  72. 'keyword' => $focus_keyword,
  73. 'url' => urldecode( $item->post_name ),
  74. 'pageTitle' => apply_filters( 'wpseo_title', wpseo_replace_vars( $this->get_title( $item->ID, $item->post_type ), $item ) ),
  75. 'meta' => apply_filters( 'wpseo_metadesc', wpseo_replace_vars( $this->get_meta_description( $item->ID, $item->post_type ), $item ) ),
  76. 'keyword_usage' => [
  77. $focus_keyword => WPSEO_Meta::keyword_usage( $focus_keyword, $item->ID ),
  78. ],
  79. ];
  80. }
  81. /**
  82. * Get the title for given post.
  83. *
  84. * @param integer $post_id The ID of the post for which to get the title.
  85. * @param string $post_type The post type.
  86. *
  87. * @return mixed|string
  88. */
  89. private function get_title( $post_id, $post_type ) {
  90. $title = WPSEO_Meta::get_value( 'title', $post_id );
  91. if ( '' !== $title ) {
  92. return $title;
  93. }
  94. $default_from_options = $this->default_from_options( 'title-tax', $post_type );
  95. if ( false !== $default_from_options ) {
  96. return str_replace( ' %%page%% ', ' ', $default_from_options );
  97. }
  98. return '%%title%%';
  99. }
  100. /**
  101. * Get the meta description for given post.
  102. *
  103. * @param integer $post_id The ID of the post for which to get the meta description.
  104. * @param string $post_type The post type.
  105. *
  106. * @return bool|string
  107. */
  108. private function get_meta_description( $post_id, $post_type ) {
  109. $meta_description = WPSEO_Meta::get_value( 'metadesc', $post_id );
  110. if ( '' !== $meta_description ) {
  111. return $meta_description;
  112. }
  113. $default_from_options = $this->default_from_options( 'metadesc', $post_type );
  114. if ( false !== $default_from_options ) {
  115. return $default_from_options;
  116. }
  117. return '';
  118. }
  119. /**
  120. * Retrieves the associated featured image if there is one present.
  121. *
  122. * @param WP_Post $item The post item to check for a featured image.
  123. *
  124. * @return string The image string.
  125. */
  126. private function add_featured_image( $item ) {
  127. if ( ! has_post_thumbnail( $item->ID ) ) {
  128. return '';
  129. }
  130. return ' ' . get_the_post_thumbnail( $item->ID );
  131. }
  132. }