nf_29_update_form_settings

Update form settings to the new storage system when the form is viewed for the first time.

Description

nf_29_update_form_settings( $form_id ); 

Parameters (1)

0. $form_id
The form id.

Usage

  1. if ( !function_exists( 'nf_29_update_form_settings' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'ninja-forms/deprecated/includes/display/upgrade-functions.php'; 
  3.  
  4. // The form id. 
  5. $form_id = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = nf_29_update_form_settings($form_id); 
  9.  

Defined (1)

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

/deprecated/includes/display/upgrade-functions.php  
  1. function nf_29_update_form_settings( $form_id ) { 
  2. global $wpdb; 
  3.  
  4. // Check to see if the conversion has been reset. 
  5. $is_reset = get_option( 'nf_converted_form_reset', 0 ); 
  6.  
  7. // Check to see if an object exists with our form id. 
  8. $type = nf_get_object_type($form_id); 
  9.  
  10. if ( $type ) { 
  11. // We have an object with our form id. 
  12.  
  13. if ( $is_reset AND 'form' == $type ) { 
  14. // Give precedence to the most recent form. 
  15.  
  16. // Set a new ID for the form being converted. 
  17. $f_id = nf_insert_object('form'); 
  18.  
  19. $fields = $wpdb->get_results("SELECT * FROM " . NINJA_FORMS_FIELDS_TABLE_NAME . " WHERE form_id = " . $form_id, ARRAY_A); 
  20.  
  21. foreach ($fields as $field) { 
  22.  
  23. unset($field['id']); 
  24.  
  25. $field['form_id'] = $f_id; 
  26.  
  27. // Copy the Fields to the new ID. 
  28. $wpdb->insert(NINJA_FORMS_FIELDS_TABLE_NAME, $field); 
  29.  
  30.  
  31. $relationships = $wpdb->get_results("SELECT * FROM " . NF_OBJECT_RELATIONSHIPS_TABLE_NAME . " WHERE parent_id = " . $form_id, ARRAY_A); 
  32.  
  33. foreach ($relationships as $relationship) { 
  34.  
  35. unset($relationship['id']); 
  36.  
  37. // Copy the object related to the form. 
  38. $object = $wpdb->get_results("SELECT * FROM " . NF_OBJECTS_TABLE_NAME . " WHERE id = " . $relationship['child_id'], ARRAY_A); 
  39.  
  40. unset($object['id']); 
  41.  
  42. $wpdb->insert(NF_OBJECTS_TABLE_NAME, $object); 
  43.  
  44. $relationship['child_id'] = $wpdb->insert_id; 
  45.  
  46. $relationship['parent_id'] = $f_id; 
  47.  
  48. // Copy the Relationships to the new ID. 
  49. $wpdb->insert(NF_OBJECT_RELATIONSHIPS_TABLE_NAME, $relationship); 
  50.  
  51.  
  52. } else { 
  53. // Give precedence to the converting form. 
  54.  
  55. // Insert a new object. 
  56. $next_id = nf_insert_object($type); 
  57.  
  58. // Replace all instances of the conflicting object ID with our new one. 
  59. $wpdb->update(NF_OBJECT_META_TABLE_NAME, array('object_id' => $next_id), array('object_id' => $form_id)); 
  60. $wpdb->update(NF_OBJECT_RELATIONSHIPS_TABLE_NAME, array('parent_id' => $next_id), array('parent_type' => $type, 'parent_id' => $form_id)); 
  61. $wpdb->update(NF_OBJECT_RELATIONSHIPS_TABLE_NAME, array('child_id' => $next_id), array('child_type' => $type, 'child_id' => $form_id)); 
  62.  
  63. // Delete the original (conflicting) object 
  64. $wpdb->query('DELETE FROM ' . NF_OBJECTS_TABLE_NAME . ' WHERE id = ' . $form_id); 
  65.  
  66.  
  67.  
  68. // Get the form from the old table. 
  69. $form = $wpdb->get_row( 'SELECT * FROM ' . NINJA_FORMS_TABLE_NAME . ' WHERE id = ' . $form_id, ARRAY_A ); 
  70.  
  71. // Set the insert form ID, if not already set. 
  72. $f_id = isset ( $f_id ) ? $f_id : nf_insert_object( 'form', $form['id'] ); 
  73.  
  74. // Unpack the converted form's settings 
  75. $settings = maybe_unserialize( $form['data'] ); 
  76. $settings['date_updated'] = $form['date_updated']; 
  77.  
  78. foreach ( $settings as $meta_key => $value ) { 
  79. nf_update_object_meta( $f_id, $meta_key, $value ); 
  80. nf_update_object_meta( $f_id, 'status', '' );