Make WordPress Core


Ignore:
Timestamp:
10/29/2014 02:31:37 AM (11 years ago)
Author:
boonebgorges
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/query.php

    r28967 r30085  
    1313
    1414        $wp_rewrite->flush_rules();
    15     }
    16 
    17     /**
    18      * @ticket 16746
    19      */
    20     function test_nextpage_at_start_of_content() {
    21         $post = $this->factory->post->create_and_get( array( 'post_content' => '<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3' ) );
    22         setup_postdata( $post );
    23 
    24         $this->assertEquals( 1, $GLOBALS['multipage'] );
    25         $this->assertCount(  3, $GLOBALS['pages']     );
    26         $this->assertEquals( 3, $GLOBALS['numpages']  );
    27         $this->assertEquals( array( 'Page 1', 'Page 2', 'Page 3' ), $GLOBALS['pages'] );
    28     }
    29 
    30     function test_setup_postdata_single_page() {
    31         $post = $this->factory->post->create_and_get( array( 'post_content' => 'Page 0' ) );
    32         setup_postdata( $post );
    33 
    34         $this->assertEquals( 0, $GLOBALS['multipage'] );
    35         $this->assertCount(  1, $GLOBALS['pages']     );
    36         $this->assertEquals( 1, $GLOBALS['numpages']  );
    37         $this->assertEquals( array( 'Page 0' ), $GLOBALS['pages'] );
    38     }
    39 
    40     function test_setup_postdata_multi_page() {
    41         $post = $this->factory->post->create_and_get( array( 'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3' ) );
    42         setup_postdata( $post );
    43 
    44         $this->assertEquals( 1, $GLOBALS['multipage'] );
    45         $this->assertCount(  4, $GLOBALS['pages']     );
    46         $this->assertEquals( 4, $GLOBALS['numpages']  );
    47         $this->assertEquals( array( 'Page 0', 'Page 1', 'Page 2', 'Page 3' ), $GLOBALS['pages'] );
    48     }
    49 
    50     /**
    51      * @ticket 24330
    52      *
    53      * setup_postdata( $a_post ) followed by the_content() in a loop that does not update
    54      * global $post should use the content of $a_post rather then the global post.
    55      */
    56     function test_setup_postdata_loop() {
    57         $post_id = $this->factory->post->create( array( 'post_content' => 'global post' ) );
    58         $GLOBALS['wp_query']->post = $GLOBALS['post'] = get_post( $post_id );
    59 
    60         $ids = $this->factory->post->create_many(5);
    61         foreach ( $ids as $id ) {
    62             $page = get_post( $id );
    63             if ( $page ) {
    64                 setup_postdata( $page );
    65                 $content = get_echo( 'the_content', array() );
    66                 $this->assertEquals( $post_id, $GLOBALS['post']->ID );
    67                 $this->assertNotEquals( '<p>global post</p>', strip_ws( $content ) );
    68                 wp_reset_postdata();
    69             }
    70         }
    7115    }
    7216
Note: See TracChangeset for help on using the changeset viewer.