WordPress.org

Make WordPress Core

Opened 6 years ago

Last modified 5 months ago

#24865 new enhancement

Allow plugins to hook into autosave and include field data from the autosave with the package.

Reported by: nerrad Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Autosave Keywords: needs-refresh
Focuses: Cc:
PR Number:

Description

Why?

Autosaves currently only save "blessed" data (post_title and post_description?). Plugin authors may want to add their own data in the autosave package so if they hook into save_post they can handle/save that data as needed.

How this works

Metaboxes are generated with an id already on the page. So what we do is add an extra parameter to the add_meta_box() function (do_autosave bool) so that when a plugin uses add_meta_box() they can set that flag.

We set a javascript object that contains the ids of the metaboxes that have had that flag set and then wp.autosave.getPostData in autosave.js has been modified to loop through the metaboxes matching those ids and return the fields in those metaboxes as an object attached to the data param.

Added a new jquery plugin (serializefullarray) as it will handle nested pseudo arrays set as the "name" values in inputs (i.e. somedata[one][two]) and create proper objects from them.

Attachments (3)

TestMetaBoxAutosave.zip (1.2 KB) - added by nerrad 6 years ago.
For testing plugins adding their data to autosaves
jquery.serializefullarray.js (1.5 KB) - added by nerrad 6 years ago.
serializefullarray library
24865-full.diff (4.3 KB) - added by nerrad 6 years ago.
add in functionality for plugin metaboxes to add their data to the autosave package

Download all attachments as: .zip

Change History (17)

#1 @nerrad
6 years ago

at #wcsf contributor day south-side 4th table.

@nerrad
6 years ago

For testing plugins adding their data to autosaves

#2 @nerrad
6 years ago

oh and this is based off of trunk

#3 @brianhogg
6 years ago

Looks pretty clean except for the console.log(), would be nice to get this included!

#4 @nerrad
6 years ago

dang of course I missed that console.log I noticed something in one of the comment docs too. Gonna put up a fix for both in a couple minutes

#5 @nerrad
6 years ago

patch updated to remove console.log() and fix comment doc

@nerrad
6 years ago

serializefullarray library

#6 @nerrad
6 years ago

didn't realize the patch didn't include the jquery serializefullarray library. As far as I can tell there are no restricting licenses on that library. It's very handy for parsing form data that is referenced via nested associated array into js objects.

#7 @DrewAPicture
6 years ago

  • Component changed from Administration to Autosave
  • Keywords dev-feedback added

#8 @azaozz
6 years ago

Good idea. Before that autosave would need a more general "overhaul". It should use heartbeat for transport, parts of it haven't been used in ages, other parts need updating. For example: saving tags, may need to JSON encode the form data, differences when autosaving a draft and a published post, etc.

#9 @nerrad
6 years ago

Would an overhaul be needed before this gets added though? Imo this could be added and plugins can interact now. The impact of future changes would be minimal because the object containing the metabox ids could just be carried forward...

Last edited 6 years ago by nerrad (previous) (diff)

@nerrad
6 years ago

add in functionality for plugin metaboxes to add their data to the autosave package

#10 @nerrad
6 years ago

just modified the patch cause I realized I got a couple var names wrong.

#11 @kenneyw
5 years ago

It appears this is still not incorporated in WP 3.9.1.

If the above solution is not going to make it into WP, at least refactor autosave.js
to use 'this.getPostData' rather than 'getPostData' etc. What's the point of exporting
it in the window.wp.autosave structure if changing it has no effect?

What I want to do a plugin script is something like:

    var as_getPostData = window.wp.autosave.getPostData;
    window.wp.autosave.getPostData = function() {
        var ret = as_getPostData.apply( this/*?*/, arguments );
        // append my metabox input fields
        return ret;
    };

As an alternative, this works fine:

    $(document).on('before-autosave', function(event,data) {
        var x = {};
        $(".postbox input, .postbox select").each( function(i,el) {
                el = $(el);
                x[ el.attr('name') ] = el.val();
        });
        $.extend( data, x );
    } );

..except that 'getCompareString' cannot be overriden to include changes in meta values.

Last edited 5 years ago by kenneyw (previous) (diff)

#12 @iseulde
4 years ago

  • Keywords needs-refresh added; has-patch removed
  • Milestone changed from Awaiting Review to Future Release

Related: #22601.

#13 @iseulde
4 years ago

  • Keywords dev-feedback removed

This ticket was mentioned in Slack in #core-editor by kadamwhite. View the logs.


4 years ago

Note: See TracTickets for help on using tickets.