WordPress.org

Make WordPress Core

Opened 4 weeks ago

Closed 4 weeks ago

#52995 closed defect (bug) (fixed)

Undefined `$post_ID` variable in `edit-form-blocks.php`

Reported by: ravipatel Owned by: SergeyBiryukov
Milestone: 5.8 Priority: normal
Severity: normal Version: 5.0
Component: Editor Keywords: has-patch
Focuses: Cc:

Description (last modified by davidbaumwald)

wp_get_post_autosave is called with the $post_ID variabl, but it is not defined. Should use $post->ID instead.

Attachments (1)

52995-edit-form-blocks.php.patch (584 bytes) - added by ravipatel 4 weeks ago.
undefine variable

Download all attachments as: .zip

Change History (5)

@ravipatel
4 weeks ago

undefine variable

#1 @davidbaumwald
4 weeks ago

  • Component changed from General to Editor
  • Description modified (diff)
  • Focuses coding-standards removed
  • Summary changed from Coding standard : Undefined varible to Undefined `$post_ID` variable in `edit-form-blocks.php`
  • Version set to 5.0

Looks like this traces back to [43815].

#2 @mukesh27
4 weeks ago

Hi there!

I investigate more on this issue and found that $post_ID is not defined on the current file but it defines in their parent files.

edit-form-blocks.php has two parent files.

File post-new.php define $post_ID like below.

$post    = get_default_post_to_edit( $post_type, true );
$post_ID = $post->ID;

File post.php define $post_ID like below.

if ( isset( $_GET['post'] ) && isset( $_POST['post_ID'] ) && (int) $_GET['post'] !== (int) $_POST['post_ID'] ) {
	wp_die( __( 'A post ID mismatch has been detected.' ), __( 'Sorry, you are not allowed to edit this item.' ), 400 );
} elseif ( isset( $_GET['post'] ) ) {
	$post_id = (int) $_GET['post'];
} elseif ( isset( $_POST['post_ID'] ) ) {
	$post_id = (int) $_POST['post_ID'];
} else {
	$post_id = 0;
}
$post_ID = $post_id;

FYI both the variables give the same post ID value in my test.

echo $post_ID;
echo $post->ID;

#3 @SergeyBiryukov
4 weeks ago

  • Milestone changed from Awaiting Review to 5.8

Hi there, thanks for the ticket!

As noted above, this is not an issue, as the variable is defined in the parent file before edit-form-blocks.php is included. That said, I think it makes sense to switch to $post->ID here avoid any future confusion.

Note: This also applies to a similar fragment and a few other instances in edit-form-advanced.php.

#4 @SergeyBiryukov
4 weeks ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 50693:

Editor: Use a consistent way to retrieve post ID on Edit Post screens.

Props mukesh27, ravipatel, davidbaumwald.
Fixes #52995.

Note: See TracTickets for help on using tickets.