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