Changeset 48523
- Timestamp:
- 07/21/2020 12:55:20 AM (4 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/sitemaps.php
r48098 r48523 18 18 * @global WP_Sitemaps $wp_sitemaps Global Core Sitemaps instance. 19 19 * 20 * @return WP_Sitemaps |null Sitemaps instance, or null if sitemaps are disabled.20 * @return WP_Sitemaps Sitemaps instance. 21 21 */ 22 22 function wp_sitemaps_get_server() { … … 24 24 25 25 $is_enabled = (bool) get_option( 'blog_public' ); 26 27 /**28 * Filters whether XML Sitemaps are enabled or not.29 *30 * @since 5.5.031 *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 26 40 27 // If there isn't a global instance, set and bootstrap the sitemaps system. -
trunk/src/wp-includes/sitemaps/class-wp-sitemaps.php
r48098 r48523 57 57 * Initiates all sitemap functionality. 58 58 * 59 * If sitemaps are disabled, only the rewrite rules will be registered 60 * by this method, in order to properly send 404s. 61 * 59 62 * @since 5.5.0 60 63 */ … … 62 65 // These will all fire on the init hook. 63 66 $this->register_rewrites(); 67 68 add_action( 'template_redirect', array( $this, 'render_sitemaps' ) ); 69 70 if ( ! $this->sitemaps_enabled() ) { 71 return; 72 } 73 64 74 $this->register_sitemaps(); 65 75 66 76 // Add additional action callbacks. 67 add_action( 'template_redirect', array( $this, 'render_sitemaps' ) );68 77 add_filter( 'pre_handle_404', array( $this, 'redirect_sitemapxml' ), 10, 2 ); 69 78 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 ); 70 105 } 71 106 … … 156 191 } 157 192 193 if ( ! $this->sitemaps_enabled() ) { 194 $wp_query->set_404(); 195 status_header( 404 ); 196 return; 197 } 198 158 199 // Render stylesheet if this is stylesheet route. 159 200 if ( $stylesheet_type ) { … … 187 228 if ( empty( $url_list ) ) { 188 229 $wp_query->set_404(); 230 status_header( 404 ); 189 231 return; 190 232 } -
trunk/tests/phpunit/tests/sitemaps/sitemaps.php
r48474 r48523 445 445 $this->assertContains( $sitemap_string, $robots_text, 'Sitemap URL not prefixed with "\n".' ); 446 446 } 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 } 447 492 }
Note: See TracChangeset
for help on using the changeset viewer.