DeleteAllData.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. class NF_AJAX_Controllers_DeleteAllData extends NF_Abstracts_Controller
  3. {
  4. public function __construct()
  5. {
  6. add_action( 'wp_ajax_nf_delete_all_data', array( $this, 'delete_all_data' ) );
  7. }
  8. public function delete_all_data()
  9. {
  10. check_ajax_referer( 'ninja_forms_settings_nonce', 'security' );
  11. global $wpdb;
  12. $total_subs_deleted = 0;
  13. $post_result = 0;
  14. $max_cnt = 500;
  15. $form_id = $_POST[ 'form' ];
  16. // SQL for getting 250 subs at a time
  17. $sub_sql = "SELECT id FROM `" . $wpdb->prefix . "posts` AS p
  18. LEFT JOIN `" . $wpdb->prefix . "postmeta` AS m ON p.id = m.post_id
  19. WHERE p.post_type = 'nf_sub' AND m.meta_key = '_form_id'
  20. AND m.meta_value = %s LIMIT " . $max_cnt;
  21. while ($post_result <= $max_cnt ) {
  22. $subs = $wpdb->get_col( $wpdb->prepare( $sub_sql, $form_id ),0 );
  23. // if we are out of subs, then stop
  24. if( 0 === count( $subs ) ) break;
  25. // otherwise, let's delete the postmeta as well
  26. $delete_meta_query = "DELETE FROM `" . $wpdb->prefix . "postmeta` WHERE post_id IN ( [IN] )";
  27. $delete_meta_query = $this->prepare_in( $delete_meta_query, $subs );
  28. $meta_result = $wpdb->query( $delete_meta_query );
  29. if ( $meta_result > 0 ) {
  30. // now we actually delete the posts(nf_sub)
  31. $delete_post_query = "DELETE FROM `" . $wpdb->prefix . "posts` WHERE id IN ( [IN] )";
  32. $delete_post_query = $this->prepare_in( $delete_post_query, $subs );
  33. $post_result = $wpdb->query( $delete_post_query );
  34. $total_subs_deleted = $total_subs_deleted + $post_result;
  35. }
  36. }
  37. $this->_data[ 'form_id' ] = $_POST[ 'form' ];
  38. $this->_data[ 'delete_count' ] = $total_subs_deleted;
  39. $this->_data[ 'success' ] = true;
  40. if ( 1 == $_POST[ 'last_form' ] ) {
  41. //if we are on the last form, then deactivate and nuke db tables
  42. $migrations = new NF_Database_Migrations();
  43. $migrations->nuke(TRUE, TRUE);
  44. $migrations->nuke_settings(TRUE, TRUE);
  45. $migrations->nuke_deprecated(TRUE, TRUE);
  46. deactivate_plugins( 'ninja-forms/ninja-forms.php' );
  47. $this->_data[ 'plugin_url' ] = admin_url( 'plugins.php' );
  48. }
  49. $this->_respond();
  50. }
  51. private function prepare_in( $sql, $vals ) {
  52. global $wpdb;
  53. $not_in_count = substr_count( $sql, '[IN]' );
  54. if ( $not_in_count > 0 ) {
  55. $args = array( str_replace( '[IN]', implode( ', ', array_fill( 0, count( $vals ), '%d' ) ), str_replace( '%', '%%', $sql ) ) );
  56. // This will populate ALL the [IN]'s with the $vals, assuming you have more than one [IN] in the sql
  57. for ( $i=0; $i < substr_count( $sql, '[IN]' ); $i++ ) {
  58. $args = array_merge( $args, $vals );
  59. }
  60. $sql = call_user_func_array( array( $wpdb, 'prepare' ), array_merge( $args ) );
  61. }
  62. return $sql;
  63. }
  64. }