-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Show autosave message when autosave exists #4218
Changes from 28 commits
cc1a856
c6d5cce
3baae76
3444476
ebe5dd4
045fd0f
c97b109
fb0988e
68f0754
4f2832c
9194bb7
5f816d1
95cb823
80768f1
f625239
3e5e744
9e97fed
cf9dd40
f1991ad
fb512ce
171c511
bdab38b
6323b25
fcd3d61
5ceca78
216dd49
52c75a5
2054d34
0c0f8d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -932,6 +932,35 @@ function gutenberg_capture_code_editor_settings( $settings ) { | |
return false; | ||
} | ||
|
||
/** | ||
* Retrieve a stored autosave that is newer than the post save. | ||
* | ||
* Deletes autosaves that are older than the post save. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not exactly true. Deletes one identical autosave only when it is newer than the post. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will adjust docblock |
||
* | ||
* @param WP_Post $post Post object. | ||
* @return WP_Post|boolean The post autosave. False if none found. | ||
*/ | ||
function get_autosave_newer_than_post_save( $post ) { | ||
// Add autosave data if it is newer and changed. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be done through the API. Why do we need the two "get" endpoints for autosaves? They are really pointless if we can't get the latest autosave revision. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, we could do this with the API instead, good suggestion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (once the API includes the ability to retrieve autosaves 😄 ) |
||
$autosave = wp_get_post_autosave( $post->ID ); | ||
|
||
if ( ! $autosave ) { | ||
return false; | ||
} | ||
|
||
// Check if the autosave is newer than the current post. | ||
if ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic is slightly more complex than I'm comfortable inlining in the script enqueue function. Could be externalized, ideally with some more comments clarifying what's being done in looping over revision fields. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I can change this. Please note this code comes verbatim from core - There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. extracted this |
||
mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if this logic is copied from elsewhere, but would it be enough to do a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is existing logic from core and I'd prefer to leave unchanged to avoid the risk on unintended side effects. |
||
) { | ||
return $autosave; | ||
} | ||
|
||
// If the autosave isn't newer, remove it. | ||
wp_delete_post_revision( $autosave->ID ); | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Scripts & Styles. | ||
* | ||
|
@@ -1104,6 +1133,13 @@ function gutenberg_editor_scripts_and_styles( $hook ) { | |
'autosaveInterval' => 10, | ||
); | ||
|
||
$post_autosave = 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 ) ), | ||
); | ||
} | ||
|
||
if ( ! empty( $color_palette ) ) { | ||
$editor_settings['colors'] = $color_palette; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: Newline before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed