Skip to content

Commit

Permalink
Improve autosave
Browse files Browse the repository at this point in the history
Use WP autosave functionality. Hook to autosave process, modify data sent
  • Loading branch information
rilwis authored and funkatron82 committed Mar 8, 2013
1 parent a86ba8b commit d444712
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 58 deletions.
4 changes: 2 additions & 2 deletions demo/demo.php
Expand Up @@ -42,8 +42,8 @@
// Order of meta box: high (default), low. Optional. // Order of meta box: high (default), low. Optional.
'priority' => 'high', 'priority' => 'high',


//Auto save interval, in seconds. Optional (default is false) // Auto save: true, false (default). Optional.
// 'autosave'=> 60, 'autosave' => true,


// List of meta fields // List of meta fields
'fields' => array( 'fields' => array(
Expand Down
49 changes: 15 additions & 34 deletions inc/classes/meta-box.php
Expand Up @@ -90,9 +90,6 @@ function __construct( $meta_box )
// @see wp_update_post(), wp_insert_attachment() // @see wp_update_post(), wp_insert_attachment()
add_action( 'edit_attachment', array( $this, 'save_post' ) ); add_action( 'edit_attachment', array( $this, 'save_post' ) );
add_action( 'add_attachment', array( $this, 'save_post' ) ); add_action( 'add_attachment', array( $this, 'save_post' ) );

//Auto save ajax call. One unique ajax call for each metabox
add_action( 'wp_ajax_rwmb_save_meta_' . $this->meta_box['id'] , array( $this, 'autosave' ) );
} }


/** /**
Expand Down Expand Up @@ -132,23 +129,9 @@ function admin_enqueue_scripts()
wp_enqueue_script( 'rwmb-validate', RWMB_JS_URL . 'validate.js', array( 'jquery-validate' ), RWMB_VER, true ); wp_enqueue_script( 'rwmb-validate', RWMB_JS_URL . 'validate.js', array( 'jquery-validate' ), RWMB_VER, true );
} }


//Add auto save // Auto save
if( is_int($this->meta_box['autosave']) ) if ( $this->meta_box['autosave'] )
{ wp_enqueue_script( 'rwmb-autosave', RWMB_JS_URL . 'autosave.js', array( 'jquery' ), RWMB_VER, true );
wp_enqueue_script( 'rwmb-save', RWMB_JS_URL . 'save.js', array( 'jquery' ), RWMB_VER, true );
}
}

/**
* Auto save ajax callback
*
* @return void
*/
function autosave()
{
if(isset( $_POST['post_ID'] ) )
$this->save_post( $_POST['post_ID'] );
exit;
} }


/************************************************** /**************************************************
Expand Down Expand Up @@ -185,12 +168,10 @@ public function show()
global $post; global $post;


$saved = self::has_been_saved( $post->ID, $this->fields ); $saved = self::has_been_saved( $post->ID, $this->fields );
//Container
echo sprintf( // Container
'<div class="rwmb-meta-box" data-meta_box_id="%s" data-autosave="%s">', echo '<div class="rwmb-meta-box">';
$this->meta_box['id'],
$this->meta_box['autosave']
);
wp_nonce_field( "rwmb-save-{$this->meta_box['id']}", "nonce_{$this->meta_box['id']}" ); wp_nonce_field( "rwmb-save-{$this->meta_box['id']}", "nonce_{$this->meta_box['id']}" );


// Allow users to add custom code before meta box content // Allow users to add custom code before meta box content
Expand Down Expand Up @@ -330,7 +311,7 @@ public function show()
do_action( 'rwmb_after' ); do_action( 'rwmb_after' );
do_action( "rwmb_after_{$this->meta_box['id']}" ); do_action( "rwmb_after_{$this->meta_box['id']}" );


//End container // End container
echo '</div>'; echo '</div>';
} }


Expand Down Expand Up @@ -432,12 +413,13 @@ static function meta( $meta, $post_id, $saved, $field )
* Save data from meta box * Save data from meta box
* *
* @param int $post_id Post ID * @param int $post_id Post ID
* @param object $post Post object
* *
* @return void * @return void
*/ */
function save_post( $post_id, $post ) function save_post( $post_id )
{ {
$post = get_post( $post_id );

// Get proper post type // Get proper post type
$post_type = null; $post_type = null;
if ( $post ) if ( $post )
Expand All @@ -448,14 +430,13 @@ function save_post( $post_id, $post )
$post_type_object = get_post_type_object( $post_type ); $post_type_object = get_post_type_object( $post_type );


// Check whether: // Check whether:
// - the post is autosaved (including revision) // - the post is autosaved (including revision), @see wp_is_post_autosave()
// - current post type is supported // - current post type is supported
// - user has proper capability // - user has proper capability
// - in Quick edit mode, @see http://wordpress.org/support/topic/quick-edit-not-working-and-problem-located // - in Quick edit mode, @see http://wordpress.org/support/topic/quick-edit-not-working-and-problem-located
if ( if (
wp_is_post_autosave( $post ) ( $this->meta_box['autosave'] != (bool) wp_is_post_autosave( $post ) )
|| ( ! in_array( $post_type, $this->meta_box['pages'] ) ) || current_user_can( $post_type_object->cap->edit_post )
|| ( ! current_user_can( $post_type_object->cap->edit_post, $post_id ) )
|| ( 'inline-save' == $_POST['action'] ) || ( 'inline-save' == $_POST['action'] )
) )
{ {
Expand Down Expand Up @@ -557,7 +538,7 @@ static function normalize( $meta_box )
'context' => 'normal', 'context' => 'normal',
'priority' => 'high', 'priority' => 'high',
'pages' => array( 'post' ), 'pages' => array( 'post' ),
'autosave' => false 'autosave' => false,
) ); ) );


// Set default values for fields // Set default values for fields
Expand Down
13 changes: 13 additions & 0 deletions js/autosave.js
@@ -0,0 +1,13 @@
jQuery( function( $ )
{
$( document ).ajaxSend( function( e, xhr, s )
{
if ( -1 != s.data.indexOf( 'action=autosave' ) )
{
$( '.rwmb-meta-box :input' ).each( function()
{
s.data += '&' + $( this ).serialize();
} );
}
} );
} );
22 changes: 0 additions & 22 deletions js/save.js

This file was deleted.

0 comments on commit d444712

Please sign in to comment.