[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/sitemaps/providers/ -> class-wp-sitemaps-taxonomies.php (source)

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


Generated: Tue Sep 21 01:00:05 2021 Cross-referenced by PHPXref 0.7.1