close

Make WordPress Core

Opened 5 months ago

Closed 5 months ago

Last modified 5 months ago

#64104 closed defect (bug) (fixed)

author-template.php generates PHP Warning with PHP 8.3

Reported by: cornwell's profile Cornwell Owned by: westonruter's profile westonruter
Milestone: 6.9 Priority: normal
Severity: normal Version: 2.8
Component: Posts, Post Types Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

I am seeing the following warning message in my php error log:

PHP Warning: Attempt to read property “ID” on null in /home/[userid]/public_html/wp-includes/author-template.php on line 93

This in WordPress release 6.8.3. Line 93 is
$last_id = get_post_meta( get_post()->ID, '_edit_last', true );

It occurs in this function:

/**
 * Retrieves the author who last edited the current post.
 *
 * @since 2.8.0
 *
 * @return string|void The author's display name, empty string if unknown.
 */
function get_the_modified_author() {
	$last_id = get_post_meta( get_post()->ID, '_edit_last', true );

	if ( $last_id ) {
		$last_user = get_userdata( $last_id );

		/**
		 * Filters the display name of the author who last edited the current post.
		 *
		 * @since 2.8.0
		 *
		 * @param string $display_name The author's display name, empty string if unknown.
		 */
		return apply_filters( 'the_modified_author', $last_user ? $last_user->display_name : '' );
	}
}

I think this code relies on line 93 returning a null value if the ID has not been set (as would probably be the case if the function were called outside the loop). However, php at current releases (mine is at 8.3) generates a warning message and this is filling my logs.

My solution has been to replace line 93 with

if ( isset(get_post()->ID) ) {
	$last_id = get_post_meta( get_post()->ID, '_edit_last', true );
} else {
	$last_id = '';
}

Since implementing this change no errors have been flagged.

I appreciate the root cause may be that the function is called outside the loop, but this function should handle this. My suggested code achieves this.

Attachments (2)

64104.patch (541 bytes) - added by jdahir0789 5 months ago.
fix-64104.patch (845 bytes) - added by dhruvang21 5 months ago.

Download all attachments as: .zip

Change History (15)

Image @jdahir0789
5 months ago

#2 Image @Cornwell
5 months ago

I'm still new to this process. Do I need to do anything further to get this change into the WordPress core, or will others now take it forward?

Image

This ticket was mentioned in Slack in #core by amykamala. View the logs.


5 months ago

#4 Image @westonruter
5 months ago

  • Milestone changed from Awaiting Review to 6.9
  • Owner set to westonruter
  • Status changed from new to reviewing

@Cornwell Thanks for reporting! I'll review the issue and the patches to fix in 6.9.

#5 Image @Cornwell
5 months ago

@westonruter That's great! Thanks for taking this forward.

#6 Image @westonruter
5 months ago

@jdahir0789 Your patch 64104.patch fixes the issue.

@dhruvang21 I like the idea of your patch fix-64104.patch in that it adds the $post_id parameter similar to get_the_date(), get_the_modified_time(), get_post_modified_time(), get_the_modified_author() function API much more similar to other similar functions, like get_the_time(), get_post_time(), get_post_datetime(), and get_post_timestamp(). However, your patch doesn't actually fix the issue. In this line here:

<?php
$post_id = $post_id ? $post_id : get_post()->ID;

If there is no global $post then get_post() will return null and the same error will occur. However, this can be tweaked

<?php
$post = get_post( $post );
if ( ! $post ) {
        return;
}
$post_id = $post->ID;

I'll make that change.

Image

This ticket was mentioned in PR #10400 on WordPress/wordpress-develop by @westonruter.


5 months ago
#7

  • Keywords has-patch has-unit-tests added

#8 Image @westonruter
5 months ago

  • Version changed from 6.8.3 to 2.8

This issue was introduced in [10661] for #9154.

Image

@westonruter commented on PR #10400:


5 months ago
#9

It might be worth noting this only applies to the classic editor, since updating a post in the block editor doesn't set the _edit_last value. Maybe that's clear/expected since it's a PHP template function?

@mindctrl woah, what? Is that tracked anywhere as a bug? That seems like an oversight.

Image

@westonruter commented on PR #10400:


5 months ago
#11

Core bug: https://core.trac.wordpress.org/ticket/50255

Still not fixed after 5 years.

Let's fix it!

#12 Image @westonruter
5 months ago

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

In 61057:

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.

#13 Image @westonruter
5 months ago

  • Component changed from General to Posts, Post Types
Note: See TracTickets for help on using tickets.