Skip to content
Permalink
Browse files

Fix `setup_postdata()` to set the (inside the loop) globals before `t…

…he_post` action is fired. Follow-up from #42814 and [44941].

Props david.binda, spacedmonkey, boonebgorges, birgire, jorbin, azaozz.
Merges [45285] from trunk.
Fixes #47114 for 5.2.



git-svn-id: https://develop.svn.wordpress.org/branches/5.2@45286 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information...
azaozz committed May 4, 2019
1 parent 54ed149 commit 54dfd64e14393956c11fc2e022ef8f2cf47b67a3
Showing with 33 additions and 11 deletions.
  1. +11 −11 src/wp-includes/class-wp-query.php
  2. +22 −0 tests/phpunit/tests/query/setupPostdata.php
@@ -4208,6 +4208,17 @@ public function setup_postdata( $post ) {
$more = $elements['more'];
$numpages = $elements['numpages'];
/**
* Fires once the post data has been setup.
*
* @since 2.8.0
* @since 4.1.0 Introduced `$this` parameter.
*
* @param WP_Post $post The Post object (passed by reference).
* @param WP_Query $this The current Query object (passed by reference).
*/
do_action_ref_array( 'the_post', array( &$post, &$this ) );
return true;
}
@@ -4298,17 +4309,6 @@ public function generate_postdata( $post ) {
$multipage = 0;
}
/**
* Fires once the post data has been setup.
*
* @since 2.8.0
* @since 4.1.0 Introduced `$this` parameter.
*
* @param WP_Post $post The Post object (passed by reference).
* @param WP_Query $this The current Query object (passed by reference).
*/
do_action_ref_array( 'the_post', array( &$post, &$this ) );
$elements = compact( 'id', 'authordata', 'currentday', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages' );
return $elements;
@@ -9,6 +9,8 @@ class Tests_Query_SetupPostdata extends WP_UnitTestCase {
protected $global_data = array();
protected $pages_global;
public function setUp() {
parent::setUp();
return;
@@ -416,4 +418,24 @@ function test_setup_postdata_loop() {
}
}
/**
* @ticket 47114
*
* setup_postdata() should set the globals before `the_post` action is fired.
*/
public function test_the_post_action() {
$post = self::factory()->post->create_and_get();
add_action( 'the_post', array( $this, 'the_post_action_callback' ) );
setup_postdata( $post );
$this->assertEquals( $GLOBALS['pages'], $this->pages_global );
}
/**
* Helpers
*/
public function the_post_action_callback() {
$this->pages_global = $GLOBALS['pages'];
}
}

0 comments on commit 54dfd64

Please sign in to comment.
You can’t perform that action at this time.