Make WordPress Core

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#50775 closed defect (bug) (fixed)

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

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

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 6 years ago.

Download all attachments as: .zip

Change History (6)

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


6 years ago
#1

  • 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
6 years ago

#2 follow-up: @scarolan
6 years ago

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

#3 in reply to: ↑ 2 @SergeyBiryukov
6 years 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
6 years ago

  • Owner set to SergeyBiryukov
  • Resolution → fixed
  • Status new → 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.