WordPress.org

Make WordPress Core

Opened 8 days ago

Closed 3 days ago

#50775 closed defect (bug) (fixed)

Correctly back up the global $post in edit-form-blocks.php

Reported by: SergeyBiryukov Owned by: SergeyBiryukov
Milestone: 5.6 Priority: normal
Severity: normal Version:
Component: Editor Keywords: good-first-bug has-patch
Focuses: Cc:

Description

wp-admin/edit-form-blocks.php goes through some effort to back up and restore the global $post:

/*
 * Ensure the global $post remains the same after API data is preloaded.
 * Because API preloading can call the_content and other filters, plugins
 * can unexpectedly modify $post.
 */
$backup_global_post = $post;

$preload_data = array_reduce(
        $preload_paths,
        'rest_preload_api_request',
        array()
);

// Restore the global $post as it was before API preloading.
$post = $backup_global_post;

This doesn't seem to work as expected, as $backup_global_post = $post; is not creating a copy of the object, it's a reference to the same object. If $post is modified, $backup_global_post is modified too.

To create a proper copy, $backup_global_post = clone $post; should be used, see Object Cloning.

Attachments (1)

50775.diff.txt (530 bytes) - added by scarolan 3 days ago.

Download all attachments as: .zip

Change History (5)

This ticket was mentioned in PR #446 on WordPress/wordpress-develop by sylvestercarolan.


3 days ago

  • Keywords has-patch added; needs-patch removed

Minor change to clone $post into $backupglobal post.

Trac ticket: https://core.trac.wordpress.org/ticket/50775

@scarolan
3 days ago

#2 follow-up: @scarolan
3 days ago

First patch submission - not sure if I have the process right... any feedback much appreciated!

#3 in reply to: ↑ 2 @SergeyBiryukov
3 days ago

Replying to scarolan:

First patch submission - not sure if I have the process right... any feedback much appreciated!

Thanks for the patch!

Just noting that https://github.com/WordPress/wordpress-develop/pull/446 appears to include some unrelated changes and should have probably been rebased.

50775.diff.txt looks good and is exactly what's needed here. One minor thing is that it would be better in the future to upload patches with the .diff extension instead of .txt, so that syntax highlighting works correctly.

#4 @SergeyBiryukov
3 days ago

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

In 48700:

Editor: Correctly back up the global $post in wp-admin/edit-form-blocks.php.

To create a copy of the object, instead of a reference to the same object, the clone keyword should be used.

Props scarolan.
Fixes #50775.

Note: See TracTickets for help on using tickets.