From fdd333fa471ec096cc4bcee71af67018b3ed765a Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 14 May 2023 17:56:26 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@55755 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/author-template.php | 20 ++++--- tests/phpunit/tests/user/getTheAuthor.php | 9 +++ tests/phpunit/tests/user/getTheAuthorMeta.php | 6 +- .../tests/user/getTheAuthorPostsLink.php | 9 +++ .../tests/user/getTheModifiedAuthor.php | 59 +++++++++++++++++++ 5 files changed, 93 insertions(+), 10 deletions(-) create mode 100644 tests/phpunit/tests/user/getTheModifiedAuthor.php diff --git a/src/wp-includes/author-template.php b/src/wp-includes/author-template.php index faeb6a89a0..184d7d0f38 100644 --- a/src/wp-includes/author-template.php +++ b/src/wp-includes/author-template.php @@ -14,11 +14,12 @@ * Retrieves the author of the current post. * * @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. * * @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 = '' ) { global $authordata; @@ -32,9 +33,9 @@ function get_the_author( $deprecated = '' ) { * * @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 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 ) { if ( ! empty( $deprecated ) ) { @@ -219,15 +220,15 @@ function the_author_meta( $field = '', $user_id = false ) { /** * 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 - * author's name. + * If the author has a home page set, return an HTML link, otherwise just return + * the author's name. * * @since 3.0.0 * * @global WP_User $authordata The current author's data. * - * @return string|null An HTML link if the author's url exist in user meta, - * else the result of get_the_author(). + * @return string An HTML link if the author's URL exists in user meta, + * otherwise the result of get_the_author(). */ function get_the_author_link() { if ( get_the_author_meta( 'url' ) ) { @@ -307,10 +308,11 @@ function the_author_posts() { * * @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() { global $authordata; + if ( ! is_object( $authordata ) ) { return ''; } diff --git a/tests/phpunit/tests/user/getTheAuthor.php b/tests/phpunit/tests/user/getTheAuthor.php index 522c5708d7..ebddcb3d5a 100644 --- a/tests/phpunit/tests/user/getTheAuthor.php +++ b/tests/phpunit/tests/user/getTheAuthor.php @@ -45,4 +45,13 @@ class Tests_User_GetTheAuthor extends WP_UnitTestCase { $this->assertSame( $user->display_name, $author_name ); $this->assertSame( 'Test Author', $author_name ); } + + /** + * @ticket 58157 + */ + public function test_get_the_author_should_return_empty_string_if_authordata_is_not_set() { + unset( $GLOBALS['authordata'] ); + + $this->assertSame( '', get_the_author() ); + } } diff --git a/tests/phpunit/tests/user/getTheAuthorMeta.php b/tests/phpunit/tests/user/getTheAuthorMeta.php index c15b6eacde..d9d225812b 100644 --- a/tests/phpunit/tests/user/getTheAuthorMeta.php +++ b/tests/phpunit/tests/user/getTheAuthorMeta.php @@ -61,7 +61,11 @@ class Tests_User_GetTheAuthorMeta extends WP_UnitTestCase { $this->assertSame( '', get_the_author_meta( 'does_not_exist' ) ); } - public function test_get_the_author_meta_no_authordata() { + /** + * @ticket 20529 + * @ticket 58157 + */ + public function test_get_the_author_meta_should_return_empty_string_if_authordata_is_not_set() { unset( $GLOBALS['authordata'] ); $this->assertSame( '', get_the_author_meta( 'id' ) ); diff --git a/tests/phpunit/tests/user/getTheAuthorPostsLink.php b/tests/phpunit/tests/user/getTheAuthorPostsLink.php index ea6b5d5b77..3fd96774df 100644 --- a/tests/phpunit/tests/user/getTheAuthorPostsLink.php +++ b/tests/phpunit/tests/user/getTheAuthorPostsLink.php @@ -79,4 +79,13 @@ class Tests_User_GetTheAuthorPostsLink extends WP_UnitTestCase { unset( $GLOBALS['authordata'] ); } + + /** + * @ticket 58157 + */ + public function test_get_the_author_posts_link_should_return_empty_string_if_authordata_is_not_set() { + unset( $GLOBALS['authordata'] ); + + $this->assertSame( '', get_the_author_posts_link() ); + } } diff --git a/tests/phpunit/tests/user/getTheModifiedAuthor.php b/tests/phpunit/tests/user/getTheModifiedAuthor.php new file mode 100644 index 0000000000..4a797bcbc6 --- /dev/null +++ b/tests/phpunit/tests/user/getTheModifiedAuthor.php @@ -0,0 +1,59 @@ +user->create( + array( + 'role' => 'author', + 'user_login' => 'test_author', + 'display_name' => 'Test Author', + 'description' => 'test_author', + 'user_url' => 'http://example.com', + ) + ); + + self::$post_id = $factory->post->create( + array( + 'post_author' => self::$author_id, + 'post_status' => 'publish', + 'post_content' => 'content', + 'post_title' => 'title', + 'post_type' => 'post', + ) + ); + + add_post_meta( self::$post_id, '_edit_last', self::$author_id ); + } + + public function set_up() { + parent::set_up(); + + $GLOBALS['post'] = self::$post_id; + } + + public function test_get_the_modified_author() { + $author_name = get_the_modified_author(); + $user = new WP_User( self::$author_id ); + + $this->assertSame( $user->display_name, $author_name ); + $this->assertSame( 'Test Author', $author_name ); + } + + /** + * @ticket 58157 + */ + public function test_get_the_modified_author_should_return_empty_string_if_user_id_does_not_exist() { + update_post_meta( self::$post_id, '_edit_last', -1 ); + + $this->assertSame( '', get_the_modified_author() ); + } +}