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