Skip to content

Commit fbe72e4

Browse files
committed
Posts, Post Types: Update get_the_modified_author() to handle missing global $post and add (missing) $post arg.
The addition of the `$post` argument (which defaults to the global post) brings `get_the_modified_author()` in line with other similar functions, including `get_the_modified_date()` and `get_the_modified_time()`. Props Cornwell, jdahir0789, dhruvang21, Presskopp, mindctrl, samirmalpande, audrasjb, johnbillion, SergeyBiryukov, desrosj, costdev, mukesh27, westonruter. Fixes #64104, #55978. git-svn-id: https://develop.svn.wordpress.org/trunk@61057 602fd350-edb4-49c9-b593-d223f7449a82
1 parent f47fedd commit fbe72e4

File tree

2 files changed

+62
-15
lines changed

2 files changed

+62
-15
lines changed

src/wp-includes/author-template.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,24 +86,31 @@ function the_author( $deprecated = '', $deprecated_echo = true ) {
8686
* Retrieves the author who last edited the current post.
8787
*
8888
* @since 2.8.0
89+
* @since 6.9.0 Added the `$post` parameter. Unknown return value is now explicitly null instead of void.
8990
*
90-
* @return string|void The author's display name, empty string if unknown.
91+
* @param int|WP_Post|null $post Optional. Post ID or post object. Default is global `$post` object.
92+
* @return string|null The author's display name. Empty string if user is unavailable. Null if there was no last editor or the post is invalid.
9193
*/
92-
function get_the_modified_author() {
93-
$last_id = get_post_meta( get_post()->ID, '_edit_last', true );
94-
95-
if ( $last_id ) {
96-
$last_user = get_userdata( $last_id );
94+
function get_the_modified_author( $post = null ) {
95+
$post = get_post( $post );
96+
if ( ! $post ) {
97+
return null;
98+
}
9799

98-
/**
99-
* Filters the display name of the author who last edited the current post.
100-
*
101-
* @since 2.8.0
102-
*
103-
* @param string $display_name The author's display name, empty string if unknown.
104-
*/
105-
return apply_filters( 'the_modified_author', $last_user ? $last_user->display_name : '' );
100+
$last_id = get_post_meta( $post->ID, '_edit_last', true );
101+
if ( ! $last_id ) {
102+
return null;
106103
}
104+
$last_user = get_userdata( $last_id );
105+
106+
/**
107+
* Filters the display name of the author who last edited the current post.
108+
*
109+
* @since 2.8.0
110+
*
111+
* @param string $display_name The author's display name, empty string if user is unavailable.
112+
*/
113+
return apply_filters( 'the_modified_author', $last_user ? $last_user->display_name : '' );
107114
}
108115

109116
/**

tests/phpunit/tests/user/getTheModifiedAuthor.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
3737
public function set_up() {
3838
parent::set_up();
3939

40-
$GLOBALS['post'] = self::$post_id;
40+
$GLOBALS['post'] = get_post( self::$post_id );
4141
}
4242

4343
public function test_get_the_modified_author() {
@@ -56,4 +56,44 @@ public function test_get_the_modified_author_should_return_empty_string_if_user_
5656

5757
$this->assertSame( '', get_the_modified_author() );
5858
}
59+
60+
/**
61+
* @ticket 64104
62+
*/
63+
public function test_get_the_modified_author_when_post_global_does_not_exist() {
64+
$GLOBALS['post'] = null;
65+
$this->assertNull( get_the_modified_author() );
66+
}
67+
68+
/**
69+
* @ticket 64104
70+
*/
71+
public function test_get_the_modified_author_when_invalid_post() {
72+
$this->assertNull( get_the_modified_author( -1 ) );
73+
}
74+
75+
/**
76+
* @ticket 64104
77+
*/
78+
public function test_get_the_modified_author_for_another_post() {
79+
$expected_display_name = 'Test Editor';
80+
81+
$editor_id = self::factory()->user->create(
82+
array(
83+
'role' => 'editor',
84+
'user_login' => 'test_editor',
85+
'display_name' => $expected_display_name,
86+
'description' => 'test_editor',
87+
)
88+
);
89+
90+
$another_post_id = self::factory()->post->create();
91+
92+
$this->assertNull( get_the_modified_author( $another_post_id ) );
93+
$this->assertNull( get_the_modified_author( get_post( $another_post_id ) ) );
94+
95+
add_post_meta( $another_post_id, '_edit_last', $editor_id );
96+
$this->assertSame( $expected_display_name, get_the_modified_author( $another_post_id ) );
97+
$this->assertSame( $expected_display_name, get_the_modified_author( get_post( $another_post_id ) ) );
98+
}
5999
}

0 commit comments

Comments
 (0)