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/src/wp-includes/formatting.php

    r44858 r44941  
    36793679 *
    36803680 * @since 1.5.0
    3681  *
    3682  * @param string $text Optional. The excerpt. If set to empty, an excerpt is generated.
     3681 * @since 5.2.0 Added the `$post` parameter.
     3682 *
     3683 * @param string             $text Optional. The excerpt. If set to empty, an excerpt is generated.
     3684 * @param WP_Post|object|int $post Optional. WP_Post instance or Post ID/object. Default is null.
    36833685 * @return string The excerpt.
    36843686 */
    3685 function wp_trim_excerpt( $text = '' ) {
     3687function wp_trim_excerpt( $text = '', $post = null ) {
    36863688    $raw_excerpt = $text;
    36873689    if ( '' == $text ) {
    3688         $text = get_the_content( '' );
     3690        $post = get_post( $post );
     3691        $text = get_the_content( '', false, $post );
    36893692
    36903693        $text = strip_shortcodes( $text );
Note: See TracChangeset for help on using the changeset viewer.