Skip to content

Commit

Permalink
Use REST api to fetch autosave over wp_get_post_autosave
Browse files Browse the repository at this point in the history
  • Loading branch information
talldan committed Nov 12, 2018
1 parent 8246aaa commit 8cb8fe3
Showing 1 changed file with 36 additions and 41 deletions.
77 changes: 36 additions & 41 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -896,46 +896,47 @@ function gutenberg_register_scripts_and_styles() {
*/
function gutenberg_api_request( $path ) {
if ( empty( $path ) ) {
return;
return null;
}

$path_parts = parse_url( $path );
if ( false === $path_parts ) {
return;
}

$request = new WP_REST_Request( 'GET', $path_parts['path'] );
if ( ! empty( $path_parts['query'] ) ) {
parse_str( $path_parts['query'], $query_params );
$request->set_query_params( $query_params );
return null;
}


// 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;

$request = new WP_REST_Request( 'GET', $path_parts['path'] );
if ( ! empty( $path_parts['query'] ) ) {
parse_str( $path_parts['query'], $query_params );
$request->set_query_params( $query_params );
}

$response = rest_do_request( $request );

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

if ( 200 === $response->status ) {
$server = rest_get_server();
$data = (array) $response->get_data();
$links = $server->get_compact_response_links( $response );
if ( ! empty( $links ) ) {
$data['_links'] = $links;
}
if ( 200 !== $response->status ) {
return null;
}

return array(
'body' => $data,
'headers' => $response->headers,
);
$server = rest_get_server();
$data = (array) $response->get_data();
$links = $server->get_compact_response_links( $response );
if ( ! empty( $links ) ) {
$data['_links'] = $links;
}

return array(
'body' => $data,
'headers' => $response->headers,
);
}

/**
Expand All @@ -948,21 +949,16 @@ function gutenberg_api_request( $path ) {
* @return array Modified reduce accumulator.
*/
function gutenberg_preload_api_request( $memo, $path ) {

// array_reduce() doesn't support passing an array in PHP 5.2
// so we need to make sure we start with one.
if ( ! is_array( $memo ) ) {
$memo = array();
}

if ( empty( $path ) ) {
return $memo;
}

$reponse = gutenberg_api_request( $path );
$api_response = gutenberg_api_request( $path );

if ( isset( $response ) ) {
$memo[ $path ] = $reponse;
if ( isset( $api_response ) ) {
$memo[ $path ] = $api_response;
}

return $memo;
Expand Down Expand Up @@ -1295,22 +1291,21 @@ function gutenberg_default_post_format_template( $settings, $post ) {
* @return WP_Post|boolean The post autosave. False if none found.
*/
function gutenberg_get_autosave_newer_than_post_save( $post ) {
// Add autosave data if it is newer and changed.
$autosave = wp_get_post_autosave( $post->ID );
$autosave_response = gutenberg_api_request( sprintf( '/wp/v2/posts/%s/autosaves?context=edit', $post->ID ) );

if ( ! $autosave ) {
if ( ! isset( $autosave_response['body'][0] ) ) {
return false;
}

$autosave = $autosave_response['body'][0];

// Check if the autosave is newer than the current post.
if (
mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false )
) {
if ( mysql2date( 'U', $autosave['modified_gmt'], false ) > mysql2date( 'U', $post->post_modified_gmt, false ) ) {
return $autosave;
}

// If the autosave isn't newer, remove it.
wp_delete_post_revision( $autosave->ID );
wp_delete_post_revision( $autosave['id'] );

return false;
}
Expand Down Expand Up @@ -1673,11 +1668,11 @@ function gutenberg_editor_scripts_and_styles( $hook ) {

$post_autosave = gutenberg_get_autosave_newer_than_post_save( $post );
if ( $post_autosave ) {
$editor_settings['autosave'] = array(
'editLink' => add_query_arg( 'gutenberg', true, get_edit_post_link( $post_autosave->ID ) ),
'title' => $post_autosave->post_title,
'content' => $post_autosave->post_content,
'excerpt' => $post_autosave->post_excerpt,
$editor_settings['autosave'] = array_merge(
array(
'editLink' => add_query_arg( 'gutenberg', true, get_edit_post_link( $post_autosave['id'] ) ),
),
$post_autosave
);
}

Expand Down

0 comments on commit 8cb8fe3

Please sign in to comment.