Skip to content
Snippets Groups Projects
Commit 066d8f79 authored by Erick Hitter's avatar Erick Hitter
Browse files

Handle Ajax-based requests to purge a given post's revisions. Fully...

Handle Ajax-based requests to purge a given post's revisions. Fully functional, though some UI work is needed.

See #1.
parent cbd0da5d
No related branches found
No related tags found
No related merge requests found
( function( $ ) { ( function( $ ) {
$( document ).ready( function() { $( document ).ready( function() {
var purge_button = $( '#wp_revisions_control .button.purge' ), var purge_button = $( '#' + wp_revisions_control.namespace + ' .button.purge' ),
post_id = null, post_id = null,
nonce = null, nonce = null,
button_text = null; button_text = null;
button_text = $( purge_button ).text();
$( purge_button ).on( 'click', click_handler_purge ); $( purge_button ).on( 'click', click_handler_purge );
/** /**
...@@ -13,7 +15,6 @@ ...@@ -13,7 +15,6 @@
function click_handler_purge() { function click_handler_purge() {
post_id = parseInt( $( this ).data( 'postid' ) ); post_id = parseInt( $( this ).data( 'postid' ) );
button_text = $( purge_button ).text();
$( purge_button ).text( wp_revisions_control.processing_text ); $( purge_button ).text( wp_revisions_control.processing_text );
var confirmed = confirm( wp_revisions_control.ays ); var confirmed = confirm( wp_revisions_control.ays );
...@@ -23,11 +24,12 @@ ...@@ -23,11 +24,12 @@
url: ajaxurl, url: ajaxurl,
cache: false, cache: false,
data: { data: {
action: wp_revisions_control.processing_text + '_purge', action: wp_revisions_control.action_base + '_purge',
post_id: post_id, post_id: post_id,
nonce: $( this ).data( 'nonce' ) nonce: $( this ).data( 'nonce' )
}, },
type: 'post', type: 'post',
dataType: 'json',
success: ajax_request_success, success: ajax_request_success,
error: ajax_request_error error: ajax_request_error
}); });
...@@ -39,9 +41,19 @@ ...@@ -39,9 +41,19 @@
/** /**
* *
*/ */
function ajax_request_success() { function ajax_request_success( response ) {
console.log( 'Yippee' ); console.log( 'Yippee' );
$( purge_button ).text( button_text );
var list_table = $( 'ul.post-revisions > li' );
$( list_table ).each( function() {
var autosave = $( this ).text().match( wp_revisions_control.autosave );
if ( ! autosave )
$( this ).slideUp( 'slow' ).remove();
} );
$( purge_button ).fadeOut( 'slow' ).after( wp_revisions_control.nothing_text );
} }
/** /**
......
...@@ -4,7 +4,7 @@ Plugin Name: WP Revisions Control ...@@ -4,7 +4,7 @@ Plugin Name: WP Revisions Control
Plugin URI: http://www.ethitter.com/plugins/wp-revisions-control/ Plugin URI: http://www.ethitter.com/plugins/wp-revisions-control/
Description: Control how many revisions are stored for each post type Description: Control how many revisions are stored for each post type
Author: Erick Hitter Author: Erick Hitter
Version: 1.0 Version: 1.1
Author URI: http://www.ethitter.com/ Author URI: http://www.ethitter.com/
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
...@@ -86,7 +86,7 @@ class WP_Revisions_Control { ...@@ -86,7 +86,7 @@ class WP_Revisions_Control {
} }
/** /**
* Register plugin's settings fields and meta box * Register plugin's admin-specific elements
* *
* Plugin title is intentionally not translatable. * Plugin title is intentionally not translatable.
* *
...@@ -110,6 +110,7 @@ class WP_Revisions_Control { ...@@ -110,6 +110,7 @@ class WP_Revisions_Control {
// Post-level functionality // Post-level functionality
add_action( 'add_meta_boxes', array( $this, 'action_add_meta_boxes' ), 10, 2 ); add_action( 'add_meta_boxes', array( $this, 'action_add_meta_boxes' ), 10, 2 );
add_action( 'wp_ajax_' . $this->settings_section . '_purge', array( $this, 'ajax_purge' ) );
} }
/** /**
...@@ -238,15 +239,18 @@ class WP_Revisions_Control { ...@@ -238,15 +239,18 @@ class WP_Revisions_Control {
$handle = 'wp-revisions-control-post'; $handle = 'wp-revisions-control-post';
wp_enqueue_script( $handle, plugins_url( 'js/post.js', __FILE__ ), array( 'jquery' ), '20130706', true ); wp_enqueue_script( $handle, plugins_url( 'js/post.js', __FILE__ ), array( 'jquery' ), '20130706', true );
wp_localize_script( $handle, $this->settings_section, array( wp_localize_script( $handle, $this->settings_section, array(
'namespace' => $this->settings_section,
'action_base' => $this->settings_section, 'action_base' => $this->settings_section,
'processing_text' => __( 'Processing…', 'wp_revisions_control' ), 'processing_text' => __( 'Processing…', 'wp_revisions_control' ),
'ays' => __( 'Are you sure?', 'wp_revisions_control' ) 'ays' => __( 'Are you sure?', 'wp_revisions_control' ),
'autosave' => __( 'Autosave' ),
'nothing_text' => __( wpautop( 'There are no revisions to remove.' ), 'wp_revisions_control' )
) ); ) );
} }
} }
/** /**
* * Render Revisions metabox with plugin's additions
*/ */
public function revisions_meta_box( $post ) { public function revisions_meta_box( $post ) {
post_revisions_meta_box( $post ); post_revisions_meta_box( $post );
...@@ -255,11 +259,54 @@ class WP_Revisions_Control { ...@@ -255,11 +259,54 @@ class WP_Revisions_Control {
<div id="<?php echo esc_attr( $this->settings_section ); ?>"> <div id="<?php echo esc_attr( $this->settings_section ); ?>">
<h4>WP Revisions Control</h4> <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> <span class="button purge" data-postid="<?php the_ID(); ?>" data-nonce="<?php echo esc_attr( wp_create_nonce( $this->settings_section . '_purge' ) ); ?>"><?php _e( 'Purge these revisions', 'wp_revisions_control' ); ?></span>
</div><!-- #<?php echo esc_attr( $this->settings_section ); ?> --> </div><!-- #<?php echo esc_attr( $this->settings_section ); ?> -->
<?php <?php
} }
/**
* Process a post-specific request to purge revisions
*
* @uses __
* @uses check_ajax_referer
* @uses current_user_can
* @uses wp_get_post_revisions
* @uses number_format_i18n
* @return string
*/
public function ajax_purge() {
$post_id = isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : false;
// Hold the current state of this Ajax request
$response = array();
// Check for necessary data and capabilities
if ( ! $post_id )
$response['error'] = __( 'No post ID was provided. Please refresh the page and try again.', 'wp_revisions_control' );
elseif ( ! check_ajax_referer( $this->settings_section . '_purge', 'nonce', false ) )
$response['error'] = __( 'Invalid request. Please refresh the page and try again.', 'wp_revisions_control' );
elseif ( ! current_user_can( 'edit_post', $post_id ) )
$response['error'] = __( 'You are not allowed to edit this post.', 'wp_revisions_control' );
// Request is valid if $response is still empty, as no errors arose above
if ( empty( $response ) ) {
$revisions = wp_get_post_revisions( $post_id );
$count = count( $revisions );
foreach ( $revisions as $revision ) {
wp_delete_post_revision( $revision->ID );
}
$response['success'] = sprintf( __( 'Removed %s revisions associated with this post.', 'wp_revisions_control' ), number_format_i18n( $count, 0 ) );
$response['count'] = $count;
}
// Pass the response back to JS
echo json_encode( $response );
exit;
}
/** /**
** PLUGIN UTILITIES ** PLUGIN UTILITIES
**/ **/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment