DataCleanup.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Class NF_Abstracts_Batch_Process
  4. */
  5. class NF_Admin_Processes_DataCleanup extends NF_Abstracts_BatchProcess
  6. {
  7. private $response = array(
  8. 'batch_complete' => false
  9. );
  10. protected $delete = array();
  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. global $wpdb;
  28. // If we've not already started the cleanup process...
  29. if ( ! get_option( 'nf_doing_data_cleanup' ) ) {
  30. // Run the startup process.
  31. $this->startup();
  32. } // Otherwise... (We've already run startup.)
  33. else {
  34. // Get our data.
  35. $data = get_option( 'nf_data_cleanup_ids' );
  36. $this->delete = explode( ',', $data );
  37. }
  38. // If our array isn't emtpy...
  39. if ( ! empty( $this->delete ) ) {
  40. // Fetch the last item on it.
  41. $id = array_pop( $this->delete );
  42. // Get a list of post IDs to delete.
  43. $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";
  44. $result = $wpdb->get_results( $sql, 'ARRAY_A' );
  45. // If we got 500 or more results...
  46. if ( 500 == count( $result ) ) {
  47. // Put this id back in our array to run again.
  48. array_push( $this->delete, $id );
  49. }
  50. // Convert our results to something we can use in a query.
  51. array_walk( $result, array( $this, 'smush_results' ) );
  52. $sub_sql = implode( ', ', $result );
  53. // If we have something to query...
  54. if ( '' != $sub_sql ) {
  55. // Delete postmeta data.
  56. $sql = "DELETE FROM `" . $wpdb->prefix . "postmeta` WHERE `post_id` IN(" . $sub_sql . ")";
  57. $wpdb->query( $sql );
  58. // Delete post data.
  59. $sql = "DELETE FROM `" . $wpdb->prefix . "posts` WHERE `id` IN(" . $sub_sql . ")";
  60. $wpdb->query( $sql );
  61. }
  62. }
  63. // If our array isn't empty...
  64. if ( ! empty( $this->delete ) ) {
  65. // Determine how many steps we have left.
  66. $this->response[ 'step_remaining' ] = count( $this->delete );
  67. update_option( 'nf_data_cleanup_ids', implode( ',', $this->delete ) );
  68. echo wp_json_encode( $this->response );
  69. wp_die();
  70. }
  71. // Run our cleanup process.
  72. $this->cleanup();
  73. echo wp_json_encode( $this->response );
  74. wp_die();
  75. }
  76. /**
  77. * Function to run any setup steps necessary to begin processing.
  78. */
  79. public function startup()
  80. {
  81. global $wpdb;
  82. // Get a list of IDs from the forms table.
  83. $sql = "SELECT DISTINCT(`id`) FROM `" . $wpdb->prefix . "nf3_forms`";
  84. $forms = $wpdb->get_results( $sql, 'ARRAY_A' );
  85. // Get a list of IDs from the Submissions data.
  86. $sql = "SELECT DISTINCT(m.meta_value) AS id FROM `" . $wpdb->prefix . "postmeta` AS m LEFT JOIN `" . $wpdb->prefix . "posts` AS p on p.id = m.post_id WHERE m.meta_key = '_form_id' AND p.post_type = 'nf_sub'";
  87. $sub_forms = $wpdb->get_results( $sql, 'ARRAY_A' );
  88. // For each form ID in the submission records...
  89. foreach( $sub_forms AS $form ) {
  90. // If the form is not currently defined in our forms table...
  91. if ( ! in_array( $form, $forms ) ) {
  92. // Add it to our list of things to delete.
  93. $this->delete[] = $form[ 'id' ];
  94. }
  95. }
  96. // Get our number of steps for the progress bar.
  97. $this->response[ 'step_total' ] = count( $this->delete );
  98. // Flag startup done.
  99. add_option( 'nf_doing_data_cleanup', 'true' );
  100. }
  101. /**
  102. * Function to cleanup any lingering temporary elements of a batch process after completion.
  103. */
  104. public function cleanup()
  105. {
  106. global $wpdb;
  107. // Delete our options.
  108. delete_option( 'nf_data_cleanup_ids' );
  109. delete_option( 'nf_doing_data_cleanup' );
  110. // Add our "finished" option.
  111. add_option( 'ninja_forms_data_is_clean', 'true' );
  112. // Tell our JS that we're done.
  113. $this->response[ 'step_remaining' ] = 0;
  114. $this->response[ 'batch_complete' ] = true;
  115. }
  116. /**
  117. * Function to compress data array and eliminate unnecessary keys.
  118. */
  119. public function smush_results( &$value, $key ) {
  120. $value = $value[ 'id' ];
  121. }
  122. }