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