/admin/recalculate/class-recalculate-terms.php

  1. <?php 
  2. /** 
  3. * @package WPSEO\Admin 
  4. */ 
  5.  
  6. /** 
  7. * This class handles the calculation of the SEO score for all terms 
  8. */ 
  9. class WPSEO_Recalculate_Terms extends WPSEO_Recalculate { 
  10.  
  11. /** 
  12. * Save the scores. 
  13. * 
  14. * @param array $scores The scores to save. 
  15. */ 
  16. public function save_scores( array $scores ) { 
  17.  
  18. $tax_meta = get_option( 'wpseo_taxonomy_meta' ); 
  19.  
  20. foreach ( $scores as $score ) { 
  21. $tax_meta[ $score['taxonomy'] ][ $score['item_id'] ]['wpseo_linkdex'] = $score['score']; 
  22.  
  23. update_option( 'wpseo_taxonomy_meta', $tax_meta ); 
  24.  
  25. /** 
  26. * Save the score. 
  27. * 
  28. * @param array $score The score to save. 
  29. */ 
  30. protected function save_score( array $score ) { 
  31. WPSEO_Meta::set_value( 'linkdex', $score['score'], $score['item_id'] ); 
  32.  
  33. /** 
  34. * Get the terms from the database by doing a WP_Query. 
  35. * 
  36. * @param integer $paged The page. 
  37. * 
  38. * @return array 
  39. */ 
  40. protected function get_items( $paged ) { 
  41. $items_per_page = max( 1, $this->items_per_page ); 
  42.  
  43. return get_terms( 
  44. get_taxonomies(),  
  45. array( 
  46. 'hide_empty' => false,  
  47. 'number' => $items_per_page,  
  48. 'offset' => $items_per_page * abs( $paged - 1 ),  
  49. ); 
  50.  
  51. /** 
  52. * Convert the given term into a analyzable object. 
  53. * 
  54. * @param mixed $item The term for which to build the analyzer data. 
  55. * 
  56. * @return array 
  57. */ 
  58. protected function item_to_response( $item ) { 
  59. $focus_keyword = $this->get_focus_keyword( $item ); 
  60. $title = str_replace( ' %%page%% ', ' ', $this->get_title( $item ) ); 
  61. $meta = $this->get_meta_description( $item ); 
  62.  
  63. $description = $item->description; 
  64.  
  65. /** 
  66. * Filter the term description for recalculation. 
  67. * 
  68. * @param string $description Content of the term. Modify to reflect front-end content. 
  69. * @oaram WP_Term $item The term the description comes from. 
  70. */ 
  71. $description = apply_filters( 'wpseo_term_description_for_recalculation', $description, $item ); 
  72.  
  73. return array( 
  74. 'term_id' => $item->term_id,  
  75. 'taxonomy' => $item->taxonomy,  
  76. 'text' => $description,  
  77. 'keyword' => $focus_keyword,  
  78. 'url' => urldecode( $item->slug ),  
  79. 'pageTitle' => apply_filters( 'wpseo_title', wpseo_replace_vars( $title, $item, array( 'page' ) ) ),  
  80. 'meta' => apply_filters( 'wpseo_metadesc', wpseo_replace_vars( $meta, $item ) ),  
  81. 'keyword_usage' => array( 
  82. $focus_keyword => WPSEO_Taxonomy_Meta::get_keyword_usage( $focus_keyword, $item->term_id, $item->taxonomy ),  
  83. ),  
  84. ); 
  85.  
  86. /** 
  87. * Gets the focus keyword for the term 
  88. * 
  89. * @param stdClass|WP_Term $term Term to determine the keyword for. 
  90. * 
  91. * @return bool|string 
  92. */ 
  93. private function get_focus_keyword( $term ) { 
  94. $focus_keyword = WPSEO_Taxonomy_Meta::get_term_meta( 'focuskw', $term->term_id, $term->taxonomy ); 
  95. if ( ! empty( $focus_keyword ) ) { 
  96. return $focus_keyword; 
  97.  
  98. return $term->name; 
  99.  
  100. /** 
  101. * Get the title for given term 
  102. * 
  103. * @param stdClass|WP_Term $term The term object. 
  104. * 
  105. * @return mixed|string 
  106. */ 
  107. private function get_title( $term ) { 
  108. $title = WPSEO_Taxonomy_Meta::get_term_meta( $term->term_id, $term->taxonomy, 'title' ); 
  109. if ( '' !== $title ) { 
  110. return $title; 
  111.  
  112. $default_from_options = $this->default_from_options( 'title-tax', $term->taxonomy ); 
  113. if ( false !== $default_from_options ) { 
  114. return $default_from_options; 
  115.  
  116. return '%%title%%'; 
  117.  
  118. /** 
  119. * Get the meta description for given post 
  120. * 
  121. * @param stdClass|WP_Term $term The term object. 
  122. * 
  123. * @return bool|string 
  124. */ 
  125. private function get_meta_description( $term ) { 
  126. $meta_description = WPSEO_Taxonomy_Meta::get_term_meta( $term->term_id, $term->taxonomy, 'desc' ); 
  127. if ( '' !== $meta_description ) { 
  128. return $meta_description; 
  129.  
  130. $default_from_options = $this->default_from_options( 'metadesc-tax', $term->taxonomy ); 
  131. if ( false !== $default_from_options ) { 
  132. return $default_from_options; 
  133.  
  134. return ''; 
.