Edit File by line
/home/barbar84/www/wp-inclu.../sitemaps
File: class-wp-sitemaps.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Sitemaps: WP_Sitemaps class
[2] Fix | Delete
*
[3] Fix | Delete
* This is the main class integrating all other classes.
[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
* Class WP_Sitemaps.
[12] Fix | Delete
*
[13] Fix | Delete
* @since 5.5.0
[14] Fix | Delete
*/
[15] Fix | Delete
class WP_Sitemaps {
[16] Fix | Delete
/**
[17] Fix | Delete
* The main index of supported sitemaps.
[18] Fix | Delete
*
[19] Fix | Delete
* @since 5.5.0
[20] Fix | Delete
*
[21] Fix | Delete
* @var WP_Sitemaps_Index
[22] Fix | Delete
*/
[23] Fix | Delete
public $index;
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* The main registry of supported sitemaps.
[27] Fix | Delete
*
[28] Fix | Delete
* @since 5.5.0
[29] Fix | Delete
*
[30] Fix | Delete
* @var WP_Sitemaps_Registry
[31] Fix | Delete
*/
[32] Fix | Delete
public $registry;
[33] Fix | Delete
[34] Fix | Delete
/**
[35] Fix | Delete
* An instance of the renderer class.
[36] Fix | Delete
*
[37] Fix | Delete
* @since 5.5.0
[38] Fix | Delete
*
[39] Fix | Delete
* @var WP_Sitemaps_Renderer
[40] Fix | Delete
*/
[41] Fix | Delete
public $renderer;
[42] Fix | Delete
[43] Fix | Delete
/**
[44] Fix | Delete
* WP_Sitemaps constructor.
[45] Fix | Delete
*
[46] Fix | Delete
* @since 5.5.0
[47] Fix | Delete
*/
[48] Fix | Delete
public function __construct() {
[49] Fix | Delete
$this->registry = new WP_Sitemaps_Registry();
[50] Fix | Delete
$this->renderer = new WP_Sitemaps_Renderer();
[51] Fix | Delete
$this->index = new WP_Sitemaps_Index( $this->registry );
[52] Fix | Delete
}
[53] Fix | Delete
[54] Fix | Delete
/**
[55] Fix | Delete
* Initiates all sitemap functionality.
[56] Fix | Delete
*
[57] Fix | Delete
* If sitemaps are disabled, only the rewrite rules will be registered
[58] Fix | Delete
* by this method, in order to properly send 404s.
[59] Fix | Delete
*
[60] Fix | Delete
* @since 5.5.0
[61] Fix | Delete
*/
[62] Fix | Delete
public function init() {
[63] Fix | Delete
// These will all fire on the init hook.
[64] Fix | Delete
$this->register_rewrites();
[65] Fix | Delete
[66] Fix | Delete
add_action( 'template_redirect', array( $this, 'render_sitemaps' ) );
[67] Fix | Delete
[68] Fix | Delete
if ( ! $this->sitemaps_enabled() ) {
[69] Fix | Delete
return;
[70] Fix | Delete
}
[71] Fix | Delete
[72] Fix | Delete
$this->register_sitemaps();
[73] Fix | Delete
[74] Fix | Delete
// Add additional action callbacks.
[75] Fix | Delete
add_filter( 'pre_handle_404', array( $this, 'redirect_sitemapxml' ), 10, 2 );
[76] Fix | Delete
add_filter( 'robots_txt', array( $this, 'add_robots' ), 0, 2 );
[77] Fix | Delete
}
[78] Fix | Delete
[79] Fix | Delete
/**
[80] Fix | Delete
* Determines whether sitemaps are enabled or not.
[81] Fix | Delete
*
[82] Fix | Delete
* @since 5.5.0
[83] Fix | Delete
*
[84] Fix | Delete
* @return bool Whether sitemaps are enabled.
[85] Fix | Delete
*/
[86] Fix | Delete
public function sitemaps_enabled() {
[87] Fix | Delete
$is_enabled = (bool) get_option( 'blog_public' );
[88] Fix | Delete
[89] Fix | Delete
/**
[90] Fix | Delete
* Filters whether XML Sitemaps are enabled or not.
[91] Fix | Delete
*
[92] Fix | Delete
* When XML Sitemaps are disabled via this filter, rewrite rules are still
[93] Fix | Delete
* in place to ensure a 404 is returned.
[94] Fix | Delete
*
[95] Fix | Delete
* @see WP_Sitemaps::register_rewrites()
[96] Fix | Delete
*
[97] Fix | Delete
* @since 5.5.0
[98] Fix | Delete
*
[99] Fix | Delete
* @param bool $is_enabled Whether XML Sitemaps are enabled or not. Defaults
[100] Fix | Delete
* to true for public sites.
[101] Fix | Delete
*/
[102] Fix | Delete
return (bool) apply_filters( 'wp_sitemaps_enabled', $is_enabled );
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
/**
[106] Fix | Delete
* Registers and sets up the functionality for all supported sitemaps.
[107] Fix | Delete
*
[108] Fix | Delete
* @since 5.5.0
[109] Fix | Delete
*/
[110] Fix | Delete
public function register_sitemaps() {
[111] Fix | Delete
$providers = array(
[112] Fix | Delete
'posts' => new WP_Sitemaps_Posts(),
[113] Fix | Delete
'taxonomies' => new WP_Sitemaps_Taxonomies(),
[114] Fix | Delete
'users' => new WP_Sitemaps_Users(),
[115] Fix | Delete
);
[116] Fix | Delete
[117] Fix | Delete
/* @var WP_Sitemaps_Provider $provider */
[118] Fix | Delete
foreach ( $providers as $name => $provider ) {
[119] Fix | Delete
$this->registry->add_provider( $name, $provider );
[120] Fix | Delete
}
[121] Fix | Delete
}
[122] Fix | Delete
[123] Fix | Delete
/**
[124] Fix | Delete
* Registers sitemap rewrite tags and routing rules.
[125] Fix | Delete
*
[126] Fix | Delete
* @since 5.5.0
[127] Fix | Delete
*/
[128] Fix | Delete
public function register_rewrites() {
[129] Fix | Delete
// Add rewrite tags.
[130] Fix | Delete
add_rewrite_tag( '%sitemap%', '([^?]+)' );
[131] Fix | Delete
add_rewrite_tag( '%sitemap-subtype%', '([^?]+)' );
[132] Fix | Delete
[133] Fix | Delete
// Register index route.
[134] Fix | Delete
add_rewrite_rule( '^wp-sitemap\.xml$', 'index.php?sitemap=index', 'top' );
[135] Fix | Delete
[136] Fix | Delete
// Register rewrites for the XSL stylesheet.
[137] Fix | Delete
add_rewrite_tag( '%sitemap-stylesheet%', '([^?]+)' );
[138] Fix | Delete
add_rewrite_rule( '^wp-sitemap\.xsl$', 'index.php?sitemap-stylesheet=sitemap', 'top' );
[139] Fix | Delete
add_rewrite_rule( '^wp-sitemap-index\.xsl$', 'index.php?sitemap-stylesheet=index', 'top' );
[140] Fix | Delete
[141] Fix | Delete
// Register routes for providers.
[142] Fix | Delete
add_rewrite_rule(
[143] Fix | Delete
'^wp-sitemap-([a-z]+?)-([a-z\d_-]+?)-(\d+?)\.xml$',
[144] Fix | Delete
'index.php?sitemap=$matches[1]&sitemap-subtype=$matches[2]&paged=$matches[3]',
[145] Fix | Delete
'top'
[146] Fix | Delete
);
[147] Fix | Delete
add_rewrite_rule(
[148] Fix | Delete
'^wp-sitemap-([a-z]+?)-(\d+?)\.xml$',
[149] Fix | Delete
'index.php?sitemap=$matches[1]&paged=$matches[2]',
[150] Fix | Delete
'top'
[151] Fix | Delete
);
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
/**
[155] Fix | Delete
* Renders sitemap templates based on rewrite rules.
[156] Fix | Delete
*
[157] Fix | Delete
* @since 5.5.0
[158] Fix | Delete
*
[159] Fix | Delete
* @global WP_Query $wp_query WordPress Query object.
[160] Fix | Delete
*/
[161] Fix | Delete
public function render_sitemaps() {
[162] Fix | Delete
global $wp_query;
[163] Fix | Delete
[164] Fix | Delete
$sitemap = sanitize_text_field( get_query_var( 'sitemap' ) );
[165] Fix | Delete
$object_subtype = sanitize_text_field( get_query_var( 'sitemap-subtype' ) );
[166] Fix | Delete
$stylesheet_type = sanitize_text_field( get_query_var( 'sitemap-stylesheet' ) );
[167] Fix | Delete
$paged = absint( get_query_var( 'paged' ) );
[168] Fix | Delete
[169] Fix | Delete
// Bail early if this isn't a sitemap or stylesheet route.
[170] Fix | Delete
if ( ! ( $sitemap || $stylesheet_type ) ) {
[171] Fix | Delete
return;
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
if ( ! $this->sitemaps_enabled() ) {
[175] Fix | Delete
$wp_query->set_404();
[176] Fix | Delete
status_header( 404 );
[177] Fix | Delete
return;
[178] Fix | Delete
}
[179] Fix | Delete
[180] Fix | Delete
// Render stylesheet if this is stylesheet route.
[181] Fix | Delete
if ( $stylesheet_type ) {
[182] Fix | Delete
$stylesheet = new WP_Sitemaps_Stylesheet();
[183] Fix | Delete
[184] Fix | Delete
$stylesheet->render_stylesheet( $stylesheet_type );
[185] Fix | Delete
exit;
[186] Fix | Delete
}
[187] Fix | Delete
[188] Fix | Delete
// Render the index.
[189] Fix | Delete
if ( 'index' === $sitemap ) {
[190] Fix | Delete
$sitemap_list = $this->index->get_sitemap_list();
[191] Fix | Delete
[192] Fix | Delete
$this->renderer->render_index( $sitemap_list );
[193] Fix | Delete
exit;
[194] Fix | Delete
}
[195] Fix | Delete
[196] Fix | Delete
$provider = $this->registry->get_provider( $sitemap );
[197] Fix | Delete
[198] Fix | Delete
if ( ! $provider ) {
[199] Fix | Delete
return;
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
if ( empty( $paged ) ) {
[203] Fix | Delete
$paged = 1;
[204] Fix | Delete
}
[205] Fix | Delete
[206] Fix | Delete
$url_list = $provider->get_url_list( $paged, $object_subtype );
[207] Fix | Delete
[208] Fix | Delete
// Force a 404 and bail early if no URLs are present.
[209] Fix | Delete
if ( empty( $url_list ) ) {
[210] Fix | Delete
$wp_query->set_404();
[211] Fix | Delete
status_header( 404 );
[212] Fix | Delete
return;
[213] Fix | Delete
}
[214] Fix | Delete
[215] Fix | Delete
$this->renderer->render_sitemap( $url_list );
[216] Fix | Delete
exit;
[217] Fix | Delete
}
[218] Fix | Delete
[219] Fix | Delete
/**
[220] Fix | Delete
* Redirects a URL to the wp-sitemap.xml
[221] Fix | Delete
*
[222] Fix | Delete
* @since 5.5.0
[223] Fix | Delete
*
[224] Fix | Delete
* @param bool $bypass Pass-through of the pre_handle_404 filter value.
[225] Fix | Delete
* @param WP_Query $query The WP_Query object.
[226] Fix | Delete
* @return bool Bypass value.
[227] Fix | Delete
*/
[228] Fix | Delete
public function redirect_sitemapxml( $bypass, $query ) {
[229] Fix | Delete
// If a plugin has already utilized the pre_handle_404 function, return without action to avoid conflicts.
[230] Fix | Delete
if ( $bypass ) {
[231] Fix | Delete
return $bypass;
[232] Fix | Delete
}
[233] Fix | Delete
[234] Fix | Delete
// 'pagename' is for most permalink types, name is for when the %postname% is used as a top-level field.
[235] Fix | Delete
if ( 'sitemap-xml' === $query->get( 'pagename' )
[236] Fix | Delete
|| 'sitemap-xml' === $query->get( 'name' )
[237] Fix | Delete
) {
[238] Fix | Delete
wp_safe_redirect( $this->index->get_index_url() );
[239] Fix | Delete
exit();
[240] Fix | Delete
}
[241] Fix | Delete
[242] Fix | Delete
return $bypass;
[243] Fix | Delete
}
[244] Fix | Delete
[245] Fix | Delete
/**
[246] Fix | Delete
* Adds the sitemap index to robots.txt.
[247] Fix | Delete
*
[248] Fix | Delete
* @since 5.5.0
[249] Fix | Delete
*
[250] Fix | Delete
* @param string $output robots.txt output.
[251] Fix | Delete
* @param bool $public Whether the site is public.
[252] Fix | Delete
* @return string The robots.txt output.
[253] Fix | Delete
*/
[254] Fix | Delete
public function add_robots( $output, $public ) {
[255] Fix | Delete
if ( $public ) {
[256] Fix | Delete
$output .= "\nSitemap: " . esc_url( $this->index->get_index_url() ) . "\n";
[257] Fix | Delete
}
[258] Fix | Delete
[259] Fix | Delete
return $output;
[260] Fix | Delete
}
[261] Fix | Delete
}
[262] Fix | Delete
[263] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function