Make WordPress Core


Ignore:
Timestamp:
03/20/2019 03:48:46 PM (8 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/class-wp-query.php

    r44518 r44941  
    41564156                }
    41574157
     4158                $elements = $this->generate_postdata( $post );
     4159                if ( false === $elements ) {
     4160                        return;
     4161                }
     4162
     4163                $id           = $elements['id'];
     4164                $authordata   = $elements['authordata'];
     4165                $currentday   = $elements['currentday'];
     4166                $currentmonth = $elements['currentmonth'];
     4167                $page         = $elements['page'];
     4168                $pages        = $elements['pages'];
     4169                $multipage    = $elements['multipage'];
     4170                $more         = $elements['more'];
     4171                $numpages     = $elements['numpages'];
     4172
     4173                return true;
     4174        }
     4175
     4176        /**
     4177         * Generate post data.
     4178         *
     4179         * @since 5.2.0
     4180         *
     4181         * @param WP_Post|object|int $post WP_Post instance or Post ID/object.
     4182         * @return array|bool $elements Elements of post or false on failure.
     4183         */
     4184        public function generate_postdata( $post ) {
     4185
     4186                if ( ! ( $post instanceof WP_Post ) ) {
     4187                        $post = get_post( $post );
     4188                }
     4189
     4190                if ( ! $post ) {
     4191                        return false;
     4192                }
     4193
    41584194                $id = (int) $post->ID;
    41594195
    … …  
    42364272                do_action_ref_array( 'the_post', array( &$post, &$this ) );
    42374273
    4238                 return true;
     4274                $elements = compact( 'id', 'authordata', 'currentday', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages' );
     4275
     4276                return $elements;
    42394277        }
    42404278        /**
Note: See TracChangeset for help on using the changeset viewer.