convert-subs.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. final class NF_Upgrade_Submissions extends NF_Upgrade
  3. {
  4. public $name = 'submissions';
  5. public $priority = '2.7';
  6. public $description = 'The new submission database allows submissions to be stored and retrieved more efficiently. It also allows for much better submission searching.';
  7. public $args = array();
  8. public $errors = array();
  9. public function loading()
  10. {
  11. $old_sub_count = $this->countOldSubs();
  12. $this->total_steps = round( ( $old_sub_count / 100 ), 0 );
  13. if ( ! $this->total_steps || 1 > $this->total_steps ) {
  14. $this->total_steps = 1;
  15. }
  16. }
  17. public function _beforeStep( $step )
  18. {
  19. if ( get_option( 'nf_convert_subs_num' ) ) {
  20. $this->args['number'] = get_option( 'nf_convert_subs_num' );
  21. }
  22. $this->args['form_id'] = isset( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : 0;
  23. update_option( 'nf_convert_subs_step', $step );
  24. }
  25. public function step( $step )
  26. {
  27. $begin = ( $step - 1 ) * 100;
  28. $subs_results = $this->getOldSubs( $begin, 100 );
  29. if ( is_array( $subs_results ) && ! empty( $subs_results ) ) {
  30. foreach ( $subs_results as $sub ) {
  31. if ( $this->args['form_id'] != $sub['form_id'] ) {
  32. $this->args['form_id'] = $sub['form_id'];
  33. $number = 1;
  34. }
  35. $converted = get_option( 'nf_converted_subs' );
  36. if ( empty( $converted ) )
  37. $converted = array();
  38. if ( ! in_array( $sub['id'], $converted ) ) {
  39. $this->convert( $sub, $number );
  40. $converted[] = $sub['id'];
  41. update_option( 'nf_converted_subs', $converted );
  42. $number++;
  43. update_option( 'nf_convert_subs_num', $number );
  44. }
  45. }
  46. }
  47. }
  48. public function _afterStep( $step )
  49. {
  50. }
  51. public function complete()
  52. {
  53. update_option( 'nf_convert_subs_step', 'complete' );
  54. delete_option( 'nf_convert_subs_num' );
  55. }
  56. public function isComplete()
  57. {
  58. return get_option( 'nf_convert_subs_step', false );
  59. }
  60. /*
  61. * PRIVATE METHODS
  62. */
  63. private function getOldSubs( $begin = '', $count = '' ) {
  64. global $wpdb;
  65. if ( $begin == '' && $count == '' ) {
  66. $limit = '';
  67. } else {
  68. $limit = ' LIMIT ' . $begin . ',' . $count;
  69. }
  70. $subs_results = $wpdb->get_results( 'SELECT * FROM ' . NINJA_FORMS_SUBS_TABLE_NAME . ' WHERE `action` != "mp_save" ORDER BY `form_id` ASC, `id` ASC ' . $limit, ARRAY_A );
  71. //Now that we have our sub results, let's loop through them and remove any that don't match our args array.
  72. if( is_array( $subs_results ) AND ! empty( $subs_results ) ) {
  73. foreach( $subs_results as $key => $val ) { //Initiate a loop that will run for all of our submissions.
  74. //Set our $data variable. This variable contains an array that looks like: array('field_id' => 13, 'user_value' => 'Hello World!').
  75. if( is_serialized( $subs_results[$key]['data'] ) ) {
  76. $subs_results[ $key ]['data'] = unserialize( $subs_results[ $key ]['data'] );
  77. }
  78. }
  79. }
  80. return $subs_results;
  81. }
  82. private function countOldSubs() {
  83. global $wpdb;
  84. $count = $wpdb->get_results( 'SELECT COUNT(*) FROM '. NINJA_FORMS_SUBS_TABLE_NAME . ' WHERE `action` != "mp_save"', ARRAY_A );
  85. if ( is_array ( $count ) && ! empty ( $count ) ) {
  86. return $count[0]['COUNT(*)'];
  87. } else {
  88. return false;
  89. }
  90. }
  91. public function convert( $sub, $num ) {
  92. if ( isset ( $sub['id'] ) ) {
  93. $old_id = $sub['id'];
  94. unset( $sub['id'] );
  95. }
  96. if ( isset ( $sub['form_id'] ) ) {
  97. $form_id = $sub['form_id'];
  98. unset ( $sub['form_id'] );
  99. }
  100. if ( isset ( $sub['action'] ) ) {
  101. $action = $sub['action'];
  102. unset ( $sub['action'] );
  103. }
  104. if ( isset ( $sub['user_id'] ) ) {
  105. $user_id = $sub['user_id'];
  106. unset ( $sub['user_id'] );
  107. }
  108. if ( isset ( $sub['date_updated'] ) ) {
  109. $date_updated = $sub['date_updated'];
  110. unset ( $sub['date_updated'] );
  111. }
  112. if ( isset ( $sub['status'] ) )
  113. unset ( $sub['status'] );
  114. if ( isset ( $sub['saved'] ) )
  115. unset ( $sub['saved'] );
  116. $sub_id = Ninja_Forms()->subs()->create( $form_id );
  117. Ninja_Forms()->sub( $sub_id )->update_action( $action );
  118. Ninja_Forms()->sub( $sub_id )->update_user_id( $user_id );
  119. Ninja_Forms()->sub( $sub_id )->update_seq_num( $num );
  120. Ninja_Forms()->sub( $sub_id )->update_date_submitted( $date_updated );
  121. Ninja_Forms()->sub( $sub_id )->update_date_modified( $date_updated );
  122. Ninja_Forms()->sub( $sub_id )->add_meta( '_old_id', $old_id );
  123. if ( isset ( $sub['data'] ) ) {
  124. foreach ( $sub['data'] as $data ) {
  125. $field_id = $data['field_id'];
  126. $value = $data['user_value'];
  127. Ninja_Forms()->sub( $sub_id )->add_field( $field_id, $value );
  128. }
  129. unset ( $sub['data'] );
  130. }
  131. if ( ! empty ( $sub ) ) {
  132. foreach ( $sub as $key => $value ) {
  133. if ( $value !== '' ) {
  134. Ninja_Forms()->sub( $sub_id )->add_meta( '_' . $key, $value );
  135. }
  136. }
  137. }
  138. }
  139. }