[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 URL information 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 * Returning 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[]|null $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 * Get the most recent posts displayed on the homepage, 117 * and then sort them by their modified date to find 118 * the date the homepage was approximately last updated. 119 */ 120 $latest_posts = new WP_Query( 121 array( 122 'post_type' => 'post', 123 'post_status' => 'publish', 124 'orderby' => 'date', 125 'order' => 'DESC', 126 'no_found_rows' => true, 127 'update_post_meta_cache' => false, 128 'update_post_term_cache' => false, 129 ) 130 ); 131 132 if ( ! empty( $latest_posts->posts ) ) { 133 $posts = wp_list_sort( $latest_posts->posts, 'post_modified_gmt', 'DESC' ); 134 135 $sitemap_entry['lastmod'] = wp_date( DATE_W3C, strtotime( $posts[0]->post_modified_gmt ) ); 136 } 137 138 /** 139 * Filters the sitemap entry for the home page when the 'show_on_front' option equals 'posts'. 140 * 141 * @since 5.5.0 142 * 143 * @param array $sitemap_entry Sitemap entry for the home page. 144 */ 145 $sitemap_entry = apply_filters( 'wp_sitemaps_posts_show_on_front_entry', $sitemap_entry ); 146 $url_list[] = $sitemap_entry; 147 } 148 149 foreach ( $query->posts as $post ) { 150 $sitemap_entry = array( 151 'loc' => get_permalink( $post ), 152 'lastmod' => wp_date( DATE_W3C, strtotime( $post->post_modified_gmt ) ), 153 ); 154 155 /** 156 * Filters the sitemap entry for an individual post. 157 * 158 * @since 5.5.0 159 * 160 * @param array $sitemap_entry Sitemap entry for the post. 161 * @param WP_Post $post Post object. 162 * @param string $post_type Name of the post_type. 163 */ 164 $sitemap_entry = apply_filters( 'wp_sitemaps_posts_entry', $sitemap_entry, $post, $post_type ); 165 $url_list[] = $sitemap_entry; 166 } 167 168 return $url_list; 169 } 170 171 /** 172 * Gets the max number of pages available for the object type. 173 * 174 * @since 5.5.0 175 * @since 5.9.0 Renamed `$post_type` to `$object_subtype` to match parent class 176 * for PHP 8 named parameter support. 177 * 178 * @param string $object_subtype Optional. Post type name. Default empty. 179 * @return int Total number of pages. 180 */ 181 public function get_max_num_pages( $object_subtype = '' ) { 182 if ( empty( $object_subtype ) ) { 183 return 0; 184 } 185 186 // Restores the more descriptive, specific name for use within this method. 187 $post_type = $object_subtype; 188 189 /** 190 * Filters the max number of pages before it is generated. 191 * 192 * Passing a non-null value will short-circuit the generation, 193 * returning that value instead. 194 * 195 * @since 5.5.0 196 * 197 * @param int|null $max_num_pages The maximum number of pages. Default null. 198 * @param string $post_type Post type name. 199 */ 200 $max_num_pages = apply_filters( 'wp_sitemaps_posts_pre_max_num_pages', null, $post_type ); 201 202 if ( null !== $max_num_pages ) { 203 return $max_num_pages; 204 } 205 206 $args = $this->get_posts_query_args( $post_type ); 207 $args['fields'] = 'ids'; 208 $args['no_found_rows'] = false; 209 210 $query = new WP_Query( $args ); 211 212 $min_num_pages = ( 'page' === $post_type && 'posts' === get_option( 'show_on_front' ) ) ? 1 : 0; 213 return isset( $query->max_num_pages ) ? max( $min_num_pages, $query->max_num_pages ) : 1; 214 } 215 216 /** 217 * Returns the query args for retrieving posts to list in the sitemap. 218 * 219 * @since 5.5.0 220 * @since 6.1.0 Added `ignore_sticky_posts` default parameter. 221 * 222 * @param string $post_type Post type name. 223 * @return array Array of WP_Query arguments. 224 */ 225 protected function get_posts_query_args( $post_type ) { 226 /** 227 * Filters the query arguments for post type sitemap queries. 228 * 229 * @see WP_Query for a full list of arguments. 230 * 231 * @since 5.5.0 232 * @since 6.1.0 Added `ignore_sticky_posts` default parameter. 233 * 234 * @param array $args Array of WP_Query arguments. 235 * @param string $post_type Post type name. 236 */ 237 $args = apply_filters( 238 'wp_sitemaps_posts_query_args', 239 array( 240 'orderby' => 'ID', 241 'order' => 'ASC', 242 'post_type' => $post_type, 243 'posts_per_page' => wp_sitemaps_get_max_urls( $this->object_type ), 244 'post_status' => array( 'publish' ), 245 'no_found_rows' => true, 246 'update_post_term_cache' => false, 247 'update_post_meta_cache' => false, 248 'ignore_sticky_posts' => true, // Sticky posts will still appear, but they won't be moved to the front. 249 ), 250 $post_type 251 ); 252 253 return $args; 254 } 255 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Sat Dec 21 08:20:01 2024 | Cross-referenced by PHPXref |