Commit 73b3391f authored by Eric Windham's avatar Eric Windham

Merge branch 'expired-submission-cleanup' into 'develop'

Expired submission cleanup

See merge request ninja-forms/ninja-forms!3618
parents 8a36db62 4c5e6047
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
This diff is collapsed.
......@@ -216,5 +216,73 @@ jQuery(document).ready(function($) {
$( '#nfTelSpinner' ).css( 'display', 'none' );
} );
} );
jQuery( '#nfTrashExpiredSubmissions' ).click( function( e ) {
var that = this;
var data = {
closeOnClick: false,
closeOnEsc: true,
content: '<p>' + nf_settings.i18n.trashExpiredSubsMessage + '<p>',
btnPrimary: {
text: nf_settings.i18n.trashExpiredSubsButtonPrimary,
callback: function( e ) {
// Hide the buttons.
deleteModal.maybeShowActions( false );
// Show the progress bar.
deleteModal.maybeShowProgress( true );
// Begin our cleanup process.
that.submissionExpirationProcess( that, -1, deleteModal );
}
},
btnSecondary: {
text: nf_settings.i18n.trashExpiredSubsButtonSecondary,
callback: function( e ) {
deleteModal.toggleModal( false );
}
},
useProgressBar: true,
};
this.submissionExpirationProcess = function( context, steps, modal ) {
var data = {
action: 'nf_batch_process',
batch_type: 'expired_submission_cleanup',
security: nf_settings.batch_nonce
};
jQuery.post( nf_settings.ajax_url, data, function( response ) {
response = JSON.parse( response );
// If we're done...
if ( response.batch_complete ) {
// Push our progress bar to 100%.
modal.setProgress( 100 );
modal.toggleModal( false );
// 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 );
// Increment the progress.
modal.incrementProgress ( maxProgress );
// Recall our function...
context.submissionExpirationProcess( context, steps, modal );
} );
}
var deleteModal = new NinjaModal( data );
});
});
......@@ -301,7 +301,7 @@ define( [], function() {
chunk: chunks[ currentChunk ],
form_id: formId
},
security: nfAdmin.ajaxNonce
security: nfAdmin.batchNonce
};
var that = this;
......
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -324,7 +324,7 @@ define( [ 'views/sections/widgets.js', 'views/sections/services.js', 'views/sect
var data = {
action: 'nf_batch_process',
batch_type: 'data_cleanup',
security: nfAdmin.ajaxNonce
security: nfAdmin.batchNonce
};
jQuery.post( ajaxurl, data, function( response ) {
response = JSON.parse( response );
......
......@@ -16,10 +16,10 @@ class NF_AJAX_REST_BatchProcess extends NF_AJAX_REST_Controller
public function post( $request_data )
{
$data = array();
// If we don't have a nonce...
// OR if the nonce is invalid...
if ( ! isset( $request_data[ 'security' ] ) || ! wp_verify_nonce( $request_data[ 'security' ] ) ) {
if ( ! isset( $request_data[ 'security' ] ) || ! wp_verify_nonce( $request_data[ 'security' ], 'ninja_forms_batch_nonce' ) ) {
// Kick the request out now.
$data[ 'error' ] = __( 'Request forbidden.', 'ninja-forms' );
}
......@@ -36,6 +36,10 @@ class NF_AJAX_REST_BatchProcess extends NF_AJAX_REST_Controller
$batch = new NF_Admin_Processes_DataCleanup(
$request_data );
break;
case 'expired_submission_cleanup':
$batch = new NF_Admin_Processes_ExpiredSubmissionCleanup(
$request_data );
break;
default:
$data[ 'error' ] = __( 'Invalid request.', 'ninja-forms' );
break;
......
......@@ -70,12 +70,7 @@ final class NF_Actions_Save extends NF_Abstracts_Action
$expiration_value = $form_id . ',' . $action_settings[ 'subs_expire_time' ];
// Check for option value...
$option = get_option( 'nf_sub_expiration', false );
// If option doesn't exist we know that we can just create the option.
if( ! $option || empty( $option ) ){
return;
}
$option = get_option( 'nf_sub_expiration', array() );
// If our expiration setting is turned on...
if( 1 == $action_settings[ 'set_subs_to_expire' ] ) {
......
......@@ -141,6 +141,7 @@ final class NF_Admin_Menus_Forms extends NF_Abstracts_Menu
wp_localize_script( 'nf-dashboard', 'nfPromotions', array_values( Ninja_Forms::config( 'DashboardPromotions' ) ) );
wp_localize_script( 'nf-dashboard', 'nfAdmin', array(
'ajaxNonce' => wp_create_nonce( 'ninja_forms_dashboard_nonce' ),
'batchNonce' => wp_create_nonce( 'ninja_forms_batch_nonce' ),
'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,
......@@ -245,6 +246,7 @@ final class NF_Admin_Menus_Forms extends NF_Abstracts_Menu
wp_localize_script( 'nf-builder', 'nfAdmin', array(
'ajaxNonce' => wp_create_nonce( 'ninja_forms_builder_nonce' ),
'batchNonce' => wp_create_nonce( 'ninja_forms_batch_nonce' ),
'requireBaseUrl' => Ninja_Forms::$url . 'assets/js/',
'previewurl' => home_url() . '/?nf_preview_form=',
'wp_locale' => $wp_locale->number_format,
......
......@@ -155,15 +155,22 @@ final class NF_Admin_Menus_Settings extends NF_Abstracts_Submenu
wp_enqueue_style( 'jBox', Ninja_Forms::$url . 'assets/css/jBox.css' );
wp_register_script( 'ninja_forms_admin_menu_settings', Ninja_Forms::$url . 'assets/js/admin-settings.js', array( 'jquery' ), FALSE, TRUE );
wp_localize_script( 'ninja_forms_admin_menu_settings', 'nf_settings', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'forms' => $form_options,
'ajax_url' => admin_url( 'admin-ajax.php' ),
'forms' => $form_options,
'nf_nuke_title' => __( 'Remove ALL Ninja Forms data and uninstall?', 'ninja-forms' ),
'nonce' => wp_create_nonce( "ninja_forms_settings_nonce" ),
'i18n' => array(
'rollbackConfirm' => __( 'Are you sure you want to rollback?', 'ninja-forms' )
'nonce' => wp_create_nonce( "ninja_forms_settings_nonce" ),
'batch_nonce' => wp_create_nonce( 'ninja_forms_batch_nonce' ),
'i18n' => array(
'rollbackConfirm' => __( 'Are you sure you want to rollback?', 'ninja-forms' ),
'trashExpiredSubsMessage' => __( 'Are you sure you want to trash all expired submissions?', 'ninja-forms' ),
'trashExpiredSubsButtonPrimary' => __( 'Trash', 'ninja-forms' ),
'trashExpiredSubsButtonSecondary' => __( 'Cancel', 'ninja-forms' ),
),
'allow_telemetry' => $allow_tel
'allow_telemetry' => $allow_tel,
));
wp_enqueue_script( 'nf-ninja-modal', Ninja_Forms::$url . 'assets/js/lib/ninjaModal.js' );
wp_enqueue_style( 'nf-builder', Ninja_Forms::$url . 'assets/css/builder.css' );
wp_enqueue_style( 'nf-font-awesome', Ninja_Forms::$url . 'assets/css/font-awesome.min.css' );
wp_enqueue_script( 'ninja_forms_admin_menu_settings' );
......
<?php if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Class NF_Abstracts_Batch_Process
*/
class NF_Admin_Processes_ExpiredSubmissionCleanup extends NF_Abstracts_BatchProcess
{
protected $expired_subs = array();
private $response = array(
'batch_complete' => false
);
/**
* Constructor
*/
public function __construct( $data = array() )
{
//Bail if we aren't in the admin.
if ( ! is_admin() )
return false;
// Run process.
$this->process();
}
/**
* Function to loop over the batch.
*/
public function process()
{
if ( ! get_option( 'nf_doing_expired_submission_cleanup' ) ) {
// Run the startup process.
$this->startup();
} // Otherwise... (We've already run startup.)
else {
// Get our remaining submissions from record.
$data = get_option( 'nf_expired_submissions' );
$this->expired_subs = $data;
}
// For the first 250 in the array.
for( $i = 0; $i < 250; $i++ ){
// if we've already finished bail..
if( empty( $this->expired_subs ) ) break;
// Pop off a sub and delete it.
$sub = array_pop( $this->expired_subs );
wp_trash_post( $sub );
}
// If our subs array isn't empty...
if( ! empty( $this->expired_subs ) ) {
// ..see how many steps we have left, update our option, and send the remaining step to the JS.
$this->response[ 'step_remaining' ] = $this->get_steps();
update_option( 'nf_expired_submissions', $this->expired_subs );
echo wp_json_encode( $this->response );
wp_die();
}
// Run our cleanup process.
$this->cleanup();
echo wp_json_encode( $this->response );
wp_die();
}
/**
* Function to run any setup steps necessary to begin processing.
*/
public function startup()
{
// Retrieves the option that contains all of our expiration data.
$expired_sub_option = get_option( 'nf_sub_expiration', array() );
// Loop over our options and ...
foreach( $expired_sub_option as $sub ) {
/*
* Separate our $option values into two positions
* $option[ 0 ] = ( int ) form_id
* $option[ 1 ] = ( int ) expiration time in days.
*/
$sub = explode( ',', $sub );
$expired_subs = $this->get_expired_subs( $sub[ 0 ], $sub[ 1 ] );
// Use the helper method to build an array of expired subs.
$this->expired_subs = array_merge( $this->expired_subs, $expired_subs );
}
// Determine how many steps this will take.
$this->response[ 'step_total' ] = $this->get_steps();
add_option( 'nf_doing_expired_submission_cleanup', 'true' );
}
/**
* Function to cleanup any lingering temporary elements of a batch process after completion.
*/
public function cleanup()
{
// Delete our options.
delete_option('nf_doing_expired_submission_cleanup' );
delete_option( 'nf_expired_submissions' );
// Tell our JS that we're done.
$this->response[ 'batch_complete' ] = true;
}
/*
* Get Steps
* Determines the amount of steps needed for the step processors.
*
* @return int of the number of steps.
*/
public function get_steps()
{
// Convent our number from int to float
$steps = count( $this->expired_subs );
$steps = floatval( $steps );
// Get the amount of steps and return.
$steps = ceil( $steps / 250.0 );
return $steps;
}
/**
* Get Expired Subs
* Gathers our expired subs puts them into an array and returns it.
*
* @param $form_id - ( int ) ID of the Form.
* @param $expiration_time - ( int ) number of days the submissions
* are set to expire in
*
* @return array of all the expired subs that were found.
*/
public function get_expired_subs( $form_id, $expiration_time )
{
// Create the that will house our expired subs.
$expired_subs = array();
// Create our deletion timestamp.
$deletion_timestamp = time() - ( 24 * 60 * 60 * $expiration_time );
// Get our subs and loop over them.
$sub = Ninja_Forms()->form( $form_id )->get_subs();
foreach( $sub as $sub_model ) {
// Get the sub date and change it to a UNIX time stamp.
$sub_timestamp = strtotime( $sub_model->get_sub_date( 'Y-m-d') );
// Compare our timestamps and any expired subs to the array.
if( $sub_timestamp <= $deletion_timestamp ) {
$expired_subs[] = $sub_model->get_id();
}
}
return $expired_subs;
}
}
\ No newline at end of file
......@@ -105,4 +105,12 @@ return apply_filters( 'ninja_forms_plugin_settings_advanced', array(
'desc' => __( 'Rollback to the most recent 2.9.x release.', 'ninja-forms' ) . '<br /><div style="color: red">' . __( 'IMPORTANT: All 3.0 data will be removed.', 'ninja-forms' ) . '<br />' . __( 'Please export any forms or submissions you do not want to be lost during this process.', 'ninja-forms' ) . '</div>',
),
'trash_expired_submissions' => array(
'id' => 'trash_expired_submissions',
'type' => 'html',
'html' => '<div id="nfTrashExpiredSubmissions" class="button">' . __( 'Move To Trash', 'ninja-forms' ) . '</div>',
'label' => __( 'Trash Expired Submissions', 'ninja-forms' ),
'desc' => __( 'This setting maybe helpful if your WordPress installation is not moving expired submissions to the trash properly.', 'ninja-forms' ),
),
));
......@@ -88,6 +88,7 @@ final class NF_Database_SubmissionExpirationCron
* Helper method that removes our expired subs.
*
* @param $expired_subs - array of sub ids that need to be deleted.
* @param $cap - The cap of the amount of subs you want deleted at 1 time.
*
* @return void
*/
......
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