balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

class-wp-sitemaps-taxonomies.php (5173B)


      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 	 *
     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 				$term_link = get_term_link( $term, $taxonomy );
    103 
    104 				if ( is_wp_error( $term_link ) ) {
    105 					continue;
    106 				}
    107 
    108 				$sitemap_entry = array(
    109 					'loc' => $term_link,
    110 				);
    111 
    112 				/**
    113 				 * Filters the sitemap entry for an individual term.
    114 				 *
    115 				 * @since 5.5.0
    116 				 *
    117 				 * @param array   $sitemap_entry Sitemap entry for the term.
    118 				 * @param WP_Term $term          Term object.
    119 				 * @param string  $taxonomy      Taxonomy name.
    120 				 */
    121 				$sitemap_entry = apply_filters( 'wp_sitemaps_taxonomies_entry', $sitemap_entry, $term, $taxonomy );
    122 				$url_list[]    = $sitemap_entry;
    123 			}
    124 		}
    125 
    126 		return $url_list;
    127 	}
    128 
    129 	/**
    130 	 * Gets the max number of pages available for the object type.
    131 	 *
    132 	 * @since 5.5.0
    133 	 *
    134 	 * @param string $taxonomy Taxonomy name.
    135 	 * @return int Total number of pages.
    136 	 */
    137 	public function get_max_num_pages( $taxonomy = '' ) {
    138 		if ( empty( $taxonomy ) ) {
    139 			return 0;
    140 		}
    141 
    142 		/**
    143 		 * Filters the max number of pages before it is generated.
    144 		 *
    145 		 * Passing a non-null value will short-circuit the generation,
    146 		 * returning that value instead.
    147 		 *
    148 		 * @since 5.5.0
    149 		 *
    150 		 * @param int    $max_num_pages The maximum number of pages. Default null.
    151 		 * @param string $taxonomy      Taxonomy name.
    152 		 */
    153 		$max_num_pages = apply_filters( 'wp_sitemaps_taxonomies_pre_max_num_pages', null, $taxonomy );
    154 
    155 		if ( null !== $max_num_pages ) {
    156 			return $max_num_pages;
    157 		}
    158 
    159 		$term_count = wp_count_terms( $this->get_taxonomies_query_args( $taxonomy ) );
    160 
    161 		return (int) ceil( $term_count / wp_sitemaps_get_max_urls( $this->object_type ) );
    162 	}
    163 
    164 	/**
    165 	 * Returns the query args for retrieving taxonomy terms to list in the sitemap.
    166 	 *
    167 	 * @since 5.5.0
    168 	 *
    169 	 * @param string $taxonomy Taxonomy name.
    170 	 * @return array Array of WP_Term_Query arguments.
    171 	 */
    172 	protected function get_taxonomies_query_args( $taxonomy ) {
    173 		/**
    174 		 * Filters the taxonomy terms query arguments.
    175 		 *
    176 		 * Allows modification of the taxonomy query arguments before querying.
    177 		 *
    178 		 * @see WP_Term_Query for a full list of arguments
    179 		 *
    180 		 * @since 5.5.0
    181 		 *
    182 		 * @param array  $args     Array of WP_Term_Query arguments.
    183 		 * @param string $taxonomy Taxonomy name.
    184 		 */
    185 		$args = apply_filters(
    186 			'wp_sitemaps_taxonomies_query_args',
    187 			array(
    188 				'fields'                 => 'ids',
    189 				'taxonomy'               => $taxonomy,
    190 				'orderby'                => 'term_order',
    191 				'number'                 => wp_sitemaps_get_max_urls( $this->object_type ),
    192 				'hide_empty'             => true,
    193 				'hierarchical'           => false,
    194 				'update_term_meta_cache' => false,
    195 			),
    196 			$taxonomy
    197 		);
    198 
    199 		return $args;
    200 	}
    201 }