Commit f9033830 authored by Kevin Stover's avatar Kevin Stover

Refactoring and commenting.

parent cbed64b6
Pipeline #546 passed with stage
in 1 minute and 39 seconds
......@@ -5,6 +5,15 @@
*/
abstract class NF_Abstracts_BatchProcess
{
protected $_db;
/**
* [$response description]
* @var array
*/
protected $response = array(
'batch_complete' => false
);
/**
* Constructor
......@@ -14,8 +23,43 @@ abstract class NF_Abstracts_BatchProcess
//Bail if we aren't in the admin.
if ( ! is_admin() )
return false;
global $wpdb;
/**
* Set $_db to $wpdb.
* This helps us by not requiring us to declare global $wpdb in every class method.
*/
$this->_db = $wpdb;
// Run init.
$this->init();
}
/**
* Decides whether we need to run startup or restart and then calls processing.
*
* @since UPDATE_VERSION_ON_MERGE
* @return void
*/
public function init()
{
if ( ! get_option( 'nf_doing_' . $this->_slug ) ) {
// Run the startup process.
$this->startup();
} else {
// Otherwise... (We've already run startup.)
$this->restart();
}
// Determine how many steps this will take.
$this->response[ 'step_total' ] = $this->get_steps();
add_option( 'nf_doing_' . $this->_slug, true );
// Run processing
$this->process();
}
/**
* Function to loop over the batch.
......@@ -27,7 +71,6 @@ abstract class NF_Abstracts_BatchProcess
*/
}
/**
* Function to run any setup steps necessary to begin processing.
*/
......@@ -37,7 +80,20 @@ abstract class NF_Abstracts_BatchProcess
* This function intentionally left empty.
*/
}
/**
* Function to run any setup steps necessary to begin processing.
*/
public function restart()
{
/**
* This function intentionally left empty.
*/
}
public function get_steps()
{
return 1;
}
/**
* Function to cleanup any lingering temporary elements of a batch process after completion.
......@@ -49,6 +105,24 @@ abstract class NF_Abstracts_BatchProcess
*/
}
public function batch_complete()
{
// Delete our options.
delete_option( 'nf_doing_' . $this->_slug );
// Tell our JS that we're done.
$this->response[ 'batch_complete' ] = true;
$this->cleanup();
$this->respond();
}
public function next_step()
{
// ..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();
$this->respond();
}
public function respond()
{
echo wp_json_encode( $this->response );
......
......@@ -5,41 +5,47 @@
*/
class NF_Admin_Processes_ExpiredSubmissionCleanup extends NF_Abstracts_BatchProcess
{
protected $_slug = 'expired_submission_cleanup';
protected $expired_subs = array();
private $response = array(
'batch_complete' => false
);
/**
* Constructor
* Function to run any setup steps necessary to begin processing.
*/
public function __construct( $data = array() )
public function startup()
{
//Bail if we aren't in the admin.
if ( ! is_admin() )
return false;
// This die() causes the batch modal to stay visible.
// Remove in production.
die();
// Retrieves the option that contains all of our expiration data.
$expired_sub_option = get_option( 'nf_sub_expiration', array() );
// Run process.
$this->process();
// 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 );
}
}
public function restart()
{
// Get our remaining submissions from record.
$this->expired_subs = get_option( 'nf_expired_submissions', array() );
}
/**
* 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++ ){
......@@ -53,62 +59,22 @@ class NF_Admin_Processes_ExpiredSubmissionCleanup extends NF_Abstracts_BatchProc
// 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 nf_expired_submissions so that we can use it in our next step.
update_option( 'nf_expired_submissions', $this->expired_subs );
echo wp_json_encode( $this->response );
wp_die();
// End processing and move to the next step.
$this->next_step();
}
// Run our cleanup process.
$this->cleanup();
echo wp_json_encode( $this->response );
wp_die();
// If we get here, then we're ready to end batch processing.
$this->batch_complete();
}
/**
* 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;
}
/*
......
This diff is collapsed.
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