ninja_forms_import_form

Import a serialized ninja form.

Description

ninja_forms_import_form( $file ); 

Parameters (1)

0. $file
The file.

Usage

  1. if ( !function_exists( 'ninja_forms_import_form' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'ninja-forms/deprecated/includes/import-export.php'; 
  3.  
  4. // The file. 
  5. $file = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = ninja_forms_import_form($file); 
  9.  

Defined (1)

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

/deprecated/includes/import-export.php  
  1. function ninja_forms_import_form( $file ) { 
  2. global $wpdb; 
  3. $form = unserialize( trim( $file ) ); 
  4. $form_fields = isset( $form['field'] ) ? $form['field'] : null; 
  5. $notifications = isset ( $form['notifications'] ) ? $form['notifications'] : null; 
  6.  
  7. unset ( $form['field'] ); 
  8. unset ( $form['notifications'] ); 
  9.  
  10. $form = apply_filters( 'ninja_forms_before_import_form', $form ); 
  11. // Remove our last_sub setting. This is our starting seq_num. 
  12. if ( isset ( $form['data']['last_sub'] ) ) 
  13. unset( $form['data']['last_sub'] ); 
  14.  
  15. // Create our form 
  16. $form_id = Ninja_Forms()->form()->create( $form['data'] ); 
  17.  
  18. $form['id'] = $form_id; 
  19.  
  20. if(is_array($form_fields)) { 
  21. for ($x=0; $x < count( $form_fields ); $x++) { 
  22. $form_fields[$x]['form_id'] = $form_id; 
  23. $form_fields[$x]['data'] = apply_filters( 'nf_before_import_field', $form_fields[$x]['data'], $form_fields[$x]['id'] ); 
  24. $form_fields[$x]['data'] = serialize( $form_fields[$x]['data'] ); 
  25. $old_field_id = $form_fields[$x]['id']; 
  26. $form_fields[$x]['id'] = NULL; 
  27. $wpdb->insert( NINJA_FORMS_FIELDS_TABLE_NAME, $form_fields[$x] ); 
  28. $form_fields[$x]['id'] = $wpdb->insert_id; 
  29. $form_fields[$x]['old_id'] = $old_field_id; 
  30. $form_fields[$x]['data'] = unserialize( $form_fields[$x]['data'] ); 
  31.  
  32. $form['field'] = $form_fields; 
  33. $form['notifications'] = $notifications;  
  34.  
  35. // Insert any notifications we might have. 
  36. if ( is_array( $notifications ) ) { 
  37. foreach ( $notifications as $n ) { 
  38. $n_id = nf_insert_notification( $form_id ); 
  39. $n = apply_filters( 'nf_import_notification_meta', $n, $n_id, $form ); 
  40. unset( $n['conditions'] ); 
  41. foreach ( $n as $meta_key => $meta_value ) { 
  42. foreach ( $form_fields as $field ) { 
  43. // We need to replace any references to old fields in our notification 
  44. if ( 'email_message' == $meta_key ) { 
  45. $meta_value = str_replace( '[ninja_forms_field id=' . $field['old_id'].']', '[ninja_forms_field id='.$field['id'].']', $meta_value ); 
  46. $meta_value = str_replace( 'ninja_forms_field_' . $field['old_id'], 'ninja_forms_field_' . $field['id'], $meta_value ); 
  47. } else { 
  48. $meta_value = preg_replace( '/\bfield_' . $field['old_id'] . '\b/u', 'field_' . $field['id'], $meta_value ); 
  49. nf_update_object_meta( $n_id, $meta_key, $meta_value ); 
  50.  
  51.  
  52. do_action( 'ninja_forms_after_import_form', $form ); 
  53. return $form['id'];