[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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


Generated : Sat Dec 21 08:20:01 2024 Cross-referenced by PHPXref