Ticket #50910: 50910.diff
File 50910.diff, 9.1 KB (added by , 4 years ago) |
---|
-
src/wp-includes/canonical.php
From 27488d854ee07b0b064659761dd62b2a9148911f Mon Sep 17 00:00:00 2001 From: Paul Biron <paul@sparrowhawkcomputing.com> Date: Sun, 16 Aug 2020 19:15:55 -0600 Subject: [PATCH] Ensure sitemap URLs with paged > 1 work as expected. Introduces the get_sitemap_url() function which is sort of equivalent to get_parmalink() but for sitemaps. That new function is used in redirect_canonical(). --- src/wp-includes/canonical.php | 10 +-- src/wp-includes/sitemaps.php | 36 +++++++++ tests/phpunit/tests/canonical/sitemaps.php | 37 +++++++++ tests/phpunit/tests/sitemaps/functions.php | 92 ++++++++++++++++++++++ 4 files changed, 169 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/canonical.php b/src/wp-includes/canonical.php index 5a63de3481..efe84baca9 100644
a b function redirect_canonical( $requested_url = null, $do_redirect = true ) { 411 411 } 412 412 413 413 // Paging and feeds. 414 if ( get_query_var( 'paged' ) || is_feed() || get_query_var( 'cpage' ) ) {414 if ( ( get_query_var( 'paged' ) || is_feed() || get_query_var( 'cpage' ) ) && ! get_query_var( 'sitemap' ) ) { 415 415 $paged = get_query_var( 'paged' ); 416 416 $feed = get_query_var( 'feed' ); 417 417 $cpage = get_query_var( 'cpage' ); … … function redirect_canonical( $requested_url = null, $do_redirect = true ) { 508 508 $redirect['path'] = trailingslashit( $redirect['path'] ) . $addl_path; 509 509 } 510 510 511 // Remove trailing slash for sitemaps requests.512 if ( ! empty( get_query_var( 'sitemap' ) ) ) {513 $redirect['path'] = untrailingslashit( $redirect['path'] );514 }515 516 511 $redirect_url = $redirect['scheme'] . '://' . $redirect['host'] . $redirect['path']; 512 } elseif ( get_query_var( 'sitemap' ) ) { 513 $redirect_url = get_sitemap_url( get_query_var( 'sitemap' ), get_query_var( 'sitemap-subtype' ), get_query_var( 'paged' ) ); 514 $redirect['query'] = remove_query_arg( array( 'sitemap', 'sitemap-subtype', 'paged' ), $redirect['query'] ); 517 515 } 518 516 519 517 if ( 'wp-register.php' === basename( $redirect['path'] ) ) { -
src/wp-includes/sitemaps.php
diff --git a/src/wp-includes/sitemaps.php b/src/wp-includes/sitemaps.php index 62752dada9..1dfcd1d69c 100644
a b function wp_sitemaps_get_max_urls( $object_type ) { 87 87 */ 88 88 return apply_filters( 'wp_sitemaps_max_urls', 2000, $object_type ); 89 89 } 90 91 /** 92 * Retrieves the full URL for a sitemap. 93 * 94 * @since 5.5.1 95 * 96 * @param string $name The sitemap name. 97 * @param string $subtype_name The sitemap subtype name. Default empty string. 98 * @param int $page The page of the sitemap. Default 1. 99 * @return string|false The sitemap URL or false if the sitemap doesn't exist. 100 */ 101 function get_sitemap_url( $name, $subtype_name = '', $page = 1 ) { 102 $sitemaps = wp_sitemaps_get_server(); 103 if ( ! $sitemaps ) { 104 return false; 105 } 106 107 if ( 'index' === $name ) { 108 return $sitemaps->index->get_index_url(); 109 } 110 111 $provider = $sitemaps->registry->get_provider( $name ); 112 if ( ! $provider ) { 113 return false; 114 } 115 116 if ( $subtype_name && ! in_array( $subtype_name, array_keys( $provider->get_object_subtypes() ), true ) ) { 117 return false; 118 } 119 120 $page = absint( $page ); 121 if ( 0 >= $page ) { 122 $page = 1; 123 } 124 return $provider->get_sitemap_url( $subtype_name, $page ); 125 } -
tests/phpunit/tests/canonical/sitemaps.php
diff --git a/tests/phpunit/tests/canonical/sitemaps.php b/tests/phpunit/tests/canonical/sitemaps.php index dd0bdfbdb6..1e0c86fad4 100644
a b class Tests_Canonical_Sitemaps extends WP_Canonical_UnitTestCase { 40 40 $this->assertCanonical( '/wp-sitemap.xsl/', '/wp-sitemap.xsl' ); 41 41 } 42 42 43 /** 44 * Ensure sitemaps redirects work as expected. 45 * 46 * @dataProvider sitemaps_canonical_redirects_provider 47 * @ticket 50910 48 */ 49 public function test_sitemaps_canonical_redirects( $test_url, $expected ) { 50 $this->assertCanonical( $test_url, $expected, 50910 ); 51 } 52 53 /** 54 * Data provider for test_sitemaps_canonical_redirects. 55 * 56 * @return array[] { 57 * Data to test with. 58 * 59 * @type string $0 The test URL. 60 * @type string $1 The expected canonical URL. 61 * } 62 */ 63 public function sitemaps_canonical_redirects_provider() { 64 return array( 65 // Ugly/incorrect versions redirect correctly. 66 array( '/?sitemap=index', '/wp-sitemap.xml' ), 67 array( '/wp-sitemap.xml/', '/wp-sitemap.xml' ), 68 array( '/?sitemap=posts&sitemap-subtype=post', '/wp-sitemap-posts-post-1.xml' ), 69 array( '/?sitemap=posts&sitemap-subtype=post&paged=2', '/wp-sitemap-posts-post-2.xml' ), 70 array( '/?sitemap=taxonomies&sitemap-subtype=category', '/wp-sitemap-taxonomies-category-1.xml' ), 71 array( '/?sitemap=taxonomies&sitemap-subtype=category&paged=2', '/wp-sitemap-taxonomies-category-2.xml' ), 72 73 // Pretty versions don't redirect incorrectly. 74 array( '/wp-sitemap-posts-post-1.xml', '/wp-sitemap-posts-post-1.xml' ), 75 array( '/wp-sitemap-posts-post-2.xml', '/wp-sitemap-posts-post-2.xml' ), 76 array( '/wp-sitemap-taxonomies-category-1.xml', '/wp-sitemap-taxonomies-category-1.xml' ), 77 array( '/wp-sitemap-taxonomies-category-2.xml', '/wp-sitemap-taxonomies-category-2.xml' ), 78 ); 79 } 43 80 } -
tests/phpunit/tests/sitemaps/functions.php
diff --git a/tests/phpunit/tests/sitemaps/functions.php b/tests/phpunit/tests/sitemaps/functions.php index 3fe904a6ae..3615671431 100644
a b class Test_Sitemaps_Functions extends WP_UnitTestCase { 58 58 $this->assertTrue( is_a( $sitemaps[ $name ], $provider ), "Default $name sitemap is not a $provider object." ); 59 59 } 60 60 } 61 62 /** 63 * Test get_sitemap_url() with ugly permalinks. 64 * 65 * @dataProvider ugly_permalinks_provider 66 * 67 * @group bar 68 */ 69 public function test_get_sitemap_url_ugly_permalinks( $name, $subtype_name, $page, $expected ) { 70 $actual = get_sitemap_url( $name, $subtype_name, $page ); 71 72 $this->assertSame( $expected, $actual ); 73 } 74 75 /** 76 * Test get_sitemap_url() with pretty permalinks. 77 * 78 * @dataProvider pretty_permalinks_provider 79 * 80 * @group bar 81 */ 82 public function test_get_sitemap_url_pretty_permalinks( $name, $subtype_name, $page, $expected ) { 83 $this->set_permalink_structure( '/%postname%/' ); 84 85 $actual = get_sitemap_url( $name, $subtype_name, $page ); 86 87 $this->assertSame( $expected, $actual ); 88 } 89 90 /** 91 * Data provider for test_get_sitemap_url_ugly_permalinks. 92 * 93 * @return array[] { 94 * Data to test with. 95 * 96 * @type string $0 Sitemap name. 97 * @type string $1 Sitemap subtype name. 98 * @type int $3 Sitemap page. 99 * @type string|false $4 Sitemap URL. 100 * } 101 */ 102 function ugly_permalinks_provider() { 103 return array( 104 array( 'posts', 'post', 1, home_url( '/?sitemap=posts&sitemap-subtype=post&paged=1' ) ), 105 array( 'posts', 'post', 0, home_url( '/?sitemap=posts&sitemap-subtype=post&paged=1' ) ), 106 array( 'posts', 'page', 1, home_url( '/?sitemap=posts&sitemap-subtype=page&paged=1' ) ), 107 array( 'posts', 'page', 5, home_url( '/?sitemap=posts&sitemap-subtype=page&paged=5' ) ), 108 // post_type doesn't exist. 109 array( 'posts', 'foo', 5, false ), 110 array( 'taxonomies', 'category', 1, home_url( '/?sitemap=taxonomies&sitemap-subtype=category&paged=1' ) ), 111 array( 'taxonomies', 'post_tag', 1, home_url( '/?sitemap=taxonomies&sitemap-subtype=post_tag&paged=1' ) ), 112 array( 'taxonomies', 'post_tag', -1, home_url( '/?sitemap=taxonomies&sitemap-subtype=post_tag&paged=1' ) ), 113 // negative paged, gets converted to it's absolute value. 114 array( 'users', '', 4, home_url( '/?sitemap=users&paged=4' ) ), 115 // users provider doesn't allow subtypes. 116 array( 'users', 'foo', 4, false ), 117 // provider doesn't exist. 118 array( 'foo', '', 4, false ), 119 ); 120 } 121 122 /** 123 * Data provider for test_get_sitemap_url_pretty_permalinks 124 * 125 * @return array[] { 126 * Data to test with. 127 * 128 * @type string $0 Sitemap name. 129 * @type string $1 Sitemap subtype name. 130 * @type int $3 Sitemap page. 131 * @type string|false $4 Sitemap URL. 132 * } 133 */ 134 function pretty_permalinks_provider() { 135 return array( 136 array( 'posts', 'post', 1, home_url( '/wp-sitemap-posts-post-1.xml' ) ), 137 array( 'posts', 'post', 0, home_url( '/wp-sitemap-posts-post-1.xml' ) ), 138 array( 'posts', 'page', 1, home_url( '/wp-sitemap-posts-page-1.xml' ) ), 139 array( 'posts', 'page', 5, home_url( '/wp-sitemap-posts-page-5.xml' ) ), 140 // post_type doesn't exist. 141 array( 'posts', 'foo', 5, false ), 142 array( 'taxonomies', 'category', 1, home_url( '/wp-sitemap-taxonomies-category-1.xml' ) ), 143 array( 'taxonomies', 'post_tag', 1, home_url( '/wp-sitemap-taxonomies-post_tag-1.xml' ) ), 144 // negative paged, gets converted to it's absolute value. 145 array( 'taxonomies', 'post_tag', -1, home_url( '/wp-sitemap-taxonomies-post_tag-1.xml' ) ), 146 array( 'users', '', 4, home_url( '/wp-sitemap-users-4.xml' ) ), 147 // users provider doesn't allow subtypes. 148 array( 'users', 'foo', 4, false ), 149 // provider doesn't exist. 150 array( 'foo', '', 4, false ), 151 ); 152 } 61 153 }