/admin/recalculate/class-recalculate-posts.php

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