Make WordPress Core


Ignore:
Timestamp:
05/14/2023 05:56:26 PM (21 months 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.