Improve global variable setting in `setup_postdata()`.

`setup_postdata()` is responsible for setting a number of global variables
that are used for post pagination (`$pages`, `$page`, `$nextpage`) and the
generation of post excerpts (`$more`). These variables should be sensitive to
the currently running instance of `WP_Query` - rather than the main query -
so that these features work properly inside of secondary `WP_Query` loops.

This changeset moves the logic of `setup_postdata()` into a method on `WP_Query`,
and converts `setup_postdata()` to a wrapper.

Props boonebgorges, wonderboymusic.
See #25349.
Fixes #9256, #20904.

git-svn-id: https://develop.svn.wordpress.org/trunk@30085 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2014-10-29 02:31:37 +00:00
parent 7c4aada4a6
commit 114c1f32ba
2 changed files with 67 additions and 97 deletions

View File

@ -3710,7 +3710,7 @@ class WP_Query {
do_action_ref_array( 'loop_start', array( &$this ) );
$post = $this->next_post();
setup_postdata($post);
$this->setup_postdata( $post );
}
/**
@ -4544,6 +4544,68 @@ class WP_Query {
return $wp_the_query === $this;
}
/**
* Set up global post data.
*
* @since 4.1.0
*
* @param object $post Post data.
* @return bool True when finished.
*/
public function setup_postdata( $post ) {
global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
$id = (int) $post->ID;
$authordata = get_userdata($post->post_author);
$currentday = mysql2date('d.m.y', $post->post_date, false);
$currentmonth = mysql2date('m', $post->post_date, false);
$numpages = 1;
$multipage = 0;
$page = $this->get( 'page' );
if ( ! $page )
$page = 1;
// Force full post content when viewing the permalink for the $post, or
// when on an RSS feed. Otherwise respect the 'more' tag.
if ( $post->ID === get_queried_object_id() && ( $this->is_page() || $this->is_single() ) ) {
$more = 1;
} else if ( $this->is_feed() ) {
$more = 1;
} else {
$more = 0;
}
$content = $post->post_content;
if ( false !== strpos( $content, '<!--nextpage-->' ) ) {
if ( $page > 1 )
$more = 1;
$content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content );
$content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content );
$content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content );
// Ignore nextpage at the beginning of the content.
if ( 0 === strpos( $content, '<!--nextpage-->' ) )
$content = substr( $content, 15 );
$pages = explode('<!--nextpage-->', $content);
$numpages = count($pages);
if ( $numpages > 1 )
$multipage = 1;
} else {
$pages = array( $post->post_content );
}
/**
* Fires once the post data has been setup.
*
* @since 2.8.0
*
* @param WP_Post &$post The Post object (passed by reference).
*/
do_action_ref_array( 'the_post', array( &$post ) );
return true;
}
/**
* After looping through a nested query, this function
* restores the $post global to the current post in this query.
@ -4630,47 +4692,11 @@ function wp_old_slug_redirect() {
* @return bool True when finished.
*/
function setup_postdata( $post ) {
global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
global $wp_query;
$id = (int) $post->ID;
$authordata = get_userdata($post->post_author);
$currentday = mysql2date('d.m.y', $post->post_date, false);
$currentmonth = mysql2date('m', $post->post_date, false);
$numpages = 1;
$multipage = 0;
$page = get_query_var('page');
if ( ! $page )
$page = 1;
if ( is_single() || is_page() || is_feed() )
$more = 1;
$content = $post->post_content;
if ( false !== strpos( $content, '<!--nextpage-->' ) ) {
if ( $page > 1 )
$more = 1;
$content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content );
$content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content );
$content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content );
// Ignore nextpage at the beginning of the content.
if ( 0 === strpos( $content, '<!--nextpage-->' ) )
$content = substr( $content, 15 );
$pages = explode('<!--nextpage-->', $content);
$numpages = count($pages);
if ( $numpages > 1 )
$multipage = 1;
} else {
$pages = array( $post->post_content );
if ( ! empty( $wp_query ) && $wp_query instanceof WP_Query ) {
return $wp_query->setup_postdata( $post );
}
/**
* Fires once the post data has been setup.
*
* @since 2.8.0
*
* @param WP_Post &$post The Post object (passed by reference).
*/
do_action_ref_array( 'the_post', array( &$post ) );
return true;
return false;
}

View File

@ -14,62 +14,6 @@ class Tests_Query extends WP_UnitTestCase {
$wp_rewrite->flush_rules();
}
/**
* @ticket 16746
*/
function test_nextpage_at_start_of_content() {
$post = $this->factory->post->create_and_get( array( 'post_content' => '<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3' ) );
setup_postdata( $post );
$this->assertEquals( 1, $GLOBALS['multipage'] );
$this->assertCount( 3, $GLOBALS['pages'] );
$this->assertEquals( 3, $GLOBALS['numpages'] );
$this->assertEquals( array( 'Page 1', 'Page 2', 'Page 3' ), $GLOBALS['pages'] );
}
function test_setup_postdata_single_page() {
$post = $this->factory->post->create_and_get( array( 'post_content' => 'Page 0' ) );
setup_postdata( $post );
$this->assertEquals( 0, $GLOBALS['multipage'] );
$this->assertCount( 1, $GLOBALS['pages'] );
$this->assertEquals( 1, $GLOBALS['numpages'] );
$this->assertEquals( array( 'Page 0' ), $GLOBALS['pages'] );
}
function test_setup_postdata_multi_page() {
$post = $this->factory->post->create_and_get( array( 'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3' ) );
setup_postdata( $post );
$this->assertEquals( 1, $GLOBALS['multipage'] );
$this->assertCount( 4, $GLOBALS['pages'] );
$this->assertEquals( 4, $GLOBALS['numpages'] );
$this->assertEquals( array( 'Page 0', 'Page 1', 'Page 2', 'Page 3' ), $GLOBALS['pages'] );
}
/**
* @ticket 24330
*
* setup_postdata( $a_post ) followed by the_content() in a loop that does not update
* global $post should use the content of $a_post rather then the global post.
*/
function test_setup_postdata_loop() {
$post_id = $this->factory->post->create( array( 'post_content' => 'global post' ) );
$GLOBALS['wp_query']->post = $GLOBALS['post'] = get_post( $post_id );
$ids = $this->factory->post->create_many(5);
foreach ( $ids as $id ) {
$page = get_post( $id );
if ( $page ) {
setup_postdata( $page );
$content = get_echo( 'the_content', array() );
$this->assertEquals( $post_id, $GLOBALS['post']->ID );
$this->assertNotEquals( '<p>global post</p>', strip_ws( $content ) );
wp_reset_postdata();
}
}
}
/**
* @ticket 24785
*