Commit 96edb7eb authored by KR Moorhouse's avatar KR Moorhouse

Added cleanup modal to our dashboard, which will request a batch process to...

Added cleanup modal to our dashboard, which will request a batch process to remove orphaned submissions.
parent 362a2540
This diff is collapsed.
This diff is collapsed.
......@@ -81,17 +81,6 @@ define( [ 'views/sections/widgets.js', 'views/sections/apps.js', 'views/sections
var childView = new WidgetView();
}
this.showChildView('content', childView );
// If form telemetry is defined...
// AND if we should run it...
if ( 'undefined' !== typeof nfAdmin.formTelemetry && 1 == nfAdmin.formTelemetry ) {
// Make our AJAX call.
var data = {
action: 'nf_form_telemetry',
security: nfAdmin.ajaxNonce
}
// Make our AJAX call.
jQuery.post( ajaxurl, data );
}
// If the user has not seen the opt-in modal yet...
if ( '1' == nfAdmin.showOptin ) {
// Declare all of our opt-in code here.
......@@ -222,6 +211,77 @@ define( [ 'views/sections/widgets.js', 'views/sections/apps.js', 'views/sections
optinModal.close();
} );
} );
} // If we've been told to run cleanup...
else if ( '1' == nfAdmin.doingCleanup ) {
var cleanupModal = new jBox( 'Modal', {
closeOnEsc: false,
closeOnClick: false,
width: 400
} );
var that = this;
// Define the modal content.
var content = document.createElement( 'div' );
content.classList.add( 'message' );
content.style.padding = '0px 20px 20px 20px';
content.innerHTML = nfi18n.cleanupContent;
var bar = document.createElement( 'div' );
bar.id = 'nf-progress-bar';
bar.classList.add( 'nf-progress-bar' );
bar.style.display = 'none';
var progress = document.createElement( 'div' );
progress.classList.add( 'nf-progress-bar-slider' );
bar.appendChild( progress );
content.appendChild( bar );
var loading = document.createElement( 'p' );
loading.id = 'nf-loading-text';
loading.style.color = '#1ea9ea';
loading.style.fontWeight = 'bold';
loading.innerHTML = nfi18n.cleanupLoading;
loading.style.display = 'none';
content.appendChild( loading );
var actions = document.createElement( 'div' );
actions.id = 'nf-action-buttons';
actions.classList.add( 'buttons' );
var cancel = document.createElement( 'div' );
cancel.id = 'nf-cancel';
cancel.classList.add( 'nf-button', 'secondary' );
cancel.innerHTML = nfi18n.cleanupSecondary;
actions.appendChild( cancel );
var confirm = document.createElement( 'button' );
confirm.id = 'nf-confirm';
confirm.classList.add( 'nf-button', 'primary', 'pull-right' );
confirm.innerHTML = nfi18n.cleanupPrimary;
actions.appendChild( confirm );
content.appendChild( actions );
// Set the options for the modal and open it.
cleanupModal.setContent( document.createElement( 'div' ).appendChild( content ).innerHTML );
cleanupModal.open();
// Setup the cancel click event.
jQuery( '#nf-cancel' ).click( function( e ) {
cleanupModal.close();
} );
// Setup the confirm click event.
jQuery( '#nf-confirm' ).click( function( e ) {
jQuery( window ).bind( 'beforeunload', function() {
return 'Leaving could cause damage to your data.';
} );
jQuery( '#nf-cancel' ).hide();
jQuery( '#nf-confirm' ).hide();
jQuery( '#nf-progress-bar' ).show();
jQuery( '#nf-loading-text' ).show();
that.cleanupProcess( that, -1, cleanupModal );
} );
}
// If form telemetry is defined...
// AND if we should run it...
if ( 'undefined' !== typeof nfAdmin.formTelemetry && 1 == nfAdmin.formTelemetry ) {
// Make our AJAX call.
var data = {
action: 'nf_form_telemetry',
security: nfAdmin.ajaxNonce
}
// Make our AJAX call.
jQuery.post( ajaxurl, data );
}
},
......@@ -243,6 +303,51 @@ define( [ 'views/sections/widgets.js', 'views/sections/apps.js', 'views/sections
return content.innerHTML;
},
}
},
cleanupProcess: function( context, steps, modal ) {
var data = {
action: 'nf_batch_process',
batch_type: 'data_cleanup',
security: nfAdmin.ajaxNonce
};
jQuery.post( ajaxurl, data, function( response ) {
response = JSON.parse( response );
// If we're done...
if ( response.batch_complete ) {
jQuery( '.nf-progress-bar-slider' ).css( 'width', '100%' );
jQuery( window ).unbind( 'beforeunload' );
modal.close();
// Exit.
return false;
}
// If we do not yet have a determined number of steps...
if ( -1 == steps ) {
// If step_toal is defined...
if ( 'undefined' != typeof response.step_total ) {
// Use the step_total.
steps = response.step_total;
} // Otherwise... (step_total is not defined)
else {
// Use step_remaining.
steps = response.step_remaining;
}
}
// Calculate our current step.
var step = steps - response.step_remaining;
// Calculate our maximum progress for this step.
var maxProgress = Math.round( step / steps * 100 );
// Get our current progress for this step.
var currentProgress = Math.round( jQuery( '.nf-progress-bar-slider' ).width() / jQuery( '.nf-progress-bar-slider' ).parent().width() * 100 );
// If our maximum progress is more than our current progress...
if ( maxProgress > currentProgress ) {
// Increment our progress bar.
currentProgress = Number( currentProgress ) + 1;
jQuery( '.nf-progress-bar-slider' ).css( 'width', currentProgress + '%' );
}
// Recall our function...
context.cleanupProcess( context, steps, modal );
} );
}
} );
return view;
......
......@@ -134,7 +134,10 @@ final class NF_Admin_Menus_Forms extends NF_Abstracts_Menu
'formTelemetry' => ( get_option( 'nf_form_tel_sent' ) ) ? 0 : 1,
'showOptin' => ( get_option( 'ninja_forms_do_not_allow_tracking' ) ||
get_option( 'ninja_forms_allow_tracking' ) ) ? 0 : 1,
'currentUserEmail' => $current_user->user_email
'currentUserEmail' => $current_user->user_email,
'doingCleanup' => ( ! get_option( 'ninja_forms_data_is_clean' ) &&
isset( $_REQUEST[ 'action' ] ) &&
'cleanup' == $_REQUEST[ 'action' ] ) ? 1 : 0,
) );
wp_enqueue_style( 'nf-builder', Ninja_Forms::$url . 'assets/css/builder.css' );
......
......@@ -3,13 +3,12 @@
/**
* Class NF_Abstracts_Batch_Process
*/
class NF_Admin_DataCleanup extends NF_Abstracts_BatchProcess
class NF_Admin_Processes_DataCleanup extends NF_Abstracts_BatchProcess
{
private $response = array(
'batch_complete' => false
);
protected $delete = array();
global $wpdb;
/**
* Constructor
......@@ -29,6 +28,7 @@ class NF_Admin_DataCleanup extends NF_Abstracts_BatchProcess
*/
public function process()
{
global $wpdb;
// If we've not already started the cleanup process...
if ( ! get_option( 'nf_doing_data_cleanup' ) ) {
// Run the startup process.
......@@ -44,7 +44,7 @@ class NF_Admin_DataCleanup extends NF_Abstracts_BatchProcess
// Fetch the last item on it.
$id = array_pop( $this->delete );
// Get a list of post IDs to delete.
$sql = "SELECT DISTINCT(`id`) FROM `" . $wpdb->prefix . "posts` WHERE `id` IN( SELECT DISTINCT(`post_id`) FROM `" . $wpdb->prefix . "postmeta` WHERE `meta_key` = '_form_id' AND `meta_value` = '0' ) AND `post_type` = 'nf_sub' LIMIT 500";
$sql = "SELECT DISTINCT(`id`) FROM `" . $wpdb->prefix . "posts` WHERE `id` IN( SELECT DISTINCT(`post_id`) FROM `" . $wpdb->prefix . "postmeta` WHERE `meta_key` = '_form_id' AND `meta_value` = '" . $id . "' ) AND `post_type` = 'nf_sub' LIMIT 500";
$result = $wpdb->get_results( $sql, 'ARRAY_A' );
// If we got 500 or more results...
if ( 500 == count( $result ) ) {
......@@ -54,15 +54,20 @@ class NF_Admin_DataCleanup extends NF_Abstracts_BatchProcess
// Convert our results to something we can use in a query.
array_walk( $result, array( $this, 'smush_results' ) );
$sub_sql = implode( ', ', $result );
// Delete postmeta data.
$sql = "DELETE FROM `" . $wpdb->prefix . "postmeta` WHERE `post_id` IN(" . $sub_sql . ")";
$wpdb->query( $sql );
// Delete post data.
$sql = "DELETE FROM `" . $wpdb->prefix . "posts` WHERE `id` IN(" . $sub_sql . ")";
$wpdb->query( $sql );
// If we have something to query...
if ( '' != $sub_sql ) {
// Delete postmeta data.
$sql = "DELETE FROM `" . $wpdb->prefix . "postmeta` WHERE `post_id` IN(" . $sub_sql . ")";
$wpdb->query( $sql );
// Delete post data.
$sql = "DELETE FROM `" . $wpdb->prefix . "posts` WHERE `id` IN(" . $sub_sql . ")";
$wpdb->query( $sql );
}
}
// If our array isn't empty...
if ( ! empty( $this->delete ) ) {
// Determine how many steps we have left.
$this->response[ 'step_remaining' ] = count( $this->delete );
update_option( 'nf_data_cleanup_ids', implode( ',', $this->delete ) );
echo wp_json_encode( $this->response );
wp_die();
......@@ -79,6 +84,7 @@ class NF_Admin_DataCleanup extends NF_Abstracts_BatchProcess
*/
public function startup()
{
global $wpdb;
// Get a list of IDs from the forms table.
$sql = "SELECT DISTINCT(`id`) FROM `" . $wpdb->prefix . "nf3_forms`";
$forms = $wpdb->get_results( $sql, 'ARRAY_A' );
......@@ -93,8 +99,10 @@ class NF_Admin_DataCleanup extends NF_Abstracts_BatchProcess
$this->delete[] = $form[ 'id' ];
}
}
// Get our number of steps for the progress bar.
$this->response[ 'step_total' ] = count( $this->delete );
// Flag startup done.
add_option( 'nf_doing_data_clenaup', 'true' );
add_option( 'nf_doing_data_cleanup', 'true' );
}
......@@ -103,19 +111,21 @@ class NF_Admin_DataCleanup extends NF_Abstracts_BatchProcess
*/
public function cleanup()
{
global $wpdb;
// Delete our options.
delete_option( 'nf_data_cleanup_ids' );
delete_option( 'nf_doing_data_cleanup' );
// Add our "finished" option.
add_option( 'ninja_forms_data_is_clean', 'true' );
// Tell our JS that we're done.
$this->response[ 'step_remaining' ] = 0;
$this->response[ 'batch_complete' ] = true;
}
/**
* Function to compress data array and eliminate unnecessary keys.
*/
public function smush_results( $value, $key ) {
public function smush_results( &$value, $key ) {
$value = $value[ 'id' ];
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment