[wp-trac] [WordPress Trac] #50775: Correctly back up the global $post in edit-form-blocks.php

WordPress Trac noreply at wordpress.org
Sun Jul 26 20:32:47 UTC 2020


#50775: Correctly back up the global $post in edit-form-blocks.php
----------------------------+----------------------------------------
 Reporter:  SergeyBiryukov  |      Owner:  (none)
     Type:  defect (bug)    |     Status:  new
 Priority:  normal          |  Milestone:  5.6
Component:  Editor          |    Version:
 Severity:  normal          |   Keywords:  needs-patch good-first-bug
  Focuses:                  |
----------------------------+----------------------------------------
 `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
 [https://www.php.net/manual/en/language.oop5.references.php 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 [https://www.php.net/manual/en/language.oop5.cloning.php Object
 Cloning].

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/50775>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list