Bez popisu

class-wp-sitemaps-taxonomies.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * Sitemaps: WP_Sitemaps_Taxonomies class
  4. *
  5. * Builds the sitemaps for the 'taxonomy' object type.
  6. *
  7. * @package WordPress
  8. * @subpackage Sitemaps
  9. * @since 5.5.0
  10. */
  11. /**
  12. * Taxonomies XML sitemap provider.
  13. *
  14. * @since 5.5.0
  15. */
  16. class WP_Sitemaps_Taxonomies extends WP_Sitemaps_Provider {
  17. /**
  18. * WP_Sitemaps_Taxonomies constructor.
  19. *
  20. * @since 5.5.0
  21. */
  22. public function __construct() {
  23. $this->name = 'taxonomies';
  24. $this->object_type = 'term';
  25. }
  26. /**
  27. * Returns all public, registered taxonomies.
  28. *
  29. * @since 5.5.0
  30. *
  31. * @return WP_Taxonomy[] Array of registered taxonomy objects keyed by their name.
  32. */
  33. public function get_object_subtypes() {
  34. $taxonomies = get_taxonomies( array( 'public' => true ), 'objects' );
  35. $taxonomies = array_filter( $taxonomies, 'is_taxonomy_viewable' );
  36. /**
  37. * Filters the list of taxonomy object subtypes available within the sitemap.
  38. *
  39. * @since 5.5.0
  40. *
  41. * @param WP_Taxonomy[] $taxonomies Array of registered taxonomy objects keyed by their name.
  42. */
  43. return apply_filters( 'wp_sitemaps_taxonomies', $taxonomies );
  44. }
  45. /**
  46. * Gets a URL list for a taxonomy sitemap.
  47. *
  48. * @since 5.5.0
  49. *
  50. * @param int $page_num Page of results.
  51. * @param string $taxonomy Optional. Taxonomy name. Default empty.
  52. * @return array Array of URLs for a sitemap.
  53. */
  54. public function get_url_list( $page_num, $taxonomy = '' ) {
  55. $supported_types = $this->get_object_subtypes();
  56. // Bail early if the queried taxonomy is not supported.
  57. if ( ! isset( $supported_types[ $taxonomy ] ) ) {
  58. return array();
  59. }
  60. /**
  61. * Filters the taxonomies URL list before it is generated.
  62. *
  63. * Passing a non-null value will effectively short-circuit the generation,
  64. * returning that value instead.
  65. *
  66. * @since 5.5.0
  67. *
  68. * @param array $url_list The URL list. Default null.
  69. * @param string $taxonomy Taxonomy name.
  70. * @param int $page_num Page of results.
  71. */
  72. $url_list = apply_filters(
  73. 'wp_sitemaps_taxonomies_pre_url_list',
  74. null,
  75. $taxonomy,
  76. $page_num
  77. );
  78. if ( null !== $url_list ) {
  79. return $url_list;
  80. }
  81. $url_list = array();
  82. // Offset by how many terms should be included in previous pages.
  83. $offset = ( $page_num - 1 ) * wp_sitemaps_get_max_urls( $this->object_type );
  84. $args = $this->get_taxonomies_query_args( $taxonomy );
  85. $args['offset'] = $offset;
  86. $taxonomy_terms = new WP_Term_Query( $args );
  87. if ( ! empty( $taxonomy_terms->terms ) ) {
  88. foreach ( $taxonomy_terms->terms as $term ) {
  89. $term_link = get_term_link( $term, $taxonomy );
  90. if ( is_wp_error( $term_link ) ) {
  91. continue;
  92. }
  93. $sitemap_entry = array(
  94. 'loc' => $term_link,
  95. );
  96. /**
  97. * Filters the sitemap entry for an individual term.
  98. *
  99. * @since 5.5.0
  100. *
  101. * @param array $sitemap_entry Sitemap entry for the term.
  102. * @param WP_Term $term Term object.
  103. * @param string $taxonomy Taxonomy name.
  104. */
  105. $sitemap_entry = apply_filters( 'wp_sitemaps_taxonomies_entry', $sitemap_entry, $term, $taxonomy );
  106. $url_list[] = $sitemap_entry;
  107. }
  108. }
  109. return $url_list;
  110. }
  111. /**
  112. * Gets the max number of pages available for the object type.
  113. *
  114. * @since 5.5.0
  115. *
  116. * @param string $taxonomy Taxonomy name.
  117. * @return int Total number of pages.
  118. */
  119. public function get_max_num_pages( $taxonomy = '' ) {
  120. if ( empty( $taxonomy ) ) {
  121. return 0;
  122. }
  123. /**
  124. * Filters the max number of pages before it is generated.
  125. *
  126. * Passing a non-null value will short-circuit the generation,
  127. * returning that value instead.
  128. *
  129. * @since 5.5.0
  130. *
  131. * @param int $max_num_pages The maximum number of pages. Default null.
  132. * @param string $taxonomy Taxonomy name.
  133. */
  134. $max_num_pages = apply_filters( 'wp_sitemaps_taxonomies_pre_max_num_pages', null, $taxonomy );
  135. if ( null !== $max_num_pages ) {
  136. return $max_num_pages;
  137. }
  138. $term_count = wp_count_terms( $this->get_taxonomies_query_args( $taxonomy ) );
  139. return (int) ceil( $term_count / wp_sitemaps_get_max_urls( $this->object_type ) );
  140. }
  141. /**
  142. * Returns the query args for retrieving taxonomy terms to list in the sitemap.
  143. *
  144. * @since 5.5.0
  145. *
  146. * @param string $taxonomy Taxonomy name.
  147. * @return array Array of WP_Term_Query arguments.
  148. */
  149. protected function get_taxonomies_query_args( $taxonomy ) {
  150. /**
  151. * Filters the taxonomy terms query arguments.
  152. *
  153. * Allows modification of the taxonomy query arguments before querying.
  154. *
  155. * @see WP_Term_Query for a full list of arguments
  156. *
  157. * @since 5.5.0
  158. *
  159. * @param array $args Array of WP_Term_Query arguments.
  160. * @param string $taxonomy Taxonomy name.
  161. */
  162. $args = apply_filters(
  163. 'wp_sitemaps_taxonomies_query_args',
  164. array(
  165. 'fields' => 'ids',
  166. 'taxonomy' => $taxonomy,
  167. 'orderby' => 'term_order',
  168. 'number' => wp_sitemaps_get_max_urls( $this->object_type ),
  169. 'hide_empty' => true,
  170. 'hierarchical' => false,
  171. 'update_term_meta_cache' => false,
  172. ),
  173. $taxonomy
  174. );
  175. return $args;
  176. }
  177. }