PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.6
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.6
28.6 28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 All 130 releases
wordpress-seo / inc / sitemaps / class-taxonomy-sitemap-provider.php

class-taxonomy-sitemap-provider.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.6, at inc/sitemaps/class-taxonomy-sitemap-provider.php

323 lines 8.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO\XML_Sitemaps
6 */
7
8 /**
9 * Sitemap provider for author archives.
10 */
11 class WPSEO_Taxonomy_Sitemap_Provider implements WPSEO_Sitemap_Provider {
12
13 /**
14 * Holds image parser instance.
15 *
16 * @var WPSEO_Sitemap_Image_Parser
17 */
18 protected static $image_parser;
19
20 /**
21 * Determines whether images should be included in the XML sitemap.
22 *
23 * @var bool
24 */
25 private $include_images;
26
27 /**
28 * Set up object properties for data reuse.
29 */
30 public function __construct() {
31 /**
32 * Filter - Allows excluding images from the XML sitemap.
33 *
34 * @param bool $include True to include, false to exclude.
35 */
36 $this->include_images = apply_filters( 'wpseo_xml_sitemap_include_images', true );
37 }
38
39 /**
40 * Check if provider supports given item type.
41 *
42 * @param string $type Type string to check for.
43 *
44 * @return bool
45 */
46 public function handles_type( $type ) {
47
48 $taxonomy = get_taxonomy( $type );
49
50 if ( $taxonomy === false || ! $this->is_valid_taxonomy( $taxonomy->name ) || ! $taxonomy->public ) {
51 return false;
52 }
53
54 return true;
55 }
56
57 /**
58 * Retrieves the links for the sitemap.
59 *
60 * @param int $max_entries Entries per sitemap.
61 *
62 * @return array
63 */
64 public function get_index_links( $max_entries ) {
65
66 $taxonomies = get_taxonomies( [ 'public' => true ], 'objects' );
67
68 if ( empty( $taxonomies ) ) {
69 return [];
70 }
71
72 $taxonomy_names = array_filter( array_keys( $taxonomies ), [ $this, 'is_valid_taxonomy' ] );
73 $taxonomies = array_intersect_key( $taxonomies, array_flip( $taxonomy_names ) );
74
75 // Retrieve all the taxonomies and their terms so we can do a proper count on them.
76
77 /**
78 * Filter the setting of excluding empty terms from the XML sitemap.
79 *
80 * @param bool $exclude Defaults to true.
81 * @param array $taxonomy_names Array of names for the taxonomies being processed.
82 */
83 $hide_empty = apply_filters( 'wpseo_sitemap_exclude_empty_terms', true, $taxonomy_names );
84
85 $all_taxonomies = [];
86
87 foreach ( $taxonomy_names as $taxonomy_name ) {
88 /**
89 * Filter the setting of excluding empty terms from the XML sitemap for a specific taxonomy.
90 *
91 * @param bool $exclude Defaults to the sitewide setting.
92 * @param string $taxonomy_name The name of the taxonomy being processed.
93 */
94 $hide_empty_tax = apply_filters( 'wpseo_sitemap_exclude_empty_terms_taxonomy', $hide_empty, $taxonomy_name );
95
96 $term_args = [
97 'hide_empty' => $hide_empty_tax,
98 'fields' => 'ids',
99 ];
100 $taxonomy_terms = get_terms( $taxonomy_name, $term_args );
101
102 if ( count( $taxonomy_terms ) > 0 ) {
103 $all_taxonomies[ $taxonomy_name ] = $taxonomy_terms;
104 }
105 }
106
107 $index = [];
108
109 foreach ( $taxonomies as $tax_name => $tax ) {
110
111 if ( ! isset( $all_taxonomies[ $tax_name ] ) ) { // No eligible terms found.
112 continue;
113 }
114
115 $total_count = ( isset( $all_taxonomies[ $tax_name ] ) ) ? count( $all_taxonomies[ $tax_name ] ) : 1;
116 $max_pages = 1;
117
118 if ( $total_count > $max_entries ) {
119 $max_pages = (int) ceil( $total_count / $max_entries );
120 }
121
122 $last_modified_gmt = WPSEO_Sitemaps::get_last_modified_gmt( $tax->object_type );
123
124 for ( $page_counter = 0; $page_counter < $max_pages; $page_counter++ ) {
125
126 $current_page = ( $page_counter === 0 ) ? '' : ( $page_counter + 1 );
127
128 if ( ! is_array( $tax->object_type ) || count( $tax->object_type ) === 0 ) {
129 continue;
130 }
131
132 $terms = array_splice( $all_taxonomies[ $tax_name ], 0, $max_entries );
133
134 if ( ! $terms ) {
135 continue;
136 }
137
138 $args = [
139 'post_type' => $tax->object_type,
140 'tax_query' => [
141 [
142 'taxonomy' => $tax_name,
143 'terms' => $terms,
144 ],
145 ],
146 'orderby' => 'modified',
147 'order' => 'DESC',
148 'posts_per_page' => 1,
149 ];
150 $query = new WP_Query( $args );
151
152 if ( $query->have_posts() ) {
153 $date = $query->posts[0]->post_modified_gmt;
154 }
155 else {
156 $date = $last_modified_gmt;
157 }
158
159 $index[] = [
160 'loc' => WPSEO_Sitemaps_Router::get_base_url( $tax_name . '-sitemap' . $current_page . '.xml' ),
161 'lastmod' => $date,
162 ];
163 }
164 }
165
166 return $index;
167 }
168
169 /**
170 * Get set of sitemap link data.
171 *
172 * @param string $type Sitemap type.
173 * @param int $max_entries Entries per sitemap.
174 * @param int $current_page Current page of the sitemap.
175 *
176 * @return array
177 *
178 * @throws OutOfBoundsException When an invalid page is requested.
179 */
180 public function get_sitemap_links( $type, $max_entries, $current_page ) {
181 global $wpdb;
182
183 $links = [];
184 if ( ! $this->handles_type( $type ) ) {
185 return $links;
186 }
187
188 $taxonomy = get_taxonomy( $type );
189
190 $steps = $max_entries;
191 $offset = ( $current_page > 1 ) ? ( ( $current_page - 1 ) * $max_entries ) : 0;
192
193 /** This filter is documented in inc/sitemaps/class-taxonomy-sitemap-provider.php */
194 $hide_empty = apply_filters( 'wpseo_sitemap_exclude_empty_terms', true, [ $taxonomy->name ] );
195 /** This filter is documented in inc/sitemaps/class-taxonomy-sitemap-provider.php */
196 $hide_empty_tax = apply_filters( 'wpseo_sitemap_exclude_empty_terms_taxonomy', $hide_empty, $taxonomy->name );
197 $terms = get_terms(
198 [
199 'taxonomy' => $taxonomy->name,
200 'hide_empty' => $hide_empty_tax,
201 'update_term_meta_cache' => false,
202 'offset' => $offset,
203 'number' => $steps,
204 ]
205 );
206
207 // If there are no terms fetched for this range, we are on an invalid page.
208 if ( empty( $terms ) ) {
209 throw new OutOfBoundsException( 'Invalid sitemap page requested' );
210 }
211
212 $post_statuses = array_map( 'esc_sql', WPSEO_Sitemaps::get_post_statuses() );
213
214 // Grab last modified date.
215 $sql = "
216 SELECT MAX(p.post_modified_gmt) AS lastmod
217 FROM $wpdb->posts AS p
218 INNER JOIN $wpdb->term_relationships AS term_rel
219 ON term_rel.object_id = p.ID
220 INNER JOIN $wpdb->term_taxonomy AS term_tax
221 ON term_tax.term_taxonomy_id = term_rel.term_taxonomy_id
222 AND term_tax.taxonomy = %s
223 AND term_tax.term_id = %d
224 WHERE p.post_status IN ('" . implode( "','", $post_statuses ) . "')
225 AND p.post_password = ''
226 ";
227
228 /**
229 * Filter: 'wpseo_exclude_from_sitemap_by_term_ids' - Allow excluding terms by ID.
230 *
231 * @api array $terms_to_exclude The terms to exclude.
232 */
233 $terms_to_exclude = apply_filters( 'wpseo_exclude_from_sitemap_by_term_ids', [] );
234
235 foreach ( $terms as $term ) {
236
237 if ( in_array( $term->term_id, $terms_to_exclude, true ) ) {
238 continue;
239 }
240
241 $url = [];
242
243 $tax_noindex = WPSEO_Taxonomy_Meta::get_term_meta( $term, $term->taxonomy, 'noindex' );
244
245 if ( $tax_noindex === 'noindex' ) {
246 continue;
247 }
248
249 $url['loc'] = WPSEO_Taxonomy_Meta::get_term_meta( $term, $term->taxonomy, 'canonical' );
250
251 if ( ! is_string( $url['loc'] ) || $url['loc'] === '' ) {
252 $url['loc'] = get_term_link( $term, $term->taxonomy );
253 }
254
255 $url['mod'] = $wpdb->get_var( $wpdb->prepare( $sql, $term->taxonomy, $term->term_id ) );
256
257 if ( $this->include_images ) {
258 $url['images'] = $this->get_image_parser()->get_term_images( $term );
259 }
260
261 // Deprecated, kept for backwards data compat. R.
262 $url['chf'] = 'daily';
263 $url['pri'] = 1;
264
265 /** This filter is documented at inc/sitemaps/class-post-type-sitemap-provider.php */
266 $url = apply_filters( 'wpseo_sitemap_entry', $url, 'term', $term );
267
268 if ( ! empty( $url ) ) {
269 $links[] = $url;
270 }
271 }
272
273 return $links;
274 }
275
276 /**
277 * Check if taxonomy by name is valid to appear in sitemaps.
278 *
279 * @param string $taxonomy_name Taxonomy name to check.
280 *
281 * @return bool
282 */
283 public function is_valid_taxonomy( $taxonomy_name ) {
284
285 if ( WPSEO_Options::get( "noindex-tax-{$taxonomy_name}" ) === true ) {
286 return false;
287 }
288
289 if ( in_array( $taxonomy_name, [ 'link_category', 'nav_menu' ], true ) ) {
290 return false;
291 }
292
293 if ( $taxonomy_name === 'post_format' && WPSEO_Options::get( 'disable-post_format', false ) ) {
294 return false;
295 }
296
297 /**
298 * Filter to exclude the taxonomy from the XML sitemap.
299 *
300 * @param bool $exclude Defaults to false.
301 * @param string $taxonomy_name Name of the taxonomy to exclude..
302 */
303 if ( apply_filters( 'wpseo_sitemap_exclude_taxonomy', false, $taxonomy_name ) ) {
304 return false;
305 }
306
307 return true;
308 }
309
310 /**
311 * Get the Image Parser.
312 *
313 * @return WPSEO_Sitemap_Image_Parser
314 */
315 protected function get_image_parser() {
316 if ( ! isset( self::$image_parser ) ) {
317 self::$image_parser = new WPSEO_Sitemap_Image_Parser();
318 }
319
320 return self::$image_parser;
321 }
322 }
323