Make WordPress Core


Ignore:
Timestamp:
03/20/2019 03:48:46 PM (7 years ago)
Author:
boonebgorges
Message:

Posts: Avoid the use of globals in get_the_content() and related functions.

This changeset introduces $post parameters to get_the_content() and
wp_trim_excerpt(). When a $post object is passed to one of these functions,
the functions will operate on the data from that object, rather than from the
post globals ($authordata, $page, etc). This ensures that the functions work
in a predictable manner when used outside of the regular post loop.

The global-mismatch problem is surfaced in cases where get_the_excerpt() is
called outside of the post loop, on posts that don't have a defined excerpt. In
these cases, the post globals - used to generate a fallback excerpt - may refer
to the incorrect object, resulting in PHP notices or other unpredictable
behavior. See #36934 for a related issue.

Props spacedmonkey, kraftbj, Shital Patel.
Fixes #42814.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post/getTheExcerpt.php

    r42343 r44941  
    5858        $this->assertSame( 'Bar', get_the_excerpt( $post_id ) );
    5959    }
     60
     61    /**
     62     * @ticket 42814
     63     */
     64    public function test_should_fall_back_on_post_content_if_excerpt_is_empty_and_post_is_inferred_from_context() {
     65        $post_id = self::factory()->post->create(
     66            array(
     67                'post_content' => 'Foo',
     68                'post_excerpt' => '',
     69            )
     70        );
     71
     72        $q = new WP_Query(
     73            array(
     74                'p' => $post_id,
     75            )
     76        );
     77
     78        while ( $q->have_posts() ) {
     79            $q->the_post();
     80            $found = get_the_excerpt();
     81        }
     82
     83        $this->assertSame( 'Foo', $found );
     84    }
     85
     86    /**
     87     * @ticket 42814
     88     */
     89    public function test_should_fall_back_on_post_content_if_excerpt_is_empty_and_post_is_provided() {
     90        $GLOBALS['post'] = self::factory()->post->create_and_get(
     91            array(
     92                'post_content' => 'Foo',
     93                'post_excerpt' => '',
     94            )
     95        );
     96        $this->assertSame( 'Foo', get_the_excerpt( $GLOBALS['post'] ) );
     97    }
     98
     99    /**
     100     * @ticket 42814
     101     */
     102    public function test_should_respect_post_parameter_in_the_loop() {
     103        $p1 = self::factory()->post->create_and_get( array( 'post_excerpt' => 'Foo' ) );
     104        $p2 = self::factory()->post->create_and_get( array( 'post_excerpt' => 'Bar' ) );
     105        $q  = new WP_Query(
     106            array(
     107                'p' => $p1->ID,
     108            )
     109        );
     110
     111        while ( $q->have_posts() ) {
     112            $q->the_post();
     113            $found = get_the_excerpt( $p2 );
     114        }
     115
     116        $this->assertSame( 'Bar', $found );
     117    }
     118
     119    /**
     120     * @ticket 42814
     121     */
     122    public function test_should_respect_post_parameter_in_the_loop_when_falling_back_on_post_content() {
     123        $p1 = self::factory()->post->create_and_get(
     124            array(
     125                'post_content' => 'Foo',
     126                'post_excerpt' => '',
     127            )
     128        );
     129        $p2 = self::factory()->post->create_and_get(
     130            array(
     131                'post_content' => 'Bar',
     132                'post_excerpt' => '',
     133            )
     134        );
     135        $q  = new WP_Query(
     136            array(
     137                'p' => $p1->ID,
     138            )
     139        );
     140
     141        while ( $q->have_posts() ) {
     142            $q->the_post();
     143            $found = get_the_excerpt( $p2 );
     144        }
     145
     146        $this->assertSame( 'Bar', $found );
     147    }
    60148}
Note: See TracChangeset for help on using the changeset viewer.