nf_csv_attachment

The Ninja Forms nf csv attachment function.

Description

nf_csv_attachment( $sub_id ); 

Parameters (1)

0. $sub_id
The sub id.

Usage

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

Defined (1)

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

/deprecated/includes/deprecated.php  
  1. function nf_csv_attachment( $sub_id ) { 
  2. global $ninja_forms_processing; 
  3.  
  4. // make sure this form is supposed to attach a CSV 
  5. if( 1 == $ninja_forms_processing->get_form_setting( 'admin_attach_csv' ) AND 'submit' == $ninja_forms_processing->get_action() ) { 
  6.  
  7. // create CSV content 
  8. $csv_content = Ninja_Forms()->sub( $sub_id )->export( true ); 
  9.  
  10. $upload_dir = wp_upload_dir(); 
  11. $path = trailingslashit( $upload_dir['path'] ); 
  12.  
  13. // create temporary file 
  14. $path = tempnam( $path, 'Sub' ); 
  15. $temp_file = fopen( $path, 'r+' ); 
  16.  
  17. // write to temp file 
  18. fwrite( $temp_file, $csv_content ); 
  19. fclose( $temp_file ); 
  20.  
  21. // find the directory we will be using for the final file 
  22. $path = pathinfo( $path ); 
  23. $dir = $path['dirname']; 
  24. $basename = $path['basename']; 
  25.  
  26. // create name for file 
  27. $new_name = apply_filters( 'ninja_forms_submission_csv_name', 'ninja-forms-submission' ); 
  28.  
  29. // remove a file if it already exists 
  30. if( file_exists( $dir.'/'.$new_name.'.csv' ) ) { 
  31. unlink( $dir.'/'.$new_name.'.csv' ); 
  32.  
  33. // move file 
  34. rename( $dir.'/'.$basename, $dir.'/'.$new_name.'.csv' ); 
  35. $file1 = $dir.'/'.$new_name.'.csv'; 
  36.  
  37. // add new file to array of existing files 
  38. $files = $ninja_forms_processing->get_form_setting( 'admin_attachments' ); 
  39. array_push( $files, $file1 ); 
  40. $ninja_forms_processing->update_form_setting( 'admin_attachments', $files ); 
  41. $ninja_forms_processing->update_extra_value( '_attachment_csv_path', $file1 );