Make WordPress Core

Ticket #50643: 50643.4.diff

File 50643.4.diff, 5.0 KB (added by swissspidy, 5 years ago)
  • src/wp-includes/sitemaps.php

    diff --git src/wp-includes/sitemaps.php src/wp-includes/sitemaps.php
    index 8678993dbd..9600540156 100644
     
    1717 *
    1818 * @global WP_Sitemaps $wp_sitemaps Global Core Sitemaps instance.
    1919 *
    20  * @return WP_Sitemaps|null Sitemaps instance, or null if sitemaps are disabled.
     20 * @return WP_Sitemaps Sitemaps instance.
    2121 */
    2222function wp_sitemaps_get_server() {
    2323        global $wp_sitemaps;
    2424
    2525        $is_enabled = (bool) get_option( 'blog_public' );
    2626
    27         /**
    28          * Filters whether XML Sitemaps are enabled or not.
    29          *
    30          * @since 5.5.0
    31          *
    32          * @param bool $is_enabled Whether XML Sitemaps are enabled or not. Defaults to true for public sites.
    33          */
    34         $is_enabled = (bool) apply_filters( 'wp_sitemaps_enabled', $is_enabled );
    35 
    36         if ( ! $is_enabled ) {
    37                 return null;
    38         }
    39 
    4027        // If there isn't a global instance, set and bootstrap the sitemaps system.
    4128        if ( empty( $wp_sitemaps ) ) {
    4229                $wp_sitemaps = new WP_Sitemaps();
  • src/wp-includes/sitemaps/class-wp-sitemaps.php

    diff --git src/wp-includes/sitemaps/class-wp-sitemaps.php src/wp-includes/sitemaps/class-wp-sitemaps.php
    index 918220317b..232efbff95 100644
    class WP_Sitemaps { 
    5656        /**
    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         */
    6164        public function init() {
    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 );
    7079        }
    7180
     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 in place to ensure a 404 is returned.
     95                 *
     96                 * @see WP_Sitemaps::register_rewrites()
     97                 *
     98                 * @since 5.5.0
     99                 *
     100                 * @param bool $is_enabled Whether XML Sitemaps are enabled or not. Defaults to true for public sites.
     101                 */
     102                return (bool) apply_filters( 'wp_sitemaps_enabled', $is_enabled );
     103        }
     104
    72105        /**
    73106         * Registers and sets up the functionality for all supported sitemaps.
    74107         *
    class WP_Sitemaps { 
    143176         * @global WP_Query $wp_query WordPress Query object.
    144177         */
    145178        public function render_sitemaps() {
     179                /* @var WP_Query $wp_query */
    146180                global $wp_query;
    147181
    148182                $sitemap         = sanitize_text_field( get_query_var( 'sitemap' ) );
    class WP_Sitemaps { 
    155189                        return;
    156190                }
    157191
     192                if ( ! $this->sitemaps_enabled() ) {
     193                        $wp_query->set_404();
     194                        status_header( 404 );
     195                        return;
     196                }
     197
    158198                // Render stylesheet if this is stylesheet route.
    159199                if ( $stylesheet_type ) {
    160200                        $stylesheet = new WP_Sitemaps_Stylesheet();
    class WP_Sitemaps { 
    186226                // Force a 404 and bail early if no URLs are present.
    187227                if ( empty( $url_list ) ) {
    188228                        $wp_query->set_404();
     229                        status_header( 404 );
    189230                        return;
    190231                }
    191232
  • tests/phpunit/tests/sitemaps/sitemaps.php

    diff --git tests/phpunit/tests/sitemaps/sitemaps.php tests/phpunit/tests/sitemaps/sitemaps.php
    index 4370f655b2..35a5d6e8b6 100644
    class Test_Sitemaps extends WP_UnitTestCase { 
    444444
    445445                $this->assertContains( $sitemap_string, $robots_text, 'Sitemap URL not prefixed with "\n".' );
    446446        }
     447
     448        /**
     449         * @ticket 50643
     450         */
     451        public function test_sitemaps_enabled() {
     452                $before = wp_sitemaps_get_server()->sitemaps_enabled();
     453                add_filter( 'wp_sitemaps_enabled', '__return_false' );
     454                $after = wp_sitemaps_get_server()->sitemaps_enabled();
     455                remove_filter( 'wp_sitemaps_enabled', '__return_false' );
     456
     457                $this->assertTrue( $before );
     458                $this->assertFalse( $after );
     459        }
     460
     461        /**
     462         * @ticket 50643
     463         * @runInSeparateProcess
     464         * @preserveGlobalState disabled
     465         */
     466        public function test_disable_sitemap_should_return_404() {
     467                add_filter( 'wp_sitemaps_enabled', '__return_false' );
     468
     469                $this->go_to( home_url( '/?sitemap=index' ) );
     470
     471                wp_sitemaps_get_server()->render_sitemaps();
     472
     473                remove_filter( 'wp_sitemaps_enabled', '__return_false' );
     474
     475                $this->assertTrue( is_404() );
     476        }
     477
     478        /**
     479         * @ticket 50643
     480         * @runInSeparateProcess
     481         * @preserveGlobalState disabled
     482         */
     483        public function test_empty_url_list_should_return_404() {
     484                wp_register_sitemap( 'foo', new WP_Sitemaps_Empty_Test_Provider( 'foo' ) );
     485
     486                $this->go_to( home_url( '/?sitemap=foo' ) );
     487
     488                wp_sitemaps_get_server()->render_sitemaps();
     489
     490                $this->assertTrue( is_404() );
     491        }
    447492}