[wp-trac] [WordPress Trac] #52450: Autosave prompts unsaved changed for custom post types

WordPress Trac noreply at wordpress.org
Thu Feb 4 17:18:49 UTC 2021


#52450: Autosave prompts unsaved changed for custom post types
--------------------------+-----------------------------
 Reporter:  dbtedg        |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Autosave      |    Version:  5.6.1
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 Our sites uses a number of custom post types configured to exclude the
 'editor' support on set up. Example:

 {{{#!php
 <?php
 register_post_type(
     'custom_post_name',
     array(
         ...
         'supports' => array( 'title', 'page-attributes', 'comments',
 'excerpt', 'thumbnail' ),
     )
 );
 }}}


 Since updating to 5.6.1, we have noticed that the WP Admin prompts for
 unsaved changes when exiting any editor page (although no changes have
 been made to the post content).

 This can be confirmed by running `wp.autosave.server.postChanged()` from
 the browser console which returns `true` for any custom post type
 configured without editor support with no content changed.

 This appears to be caused by `setInitialCompare()` (wp-
 includes/js/autosave.js [48]) which was added in 5.6.1.

 This function checks if element `#content` value exists and if not sets an
 empty string:


 {{{
 initialCompareData = {
     post_title: $( '#title' ).val() || '',
     content: $( '#content' ).val() || '',
     excerpt: $( '#excerpt' ).val() || ''
 };
 }}}


 During `postChanged()` (wp-includes/js/autosave.js [704]) there logic
 check for the difference between the current editor value and the
 initialComparedData():

 {{{
 if ( $( '#' + field ).val() !== initialCompareData[ field ] ) {
 }}}

 This compares `$('#content').val()` (undefined) to
 `initialCompareData['content']` (empty string), which fails as `undefined
 !== ''`


 == Expected result


 Editor should only prompt for unsaved changes when content has changed,
 regardless of the custom post type support for "editor".

 A similar logic check should be added on `$( '#' + field ).val()` to
 return an empty string, or `initialCompareData[ field ]` should be set as
 `undefined`

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


More information about the wp-trac mailing list