"Fossies" - the Fresh Open Source Software Archive

Member "wordpress/wp-includes/sitemaps/providers/class-wp-sitemaps-posts.php" (14 Jul 2020, 5875 Bytes) of package /linux/www/wordpress-5.7-RC1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "class-wp-sitemaps-posts.php" see the Fossies "Dox" file reference documentation.

    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      *
   57      * @param int    $page_num  Page of results.
   58      * @param string $post_type Optional. Post type name. Default empty.
   59      * @return array Array of URLs for a sitemap.
   60      */
   61     public function get_url_list( $page_num, $post_type = '' ) {
   62         // Bail early if the queried post type is not supported.
   63         $supported_types = $this->get_object_subtypes();
   64 
   65         if ( ! isset( $supported_types[ $post_type ] ) ) {
   66             return array();
   67         }
   68 
   69         /**
   70          * Filters the posts URL list before it is generated.
   71          *
   72          * Passing a non-null value will effectively short-circuit the generation,
   73          * returning that value instead.
   74          *
   75          * @since 5.5.0
   76          *
   77          * @param array  $url_list  The URL list. Default null.
   78          * @param string $post_type Post type name.
   79          * @param int    $page_num  Page of results.
   80          */
   81         $url_list = apply_filters(
   82             'wp_sitemaps_posts_pre_url_list',
   83             null,
   84             $post_type,
   85             $page_num
   86         );
   87 
   88         if ( null !== $url_list ) {
   89             return $url_list;
   90         }
   91 
   92         $args          = $this->get_posts_query_args( $post_type );
   93         $args['paged'] = $page_num;
   94 
   95         $query = new WP_Query( $args );
   96 
   97         $url_list = array();
   98 
   99         /*
  100          * Add a URL for the homepage in the pages sitemap.
  101          * Shows only on the first page if the reading settings are set to display latest posts.
  102          */
  103         if ( 'page' === $post_type && 1 === $page_num && 'posts' === get_option( 'show_on_front' ) ) {
  104             // Extract the data needed for home URL to add to the array.
  105             $sitemap_entry = array(
  106                 'loc' => home_url( '/' ),
  107             );
  108 
  109             /**
  110              * Filters the sitemap entry for the home page when the 'show_on_front' option equals 'posts'.
  111              *
  112              * @since 5.5.0
  113              *
  114              * @param array $sitemap_entry Sitemap entry for the home page.
  115              */
  116             $sitemap_entry = apply_filters( 'wp_sitemaps_posts_show_on_front_entry', $sitemap_entry );
  117             $url_list[]    = $sitemap_entry;
  118         }
  119 
  120         foreach ( $query->posts as $post ) {
  121             $sitemap_entry = array(
  122                 'loc' => get_permalink( $post ),
  123             );
  124 
  125             /**
  126              * Filters the sitemap entry for an individual post.
  127              *
  128              * @since 5.5.0
  129              *
  130              * @param array   $sitemap_entry Sitemap entry for the post.
  131              * @param WP_Post $post          Post object.
  132              * @param string  $post_type     Name of the post_type.
  133              */
  134             $sitemap_entry = apply_filters( 'wp_sitemaps_posts_entry', $sitemap_entry, $post, $post_type );
  135             $url_list[]    = $sitemap_entry;
  136         }
  137 
  138         return $url_list;
  139     }
  140 
  141     /**
  142      * Gets the max number of pages available for the object type.
  143      *
  144      * @since 5.5.0
  145      *
  146      * @param string $post_type Optional. Post type name. Default empty.
  147      * @return int Total number of pages.
  148      */
  149     public function get_max_num_pages( $post_type = '' ) {
  150         if ( empty( $post_type ) ) {
  151             return 0;
  152         }
  153 
  154         /**
  155          * Filters the max number of pages before it is generated.
  156          *
  157          * Passing a non-null value will short-circuit the generation,
  158          * returning that value instead.
  159          *
  160          * @since 5.5.0
  161          *
  162          * @param int|null $max_num_pages The maximum number of pages. Default null.
  163          * @param string   $post_type     Post type name.
  164          */
  165         $max_num_pages = apply_filters( 'wp_sitemaps_posts_pre_max_num_pages', null, $post_type );
  166 
  167         if ( null !== $max_num_pages ) {
  168             return $max_num_pages;
  169         }
  170 
  171         $args                  = $this->get_posts_query_args( $post_type );
  172         $args['fields']        = 'ids';
  173         $args['no_found_rows'] = false;
  174 
  175         $query = new WP_Query( $args );
  176 
  177         $min_num_pages = ( 'page' === $post_type && 'posts' === get_option( 'show_on_front' ) ) ? 1 : 0;
  178         return isset( $query->max_num_pages ) ? max( $min_num_pages, $query->max_num_pages ) : 1;
  179     }
  180 
  181     /**
  182      * Returns the query args for retrieving posts to list in the sitemap.
  183      *
  184      * @since 5.5.0
  185      *
  186      * @param string $post_type Post type name.
  187      * @return array Array of WP_Query arguments.
  188      */
  189     protected function get_posts_query_args( $post_type ) {
  190         /**
  191          * Filters the query arguments for post type sitemap queries.
  192          *
  193          * @see WP_Query for a full list of arguments.
  194          *
  195          * @since 5.5.0
  196          *
  197          * @param array  $args      Array of WP_Query arguments.
  198          * @param string $post_type Post type name.
  199          */
  200         $args = apply_filters(
  201             'wp_sitemaps_posts_query_args',
  202             array(
  203                 'orderby'                => 'ID',
  204                 'order'                  => 'ASC',
  205                 'post_type'              => $post_type,
  206                 'posts_per_page'         => wp_sitemaps_get_max_urls( $this->object_type ),
  207                 'post_status'            => array( 'publish' ),
  208                 'no_found_rows'          => true,
  209                 'update_post_term_cache' => false,
  210                 'update_post_meta_cache' => false,
  211             ),
  212             $post_type
  213         );
  214 
  215         return $args;
  216     }
  217 }