[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
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 /** 13 * Taxonomies XML sitemap provider. 14 * 15 * @since 5.5.0 16 */ 17 class WP_Sitemaps_Taxonomies extends WP_Sitemaps_Provider { 18 /** 19 * WP_Sitemaps_Taxonomies constructor. 20 * 21 * @since 5.5.0 22 */ 23 public function __construct() { 24 $this->name = 'taxonomies'; 25 $this->object_type = 'term'; 26 } 27 28 /** 29 * Returns all public, registered taxonomies. 30 * 31 * @since 5.5.0 32 * 33 * @return WP_Taxonomy[] Array of registered taxonomy objects keyed by their name. 34 */ 35 public function get_object_subtypes() { 36 $taxonomies = get_taxonomies( array( 'public' => true ), 'objects' ); 37 38 $taxonomies = array_filter( $taxonomies, 'is_taxonomy_viewable' ); 39 40 /** 41 * Filters the list of taxonomy object subtypes available within the sitemap. 42 * 43 * @since 5.5.0 44 * 45 * @param WP_Taxonomy[] $taxonomies Array of registered taxonomy objects keyed by their name. 46 */ 47 return apply_filters( 'wp_sitemaps_taxonomies', $taxonomies ); 48 } 49 50 /** 51 * Gets a URL list for a taxonomy sitemap. 52 * 53 * @since 5.5.0 54 * @since 5.9.0 Renamed `$taxonomy` to `$object_subtype` to match parent class 55 * for PHP 8 named parameter support. 56 * 57 * @param int $page_num Page of results. 58 * @param string $object_subtype Optional. Taxonomy name. Default empty. 59 * @return array[] Array of URL information for a sitemap. 60 */ 61 public function get_url_list( $page_num, $object_subtype = '' ) { 62 // Restores the more descriptive, specific name for use within this method. 63 $taxonomy = $object_subtype; 64 $supported_types = $this->get_object_subtypes(); 65 66 // Bail early if the queried taxonomy is not supported. 67 if ( ! isset( $supported_types[ $taxonomy ] ) ) { 68 return array(); 69 } 70 71 /** 72 * Filters the taxonomies URL list before it is generated. 73 * 74 * Returning a non-null value will effectively short-circuit the generation, 75 * returning that value instead. 76 * 77 * @since 5.5.0 78 * 79 * @param array[]|null $url_list The URL list. Default null. 80 * @param string $taxonomy Taxonomy name. 81 * @param int $page_num Page of results. 82 */ 83 $url_list = apply_filters( 84 'wp_sitemaps_taxonomies_pre_url_list', 85 null, 86 $taxonomy, 87 $page_num 88 ); 89 90 if ( null !== $url_list ) { 91 return $url_list; 92 } 93 94 $url_list = array(); 95 96 // Offset by how many terms should be included in previous pages. 97 $offset = ( $page_num - 1 ) * wp_sitemaps_get_max_urls( $this->object_type ); 98 99 $args = $this->get_taxonomies_query_args( $taxonomy ); 100 $args['fields'] = 'all'; 101 $args['offset'] = $offset; 102 103 $taxonomy_terms = new WP_Term_Query( $args ); 104 105 if ( ! empty( $taxonomy_terms->terms ) ) { 106 foreach ( $taxonomy_terms->terms as $term ) { 107 $term_link = get_term_link( $term, $taxonomy ); 108 109 if ( is_wp_error( $term_link ) ) { 110 continue; 111 } 112 113 $sitemap_entry = array( 114 'loc' => $term_link, 115 ); 116 117 /** 118 * Filters the sitemap entry for an individual term. 119 * 120 * @since 5.5.0 121 * @since 6.0.0 Added `$term` argument containing the term object. 122 * 123 * @param array $sitemap_entry Sitemap entry for the term. 124 * @param int $term_id Term ID. 125 * @param string $taxonomy Taxonomy name. 126 * @param WP_Term $term Term object. 127 */ 128 $sitemap_entry = apply_filters( 'wp_sitemaps_taxonomies_entry', $sitemap_entry, $term->term_id, $taxonomy, $term ); 129 $url_list[] = $sitemap_entry; 130 } 131 } 132 133 return $url_list; 134 } 135 136 /** 137 * Gets the max number of pages available for the object type. 138 * 139 * @since 5.5.0 140 * @since 5.9.0 Renamed `$taxonomy` to `$object_subtype` to match parent class 141 * for PHP 8 named parameter support. 142 * 143 * @param string $object_subtype Optional. Taxonomy name. Default empty. 144 * @return int Total number of pages. 145 */ 146 public function get_max_num_pages( $object_subtype = '' ) { 147 if ( empty( $object_subtype ) ) { 148 return 0; 149 } 150 151 // Restores the more descriptive, specific name for use within this method. 152 $taxonomy = $object_subtype; 153 154 /** 155 * Filters the max number of pages for a taxonomy sitemap before it is generated. 156 * 157 * Passing a non-null value will short-circuit the generation, 158 * returning that value instead. 159 * 160 * @since 5.5.0 161 * 162 * @param int|null $max_num_pages The maximum number of pages. Default null. 163 * @param string $taxonomy Taxonomy name. 164 */ 165 $max_num_pages = apply_filters( 'wp_sitemaps_taxonomies_pre_max_num_pages', null, $taxonomy ); 166 167 if ( null !== $max_num_pages ) { 168 return $max_num_pages; 169 } 170 171 $term_count = wp_count_terms( $this->get_taxonomies_query_args( $taxonomy ) ); 172 173 return (int) ceil( $term_count / wp_sitemaps_get_max_urls( $this->object_type ) ); 174 } 175 176 /** 177 * Returns the query args for retrieving taxonomy terms to list in the sitemap. 178 * 179 * @since 5.5.0 180 * 181 * @param string $taxonomy Taxonomy name. 182 * @return array Array of WP_Term_Query arguments. 183 */ 184 protected function get_taxonomies_query_args( $taxonomy ) { 185 /** 186 * Filters the taxonomy terms query arguments. 187 * 188 * Allows modification of the taxonomy query arguments before querying. 189 * 190 * @see WP_Term_Query for a full list of arguments 191 * 192 * @since 5.5.0 193 * 194 * @param array $args Array of WP_Term_Query arguments. 195 * @param string $taxonomy Taxonomy name. 196 */ 197 $args = apply_filters( 198 'wp_sitemaps_taxonomies_query_args', 199 array( 200 'taxonomy' => $taxonomy, 201 'orderby' => 'term_order', 202 'number' => wp_sitemaps_get_max_urls( $this->object_type ), 203 'hide_empty' => true, 204 'hierarchical' => false, 205 'update_term_meta_cache' => false, 206 ), 207 $taxonomy 208 ); 209 210 return $args; 211 } 212 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Dec 22 01:00:02 2024 | Cross-referenced by PHPXref 0.7.1 |