diff --git a/src/wp-includes/canonical.php b/src/wp-includes/canonical.php index 2b7553bda5403..c284fe41fa00c 100644 --- a/src/wp-includes/canonical.php +++ b/src/wp-includes/canonical.php @@ -707,9 +707,7 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) { } // Remove trailing slash for robots.txt or sitemap requests. - if ( is_robots() - || ! empty( get_query_var( 'sitemap' ) ) || ! empty( get_query_var( 'sitemap-stylesheet' ) ) - ) { + if ( is_robots() || ! empty( get_query_var( 'sitemap' ) ) ) { $redirect['path'] = untrailingslashit( $redirect['path'] ); } diff --git a/src/wp-includes/class-wp.php b/src/wp-includes/class-wp.php index 67201d1394e91..1cfa0df8f66b3 100644 --- a/src/wp-includes/class-wp.php +++ b/src/wp-includes/class-wp.php @@ -748,7 +748,7 @@ public function handle_404() { // Never 404 here for the admin, robots, favicon, or sitemaps. // Sitemap routes send their own status in WP_Sitemaps::render_sitemaps(). - if ( is_admin() || is_robots() || is_favicon() || is_sitemap() || get_query_var( 'sitemap-stylesheet' ) ) { + if ( is_admin() || is_robots() || is_favicon() || is_sitemap() ) { $set_404 = false; // If posts were found, check for paged content. diff --git a/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php b/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php index 10a1ef1e7fcd5..b1ae1db5cc6dd 100644 --- a/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php +++ b/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php @@ -20,6 +20,7 @@ class WP_Sitemaps_Renderer { * XSL stylesheet for styling a sitemap for web browsers. * * @since 5.5.0 + * @deprecated 7.2.0 Stylesheets are no longer supported. * * @var string */ @@ -29,6 +30,7 @@ class WP_Sitemaps_Renderer { * XSL stylesheet for styling a sitemap for web browsers. * * @since 5.5.0 + * @deprecated 7.2.0 Stylesheets are no longer supported. * * @var string */ @@ -38,81 +40,64 @@ class WP_Sitemaps_Renderer { * WP_Sitemaps_Renderer constructor. * * @since 5.5.0 + * @since 7.2.0 No longer sets up the stylesheets, which are no longer supported. */ - public function __construct() { - $stylesheet_url = $this->get_sitemap_stylesheet_url(); - - if ( $stylesheet_url ) { - $this->stylesheet = ''; - } - - $stylesheet_index_url = $this->get_sitemap_index_stylesheet_url(); - - if ( $stylesheet_index_url ) { - $this->stylesheet_index = ''; - } - } + public function __construct() {} /** * Gets the URL for the sitemap stylesheet. * * @since 5.5.0 + * @deprecated 7.2.0 Stylesheets are no longer supported. * - * @global WP_Rewrite $wp_rewrite WordPress rewrite component. - * - * @return string The sitemap stylesheet URL. + * @return string Empty string. */ public function get_sitemap_stylesheet_url() { - global $wp_rewrite; - - $sitemap_url = home_url( '/wp-sitemap.xsl' ); - - if ( ! $wp_rewrite->using_permalinks() ) { - $sitemap_url = home_url( '/?sitemap-stylesheet=sitemap' ); - } + _deprecated_function( __METHOD__, '7.2.0' ); /** * Filters the URL for the sitemap stylesheet. * * If a falsey value is returned, no stylesheet will be used and - * the "raw" XML of the sitemap will be displayed. + * Following the deprecation of XSLT by browser manufacturers the + * outcome of this filter is ignored. * * @since 5.5.0 + * @deprecated 7.2.0 Stylesheets are no longer supported. * * @param string $sitemap_url Full URL for the sitemaps XSL file. */ - return apply_filters( 'wp_sitemaps_stylesheet_url', $sitemap_url ); + apply_filters_deprecated( 'wp_sitemaps_stylesheet_url', array( '' ), '7.2.0' ); + + return ''; } /** * Gets the URL for the sitemap index stylesheet. * * @since 5.5.0 + * @deprecated 7.2.0 Stylesheets are no longer supported. * - * @global WP_Rewrite $wp_rewrite WordPress rewrite component. - * - * @return string The sitemap index stylesheet URL. + * @return string Empty string. */ public function get_sitemap_index_stylesheet_url() { - global $wp_rewrite; - - $sitemap_url = home_url( '/wp-sitemap-index.xsl' ); - - if ( ! $wp_rewrite->using_permalinks() ) { - $sitemap_url = home_url( '/?sitemap-stylesheet=index' ); - } + _deprecated_function( __METHOD__, '7.2.0' ); /** * Filters the URL for the sitemap index stylesheet. * * If a falsey value is returned, no stylesheet will be used and - * the "raw" XML of the sitemap index will be displayed. + * Following the deprecation of XSLT by browser manufacturers the + * outcome of this filter is ignored. * * @since 5.5.0 + * @deprecated 7.2.0 Stylesheets are no longer supported. * * @param string $sitemap_url Full URL for the sitemaps index XSL file. */ - return apply_filters( 'wp_sitemaps_stylesheet_index_url', $sitemap_url ); + apply_filters_deprecated( 'wp_sitemaps_stylesheet_index_url', array( '' ), '7.2.0' ); + + return ''; } /** @@ -144,11 +129,12 @@ public function render_index( $sitemaps ) { * @return string|false A well-formed XML string for a sitemap index. False on error. */ public function get_sitemap_index_xml( $sitemaps ) { + $this->apply_deprecated_stylesheet_filters( 'index' ); + $sitemap_index = new SimpleXMLElement( sprintf( - '%1$s%2$s%3$s', + '%1$s%2$s', '', - $this->stylesheet_index, '' ) ); @@ -208,11 +194,12 @@ public function render_sitemap( $url_list ) { * @return string|false A well-formed XML string for a sitemap index. False on error. */ public function get_sitemap_xml( $url_list ) { + $this->apply_deprecated_stylesheet_filters( 'sitemap' ); + $urlset = new SimpleXMLElement( sprintf( - '%1$s%2$s%3$s', + '%1$s%2$s', '', - $this->stylesheet, '' ) ); @@ -243,6 +230,56 @@ public function get_sitemap_xml( $url_list ) { return $urlset->asXML(); } + /** + * Applies the removed stylesheet filters so that their callbacks trigger deprecation notices. + * + * The stylesheet filters no longer have any effect. Applying them where a sitemap is + * generated ensures a callback still added to one is reported rather than silently ignored. + * No notice is triggered for a filter that has no callbacks. + * + * @since 7.2.0 + * + * @param 'sitemap'|'index' $type Sitemap type. + */ + private function apply_deprecated_stylesheet_filters( string $type ): void { + if ( 'index' === $type ) { + /** This filter is documented in wp-includes/sitemaps/class-wp-sitemaps-renderer.php */ + apply_filters_deprecated( 'wp_sitemaps_stylesheet_index_url', array( '' ), '7.2.0' ); + + /** + * Filters the content of the sitemap index stylesheet. + * + * Following the deprecation of XSLT by browser manufacturers the + * outcome of this filter is ignored. + * + * @since 5.5.0 + * @deprecated 7.2.0 Stylesheets are no longer supported. + * + * @param string $xsl_content Full content for the XML stylesheet. + */ + apply_filters_deprecated( 'wp_sitemaps_stylesheet_index_content', array( '' ), '7.2.0' ); + } else { + /** This filter is documented in wp-includes/sitemaps/class-wp-sitemaps-renderer.php */ + apply_filters_deprecated( 'wp_sitemaps_stylesheet_url', array( '' ), '7.2.0' ); + + /** + * Filters the content of the sitemap stylesheet. + * + * Following the deprecation of XSLT by browser manufacturers the + * outcome of this filter is ignored. + * + * @since 5.5.0 + * @deprecated 7.2.0 Stylesheets are no longer supported. + * + * @param string $xsl_content Full content for the XML stylesheet. + */ + apply_filters_deprecated( 'wp_sitemaps_stylesheet_content', array( '' ), '7.2.0' ); + } + + /** This filter is documented in wp-includes/sitemaps/class-wp-sitemaps-stylesheet.php */ + apply_filters_deprecated( 'wp_sitemaps_stylesheet_css', array( '' ), '7.2.0' ); + } + /** * Checks for the availability of the SimpleXML extension and errors if missing. * diff --git a/src/wp-includes/sitemaps/class-wp-sitemaps-stylesheet.php b/src/wp-includes/sitemaps/class-wp-sitemaps-stylesheet.php index b07fe5d4cfe84..6e5b6d1526cc4 100644 --- a/src/wp-includes/sitemaps/class-wp-sitemaps-stylesheet.php +++ b/src/wp-includes/sitemaps/class-wp-sitemaps-stylesheet.php @@ -2,17 +2,19 @@ /** * Sitemaps: WP_Sitemaps_Stylesheet class * - * This class provides the XSL stylesheets to style all sitemaps. + * This class is retained for backward compatibility. * * @package WordPress * @subpackage Sitemaps * @since 5.5.0 + * @deprecated 7.2.0 Stylesheets are no longer supported. */ /** * Stylesheet provider class. * * @since 5.5.0 + * @deprecated 7.2.0 Stylesheets are no longer supported. */ #[AllowDynamicProperties] class WP_Sitemaps_Stylesheet { @@ -20,23 +22,15 @@ class WP_Sitemaps_Stylesheet { * Renders the XSL stylesheet depending on whether it's the sitemap index or not. * * @since 5.5.0 + * @deprecated 7.2.0 Stylesheets are no longer supported. * * @param string $type Stylesheet type. Either 'sitemap' or 'index'. * @return never */ public function render_stylesheet( $type ) { - header( 'Content-Type: application/xml; charset=UTF-8' ); - - if ( 'sitemap' === $type ) { - // All content is escaped below. - echo $this->get_sitemap_stylesheet(); - } - - if ( 'index' === $type ) { - // All content is escaped below. - echo $this->get_sitemap_index_stylesheet(); - } - + unset( $type ); + status_header( 410 ); + _deprecated_function( __METHOD__, '7.2.0' ); exit; } @@ -44,277 +38,52 @@ public function render_stylesheet( $type ) { * Returns the escaped XSL for all sitemaps, except index. * * @since 5.5.0 + * @deprecated 7.2.0 Stylesheets are no longer supported. + * + * @return string Empty string. */ public function get_sitemap_stylesheet() { - $css = $this->get_stylesheet_css(); - $title = esc_xml( __( 'XML Sitemap' ) ); - $description = esc_xml( __( 'This XML Sitemap is generated by WordPress to make your content more visible for search engines.' ) ); - $learn_more = sprintf( - '%s', - esc_url( __( 'https://www.sitemaps.org/' ) ), - esc_xml( __( 'Learn more about XML sitemaps.' ) ) - ); - - $text = sprintf( - /* translators: %s: Number of URLs. */ - esc_xml( __( 'Number of URLs in this XML Sitemap: %s.' ) ), - '' - ); - - $lang = get_language_attributes( 'html' ); - $url = esc_xml( __( 'URL' ) ); - $lastmod = esc_xml( __( 'Last Modified' ) ); - $changefreq = esc_xml( __( 'Change Frequency' ) ); - $priority = esc_xml( __( 'Priority' ) ); - - $xsl_content = << - - - - - - - - - - - - - {$title} - - - -
-
-

