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 Jul 20, 2018
1 parent 8aa0bdf commit 78c507d
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 @@ -586,7 +586,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 @@ -1107,22 +1119,12 @@ function gutenberg_editor_scripts_and_styles( $hook ) {
sprintf( '/wp/v2/users/me?post_type=%s&context=edit', $post_type ),
);

// 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 78c507d

Please sign in to comment.