SubmissionExpirationCron.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. final class NF_Database_SubmissionExpirationCron
  3. {
  4. /**
  5. * NF_Database_SubmissionExpirationCron constructor.
  6. * Sets up our our submission expiration CRON job.
  7. *
  8. */
  9. public function __construct()
  10. {
  11. // Retrieves the option that contains all of our expiration data.
  12. $options = get_option( 'nf_sub_expiration', false );
  13. // Schedules our CRON job.
  14. if( ! wp_next_scheduled( 'nf_submission_expiration_cron' ) && ! empty( $options ) ) {
  15. wp_schedule_event( time(), 'daily', 'nf_submission_expiration_cron' );
  16. }
  17. add_action( 'nf_submission_expiration_cron', array( $this, 'expired_submission_cron' ) );
  18. }
  19. /**
  20. * Expired Submission Cron
  21. * Checks our subs to see if any are expired and sends them to be
  22. * deleted if there are any that need to be removed.
  23. *
  24. * @param $options
  25. * @return void
  26. */
  27. public function expired_submission_cron()
  28. {
  29. $options = get_option( 'nf_sub_expiration', false );
  30. // If options are empty bail..
  31. if( ! $options || ! is_array( $options ) ) return;
  32. // Loop over our options and ...
  33. foreach( $options as $option ) {
  34. /*
  35. * Separate our $option values into two positions
  36. * $option[ 0 ] = ( int ) form_id
  37. * $option[ 1 ] = ( int ) expiration time in days.
  38. */
  39. $option = explode( ',', $option );
  40. // Use the helper method to build an array of expired subs.
  41. $expired_subs[] = $this->get_expired_subs( $option[ 0 ], $option[ 1 ] );
  42. }
  43. // If the expired subs array is empty bail.
  44. if( empty( $expired_subs ) ) return;
  45. // Call the helper method that deletes the expired subs.
  46. $this->delete_expired_subs( $expired_subs );
  47. }
  48. /**
  49. * Get Expired Subs
  50. * Gathers our expired subs puts them into an array and returns it.
  51. *
  52. * @param $form_id - ( int ) ID of the Form.
  53. * @param $expiration_time - ( int ) number of days the submissions
  54. * are set to expire in
  55. *
  56. * @return array of all the expired subs that were found.
  57. */
  58. public function get_expired_subs( $form_id, $expiration_time )
  59. {
  60. // Create the that will house our expired subs.
  61. $expired_subs = array();
  62. // Create our deletion timestamp.
  63. $deletion_timestamp = time() - ( 24 * 60 * 60 * $expiration_time );
  64. // Get our subs and loop over them.
  65. $sub = Ninja_Forms()->form( $form_id )->get_subs();
  66. foreach( $sub as $sub_model ) {
  67. // Get the sub date and change it to a UNIX time stamp.
  68. $sub_timestamp = strtotime( $sub_model->get_sub_date( 'Y-m-d') );
  69. // Compare our timestamps and any expired subs to the array.
  70. if( $sub_timestamp <= $deletion_timestamp ) {
  71. $expired_subs[] = $sub_model->get_id();
  72. }
  73. }
  74. return $expired_subs;
  75. }
  76. /**
  77. * Delete Expired Subs
  78. * Helper method that removes our expired subs.
  79. *
  80. * @param $expired_subs - array of sub ids that need to be deleted.
  81. * @param $cap - The cap of the amount of subs you want deleted at 1 time.
  82. *
  83. * @return void
  84. */
  85. public function delete_expired_subs( $expired_subs )
  86. {
  87. $i = 0;
  88. // Loop over our subs
  89. foreach( $expired_subs as $subs ) {
  90. foreach( $subs as $sub ) {
  91. if( $i >= 100 ) break;
  92. wp_trash_post( $sub );
  93. $i++;
  94. }
  95. }
  96. }
  97. }