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