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