Skip to content

Commit fdd333f

Browse files
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
1 parent 02d89bc commit fdd333f

File tree

5 files changed

+93
-10
lines changed

5 files changed

+93
-10
lines changed

src/wp-includes/author-template.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
* Retrieves the author of the current post.
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
*/
2324
function get_the_author( $deprecated = '' ) {
2425
global $authordata;
@@ -32,9 +33,9 @@ function get_the_author( $deprecated = '' ) {
3233
*
3334
* @since 2.9.0
3435
*
35-
* @param string|null $display_name The author's display name.
36+
* @param string $display_name The author's display name.
3637
*/
37-
return apply_filters( 'the_author', is_object( $authordata ) ? $authordata->display_name : null );
38+
return apply_filters( 'the_author', is_object( $authordata ) ? $authordata->display_name : '' );
3839
}
3940

4041
/**
@@ -55,7 +56,7 @@ function get_the_author( $deprecated = '' ) {
5556
*
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
*/
6061
function the_author( $deprecated = '', $deprecated_echo = true ) {
6162
if ( ! empty( $deprecated ) ) {
@@ -219,15 +220,15 @@ function the_author_meta( $field = '', $user_id = false ) {
219220
/**
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
226227
*
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
*/
232233
function get_the_author_link() {
233234
if ( get_the_author_meta( 'url' ) ) {
@@ -307,10 +308,11 @@ function the_author_posts() {
307308
*
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
*/
312313
function get_the_author_posts_link() {
313314
global $authordata;
315+
314316
if ( ! is_object( $authordata ) ) {
315317
return '';
316318
}

tests/phpunit/tests/user/getTheAuthor.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,13 @@ public function test_get_the_author() {
4545
$this->assertSame( $user->display_name, $author_name );
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
}

tests/phpunit/tests/user/getTheAuthorMeta.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ public function test_get_the_author_meta() {
6161
$this->assertSame( '', get_the_author_meta( 'does_not_exist' ) );
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

6771
$this->assertSame( '', get_the_author_meta( 'id' ) );

tests/phpunit/tests/user/getTheAuthorPostsLink.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,13 @@ public function test_get_the_author_posts_link_with_permalinks() {
7979

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
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/**
4+
* @group author
5+
* @group user
6+
*
7+
* @covers ::get_the_modified_author
8+
*/
9+
class Tests_User_GetTheModifiedAuthor extends WP_UnitTestCase {
10+
protected static $author_id = 0;
11+
protected static $post_id = 0;
12+
13+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
14+
self::$author_id = $factory->user->create(
15+
array(
16+
'role' => 'author',
17+
'user_login' => 'test_author',
18+
'display_name' => 'Test Author',
19+
'description' => 'test_author',
20+
'user_url' => 'http://example.com',
21+
)
22+
);
23+
24+
self::$post_id = $factory->post->create(
25+
array(
26+
'post_author' => self::$author_id,
27+
'post_status' => 'publish',
28+
'post_content' => 'content',
29+
'post_title' => 'title',
30+
'post_type' => 'post',
31+
)
32+
);
33+
34+
add_post_meta( self::$post_id, '_edit_last', self::$author_id );
35+
}
36+
37+
public function set_up() {
38+
parent::set_up();
39+
40+
$GLOBALS['post'] = self::$post_id;
41+
}
42+
43+
public function test_get_the_modified_author() {
44+
$author_name = get_the_modified_author();
45+
$user = new WP_User( self::$author_id );
46+
47+
$this->assertSame( $user->display_name, $author_name );
48+
$this->assertSame( 'Test Author', $author_name );
49+
}
50+
51+
/**
52+
* @ticket 58157
53+
*/
54+
public function test_get_the_modified_author_should_return_empty_string_if_user_id_does_not_exist() {
55+
update_post_meta( self::$post_id, '_edit_last', -1 );
56+
57+
$this->assertSame( '', get_the_modified_author() );
58+
}
59+
}

0 commit comments

Comments
 (0)