{$title}

-

{$description}

-

{$learn_more}

-
-
-

{$text}

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
{$url}{$lastmod}{$changefreq}{$priority}
-
-
- - -
-
- -XSL; - - /** - * Filters the content of the sitemap stylesheet. - * - * @since 5.5.0 - * - * @param string $xsl_content Full content for the XML stylesheet. - */ - return apply_filters( 'wp_sitemaps_stylesheet_content', $xsl_content ); + _deprecated_function( __METHOD__, '7.2.0' ); + return ''; } /** * Returns the escaped XSL for the index sitemaps. * * @since 5.5.0 + * @deprecated 7.2.0 Stylesheets are no longer supported. + * + * @return string Empty string. */ public function get_sitemap_index_stylesheet() { - $css = $this->get_stylesheet_css(); - $title = esc_xml( __( 'XML Sitemap' ) ); - $description = esc_xml( __( 'This XML Sitemap is generated by WordPress to make your content more visible for search engines.' ) ); - $learn_more = sprintf( - '%s', - esc_url( __( 'https://www.sitemaps.org/' ) ), - esc_xml( __( 'Learn more about XML sitemaps.' ) ) - ); - - $text = sprintf( - /* translators: %s: Number of URLs. */ - esc_xml( __( 'Number of URLs in this XML Sitemap: %s.' ) ), - '' - ); - - $lang = get_language_attributes( 'html' ); - $url = esc_xml( __( 'URL' ) ); - $lastmod = esc_xml( __( 'Last Modified' ) ); - - $xsl_content = << - - - - - - - - - - - {$title} - - - -
-
-

{$title}

-

{$description}

-

{$learn_more}

-
-
-

{$text}

- - - - - - - - - - - - - - - - - - - -
{$url}{$lastmod}
-
-
- - -
-
- -XSL; - - /** - * Filters the content of the sitemap index stylesheet. - * - * @since 5.5.0 - * - * @param string $xsl_content Full content for the XML stylesheet. - */ - return apply_filters( 'wp_sitemaps_stylesheet_index_content', $xsl_content ); + _deprecated_function( __METHOD__, '7.2.0' ); + return ''; } /** * Gets the CSS to be included in sitemap XSL stylesheets. * * @since 5.5.0 + * @deprecated 7.2.0 Stylesheets are no longer supported. * - * @return string The CSS. + * @return string Empty string. */ public function get_stylesheet_css() { - $text_align = is_rtl() ? 'right' : 'left'; - - $css = <<send_404(); return; } @@ -188,20 +182,6 @@ public function render_sitemaps() { return; } - // Render stylesheet if this is stylesheet route. - if ( $stylesheet_type ) { - // Force a 404 and bail early if the stylesheet type is not recognized. - if ( ! in_array( $stylesheet_type, array( 'sitemap', 'index' ), true ) ) { - $this->send_404(); - return; - } - - $stylesheet = new WP_Sitemaps_Stylesheet(); - - $stylesheet->render_stylesheet( $stylesheet_type ); - exit; - } - // Render the index. if ( 'index' === $sitemap ) { $sitemap_list = $this->index->get_sitemap_list(); diff --git a/tests/phpstan/baselines/deadCode.unreachable.neon b/tests/phpstan/baselines/deadCode.unreachable.neon index 8a4288bc40c43..2282799c3903d 100644 --- a/tests/phpstan/baselines/deadCode.unreachable.neon +++ b/tests/phpstan/baselines/deadCode.unreachable.neon @@ -63,8 +63,3 @@ parameters: identifier: deadCode.unreachable count: 1 path: ../../../src/wp-includes/pluggable.php - - - message: '#^Unreachable statement \- code above always terminates\.$#' - identifier: deadCode.unreachable - count: 1 - path: ../../../src/wp-includes/sitemaps/class-wp-sitemaps.php diff --git a/tests/phpunit/tests/canonical/sitemaps.php b/tests/phpunit/tests/canonical/sitemaps.php index 97fa6c4ee8937..1ec0c8a0a81ee 100644 --- a/tests/phpunit/tests/canonical/sitemaps.php +++ b/tests/phpunit/tests/canonical/sitemaps.php @@ -20,12 +20,6 @@ public function test_remove_trailing_slashes_for_sitemap_index_requests() { $this->assertCanonical( '/wp-sitemap.xml/', '/wp-sitemap.xml' ); } - public function test_remove_trailing_slashes_for_sitemap_index_stylesheet_requests() { - $this->set_permalink_structure( '/%postname%/' ); - $this->assertCanonical( '/wp-sitemap-index.xsl', '/wp-sitemap-index.xsl' ); - $this->assertCanonical( '/wp-sitemap-index.xsl/', '/wp-sitemap-index.xsl' ); - } - public function test_remove_trailing_slashes_for_sitemap_requests() { $this->set_permalink_structure( '/%postname%/' ); $this->assertCanonical( '/wp-sitemap-posts-post-1.xml', '/wp-sitemap-posts-post-1.xml' ); @@ -34,12 +28,6 @@ public function test_remove_trailing_slashes_for_sitemap_requests() { $this->assertCanonical( '/wp-sitemap-users-1.xml/', '/wp-sitemap-users-1.xml' ); } - public function test_remove_trailing_slashes_for_sitemap_stylesheet_requests() { - $this->set_permalink_structure( '/%postname%/' ); - $this->assertCanonical( '/wp-sitemap.xsl', '/wp-sitemap.xsl' ); - $this->assertCanonical( '/wp-sitemap.xsl/', '/wp-sitemap.xsl' ); - } - /** * Ensure sitemaps redirects work as expected with pretty permalinks. * diff --git a/tests/phpunit/tests/query/isSitemap.php b/tests/phpunit/tests/query/isSitemap.php index f772ed09d26e5..5430db0c0d47f 100644 --- a/tests/phpunit/tests/query/isSitemap.php +++ b/tests/phpunit/tests/query/isSitemap.php @@ -109,20 +109,6 @@ public function test_is_sitemap_false_for_empty_sitemap_var(): void { $this->assertFalse( $query->is_sitemap(), 'An empty "sitemap" query var should not set is_sitemap.' ); } - /** - * The sitemap stylesheet route uses the "sitemap-stylesheet" query var, which must - * not flag the query as a sitemap. - * - * @ticket 51543 - * - * @covers WP_Query::parse_query - */ - public function test_is_sitemap_false_for_stylesheet_route(): void { - $query = new WP_Query( array( 'sitemap-stylesheet' => 'sitemap' ) ); - - $this->assertFalse( $query->is_sitemap(), 'The sitemap stylesheet route should not flag the query as a sitemap.' ); - } - /** * is_robots takes precedence over is_sitemap in the parse_query branch. * diff --git a/tests/phpunit/tests/query/vars.php b/tests/phpunit/tests/query/vars.php index 87e7fa7bb75c6..f4b5f6a695b11 100644 --- a/tests/phpunit/tests/query/vars.php +++ b/tests/phpunit/tests/query/vars.php @@ -73,7 +73,6 @@ public function testPublicQueryVarsAreAsExpected() { 'rest_route', 'sitemap', 'sitemap-subtype', - 'sitemap-stylesheet', ), $wp->public_query_vars, diff --git a/tests/phpunit/tests/sitemaps/sitemaps.php b/tests/phpunit/tests/sitemaps/sitemaps.php index 3a0d3adc4e675..a4062e887123c 100644 --- a/tests/phpunit/tests/sitemaps/sitemaps.php +++ b/tests/phpunit/tests/sitemaps/sitemaps.php @@ -548,34 +548,9 @@ public function data_unusable_sitemap_query_vars(): array { return array( 'value stripped by sanitizing' => array( '?sitemap=<>' ), 'array sitemap value' => array( '?sitemap[]=index' ), - 'array stylesheet value' => array( '?sitemap-stylesheet[]=sitemap' ), ); } - /** - * Ensures an unrecognized stylesheet type 404s from render_sitemaps(). - * - * WP::handle_404() exempts any request carrying a `sitemap-stylesheet` - * query var, and WP_Sitemaps_Stylesheet::render_stylesheet() echoes nothing - * for a type other than 'sitemap' or 'index', so this route would otherwise - * be served as a 200 with an empty body. - * - * @ticket 65945 - */ - public function test_unrecognized_stylesheet_type_should_return_404() { - // Instantiate the server before navigating: registering the sitemap rewrite - // tags is what adds `sitemap-stylesheet` to `$wp->public_query_vars`. - $sitemaps = wp_sitemaps_get_server(); - - $this->go_to( home_url( '/?sitemap-stylesheet=this-is-not-a-stylesheet' ) ); - - $this->assertFalse( is_404(), 'WP::handle_404() should not have set a 404.' ); - - $sitemaps->render_sitemaps(); - - $this->assertTrue( is_404(), 'render_sitemaps() should have set a 404.' ); - } - /** * Ensures an unregistered provider 404s from render_sitemaps(). * diff --git a/tests/phpunit/tests/sitemaps/wpSitemapsRenderer.php b/tests/phpunit/tests/sitemaps/wpSitemapsRenderer.php index b56737d0b8a75..31c3cb6bb7aef 100644 --- a/tests/phpunit/tests/sitemaps/wpSitemapsRenderer.php +++ b/tests/phpunit/tests/sitemaps/wpSitemapsRenderer.php @@ -5,46 +5,6 @@ */ class Tests_Sitemaps_wpSitemapsRenderer extends WP_Test_XML_TestCase { - public function test_get_sitemap_stylesheet_url() { - $sitemap_renderer = new WP_Sitemaps_Renderer(); - $stylesheet_url = $sitemap_renderer->get_sitemap_stylesheet_url(); - - $this->assertStringEndsWith( '/?sitemap-stylesheet=sitemap', $stylesheet_url ); - } - - public function test_get_sitemap_stylesheet_url_pretty_permalinks() { - // Set permalinks for testing. - $this->set_permalink_structure( '/%year%/%postname%/' ); - - $sitemap_renderer = new WP_Sitemaps_Renderer(); - $stylesheet_url = $sitemap_renderer->get_sitemap_stylesheet_url(); - - // Clean up permalinks. - $this->set_permalink_structure(); - - $this->assertStringEndsWith( '/wp-sitemap.xsl', $stylesheet_url ); - } - - public function test_get_sitemap_index_stylesheet_url() { - $sitemap_renderer = new WP_Sitemaps_Renderer(); - $stylesheet_url = $sitemap_renderer->get_sitemap_index_stylesheet_url(); - - $this->assertStringEndsWith( '/?sitemap-stylesheet=index', $stylesheet_url ); - } - - public function test_get_sitemap_index_stylesheet_url_pretty_permalinks() { - // Set permalinks for testing. - $this->set_permalink_structure( '/%year%/%postname%/' ); - - $sitemap_renderer = new WP_Sitemaps_Renderer(); - $stylesheet_url = $sitemap_renderer->get_sitemap_index_stylesheet_url(); - - // Clean up permalinks. - $this->set_permalink_structure(); - - $this->assertStringEndsWith( '/wp-sitemap-index.xsl', $stylesheet_url ); - } - /** * Test XML output for the sitemap index renderer. */ @@ -71,7 +31,6 @@ public function test_get_sitemap_index_xml() { $actual = $renderer->get_sitemap_index_xml( $entries ); $expected = '' . - '' . '' . 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-post-1.xml' . 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-page-1.xml' . @@ -114,7 +73,6 @@ public function test_get_sitemap_index_xml_with_lastmod() { $actual = $renderer->get_sitemap_index_xml( $entries ); $expected = '' . - '' . '' . 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-post-1.xml2005-01-01' . 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-page-1.xml2005-01-01' . @@ -160,30 +118,6 @@ public function test_get_sitemap_index_xml_extra_elements() { ); } - /** - * Test XML output for the sitemap index renderer when stylesheet is disabled. - */ - public function test_get_sitemap_index_xml_without_stylesheet() { - $entries = array( - array( - 'loc' => 'http://' . WP_TESTS_DOMAIN . '/wp-sitemap-posts-post-1.xml', - ), - ); - - add_filter( 'wp_sitemaps_stylesheet_index_url', '__return_false' ); - - $renderer = new WP_Sitemaps_Renderer(); - - $xml_dom = $this->loadXML( $renderer->get_sitemap_index_xml( $entries ) ); - $xpath = new DOMXPath( $xml_dom ); - - $this->assertSame( - 0, - $xpath->query( '//processing-instruction( "xml-stylesheet" )' )->length, - 'Sitemap index incorrectly contains the xml-stylesheet processing instruction.' - ); - } - /** * Test XML output for the sitemap page renderer. */ @@ -210,7 +144,6 @@ public function test_get_sitemap_xml() { $actual = $renderer->get_sitemap_xml( $url_list ); $expected = '' . - '' . '' . 'http://' . WP_TESTS_DOMAIN . '/2019/10/post-1' . 'http://' . WP_TESTS_DOMAIN . '/2019/10/post-2' . @@ -222,30 +155,6 @@ public function test_get_sitemap_xml() { $this->assertXMLEquals( $expected, $actual, 'Sitemap page markup incorrect.' ); } - /** - * Test XML output for the sitemap page renderer when stylesheet is disabled. - */ - public function test_get_sitemap_xml_without_stylesheet() { - $url_list = array( - array( - 'loc' => 'http://' . WP_TESTS_DOMAIN . '/2019/10/post-1', - ), - ); - - add_filter( 'wp_sitemaps_stylesheet_url', '__return_false' ); - - $renderer = new WP_Sitemaps_Renderer(); - - $xml_dom = $this->loadXML( $renderer->get_sitemap_xml( $url_list ) ); - $xpath = new DOMXPath( $xml_dom ); - - $this->assertSame( - 0, - $xpath->query( '//processing-instruction( "xml-stylesheet" )' )->length, - 'Sitemap incorrectly contains the xml-stylesheet processing instruction.' - ); - } - /** * Test that all children of Q{http://www.sitemaps.org/schemas/sitemap/0.9}url in the * rendered sitemap XML are defined in the Sitemaps spec (i.e., loc, lastmod, changefreq, priority). @@ -283,11 +192,13 @@ public function test_get_sitemap_xml_extra_elements() { } /** - * Test that the sitemap stylesheet URL can be filtered. + * Test that the sitemap stylesheet URL filter cannot add a stylesheet. * - * @covers WP_Sitemaps_Renderer::get_sitemap_stylesheet_url + * @covers WP_Sitemaps_Renderer::get_sitemap_xml */ public function test_get_sitemap_stylesheet_url_filter() { + $this->setExpectedDeprecated( 'wp_sitemaps_stylesheet_url' ); + $custom_url = 'https://example.com/custom-sitemap.xsl'; add_filter( @@ -305,15 +216,17 @@ static function () use ( $custom_url ) { ); $actual = $sitemap_renderer->get_sitemap_xml( $entries ); - $this->assertStringContainsString( '', $actual ); + $this->assertStringNotContainsString( 'setExpectedDeprecated( 'wp_sitemaps_stylesheet_index_url' ); + $custom_url = 'https://example.com/custom-sitemap-index.xsl'; add_filter( @@ -331,7 +244,89 @@ static function () use ( $custom_url ) { ); $actual = $sitemap_renderer->get_sitemap_index_xml( $entries ); - $this->assertStringContainsString( '', $actual ); + $this->assertStringNotContainsString( 'setExpectedDeprecated( $hook_name ); + + add_filter( $hook_name, '__return_empty_string' ); + + $sitemap_renderer = new WP_Sitemaps_Renderer(); + $entries = array( + array( + 'loc' => 'http://' . WP_TESTS_DOMAIN . '/2019/10/post-1', + ), + ); + + $actual = $sitemap_renderer->$method( $entries ); + $this->assertIsString( $actual ); + $this->assertStringNotContainsString( 'assertSame( '', $sitemap_renderer->get_sitemap_stylesheet_url(), 'The sitemap stylesheet URL should be empty.' ); + $this->assertSame( '', $sitemap_renderer->get_sitemap_index_stylesheet_url(), 'The sitemap index stylesheet URL should be empty.' ); + } + + /** + * Data provider for {@see self::test_deprecated_stylesheet_filters()}. + * + * @return array + */ + public function data_deprecated_stylesheet_filters(): array { + return array( + 'stylesheet URL' => array( + 'hook_name' => 'wp_sitemaps_stylesheet_url', + 'method' => 'get_sitemap_xml', + ), + 'stylesheet content' => array( + 'hook_name' => 'wp_sitemaps_stylesheet_content', + 'method' => 'get_sitemap_xml', + ), + 'stylesheet CSS for sitemap' => array( + 'hook_name' => 'wp_sitemaps_stylesheet_css', + 'method' => 'get_sitemap_xml', + ), + 'index stylesheet URL' => array( + 'hook_name' => 'wp_sitemaps_stylesheet_index_url', + 'method' => 'get_sitemap_index_xml', + ), + 'index stylesheet content' => array( + 'hook_name' => 'wp_sitemaps_stylesheet_index_content', + 'method' => 'get_sitemap_index_xml', + ), + 'stylesheet CSS for index' => array( + 'hook_name' => 'wp_sitemaps_stylesheet_css', + 'method' => 'get_sitemap_index_xml', + ), + ); } /** @@ -343,7 +338,6 @@ public function test_get_sitemap_xml_empty() { $renderer = new WP_Sitemaps_Renderer(); $actual = $renderer->get_sitemap_xml( array() ); $expected = '' . - '' . ''; $this->assertXMLEquals( $expected, $actual, 'Empty sitemap markup incorrect.' ); @@ -358,7 +352,6 @@ public function test_get_sitemap_index_xml_empty() { $renderer = new WP_Sitemaps_Renderer(); $actual = $renderer->get_sitemap_index_xml( array() ); $expected = '' . - '' . ''; $this->assertXMLEquals( $expected, $actual, 'Empty sitemap index markup incorrect.' ); @@ -389,7 +382,6 @@ public function test_get_sitemap_xml_with_all_supported_fields() { $actual = $renderer->get_sitemap_xml( $url_list ); $expected = '' . - '' . '' . '' . 'http://' . WP_TESTS_DOMAIN . '/2019/10/post-1' . diff --git a/tests/phpunit/tests/sitemaps/wpSitemapsStylesheet.php b/tests/phpunit/tests/sitemaps/wpSitemapsStylesheet.php index b4dab08e48eee..663180cce0aa9 100644 --- a/tests/phpunit/tests/sitemaps/wpSitemapsStylesheet.php +++ b/tests/phpunit/tests/sitemaps/wpSitemapsStylesheet.php @@ -2,42 +2,38 @@ /** * @group sitemaps + * + * @covers WP_Sitemaps_Stylesheet */ class Tests_Sitemaps_wpSitemapsStylesheet extends WP_UnitTestCase { /** - * Test that stylesheet content can be filtered. + * Tests that the stylesheet methods are deprecated and return an empty string. + * + * @ticket 65593 + * + * @dataProvider data_deprecated_methods + * + * @param string $method Deprecated method name. */ - public function test_filter_sitemaps_stylesheet_content() { - $stylesheet = new WP_Sitemaps_Stylesheet(); - - add_filter( 'wp_sitemaps_stylesheet_content', '__return_empty_string' ); - $content = $stylesheet->get_sitemap_stylesheet(); + public function test_deprecated_methods( string $method ): void { + $this->setExpectedDeprecated( "WP_Sitemaps_Stylesheet::$method" ); - $this->assertSame( '', $content, 'Could not filter stylesheet content' ); - } - - /** - * Test that sitemap index stylesheet content can be filtered. - */ - public function test_filter_sitemaps_stylesheet_index_content() { $stylesheet = new WP_Sitemaps_Stylesheet(); - add_filter( 'wp_sitemaps_stylesheet_index_content', '__return_empty_string' ); - $content = $stylesheet->get_sitemap_index_stylesheet(); - - $this->assertSame( '', $content, 'Could not filter sitemap index stylesheet content' ); + $this->assertSame( '', $stylesheet->$method() ); } /** - * Test that sitemap stylesheet CSS can be filtered. + * Data provider for {@see self::test_deprecated_methods()}. + * + * @return array */ - public function test_filter_sitemaps_stylesheet_css() { - $stylesheet = new WP_Sitemaps_Stylesheet(); - - add_filter( 'wp_sitemaps_stylesheet_css', '__return_empty_string' ); - $css = $stylesheet->get_stylesheet_css(); - - $this->assertSame( '', $css, 'Could not filter sitemap stylesheet CSS' ); + public function data_deprecated_methods(): array { + return array( + 'sitemap stylesheet' => array( 'method' => 'get_sitemap_stylesheet' ), + 'index stylesheet' => array( 'method' => 'get_sitemap_index_stylesheet' ), + 'stylesheet CSS' => array( 'method' => 'get_stylesheet_css' ), + ); } } diff --git a/tests/phpunit/tests/wp/handle404.php b/tests/phpunit/tests/wp/handle404.php index 4b6b55064367e..e43af4da01d04 100644 --- a/tests/phpunit/tests/wp/handle404.php +++ b/tests/phpunit/tests/wp/handle404.php @@ -59,34 +59,20 @@ public function data_sitemap_requests(): array { } /** - * The sitemap stylesheet routes must not be 404ed either. - * - * Covered separately because is_sitemap() only reflects the `sitemap` query var. + * A legacy sitemap stylesheet query var must not bypass 404 handling. * * @ticket 65945 - * - * @dataProvider data_sitemap_stylesheet_requests - * - * @param non-falsy-string $url Stylesheet URL to request. */ - public function test_sitemap_stylesheet_requests_should_not_be_404ed_by_an_empty_main_query( string $url ) { - $this->go_to( home_url( $url ) ); + public function test_legacy_sitemap_stylesheet_query_var_does_not_bypass_404() { + global $wp; - $this->assertFalse( is_404(), 'WP::handle_404() should not have set a 404.' ); - } + $public_query_vars = $wp->public_query_vars; + $wp->add_query_var( 'sitemap-stylesheet' ); - /** - * Data provider. - * - * @return array - */ - public function data_sitemap_stylesheet_requests(): array { - return array( - 'sitemap stylesheet' => array( '/?sitemap-stylesheet=sitemap' ), - 'index stylesheet' => array( '/?sitemap-stylesheet=index' ), - // Not a real route, but the only stylesheet case is_home() doesn't already cover. - 'sitemap stylesheet, paged' => array( '/?sitemap-stylesheet=sitemap&paged=2' ), - ); + $this->go_to( home_url( '/this-page-does-not-exist/?sitemap-stylesheet=sitemap' ) ); + $wp->public_query_vars = $public_query_vars; + + $this->assertTrue( is_404(), 'A legacy sitemap stylesheet query var should not bypass 404 handling.' ); } /**