Make WordPress Core

Changeset 55755


Ignore:
Timestamp:
05/14/2023 05:56:26 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Correct fallback return value in get_the_author().

If the $authordata global is not set, get_the_author() returned null, causing a PHP 8.1 "null to non-nullable" deprecation notice in ent2ncr() hooked via the_author filter:

str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

This commit updates get_the_author() to return an empty string if called before $authordata is set, bringing consistency with a few other similar functions which also return an empty string in this case:

  • get_the_author_meta()
  • get_the_author_posts_link()
  • get_the_modified_author()

Follow-up to [695/tests], [2858], [11138], [12284], [20575], [34677], [44616], [53187].

Props Soean, jrf, sabernhardt, salvoaranzulla, antpb, ebai4, sajjad67, tijmensmit, SergeyBiryukov.
Fixes #58157.

Location:
trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/author-template.php

    r55444 r55755  
    1515 *
    1616 * @since 1.5.0
     17 * @since 6.3.0 Returns an empty string if the author's display name is unknown.
    1718 *
    1819 * @global WP_User $authordata The current author's data.
    1920 *
    2021 * @param string $deprecated Deprecated.
    21  * @return string|null The author's display name.
     22 * @return string The author's display name, empty string if unknown.
    2223 */
    2324function get_the_author( $deprecated = '' ) {
    … …  
    3334         * @since 2.9.0
    3435         *
    35          * @param string|null $display_name The author's display name.
    36          */
    37         return apply_filters( 'the_author', is_object( $authordata ) ? $authordata->display_name : null );
     36         * @param string $display_name The author's display name.
     37         */
     38        return apply_filters( 'the_author', is_object( $authordata ) ? $authordata->display_name : '' );
    3839}
    3940
    … …  
    5657 * @param string $deprecated      Deprecated.
    5758 * @param bool   $deprecated_echo Deprecated. Use get_the_author(). Echo the string or return it.
    58  * @return string|null The author's display name, from get_the_author().
     59 * @return string The author's display name, from get_the_author().
    5960 */
    6061function the_author( $deprecated = '', $deprecated_echo = true ) {
    … …  
    220221 * Retrieves either author's link or author's name.
    221222 *
    222  * If the author has a home page set, return an HTML link, otherwise just return the
    223  * author's name.
     223 * If the author has a home page set, return an HTML link, otherwise just return
     224 * the author's name.
    224225 *
    225226 * @since 3.0.0
    … …  
    227228 * @global WP_User $authordata The current author's data.
    228229 *
    229  * @return string|null An HTML link if the author's url exist in user meta,
    230  *                     else the result of get_the_author().
     230 * @return string An HTML link if the author's URL exists in user meta,
     231 *                otherwise the result of get_the_author().
    231232 */
    232233function get_the_author_link() {
    … …  
    308309 * @global WP_User $authordata The current author's data.
    309310 *
    310  * @return string An HTML link to the author page, or an empty string if $authordata isn't defined.
     311 * @return string An HTML link to the author page, or an empty string if $authordata is not set.
    311312 */
    312313function get_the_author_posts_link() {
    313314        global $authordata;
     315
    314316        if ( ! is_object( $authordata ) ) {
    315317                return '';
  • trunk/tests/phpunit/tests/user/getTheAuthor.php

    r55754 r55755  
    4646                $this->assertSame( 'Test Author', $author_name );
    4747        }
     48
     49        /**
     50         * @ticket 58157
     51         */
     52        public function test_get_the_author_should_return_empty_string_if_authordata_is_not_set() {
     53                unset( $GLOBALS['authordata'] );
     54
     55                $this->assertSame( '', get_the_author() );
     56        }
    4857}
  • trunk/tests/phpunit/tests/user/getTheAuthorMeta.php

    r55754 r55755  
    6262        }
    6363
    64         public function test_get_the_author_meta_no_authordata() {
     64        /**
     65         * @ticket 20529
     66         * @ticket 58157
     67         */
     68        public function test_get_the_author_meta_should_return_empty_string_if_authordata_is_not_set() {
    6569                unset( $GLOBALS['authordata'] );
    6670
  • trunk/tests/phpunit/tests/user/getTheAuthorPostsLink.php

    r55754 r55755  
    8080                unset( $GLOBALS['authordata'] );
    8181        }
     82
     83        /**
     84         * @ticket 58157
     85         */
     86        public function test_get_the_author_posts_link_should_return_empty_string_if_authordata_is_not_set() {
     87                unset( $GLOBALS['authordata'] );
     88
     89                $this->assertSame( '', get_the_author_posts_link() );
     90        }
    8291}
Note: See TracChangeset for help on using the changeset viewer.