Make WordPress Core


Ignore:
Timestamp:
07/21/2020 12:55:20 AM (4 years ago)
Author:
whyisjake
Message:

Sitemaps: Ensure correct HTTP status when sitemaps are disabled

If sitemaps are disabled, previously there would be a rewrite rule for the sitemap endpoint. This endpoint would display the homepage since there was a rewrite rule. Now, Sitemaps are loaded, and the proper HTTP headers are returned.

Fixes #50643.
Props swissspidy, kraftbj, donmhico.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/sitemaps/class-wp-sitemaps.php

    r48098 r48523  
    5757     * Initiates all sitemap functionality.
    5858     *
     59     * If sitemaps are disabled, only the rewrite rules will be registered
     60     * by this method, in order to properly send 404s.
     61     *
    5962     * @since 5.5.0
    6063     */
     
    6265        // These will all fire on the init hook.
    6366        $this->register_rewrites();
     67
     68        add_action( 'template_redirect', array( $this, 'render_sitemaps' ) );
     69
     70        if ( ! $this->sitemaps_enabled() ) {
     71            return;
     72        }
     73
    6474        $this->register_sitemaps();
    6575
    6676        // Add additional action callbacks.
    67         add_action( 'template_redirect', array( $this, 'render_sitemaps' ) );
    6877        add_filter( 'pre_handle_404', array( $this, 'redirect_sitemapxml' ), 10, 2 );
    6978        add_filter( 'robots_txt', array( $this, 'add_robots' ), 0, 2 );
     79    }
     80
     81    /**
     82     * Determines whether sitemaps are enabled or not.
     83     *
     84     * @since 5.5.0
     85     *
     86     * @return bool Whether sitemaps are enabled.
     87     */
     88    public function sitemaps_enabled() {
     89        $is_enabled = (bool) get_option( 'blog_public' );
     90
     91        /**
     92         * Filters whether XML Sitemaps are enabled or not.
     93         *
     94         * When XML Sitemaps are disabled via this filter, rewrite rules are still
     95         * in place to ensure a 404 is returned.
     96         *
     97         * @see WP_Sitemaps::register_rewrites()
     98         *
     99         * @since 5.5.0
     100         *
     101         * @param bool $is_enabled Whether XML Sitemaps are enabled or not. Defaults
     102         * to true for public sites.
     103         */
     104        return (bool) apply_filters( 'wp_sitemaps_enabled', $is_enabled );
    70105    }
    71106
     
    156191        }
    157192
     193        if ( ! $this->sitemaps_enabled() ) {
     194            $wp_query->set_404();
     195            status_header( 404 );
     196            return;
     197        }
     198
    158199        // Render stylesheet if this is stylesheet route.
    159200        if ( $stylesheet_type ) {
     
    187228        if ( empty( $url_list ) ) {
    188229            $wp_query->set_404();
     230            status_header( 404 );
    189231            return;
    190232        }
Note: See TracChangeset for help on using the changeset viewer.