NF_Admin_CPT_DownloadAllSubmissions

The Ninja Forms NF Admin CPT DownloadAllSubmissions class.

Defined (1)

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

/includes/Admin/CPT/DownloadAllSubmissions.php  
  1. class NF_Admin_CPT_DownloadAllSubmissions extends NF_Step_Processing { 
  2.  
  3. function __construct() { 
  4. $this->action = 'download_all_subs'; 
  5.  
  6. parent::__construct(); 
  7.  
  8. add_action( 'admin_footer-edit.php', array( $this, 'bulk_admin_footer' ) ); 
  9.  
  10. public function loading() { 
  11. $form_id = isset( $this->args['form_id'] ) ? absint( $this->args['form_id'] ) : 0; 
  12.  
  13. if ( empty( $form_id ) ) { 
  14.  
  15. return array( 'complete' => true ); 
  16.  
  17. $sub_count = $this->get_sub_count( $form_id ); 
  18.  
  19. if( empty( $this->total_steps ) || $this->total_steps <= 1 ) { 
  20. $this->total_steps = round( ( $sub_count / 250 ), 0 ) + 2; 
  21.  
  22. $args = array( 
  23. 'total_steps' => $this->total_steps,  
  24. ); 
  25.  
  26. $this->args['filename'] = $this->random_filename( 'all-subs' ); 
  27. update_user_option( get_current_user_id(), 'nf_download_all_subs_filename', $this->args['filename'] ); 
  28. $this->redirect = esc_url_raw( add_query_arg( array( 'download_all' => $this->args['filename'] ), $this->args['redirect'] ) ); 
  29.  
  30. $this->loaded = true; 
  31. return $args; 
  32.  
  33. public function step() { 
  34. if( ! is_numeric( $this->args[ 'form_id' ] ) ) { 
  35. wp_die( __( 'Invalid form id', 'ninja-forms' ) ); 
  36.  
  37. $this->args[ 'filename' ] = wp_kses_post( $this->args[ 'filename' ] ); 
  38.  
  39. $exported_subs = get_user_option( get_current_user_id(), 'nf_download_all_subs_ids' ); 
  40. if ( ! is_array( $exported_subs ) ) { 
  41. $exported_subs = array(); 
  42.  
  43. $previous_name = get_user_option( get_current_user_id(), 'nf_download_all_subs_filename' ); 
  44. if ( $previous_name ) { 
  45. $this->args['filename'] = $previous_name; 
  46.  
  47. $args = array( 
  48. 'posts_per_page' => 250,  
  49. 'paged' => $this->step,  
  50. 'post_type' => 'nf_sub',  
  51. 'meta_query' => array( 
  52. array( 
  53. 'key' => '_form_id',  
  54. 'value' => $this->args['form_id'],  
  55. ),  
  56. ),  
  57. ); 
  58.  
  59. $subs_results = get_posts( $args ); 
  60.  
  61. if ( is_array( $subs_results ) && ! empty( $subs_results ) ) { 
  62. $upload_dir = wp_upload_dir(); 
  63. $file_path = trailingslashit( $upload_dir['path'] ) . $this->args['filename'] . '.csv'; 
  64. $myfile = fopen( $file_path, 'a' ) or die( 'Unable to open file!' ); 
  65. $x = 0; 
  66. $export = ''; 
  67. foreach ( $subs_results as $sub ) { 
  68. $sub_export = NF_Database_Models_Submission::export( $this->args['form_id'], array( $sub->ID ), TRUE ); 
  69. if ( $x > 0 || $this->step > 1 ) { 
  70. $sub_export = substr( $sub_export, strpos( $sub_export, "\n" ) + 1 ); 
  71. if ( ! in_array( $sub->ID, $exported_subs ) ) { 
  72. $export .= $sub_export; 
  73. $exported_subs[] = $sub->ID; 
  74. $x++; 
  75. fwrite( $myfile, $export ); 
  76. fclose( $myfile ); 
  77.  
  78. update_user_option( get_current_user_id(), 'nf_download_all_subs_ids', $exported_subs ); 
  79.  
  80. public function complete() { 
  81. delete_user_option( get_current_user_id(), 'nf_download_all_subs_ids' ); 
  82. delete_user_option( get_current_user_id(), 'nf_download_all_subs_filename' ); 
  83.  
  84. /** 
  85. * Add an integar to the end of our filename to make sure it is unique 
  86. * @access public 
  87. * @since 2.7.6 
  88. * @return $filename 
  89. */ 
  90. public function random_filename( $filename ) { 
  91. $upload_dir = wp_upload_dir(); 
  92. $file_path = trailingslashit( $upload_dir['path'] ) . $filename . '.csv'; 
  93. if ( file_exists ( $file_path ) ) { 
  94. for ($x = 0; $x < 999 ; $x++) { 
  95. $tmp_name = $filename . '-' . $x; 
  96. $tmp_path = trailingslashit( $upload_dir['path'] ); 
  97. if ( file_exists( $tmp_path . $tmp_name . '.csv' ) ) { 
  98. $this->random_filename( $tmp_name ); 
  99. break; 
  100. } else { 
  101. $this->filename = $tmp_name; 
  102. break; 
  103.  
  104. return $filename; 
  105.  
  106. public function bulk_admin_footer() { 
  107. global $post_type; 
  108.  
  109. if ( ! is_admin() ) 
  110. return false; 
  111.  
  112. if( $post_type == 'nf_sub' && isset ( $_REQUEST['post_status'] ) && $_REQUEST['post_status'] == 'all' ) { 
  113. ?> 
  114. <script type="text/javascript"> 
  115. jQuery(document).ready(function() { 
  116. jQuery('<option>').val('export').text('<?php _e('Export')?>').appendTo("select[name='action']"); 
  117. jQuery('<option>').val('export').text('<?php _e('Export')?>').appendTo("select[name='action2']"); 
  118. <?php 
  119. if ( ( isset ( $_POST['action'] ) && $_POST['action'] == 'export' ) || ( isset ( $_POST['action2'] ) && $_POST['action2'] == 'export' ) ) { 
  120. ?> 
  121. setInterval(function() { 
  122. jQuery( "select[name='action'" ).val( '-1' ); 
  123. jQuery( "select[name='action2'" ).val( '-1' ); 
  124. jQuery( '#posts-filter' ).submit(); 
  125. }, 5000); 
  126. <?php 
  127.  
  128. if ( isset ( $_REQUEST['form_id'] ) && ! empty ( $_REQUEST['form_id'] ) ) { 
  129. $redirect = urlencode( remove_query_arg( array( 'download_all', 'download_file' ) ) ); 
  130. $url = admin_url( 'admin.php?page=nf-processing&action=download_all_subs&form_id=' . absint( $_REQUEST['form_id'] ) . '&redirect=' . $redirect ); 
  131. $url = esc_url( $url ); 
  132. ?> 
  133. var button = '<a href="<?php echo $url; ?>" class="button-secondary nf-download-all"><?php echo __( 'Download All Submissions', 'ninja-forms' ); ?></a>'; 
  134. jQuery( '#doaction2' ).after( button ); 
  135. <?php 
  136.  
  137. if ( isset ( $_REQUEST['download_all'] ) && $_REQUEST['download_all'] != '' ) { 
  138. $redirect = esc_url_raw( add_query_arg( array( 'download_file' => esc_html( $_REQUEST['download_all'] ) ) ) ); 
  139. $redirect = remove_query_arg( array( 'download_all' ), $redirect ); 
  140. ?> 
  141. document.location.href = "<?php echo $redirect; ?>"; 
  142. <?php 
  143.  
  144. ?> 
  145. }); 
  146. </script> 
  147. <?php 
  148.  
  149. function get_sub_count( $form_id, $post_status = 'publish' ) { 
  150. global $wpdb; 
  151.  
  152. $meta_key = '_form_id'; 
  153. $meta_value = $form_id; 
  154.  
  155. $sql = "SELECT count(DISTINCT pm.post_id) 
  156. FROM $wpdb->postmeta pm 
  157. JOIN $wpdb->posts p ON (p.ID = pm.post_id) 
  158. WHERE pm.meta_key = %s 
  159. AND pm.meta_value = %s 
  160. AND p.post_type = 'nf_sub' 
  161. AND p.post_status = %s"; 
  162.  
  163. $count = $wpdb->get_var( $wpdb->prepare( $sql, $meta_key, $meta_value, $post_status ) ); 
  164.  
  165. return $count; 
  166.