Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#55420 closed defect (bug) (fixed)

Warning thrown by method get_the_modified_author if author was removed

Reported by: juanlopez4691's profile juanlopez4691 Owned by: audrasjb's profile audrasjb
Milestone: 6.0 Priority: normal
Severity: normal Version:
Component: Users Keywords: has-patch
Focuses: administration Cc:

Description

If a post has been last modified by a removed author, method get_the_modified_author throws a warning:

Warning: Attempt to read property "display_name" on bool in htdocs/wp-includes/author-template.php on line 104

This happens because get_userdata returns false if the user that modified the post last no longer exists.

A possible solution:

Modify file wp-includes/author-template.php, line 104, from
return apply_filters( 'the_modified_author', $last_user->display_name );
to
return apply_filters( 'the_modified_author', $last_user ? $last_user->display_name : '' );

Attachments (2)

author-template.patch (497 bytes) - added by juanlopez4691 2 years ago.
Delete User.png (14.7 KB) - added by mukesh27 2 years ago.

Download all attachments as: .zip

Change History (11)

#1 @SergeyBiryukov
2 years ago

  • Milestone changed from Awaiting Review to 6.0

This ticket was mentioned in PR #2430 on WordPress/wordpress-develop by juanlopez4691.


2 years ago
#2

  • Keywords has-patch added

A warning is thrown when retrieving the the last author that modified the post if that user was removed.

See https://core.trac.wordpress.org/ticket/55420

Trac ticket: [](https://core.trac.wordpress.org/ticket/55420)

#3 @mukesh27
2 years ago

  • Version 5.9.2 deleted

Hi there!

Welcome to Trac and thanks for the ticket and patch.

Can you please share steps to replicate the issue because I can't replicate it from the given instruction?

#4 @juanlopez4691
2 years ago

@mukesh27 You can try as follows:

  1. Log in admin create a post with any existing user.
  2. Create a new user.
  3. Logout and login back as user /2/.
  4. Modify the post created in /1/
  5. Log out and login back as user /1/ (should have administrator role).
  6. Delete user #2.

The result is:

  • postmeta field _edit_last for the modified post is still holding the removed user /2/.
  • Using method get_the_modified_author to retrieve the name of the user who last modified post #1 raises a warning, as described in the ticket.

My proposed patch is just a defensive measure similar to what is applied in method get_the_author_meta, line 175 (https://bit.ly/3N91DIi).

Another possible solution could be to modify file wp-includes/author-template.php, line 104
from
return apply_filters( 'the_modified_author', $last_user->display_name );
to
return apply_filters( 'the_modified_author', empty( $last_user ) ? '' : $last_user->display_name );

Last edited 2 years ago by juanlopez4691 (previous) (diff)

#5 @mukesh27
2 years ago

Thanks for the steps.

If I try to remove a user from WordPress it shows me the attached screen in which the first option removes posts/content that the user added on the site. If you select the 2nd option then it will assign a new author in the post/content which old author created.

How do you delete the user?

@mukesh27
2 years ago

#6 @juanlopez4691
2 years ago

@mukesh27 Just the same way. The problem is that attributing a new user to all posts created by the removed user do not fix the _edit_last postmeta entries for those posts. It just updates column post_author in the posts table.

Is not about who created the posts, but who modified them the last.

#7 @audrasjb
2 years ago

  • Owner set to audrasjb
  • Status changed from new to reviewing

The above PR looks good to me.
Self assigning for review.

#8 @audrasjb
2 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 53187:

Users: Avoid warning on the_modified_author when get_userdata() returns false.

This change adds a check of the value returned by get_userdata() before using it in get_the_modified_author(). It avoids a warning when retrieving the last author who edited the current post.

Props juanlopez4691, mukesh27.
Fixes #55420.

#9 @audrasjb
2 years ago

In 53188:

Docs: Fix a docblock typo in get_the_modified_author.

Follow-up to [53187].

See #55420.

Note: See TracTickets for help on using tickets.