DownloadAllSubmissions.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * This module utilizes deprecated code, which should eventually be replaced.
  4. */
  5. require_once Ninja_Forms::$dir . 'lib/StepProcessing/step-processing.php';
  6. class NF_Admin_CPT_DownloadAllSubmissions extends NF_Step_Processing {
  7. function __construct() {
  8. $this->action = 'download_all_subs';
  9. parent::__construct();
  10. add_action( 'admin_footer-edit.php', array( $this, 'bulk_admin_footer' ) );
  11. }
  12. public function loading() {
  13. $subs_per_step = apply_filters( 'ninja_forms_export_subs_per_step', 10 );
  14. $form_id = isset( $this->args['form_id'] ) ? absint( $this->args['form_id'] ) : 0;
  15. if ( empty( $form_id ) ) {
  16. return array( 'complete' => true );
  17. }
  18. $sub_count = $this->get_sub_count( $form_id );
  19. if( empty( $this->total_steps ) || $this->total_steps <= 1 ) {
  20. $this->total_steps = round( ( $sub_count / $subs_per_step ), 0 ) + 2;
  21. }
  22. $args = array(
  23. 'total_steps' => $this->total_steps,
  24. );
  25. $this->args['filename'] = $this->random_filename( 'all-subs' );
  26. update_user_option( get_current_user_id(), 'nf_download_all_subs_filename', $this->args['filename'] );
  27. $this->redirect = esc_url_raw( add_query_arg( array( 'download_all' => $this->args['filename'] ), $this->args['redirect'] ) );
  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. $subs_per_step = apply_filters( 'ninja_forms_export_subs_per_step', 10 );
  36. $this->args[ 'filename' ] = wp_kses_post( $this->args[ 'filename' ] );
  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' => $subs_per_step,
  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. $subs_results = get_posts( $args );
  57. if ( is_array( $subs_results ) && ! empty( $subs_results ) ) {
  58. $upload_dir = wp_upload_dir();
  59. $file_path = trailingslashit( $upload_dir['path'] ) . $this->args['filename'] . '.csv';
  60. $myfile = fopen( $file_path, 'a' ) or die( 'Unable to open file!' );
  61. $x = 0;
  62. $export = '';
  63. $sub_ids = array();
  64. foreach( $subs_results as $result ){
  65. $sub_ids[] = $result->ID;
  66. }
  67. $export .= NF_Database_Models_Submission::export( $this->args['form_id'], $sub_ids, TRUE );
  68. if( 1 < $this->step ) {
  69. $export = substr( $export, strpos( $export, PHP_EOL ) + 1 );
  70. }
  71. fwrite( $myfile, $export );
  72. fclose( $myfile );
  73. }
  74. update_user_option( get_current_user_id(), 'nf_download_all_subs_ids', $exported_subs );
  75. }
  76. public function complete() {
  77. delete_user_option( get_current_user_id(), 'nf_download_all_subs_ids' );
  78. delete_user_option( get_current_user_id(), 'nf_download_all_subs_filename' );
  79. }
  80. /**
  81. * Add an integar to the end of our filename to make sure it is unique
  82. *
  83. * @access public
  84. * @since 2.7.6
  85. * @return $filename
  86. */
  87. public function random_filename( $filename ) {
  88. $upload_dir = wp_upload_dir();
  89. $file_path = trailingslashit( $upload_dir['path'] ) . $filename . '.csv';
  90. if ( file_exists ( $file_path ) ) {
  91. for ($x = 0; $x < 999 ; $x++) {
  92. $tmp_name = $filename . '-' . $x;
  93. $tmp_path = trailingslashit( $upload_dir['path'] );
  94. if ( file_exists( $tmp_path . $tmp_name . '.csv' ) ) {
  95. $this->random_filename( $tmp_name );
  96. break;
  97. } else {
  98. $this->filename = $tmp_name;
  99. break;
  100. }
  101. }
  102. }
  103. return $filename;
  104. }
  105. public function bulk_admin_footer() {
  106. global $post_type;
  107. if ( ! is_admin() )
  108. return false;
  109. if( $post_type == 'nf_sub' && isset ( $_REQUEST['post_status'] ) && $_REQUEST['post_status'] == 'all' ) {
  110. ?>
  111. <script type="text/javascript">
  112. jQuery(document).ready(function() {
  113. <?php
  114. if ( ( isset ( $_POST['action'] ) && $_POST['action'] == 'export' ) || ( isset ( $_POST['action2'] ) && $_POST['action2'] == 'export' ) ) {
  115. ?>
  116. setInterval(function(){
  117. jQuery( "select[name='action'" ).val( '-1' );
  118. jQuery( "select[name='action2'" ).val( '-1' );
  119. jQuery( '#posts-filter' ).submit();
  120. },5000);
  121. <?php
  122. }
  123. if ( isset ( $_REQUEST['form_id'] ) && ! empty ( $_REQUEST['form_id'] ) ) {
  124. $redirect = urlencode( remove_query_arg( array( 'download_all', 'download_file' ) ) );
  125. $url = admin_url( 'admin.php?page=nf-processing&action=download_all_subs&form_id=' . absint( $_REQUEST['form_id'] ) . '&redirect=' . $redirect );
  126. $url = esc_url( apply_filters( 'ninja_forms_download_all_submissions_url', $url, absint( $_REQUEST['form_id'] ) ) );
  127. ?>
  128. var button = '<a href="<?php echo $url; ?>" class="button-secondary nf-download-all"><?php echo __( 'Download All Submissions', 'ninja-forms' ); ?></a>';
  129. jQuery( '#doaction2' ).after( button );
  130. <?php
  131. }
  132. if ( isset ( $_REQUEST['download_all'] ) && $_REQUEST['download_all'] != '' ) {
  133. $redirect = esc_url_raw( add_query_arg( array( 'download_file' => esc_html( $_REQUEST['download_all'] ) ) ) );
  134. $redirect = remove_query_arg( array( 'download_all' ), $redirect );
  135. ?>
  136. document.location.href = "<?php echo $redirect; ?>";
  137. <?php
  138. }
  139. ?>
  140. });
  141. </script>
  142. <?php
  143. }
  144. }
  145. function get_sub_count( $form_id, $post_status = 'publish' ) {
  146. global $wpdb;
  147. $meta_key = '_form_id';
  148. $meta_value = $form_id;
  149. $sql = "SELECT count(DISTINCT pm.post_id)
  150. FROM $wpdb->postmeta pm
  151. JOIN $wpdb->posts p ON (p.ID = pm.post_id)
  152. WHERE pm.meta_key = %s
  153. AND pm.meta_value = %s
  154. AND p.post_type = 'nf_sub'
  155. AND p.post_status = %s";
  156. $count = $wpdb->get_var( $wpdb->prepare( $sql, $meta_key, $meta_value, $post_status ) );
  157. return $count;
  158. }
  159. }