ExpiredSubmissionCleanup.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Class NF_Abstracts_Batch_Process
  4. */
  5. class NF_Admin_Processes_ExpiredSubmissionCleanup extends NF_Abstracts_BatchProcess
  6. {
  7. protected $expired_subs = array();
  8. private $response = array(
  9. 'batch_complete' => false
  10. );
  11. /**
  12. * Constructor
  13. */
  14. public function __construct( $data = array() )
  15. {
  16. //Bail if we aren't in the admin.
  17. if ( ! is_admin() )
  18. return false;
  19. // Run process.
  20. $this->process();
  21. }
  22. /**
  23. * Function to loop over the batch.
  24. */
  25. public function process()
  26. {
  27. if ( ! get_option( 'nf_doing_expired_submission_cleanup' ) ) {
  28. // Run the startup process.
  29. $this->startup();
  30. } // Otherwise... (We've already run startup.)
  31. else {
  32. // Get our remaining submissions from record.
  33. $data = get_option( 'nf_expired_submissions' );
  34. $this->expired_subs = $data;
  35. }
  36. // For the first 250 in the array.
  37. for( $i = 0; $i < 250; $i++ ){
  38. // if we've already finished bail..
  39. if( empty( $this->expired_subs ) ) break;
  40. // Pop off a sub and delete it.
  41. $sub = array_pop( $this->expired_subs );
  42. wp_trash_post( $sub );
  43. }
  44. // If our subs array isn't empty...
  45. if( ! empty( $this->expired_subs ) ) {
  46. // ..see how many steps we have left, update our option, and send the remaining step to the JS.
  47. $this->response[ 'step_remaining' ] = $this->get_steps();
  48. update_option( 'nf_expired_submissions', $this->expired_subs );
  49. echo wp_json_encode( $this->response );
  50. wp_die();
  51. }
  52. // Run our cleanup process.
  53. $this->cleanup();
  54. echo wp_json_encode( $this->response );
  55. wp_die();
  56. }
  57. /**
  58. * Function to run any setup steps necessary to begin processing.
  59. */
  60. public function startup()
  61. {
  62. // Retrieves the option that contains all of our expiration data.
  63. $expired_sub_option = get_option( 'nf_sub_expiration', array() );
  64. // Loop over our options and ...
  65. foreach( $expired_sub_option as $sub ) {
  66. /*
  67. * Separate our $option values into two positions
  68. * $option[ 0 ] = ( int ) form_id
  69. * $option[ 1 ] = ( int ) expiration time in days.
  70. */
  71. $sub = explode( ',', $sub );
  72. $expired_subs = $this->get_expired_subs( $sub[ 0 ], $sub[ 1 ] );
  73. // Use the helper method to build an array of expired subs.
  74. $this->expired_subs = array_merge( $this->expired_subs, $expired_subs );
  75. }
  76. // Determine how many steps this will take.
  77. $this->response[ 'step_total' ] = $this->get_steps();
  78. add_option( 'nf_doing_expired_submission_cleanup', 'true' );
  79. }
  80. /**
  81. * Function to cleanup any lingering temporary elements of a batch process after completion.
  82. */
  83. public function cleanup()
  84. {
  85. // Delete our options.
  86. delete_option('nf_doing_expired_submission_cleanup' );
  87. delete_option( 'nf_expired_submissions' );
  88. // Tell our JS that we're done.
  89. $this->response[ 'batch_complete' ] = true;
  90. }
  91. /*
  92. * Get Steps
  93. * Determines the amount of steps needed for the step processors.
  94. *
  95. * @return int of the number of steps.
  96. */
  97. public function get_steps()
  98. {
  99. // Convent our number from int to float
  100. $steps = count( $this->expired_subs );
  101. $steps = floatval( $steps );
  102. // Get the amount of steps and return.
  103. $steps = ceil( $steps / 250.0 );
  104. return $steps;
  105. }
  106. /**
  107. * Get Expired Subs
  108. * Gathers our expired subs puts them into an array and returns it.
  109. *
  110. * @param $form_id - ( int ) ID of the Form.
  111. * @param $expiration_time - ( int ) number of days the submissions
  112. * are set to expire in
  113. *
  114. * @return array of all the expired subs that were found.
  115. */
  116. public function get_expired_subs( $form_id, $expiration_time )
  117. {
  118. // Create the that will house our expired subs.
  119. $expired_subs = array();
  120. // Create our deletion timestamp.
  121. $deletion_timestamp = time() - ( 24 * 60 * 60 * $expiration_time );
  122. // Get our subs and loop over them.
  123. $sub = Ninja_Forms()->form( $form_id )->get_subs();
  124. foreach( $sub as $sub_model ) {
  125. // Get the sub date and change it to a UNIX time stamp.
  126. $sub_timestamp = strtotime( $sub_model->get_sub_date( 'Y-m-d') );
  127. // Compare our timestamps and any expired subs to the array.
  128. if( $sub_timestamp <= $deletion_timestamp ) {
  129. $expired_subs[] = $sub_model->get_id();
  130. }
  131. }
  132. return $expired_subs;
  133. }
  134. }