Canonical/Rewrite: sanity check posts that are paged with <!--nextpage-->. Page numbers past the max number of pages are returning the last page of content and causing infinite duplicate content.

Awesome rewrite bug: the `page` query var was being set to `'/4'` in `$wp`. When cast to `int`, it returns `0` (Bless you, PHP). `WP_Query` calls `trim( $page, '/' )` when setting its own query var. The few places that were checking `page`	before posts were queried now have sanity checks, so that these changes work without flushing rewrites.	

Adds/updates unit tests.

Props wonderboymusic, dd32.
See #11694.


git-svn-id: https://develop.svn.wordpress.org/trunk@34492 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor
2015-09-24 14:03:05 +00:00
parent c9093207df
commit 0111ecef55
6 changed files with 54 additions and 8 deletions

View File

@@ -587,7 +587,7 @@ class WP {
* @global WP_Query $wp_query
*/
public function handle_404() {
global $wp_query;
global $wp_query, $wp;
// If we've already issued a 404, bail.
if ( is_404() )
@@ -596,16 +596,26 @@ class WP {
// Never 404 for the admin, robots, or if we found posts.
if ( is_admin() || is_robots() || $wp_query->posts ) {
// Only set X-Pingback for single posts.
$success = true;
if ( is_singular() ) {
$p = clone $wp_query->post;
// Only set X-Pingback for single posts that allow pings.
if ( $p && pings_open( $p ) ) {
@header( 'X-Pingback: ' . get_bloginfo( 'pingback_url' ) );
}
// check for paged content that exceeds the max number of pages
$next = '<!--nextpage-->';
if ( $p && false !== strpos( $p->post_content, $next ) && ! empty( $wp->query_vars['page'] ) ) {
$page = trim( $wp->query_vars['page'], '/' );
$success = (int) $page <= ( substr_count( $p->post_content, $next ) + 1 );
}
}
status_header( 200 );
return;
if ( $success ) {
status_header( 200 );
return;
}
}
// We will 404 for paged queries, as no posts were found.