NF_Download_All_Subs

The Ninja Forms NF Download All Subs class.

Defined (1)

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

/deprecated/classes/download-all-subs.php  
  1. class NF_Download_All_Subs extends NF_Step_Processing { 
  2.  
  3. function __construct() { 
  4. $this->action = 'download_all_subs'; 
  5.  
  6. parent::__construct(); 
  7.  
  8. public function loading() { 
  9. $form_id = isset( $this->args['form_id'] ) ? absint( $this->args['form_id'] ) : 0; 
  10.  
  11. if ( empty( $form_id ) ) { 
  12.  
  13. return array( 'complete' => true ); 
  14.  
  15. $sub_count = nf_get_sub_count( $form_id ); 
  16.  
  17. if( empty( $this->total_steps ) || $this->total_steps <= 1 ) { 
  18. $this->total_steps = round( ( $sub_count / 250 ), 0 ) + 2; 
  19.  
  20. $args = array( 
  21. 'total_steps' => $this->total_steps,  
  22. ); 
  23.  
  24. $this->args['filename'] = $this->random_filename( 'all-subs' ); 
  25. update_user_option( get_current_user_id(), 'nf_download_all_subs_filename', $this->args['filename'] ); 
  26. $this->redirect = esc_url_raw( add_query_arg( array( 'download_all' => $this->args['filename'] ), $this->args['redirect'] ) ); 
  27.  
  28. $this->loaded = true; 
  29. return $args; 
  30.  
  31. public function step() { 
  32. if( ! is_numeric( $this->args[ 'form_id' ] ) ) { 
  33. wp_die( __( 'Invalid form id', 'ninja-forms' ) ); 
  34.  
  35. $this->args[ 'filename' ] = wp_kses_post( $this->args[ 'filename' ] ); 
  36.  
  37. $exported_subs = get_user_option( get_current_user_id(), 'nf_download_all_subs_ids' ); 
  38. if ( ! is_array( $exported_subs ) ) { 
  39. $exported_subs = array(); 
  40.  
  41. $previous_name = get_user_option( get_current_user_id(), 'nf_download_all_subs_filename' ); 
  42. if ( $previous_name ) { 
  43. $this->args['filename'] = $previous_name; 
  44.  
  45. $args = array( 
  46. 'posts_per_page' => 250,  
  47. 'paged' => $this->step,  
  48. 'post_type' => 'nf_sub',  
  49. 'meta_query' => array( 
  50. array( 
  51. 'key' => '_form_id',  
  52. 'value' => $this->args['form_id'],  
  53. ),  
  54. ),  
  55. ); 
  56.  
  57. $subs_results = get_posts( $args ); 
  58.  
  59. if ( is_array( $subs_results ) && ! empty( $subs_results ) ) { 
  60. $upload_dir = wp_upload_dir(); 
  61. $file_path = trailingslashit( $upload_dir['path'] ) . $this->args['filename'] . '.csv'; 
  62. $myfile = fopen( $file_path, 'a' ) or die( 'Unable to open file!' ); 
  63. $x = 0; 
  64. $export = ''; 
  65. foreach ( $subs_results as $sub ) { 
  66. $sub_export = Ninja_Forms()->sub( $sub->ID )->export( true ); 
  67. if ( $x > 0 || $this->step > 1 ) { 
  68. $sub_export = substr( $sub_export, strpos( $sub_export, "\n" ) + 1 ); 
  69. if ( ! in_array( $sub->ID, $exported_subs ) ) { 
  70. $export .= $sub_export; 
  71. $exported_subs[] = $sub->ID; 
  72. $x++; 
  73. fwrite( $myfile, $export ); 
  74. fclose( $myfile ); 
  75.  
  76. update_user_option( get_current_user_id(), 'nf_download_all_subs_ids', $exported_subs ); 
  77.  
  78. public function complete() { 
  79. delete_user_option( get_current_user_id(), 'nf_download_all_subs_ids' ); 
  80. delete_user_option( get_current_user_id(), 'nf_download_all_subs_filename' ); 
  81.  
  82. /** 
  83. * Add an integar to the end of our filename to make sure it is unique 
  84. * @access public 
  85. * @since 2.7.6 
  86. * @return $filename 
  87. */ 
  88. public function random_filename( $filename ) { 
  89. $upload_dir = wp_upload_dir(); 
  90. $file_path = trailingslashit( $upload_dir['path'] ) . $filename . '.csv'; 
  91. if ( file_exists ( $file_path ) ) { 
  92. for ($x = 0; $x < 999 ; $x++) { 
  93. $tmp_name = $filename . '-' . $x; 
  94. $tmp_path = trailingslashit( $upload_dir['path'] ); 
  95. if ( file_exists( $tmp_path . $tmp_name . '.csv' ) ) { 
  96. $this->random_filename( $tmp_name ); 
  97. break; 
  98. } else { 
  99. $this->filename = $tmp_name; 
  100. break; 
  101.  
  102. return $filename; 
  103.