[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Sitemaps: WP_Sitemaps_Posts class
   4   *
   5   * Builds the sitemaps for the 'post' object type.
   6   *
   7   * @package WordPress
   8   * @subpackage Sitemaps
   9   * @since 5.5.0
  10   */
  11  
  12  /**
  13   * Posts XML sitemap provider.
  14   *
  15   * @since 5.5.0
  16   */
  17  class WP_Sitemaps_Posts extends WP_Sitemaps_Provider {
  18      /**
  19       * WP_Sitemaps_Posts constructor.
  20       *
  21       * @since 5.5.0
  22       */
  23  	public function __construct() {
  24          $this->name        = 'posts';
  25          $this->object_type = 'post';
  26      }
  27  
  28      /**
  29       * Returns the public post types, which excludes nav_items and similar types.
  30       * Attachments are also excluded. This includes custom post types with public = true.
  31       *
  32       * @since 5.5.0
  33       *
  34       * @return WP_Post_Type[] Array of registered post type objects keyed by their name.
  35       */
  36  	public function get_object_subtypes() {
  37          $post_types = get_post_types( array( 'public' => true ), 'objects' );
  38          unset( $post_types['attachment'] );
  39  
  40          $post_types = array_filter( $post_types, 'is_post_type_viewable' );
  41  
  42          /**
  43           * Filters the list of post object sub types available within the sitemap.
  44           *
  45           * @since 5.5.0
  46           *
  47           * @param WP_Post_Type[] $post_types Array of registered post type objects keyed by their name.
  48           */
  49          return apply_filters( 'wp_sitemaps_post_types', $post_types );
  50      }
  51  
  52      /**
  53       * Gets a URL list for a post type sitemap.
  54       *
  55       * @since 5.5.0
  56       * @since 5.9.0 Renamed `$post_type` to `$object_subtype` to match parent class
  57       *              for PHP 8 named parameter support.
  58       *
  59       * @param int    $page_num       Page of results.
  60       * @param string $object_subtype Optional. Post type name. Default empty.
  61       *
  62       * @return array Array of URLs for a sitemap.
  63       */
  64  	public function get_url_list( $page_num, $object_subtype = '' ) {
  65          // Restores the more descriptive, specific name for use within this method.
  66          $post_type = $object_subtype;
  67  
  68          // Bail early if the queried post type is not supported.
  69          $supported_types = $this->get_object_subtypes();
  70  
  71          if ( ! isset( $supported_types[ $post_type ] ) ) {
  72              return array();
  73          }
  74  
  75          /**
  76           * Filters the posts URL list before it is generated.
  77           *
  78           * Passing a non-null value will effectively short-circuit the generation,
  79           * returning that value instead.
  80           *
  81           * @since 5.5.0
  82           *
  83           * @param array  $url_list  The URL list. Default null.
  84           * @param string $post_type Post type name.
  85           * @param int    $page_num  Page of results.
  86           */
  87          $url_list = apply_filters(
  88              'wp_sitemaps_posts_pre_url_list',
  89              null,
  90              $post_type,
  91              $page_num
  92          );
  93  
  94          if ( null !== $url_list ) {
  95              return $url_list;
  96          }
  97  
  98          $args          = $this->get_posts_query_args( $post_type );
  99          $args['paged'] = $page_num;
 100  
 101          $query = new WP_Query( $args );
 102  
 103          $url_list = array();
 104  
 105          /*
 106           * Add a URL for the homepage in the pages sitemap.
 107           * Shows only on the first page if the reading settings are set to display latest posts.
 108           */
 109          if ( 'page' === $post_type && 1 === $page_num && 'posts' === get_option( 'show_on_front' ) ) {
 110              // Extract the data needed for home URL to add to the array.
 111              $sitemap_entry = array(
 112                  'loc' => home_url( '/' ),
 113              );
 114  
 115              /**
 116               * Filters the sitemap entry for the home page when the 'show_on_front' option equals 'posts'.
 117               *
 118               * @since 5.5.0
 119               *
 120               * @param array $sitemap_entry Sitemap entry for the home page.
 121               */
 122              $sitemap_entry = apply_filters( 'wp_sitemaps_posts_show_on_front_entry', $sitemap_entry );
 123              $url_list[]    = $sitemap_entry;
 124          }
 125  
 126          foreach ( $query->posts as $post ) {
 127              $sitemap_entry = array(
 128                  'loc' => get_permalink( $post ),
 129              );
 130  
 131              /**
 132               * Filters the sitemap entry for an individual post.
 133               *
 134               * @since 5.5.0
 135               *
 136               * @param array   $sitemap_entry Sitemap entry for the post.
 137               * @param WP_Post $post          Post object.
 138               * @param string  $post_type     Name of the post_type.
 139               */
 140              $sitemap_entry = apply_filters( 'wp_sitemaps_posts_entry', $sitemap_entry, $post, $post_type );
 141              $url_list[]    = $sitemap_entry;
 142          }
 143  
 144          return $url_list;
 145      }
 146  
 147      /**
 148       * Gets the max number of pages available for the object type.
 149       *
 150       * @since 5.5.0
 151       * @since 5.9.0 Renamed `$post_type` to `$object_subtype` to match parent class
 152       *              for PHP 8 named parameter support.
 153       *
 154       * @param string $object_subtype Optional. Post type name. Default empty.
 155       * @return int Total number of pages.
 156       */
 157  	public function get_max_num_pages( $object_subtype = '' ) {
 158          if ( empty( $object_subtype ) ) {
 159              return 0;
 160          }
 161  
 162          // Restores the more descriptive, specific name for use within this method.
 163          $post_type = $object_subtype;
 164  
 165          /**
 166           * Filters the max number of pages before it is generated.
 167           *
 168           * Passing a non-null value will short-circuit the generation,
 169           * returning that value instead.
 170           *
 171           * @since 5.5.0
 172           *
 173           * @param int|null $max_num_pages The maximum number of pages. Default null.
 174           * @param string   $post_type     Post type name.
 175           */
 176          $max_num_pages = apply_filters( 'wp_sitemaps_posts_pre_max_num_pages', null, $post_type );
 177  
 178          if ( null !== $max_num_pages ) {
 179              return $max_num_pages;
 180          }
 181  
 182          $args                  = $this->get_posts_query_args( $post_type );
 183          $args['fields']        = 'ids';
 184          $args['no_found_rows'] = false;
 185  
 186          $query = new WP_Query( $args );
 187  
 188          $min_num_pages = ( 'page' === $post_type && 'posts' === get_option( 'show_on_front' ) ) ? 1 : 0;
 189          return isset( $query->max_num_pages ) ? max( $min_num_pages, $query->max_num_pages ) : 1;
 190      }
 191  
 192      /**
 193       * Returns the query args for retrieving posts to list in the sitemap.
 194       *
 195       * @since 5.5.0
 196       *
 197       * @param string $post_type Post type name.
 198       * @return array Array of WP_Query arguments.
 199       */
 200  	protected function get_posts_query_args( $post_type ) {
 201          /**
 202           * Filters the query arguments for post type sitemap queries.
 203           *
 204           * @see WP_Query for a full list of arguments.
 205           *
 206           * @since 5.5.0
 207           *
 208           * @param array  $args      Array of WP_Query arguments.
 209           * @param string $post_type Post type name.
 210           */
 211          $args = apply_filters(
 212              'wp_sitemaps_posts_query_args',
 213              array(
 214                  'orderby'                => 'ID',
 215                  'order'                  => 'ASC',
 216                  'post_type'              => $post_type,
 217                  'posts_per_page'         => wp_sitemaps_get_max_urls( $this->object_type ),
 218                  'post_status'            => array( 'publish' ),
 219                  'no_found_rows'          => true,
 220                  'update_post_term_cache' => false,
 221                  'update_post_meta_cache' => false,
 222              ),
 223              $post_type
 224          );
 225  
 226          return $args;
 227      }
 228  }


Generated: Fri Sep 24 01:00:04 2021 Cross-referenced by PHPXref 0.7.1