class-wp-sitemaps-taxonomies.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. * @since 5.9.0 Renamed `$taxonomy` to `$object_subtype` to match parent class
  50. * for PHP 8 named parameter support.
  51. *
  52. * @param int $page_num Page of results.
  53. * @param string $object_subtype Optional. Taxonomy name. Default empty.
  54. * @return array[] Array of URL information for a sitemap.
  55. */
  56. public function get_url_list( $page_num, $object_subtype = '' ) {
  57. // Restores the more descriptive, specific name for use within this method.
  58. $taxonomy = $object_subtype;
  59. $supported_types = $this->get_object_subtypes();
  60. // Bail early if the queried taxonomy is not supported.
  61. if ( ! isset( $supported_types[ $taxonomy ] ) ) {
  62. return array();
  63. }
  64. /**
  65. * Filters the taxonomies URL list before it is generated.
  66. *
  67. * Returning a non-null value will effectively short-circuit the generation,
  68. * returning that value instead.
  69. *
  70. * @since 5.5.0
  71. *
  72. * @param array[]|null $url_list The URL list. Default null.
  73. * @param string $taxonomy Taxonomy name.
  74. * @param int $page_num Page of results.
  75. */
  76. $url_list = apply_filters(
  77. 'wp_sitemaps_taxonomies_pre_url_list',
  78. null,
  79. $taxonomy,
  80. $page_num
  81. );
  82. if ( null !== $url_list ) {
  83. return $url_list;
  84. }
  85. $url_list = array();
  86. // Offset by how many terms should be included in previous pages.
  87. $offset = ( $page_num - 1 ) * wp_sitemaps_get_max_urls( $this->object_type );
  88. $args = $this->get_taxonomies_query_args( $taxonomy );
  89. $args['fields'] = 'all';
  90. $args['offset'] = $offset;
  91. $taxonomy_terms = new WP_Term_Query( $args );
  92. if ( ! empty( $taxonomy_terms->terms ) ) {
  93. foreach ( $taxonomy_terms->terms as $term ) {
  94. $term_link = get_term_link( $term, $taxonomy );
  95. if ( is_wp_error( $term_link ) ) {
  96. continue;
  97. }
  98. $sitemap_entry = array(
  99. 'loc' => $term_link,
  100. );
  101. /**
  102. * Filters the sitemap entry for an individual term.
  103. *
  104. * @since 5.5.0
  105. * @since 6.0.0 Added `$term` argument containing the term object.
  106. *
  107. * @param array $sitemap_entry Sitemap entry for the term.
  108. * @param int $term_id Term ID.
  109. * @param string $taxonomy Taxonomy name.
  110. * @param WP_Term $term Term object.
  111. */
  112. $sitemap_entry = apply_filters( 'wp_sitemaps_taxonomies_entry', $sitemap_entry, $term->term_id, $taxonomy, $term );
  113. $url_list[] = $sitemap_entry;
  114. }
  115. }
  116. return $url_list;
  117. }
  118. /**
  119. * Gets the max number of pages available for the object type.
  120. *
  121. * @since 5.5.0
  122. * @since 5.9.0 Renamed `$taxonomy` to `$object_subtype` to match parent class
  123. * for PHP 8 named parameter support.
  124. *
  125. * @param string $object_subtype Optional. Taxonomy name. Default empty.
  126. * @return int Total number of pages.
  127. */
  128. public function get_max_num_pages( $object_subtype = '' ) {
  129. if ( empty( $object_subtype ) ) {
  130. return 0;
  131. }
  132. // Restores the more descriptive, specific name for use within this method.
  133. $taxonomy = $object_subtype;
  134. /**
  135. * Filters the max number of pages for a taxonomy sitemap before it is generated.
  136. *
  137. * Passing a non-null value will short-circuit the generation,
  138. * returning that value instead.
  139. *
  140. * @since 5.5.0
  141. *
  142. * @param int|null $max_num_pages The maximum number of pages. Default null.
  143. * @param string $taxonomy Taxonomy name.
  144. */
  145. $max_num_pages = apply_filters( 'wp_sitemaps_taxonomies_pre_max_num_pages', null, $taxonomy );
  146. if ( null !== $max_num_pages ) {
  147. return $max_num_pages;
  148. }
  149. $term_count = wp_count_terms( $this->get_taxonomies_query_args( $taxonomy ) );
  150. return (int) ceil( $term_count / wp_sitemaps_get_max_urls( $this->object_type ) );
  151. }
  152. /**
  153. * Returns the query args for retrieving taxonomy terms to list in the sitemap.
  154. *
  155. * @since 5.5.0
  156. *
  157. * @param string $taxonomy Taxonomy name.
  158. * @return array Array of WP_Term_Query arguments.
  159. */
  160. protected function get_taxonomies_query_args( $taxonomy ) {
  161. /**
  162. * Filters the taxonomy terms query arguments.
  163. *
  164. * Allows modification of the taxonomy query arguments before querying.
  165. *
  166. * @see WP_Term_Query for a full list of arguments
  167. *
  168. * @since 5.5.0
  169. *
  170. * @param array $args Array of WP_Term_Query arguments.
  171. * @param string $taxonomy Taxonomy name.
  172. */
  173. $args = apply_filters(
  174. 'wp_sitemaps_taxonomies_query_args',
  175. array(
  176. 'taxonomy' => $taxonomy,
  177. 'orderby' => 'term_order',
  178. 'number' => wp_sitemaps_get_max_urls( $this->object_type ),
  179. 'hide_empty' => true,
  180. 'hierarchical' => false,
  181. 'update_term_meta_cache' => false,
  182. ),
  183. $taxonomy
  184. );
  185. return $args;
  186. }
  187. }