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.
Built from https://develop.svn.wordpress.org/trunk@55755


git-svn-id: http://core.svn.wordpress.org/trunk@55267 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2023-05-14 17:58:24 +00:00
parent 8abc201cfe
commit ae3c5a4be6
2 changed files with 12 additions and 10 deletions

View File

@ -14,11 +14,12 @@
* Retrieves the author of the current post. * Retrieves the author of the current post.
* *
* @since 1.5.0 * @since 1.5.0
* @since 6.3.0 Returns an empty string if the author's display name is unknown.
* *
* @global WP_User $authordata The current author's data. * @global WP_User $authordata The current author's data.
* *
* @param string $deprecated Deprecated. * @param string $deprecated Deprecated.
* @return string|null The author's display name. * @return string The author's display name, empty string if unknown.
*/ */
function get_the_author( $deprecated = '' ) { function get_the_author( $deprecated = '' ) {
global $authordata; global $authordata;
@ -32,9 +33,9 @@ function get_the_author( $deprecated = '' ) {
* *
* @since 2.9.0 * @since 2.9.0
* *
* @param string|null $display_name The author's display name. * @param string $display_name The author's display name.
*/ */
return apply_filters( 'the_author', is_object( $authordata ) ? $authordata->display_name : null ); return apply_filters( 'the_author', is_object( $authordata ) ? $authordata->display_name : '' );
} }
/** /**
@ -55,7 +56,7 @@ function get_the_author( $deprecated = '' ) {
* *
* @param string $deprecated Deprecated. * @param string $deprecated Deprecated.
* @param bool $deprecated_echo Deprecated. Use get_the_author(). Echo the string or return it. * @param bool $deprecated_echo Deprecated. Use get_the_author(). Echo the string or return it.
* @return string|null The author's display name, from get_the_author(). * @return string The author's display name, from get_the_author().
*/ */
function the_author( $deprecated = '', $deprecated_echo = true ) { function the_author( $deprecated = '', $deprecated_echo = true ) {
if ( ! empty( $deprecated ) ) { if ( ! empty( $deprecated ) ) {
@ -219,15 +220,15 @@ function the_author_meta( $field = '', $user_id = false ) {
/** /**
* Retrieves either author's link or author's name. * Retrieves either author's link or author's name.
* *
* If the author has a home page set, return an HTML link, otherwise just return the * If the author has a home page set, return an HTML link, otherwise just return
* author's name. * the author's name.
* *
* @since 3.0.0 * @since 3.0.0
* *
* @global WP_User $authordata The current author's data. * @global WP_User $authordata The current author's data.
* *
* @return string|null An HTML link if the author's url exist in user meta, * @return string An HTML link if the author's URL exists in user meta,
* else the result of get_the_author(). * otherwise the result of get_the_author().
*/ */
function get_the_author_link() { function get_the_author_link() {
if ( get_the_author_meta( 'url' ) ) { if ( get_the_author_meta( 'url' ) ) {
@ -307,10 +308,11 @@ function the_author_posts() {
* *
* @global WP_User $authordata The current author's data. * @global WP_User $authordata The current author's data.
* *
* @return string An HTML link to the author page, or an empty string if $authordata isn't defined. * @return string An HTML link to the author page, or an empty string if $authordata is not set.
*/ */
function get_the_author_posts_link() { function get_the_author_posts_link() {
global $authordata; global $authordata;
if ( ! is_object( $authordata ) ) { if ( ! is_object( $authordata ) ) {
return ''; return '';
} }

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.3-alpha-55754'; $wp_version = '6.3-alpha-55755';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.