Skip to content

Commit

Permalink
Move handling of backup post to be implicit so that api requests can …
Browse files Browse the repository at this point in the history
…be more safely used at call site
  • Loading branch information
talldan committed Nov 12, 2018
1 parent 5d0d024 commit 8246aaa
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,19 @@ function gutenberg_api_request( $path ) {
$request->set_query_params( $query_params );
}


// Ensure the global $post remains the same after the API request is made.
// Because API requests can call the_content and other filters, callbacks
// can unexpectedly modify $post resulting in issues
// like https://github.com/WordPress/gutenberg/issues/7468.
global $post;
$backup_global_post = $post;

$response = rest_do_request( $request );

// restore the global post
$post = $backup_global_post;

if ( 200 === $response->status ) {
$server = rest_get_server();
$data = (array) $response->get_data();
Expand Down Expand Up @@ -1462,22 +1474,12 @@ function gutenberg_editor_scripts_and_styles( $hook ) {
*/
$preload_paths = apply_filters( 'block_editor_preload_paths', $preload_paths, $post );

// Ensure the global $post remains the same after
// API data is preloaded. Because API preloading
// can call the_content and other filters, callbacks
// can unexpectedly modify $post resulting in issues
// like https://github.com/WordPress/gutenberg/issues/7468.
$backup_global_post = $post;

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

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

wp_add_inline_script(
'wp-api-fetch',
sprintf( 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', wp_json_encode( $preload_data ) ),
Expand Down

0 comments on commit 8246aaa

Please sign in to comment.