From cbd0da5dd34965808cb7532db36c95c8994a47ae Mon Sep 17 00:00:00 2001
From: Erick Hitter <ehitter@gmail.com>
Date: Sat, 6 Jul 2013 11:57:25 -0400
Subject: [PATCH] Start of Ajax handling to purge a post's revisions on demand.

See #1.
---
 js/post.js               | 54 ++++++++++++++++++++++++++++++++++++++++
 wp-revisions-control.php | 13 +++++++++-
 2 files changed, 66 insertions(+), 1 deletion(-)
 create mode 100644 js/post.js

diff --git a/js/post.js b/js/post.js
new file mode 100644
index 0000000..af50901
--- /dev/null
+++ b/js/post.js
@@ -0,0 +1,54 @@
+( function( $ ) {
+	$( document ).ready( function() {
+		var purge_button = $( '#wp_revisions_control .button.purge' ),
+			post_id      = null,
+			nonce        = null,
+			button_text  = null;
+
+		$( purge_button ).on( 'click', click_handler_purge );
+
+		/**
+		 *
+		 */
+		function click_handler_purge() {
+			post_id = parseInt( $( this ).data( 'postid' ) );
+
+			button_text = $( purge_button ).text();
+			$( purge_button ).text( wp_revisions_control.processing_text );
+
+			var confirmed = confirm( wp_revisions_control.ays );
+
+			if ( confirmed && post_id ) {
+				$.ajax({
+					url: ajaxurl,
+					cache: false,
+					data: {
+						action: wp_revisions_control.processing_text + '_purge',
+						post_id: post_id,
+						nonce: $( this ).data( 'nonce' )
+					},
+					type: 'post',
+					success: ajax_request_success,
+					error: ajax_request_error
+				});
+			} else {
+				$( purge_button ).text( button_text );
+			}
+		}
+
+		/**
+		 *
+		 */
+		function ajax_request_success() {
+			console.log( 'Yippee' );
+			$( purge_button ).text( button_text );
+		}
+
+		/**
+		 *
+		 */
+		function ajax_request_error() {
+			console.log( 'Sad panda' );
+		}
+	} );
+} )( jQuery );
\ No newline at end of file
diff --git a/wp-revisions-control.php b/wp-revisions-control.php
index 5e0198a..2c184c7 100644
--- a/wp-revisions-control.php
+++ b/wp-revisions-control.php
@@ -232,8 +232,17 @@ class WP_Revisions_Control {
 	public function action_add_meta_boxes( $post_type, $post ) {
 		remove_meta_box( 'revisionsdiv', null, 'normal' );
 
-		if ( post_type_supports( $post_type, 'revisions' ) && 'auto-draft' != get_post_status() && count( wp_get_post_revisions( $post ) ) > 1 )
+		if ( post_type_supports( $post_type, 'revisions' ) && 'auto-draft' != get_post_status() && count( wp_get_post_revisions( $post ) ) > 1 ) {
 			add_meta_box( 'revisionsdiv-wp-rev-ctl', __('Revisions'), array( $this, 'revisions_meta_box' ), null, 'normal', 'core' );
+
+			$handle = 'wp-revisions-control-post';
+			wp_enqueue_script( $handle, plugins_url( 'js/post.js', __FILE__ ), array( 'jquery' ), '20130706', true );
+			wp_localize_script( $handle, $this->settings_section, array(
+				'action_base'     => $this->settings_section,
+				'processing_text' => __( 'Processing&hellip;', 'wp_revisions_control' ),
+				'ays'             => __( 'Are you sure?', 'wp_revisions_control' )
+			) );
+		}
 	}
 
 	/**
@@ -245,6 +254,8 @@ class WP_Revisions_Control {
 		?>
 		<div id="<?php echo esc_attr( $this->settings_section ); ?>">
 			<h4>WP Revisions Control</h4>
+
+			<span class="button purge" data-postid="<?php the_ID(); ?>" data-nonce="<?php echo esc_attr( wp_create_nonce( $this->settings_section . '_purge' ) ); ?>_purge"><?php _e( 'Purge these revisions', 'wp_revisions_control' ); ?></span>
 		</div><!-- #<?php echo esc_attr( $this->settings_section ); ?> -->
 		<?php
 	}
-- 
GitLab