This repository has been archived by the owner on Jan 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass-advanced-cf7-db-export-scheduler.php
executable file
·345 lines (260 loc) · 11.7 KB
/
class-advanced-cf7-db-export-scheduler.php
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
/**
* The file that defines the core plugin class
*
* A class definition that includes attributes and functions used across both the
* public-facing side of the site and the admin area.
*
* @link khorshidlab.com
* @since 1.0.0
* @package advanced_cf7_db_export_scheduler
*/
class Advanced_Cf7_Db_Export_Scheduler {
public function __construct() {
global $wpdb;
$this->plugin_name = 'Advanced CF7 DB Export Scheduler';
$this->version = '1.0.1';
$this->wpdb = $wpdb;
$this->separator = ",";
$this->table = $this->wpdb->prefix . 'cf7_vdata_entry';
$this->define_admin_hooks();
}
/**
* Register all of the hooks related to the admin area functionality
* of the plugin.
*
* @since 1.0.0
* @access private
*/
private function define_admin_hooks() {
add_action( 'admin_menu', [ $this, 'add_submenu_to_advanced_cf7_db_menu' ] );
add_action( 'cf7_db_schedule_report_hook', [ $this, 'cf7_db_schedule_report_exec' ] );
add_action( 'admin_init', [ $this, 'save_schedule_report_form' ] );
add_action( 'admin_init', [ $this, 'send_report_to_email' ] );
add_action( 'plugins_loaded', $this, 'load_plugin_textdomain' );
add_filter( 'cron_schedules', [ $this, 'add_cron_interval' ] );
}
public function load_plugin_textdomain() {
load_plugin_textdomain(
'kh-acsr',
false,
dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages/'
);
}
public function add_submenu_to_advanced_cf7_db_menu() {
add_submenu_page(
'contact-form-listing',
__( 'Forms Schedule Reports Settings', 'kh-acsr' ),
__( 'Forms Schedule Reports Settings', 'kh-acsr' ),
'manage_options',
'export_forms_settings',
[ $this, 'export_forms_settings_content' ],
);
}
public function export_forms_settings_content() {
$forms = get_posts( [ 'post_type' => 'wpcf7_contact_form', 'numberposts' => -1 ] );
$reports_options = get_option( 'kh-schedule-reports-options' );
?>
<div class="wrap">
<H1><?php _e( 'Forms Schedule Reports Settings', 'kh-acsr' ) ?></H1>
<hr>
<h3><?php _e( 'Select your forms', 'kh-acsr' ) ?></h3>
<form method="post">
<?php
if( $forms ) {
foreach( $forms as $form ) {
?>
<p>
<input id="schedule-report-form-id-<?php echo $form->ID ?>" name="schedule-report-form-id[]" value="<?php echo $form->ID ?>" type="checkbox" <?php echo isset( $reports_options['schedule-report-form-id'] ) && in_array( $form->ID, $reports_options['schedule-report-form-id'] ) ? 'checked="checked"' : '' ?> />
<label for="schedule-report-form-id-<?php echo $form->ID ?>"><?php echo $form->post_title ?></label>
</p>
<?php
}
}
?>
<br>
<h3><?php _e( 'Report Settings', 'kh-acsr' ) ?></h3>
<p>
<input id="kh-schedule-report-send-email" name="send-email" value="1" type="checkbox" <?php echo isset( $reports_options['send-email'] ) ? 'checked="checked"' : '' ?> />
<label for="kh-schedule-report-send-email"><?php _e( 'Send exported file to email after the report completed', 'kh-acsr' ) ?></label>
</p>
<p>
<input id="kh-schedule-report-remove-db-data" name="remove-db-data" value="1" type="checkbox" <?php echo isset( $reports_options['remove-db-data'] ) ? 'checked="checked"' : '' ?> />
<label for="kh-schedule-report-remove-db-data"><?php _e( 'Remove Advanced CF7 data rows from database after the report completed', 'kh-acsr' ) ?></label>
</p>
<p>
<label for="kh-schedule-report-interval"><?php _e( 'Enter schedule report intervals in days', 'kh-acsr' ) ?></label>
<input id="kh-schedule-report-interval" name="report-interval" value="<?php echo isset( $reports_options['report-interval'] ) ? $reports_options['report-interval'] : '30' ?>" type="number" />
</p>
<p><input type="submit" name="schedule-report-form-submit" id="submit" class="button button-primary" value="<?php _e( 'Save', 'kh-acsr' ) ?>"></p>
</form>
<br>
<hr>
<h3><?php _e( 'Send Report to Email', 'kh-acsr' ) ?></h3>
<p><?php _e( 'You can send selected forms reports to your email immediately. Before that, make sure you have been saved your selected forms.', 'kh-acsr' ) ?></p>
<form method="post">
<input type="email" name="email" placeholder="<?php _e( 'Enter your email address', 'kh-acsr' ) ?>">
<input type="submit" name="schedule-report-send-to-email" class="button button-primary" value="<?php _e( 'Send Email', 'kh-acsr' ) ?>">
</form>
</div>
<?php
}
public function save_schedule_report_form() {
if( isset( $_POST['schedule-report-form-submit'] ) ) {
$options = [];
foreach( $_POST as $k => $v ) {
if( $k != 'schedule-report-form-submit' ) {
$options[ $k ] = $v;
}
}
update_option( 'kh-schedule-reports-options', $options );
if( isset( $options['report-interval'] ) ) {
$this->setup_schedule_report_cron();
}
}
}
public function cf7_db_schedule_report_exec() {
// Check for current user privileges
if( !current_user_can( 'manage_options' ) ) {
return false;
}
// Check if we are in WP-Admin
if( !is_admin() ) {
return false;
}
$reports_options = get_option( 'kh-schedule-reports-options' );
$csv_output = $this->generate_csv();
if( isset( $reports_options['send-email'] ) ) {
$send_email = $this->send_output_csv_to_admin_email( $csv_output );
if( $send_email ) {
if( isset( $reports_options['remove-db-data'] ) ) {
$this->remove_advanced_cf7_db_data();
}
}
}
}
public static function setup_schedule_report_cron() {
wp_schedule_event( time(), 'kh_custom_interval', 'cf7_db_schedule_report_hook' );
}
public static function unset_schedule_report_cron() {
$timestamp = wp_next_scheduled( 'cf7_db_schedule_report_hook' );
wp_unschedule_event( $timestamp, 'cf7_db_schedule_report_hook' );
}
public function add_cron_interval( $schedules ) {
$reports_options = get_option( 'kh-schedule-reports-options' );
$report_interval = isset( $reports_options['report-interval'] ) ? $reports_options['report-interval'] : '30';
$schedules['kh_custom_interval'] = [
'interval' => $report_interval * DAY_IN_SECONDS,
'display' => esc_html__( 'CF7 Schedule Report Custom Interval' )
];
return $schedules;
}
/**
* Fetch results from table and return a string containing all the data
*
* @return string
*/
public function generate_csv() {
ob_start();
$csv_output = '';
$csv_output .= $this->get_columns();
$csv_output .= "\n";
$csv_output .= $this->get_data();
$filename = "advanced_cf7_db_export_scheduler_" . date_i18n( 'Y-m-d@H-i', current_time('timestamp') ) . ".csv";
$upload = wp_upload_dir();
$upload_dir = $upload['basedir'];
$upload_dir = $upload_dir . '/khorshid-exports/';
if ( !is_dir( $upload_dir ) ) {
wp_mkdir_p( $upload_dir );
}
$fh = fopen( $upload_dir . $filename, 'w' );
if( $fh === FALSE ) {
die('Failed to open temporary file');
}
fprintf( $fh, chr(0xEF) . chr(0xBB) . chr(0xBF) ); // UTF-8
fwrite( $fh, $csv_output );
rewind( $fh );
fclose( $fh );
ob_end_flush();
return $upload_dir . $filename;
}
public function send_output_csv_to_admin_email( $attachment_path, $to = null ) {
require ABSPATH . 'wp-includes/pluggable.php';
$date = date_i18n( 'j F Y', current_time( 'timestamp' ) ) . ' @ ' . date_i18n( 'H:i:s', current_time( 'timestamp' ) );
$to = $to == null ? get_option('admin_email') : $to;
$body = __( 'Please find the exported file of Advanced CF7 schedule reports attached to this email.', 'kh-acsr' ) . ' <br>';
$subject = __( 'Advanced CF7 Export - Date: ', 'kh-acsr' ) . $date;
$body .= __( '<br> Report Date: ', 'kh-acsr' ) . $date;
$headers = [ 'Content-Type: text/html; charset=UTF-8' ];
$send = wp_mail( $to, $subject, $body, $headers, $attachment_path );
return $send;
}
public function remove_advanced_cf7_db_data() {
$this->wpdb->query( "TRUNCATE TABLE `" . $this->table . "`" );
$this->wpdb->query( "TRUNCATE TABLE `wp_cf7_vdata`" );
}
/**
* Get table columns as a string separated by $this->separator;
*
* @return string
*/
public function get_columns() {
$output = "";
$query = "SHOW COLUMNS FROM `" . $this->table . "`";
$result = $this->wpdb->get_results( $query );
if ( count($result) > 0 ) {
foreach ( $result as $row ) {
$output = $output . $row->Field . $this->separator;
}
$output = substr( $output, 0, -1 );
} else {
$output = __( 'No form found!', 'kh-acsr' );
}
return $output;
}
/**
* Get table data as a string separated by $this->separator
*
* @return string
*/
public function get_data() {
$reports_options = get_option( 'kh-schedule-reports-options' );
$output = "";
$ids = implode( ',', $reports_options['schedule-report-form-id'] );
$query = "SELECT * FROM `" . $this->table . "` WHERE cf7_id IN(" . $ids . ")";
$values = $this->wpdb->get_results( $query );
if( $values ) {
foreach ( $values as $rowr ) {
$fields = array_values( (array)$rowr );
$output .= implode( $this->separator, $fields );
$output .= "\n";
}
} else {
$output = __( 'No form found!', 'kh-acsr' );
}
return $output;
}
public function send_report_to_email() {
if( isset( $_POST['schedule-report-send-to-email'] ) ) {
$csv_output = $this->generate_csv();
$send_email = $this->send_output_csv_to_admin_email( $csv_output, $_POST['email'] );
if( $send_email ) {
add_action( 'admin_notices', function () {
?>
<div class="notice notice-success is-dismissible">
<p><?php _e( 'The report email sent successfully!', 'sample-text-domain' ); ?></p>
</div>
<?php
} );
} else {
add_action( 'admin_notices', function () {
?>
<div class="notice notice-error is-dismissible">
<p><?php _e( 'An error occurred while sending the report email!', 'sample-text-domain' ); ?></p>
</div>
<?php
} );
}
}
}
}