NF_Upgrade_Forms

The Ninja Forms NF Upgrade Forms class.

Defined (1)

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

/deprecated/includes/admin/upgrades/convert-forms.php  
  1. final class NF_Upgrade_Forms extends NF_Upgrade 
  2. public $name = 'forms'; 
  3.  
  4. public $priority = '2.9.0'; 
  5.  
  6. public $description = 'Form settings and information needs to be transferred to a new forms database. This new storage method will make it much easier effecient to interact with forms.'; 
  7.  
  8. public $completed_forms = array(); 
  9.  
  10. public $args = array(); 
  11.  
  12. public $errors = array(); 
  13.  
  14. public function loading() 
  15. global $wpdb; 
  16.  
  17. // Check that the table exists 
  18. if( 0 == $wpdb->query( "SHOW TABLES LIKE '" . NINJA_FORMS_TABLE_NAME . "'" ) ) { 
  19. //TODO Maybe change error to debug message instead 
  20. $this->errors[] = NINJA_FORMS_TABLE_NAME . ' is not in the database'; 
  21. return; 
  22.  
  23. // Get all our forms 
  24. $forms = $wpdb->get_results( 'SELECT id FROM ' . NINJA_FORMS_TABLE_NAME, ARRAY_A ); 
  25.  
  26. $x = 1; 
  27. if ( is_array( $forms ) ) { 
  28. foreach ( $forms as $form ) { 
  29. $this->args['forms'][$x] = $form['id']; 
  30. $x++; 
  31.  
  32. $this->total_steps = count( $this->args['forms'] ); 
  33.  
  34. if( empty( $this->total_steps ) || $this->total_steps <= 1 ) { 
  35. $this->total_steps = 1; 
  36.  
  37.  
  38. public function _beforeStep( $step ) 
  39. // Get a list of forms that we've already converted. 
  40. $this->completed_forms = get_option( 'nf_converted_forms', array() ); 
  41.  
  42. if ( ! is_array( $this->completed_forms ) ) { 
  43. $this->completed_forms = array(); 
  44.  
  45. $this->form_id = $this->args['forms'][ $step ]; 
  46.  
  47. Ninja_Forms()->form( $this->form_id )->dumpCache(); 
  48.  
  49. // Bail if we've already converted the db for this form. 
  50. if ( in_array( $this->form_id, $this->completed_forms ) ) { 
  51. return false; 
  52.  
  53. public function step( $step ) 
  54. $this->update_form_settings( $this->form_id ); 
  55.  
  56. public function _afterStep( $step ) 
  57. $this->completed_forms[] = $this->form_id; 
  58. update_option( 'nf_converted_forms', $this->completed_forms ); 
  59.  
  60. Ninja_Forms()->form( $this->form_id )->dumpCache(); 
  61.  
  62. public function complete() 
  63. update_option( 'nf_convert_forms_complete', true ); 
  64. update_option( 'nf_converted_form_reset', false ); 
  65.  
  66. public function isComplete() 
  67. return get_option( 'nf_convert_forms_complete', false ); 
  68.  
  69. /** 
  70. * CONVERSION METHODS 
  71. */ 
  72.  
  73. public function update_form_settings( $form_id ) 
  74. global $wpdb; 
  75.  
  76. // Check to see if the conversion has been reset. 
  77. $is_reset = get_option( 'nf_converted_form_reset', 0 ); 
  78.  
  79. // Check to see if an object exists with our form id. 
  80. $type = nf_get_object_type($form_id); 
  81.  
  82. if ( $type ) { 
  83. // We have an object with our form id. 
  84.  
  85. if ( $is_reset AND 'form' == $type ) { 
  86. // Give precedence to the most recent form. 
  87.  
  88. // Set a new ID for the form being converted. 
  89. $f_id = nf_insert_object('form'); 
  90.  
  91. $fields = $wpdb->get_results("SELECT * FROM " . NINJA_FORMS_FIELDS_TABLE_NAME . " WHERE form_id = " . $form_id, ARRAY_A); 
  92.  
  93. foreach ($fields as $field) { 
  94.  
  95. unset($field['id']); 
  96.  
  97. $field['form_id'] = $f_id; 
  98.  
  99. // Copy the Fields to the new ID. 
  100. $wpdb->insert(NINJA_FORMS_FIELDS_TABLE_NAME, $field); 
  101.  
  102.  
  103. $relationships = $wpdb->get_results("SELECT * FROM " . NF_OBJECT_RELATIONSHIPS_TABLE_NAME . " WHERE parent_id = " . $form_id, ARRAY_A); 
  104.  
  105. foreach ($relationships as $relationship) { 
  106.  
  107. unset($relationship['id']); 
  108.  
  109. // Copy the object related to the form. 
  110. $object = $wpdb->get_results("SELECT * FROM " . NF_OBJECTS_TABLE_NAME . " WHERE id = " . $relationship['child_id'], ARRAY_A); 
  111.  
  112. unset($object['id']); 
  113.  
  114. $wpdb->insert(NF_OBJECTS_TABLE_NAME, $object); 
  115.  
  116. $relationship['child_id'] = $wpdb->insert_id; 
  117.  
  118. $relationship['parent_id'] = $f_id; 
  119.  
  120. // Copy the Relationships to the new ID. 
  121. $wpdb->insert(NF_OBJECT_RELATIONSHIPS_TABLE_NAME, $relationship); 
  122.  
  123.  
  124. } else { 
  125. // Give precedence to the converting form. 
  126.  
  127. // Insert a new object. 
  128. $next_id = nf_insert_object($type); 
  129.  
  130. // Replace all instances of the conflicting object ID with our new one. 
  131. $wpdb->update(NF_OBJECT_META_TABLE_NAME, array('object_id' => $next_id), array('object_id' => $form_id)); 
  132. $wpdb->update(NF_OBJECT_RELATIONSHIPS_TABLE_NAME, array('parent_id' => $next_id), array('parent_type' => $type, 'parent_id' => $form_id)); 
  133. $wpdb->update(NF_OBJECT_RELATIONSHIPS_TABLE_NAME, array('child_id' => $next_id), array('child_type' => $type, 'child_id' => $form_id)); 
  134.  
  135. // Delete the original (conflicting) object 
  136. $wpdb->query('DELETE FROM ' . NF_OBJECTS_TABLE_NAME . ' WHERE id = ' . $form_id); 
  137.  
  138.  
  139.  
  140. // Get the form from the old table. 
  141. $form = $wpdb->get_row( 'SELECT * FROM ' . NINJA_FORMS_TABLE_NAME . ' WHERE id = ' . $form_id, ARRAY_A ); 
  142.  
  143. // Set the insert form ID, if not already set. 
  144. $f_id = isset ( $f_id ) ? $f_id : nf_insert_object( 'form', $form['id'] ); 
  145.  
  146. // Unpack the converted form's settings 
  147. $settings = maybe_unserialize( $form['data'] ); 
  148. $settings['date_updated'] = $form['date_updated']; 
  149.  
  150. foreach ( $settings as $meta_key => $value ) { 
  151. nf_update_object_meta( $f_id, $meta_key, $value ); 
  152. nf_update_object_meta( $f_id, 'status', '' );