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