NF_Upgrade_Submissions

The Ninja Forms NF Upgrade Submissions class.

Defined (1)

The class is defined in the following location(s).

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