PluginProbe ʕ •ᴥ•ʔ
Check & Log Email – Easy Email Testing & Mail logging / 1.0.13
Check & Log Email – Easy Email Testing & Mail logging v1.0.13
1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 2.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.13.1 2.0.13.2 2.0.14 2.0.2 2.0.3 2.0.4 2.0.5 2.0.5.1 2.0.6 2.0.7 2.0.8 2.0.9 trunk 0.5.7 0.6.0 0.6.1 0.6.2 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.12.1 1.0.13 1.0.13.1 1.0.2 1.0.3
check-email / include / Core / Check_Email_Export_Log.php
check-email / include / Core Last commit date
DB 2 years ago Request 2 years ago UI 2 years ago Check_Email_Admin_Capability_Giver.php 2 years ago Check_Email_Export_Log.php 2 years ago Check_Email_From_Handler.php 2 years ago Check_Email_Log.php 2 years ago Check_Email_Logger.php 2 years ago Check_Email_Review.php 2 years ago Loadie.php 2 years ago
Check_Email_Export_Log.php
449 lines
1 <?php namespace CheckEmail\Core;
2
3 defined( 'ABSPATH' ) || exit; // Exit if accessed directly
4
5 /**
6 * Export log data into CSV file
7 * @since 1.0.11
8 */
9 class Check_Email_Export_Log {
10
11 private $separator;
12
13 public function __construct() {
14
15 $this->separator = ',';
16
17 add_action('wp_ajax_ck_mail_export_logs', array($this, 'ck_mail_export_logs'));
18 add_action('wp_ajax_ck_email_export_filter_popup', array($this, 'ck_email_export_filter_popup'));
19 }
20
21 /**
22 * Export email logs to csv file
23 * @since 1.0.11
24 * */
25 public function ck_mail_export_logs(){
26
27 if(!isset($_GET['ck_mail_export_nonce'])){
28 wp_die( -1 );
29 }
30
31 if ( !wp_verify_nonce( $_GET['ck_mail_export_nonce'], 'ck_mail_ajax_check_nonce' ) ){
32 wp_die( -1 );
33 }
34
35 if ( ! current_user_can( 'manage_check_email' ) ) {
36 wp_die( -1 );
37 }
38
39 $file_format = 'csv';
40 $file_name = 'email_logs.csv';
41
42 if(isset($_GET['export_type']) && !empty($_GET['export_type'])){
43 $file_format = sanitize_text_field($_GET['export_type']);
44 }
45
46 switch($file_format){
47 case 'csv':
48 $this->separator = ',';
49 $file_name = 'email_logs.csv';
50 header("Content-type: application/csv");
51 break;
52
53 case 'xls':
54 $this->separator = "\t";
55 $file_name = 'email_logs.xls';
56 header('Content-Type: application/vnd.ms-excel');
57 break;
58
59 case 'xlsx':
60 $this->separator = "\t";
61 $file_name = 'email_logs.xlsx';
62 header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
63 break;
64
65 case 'txt':
66 $this->separator = "\t";
67 $file_name = 'email_logs.txt';
68 header('Content-Type: text/plain');
69 break;
70
71 default:
72 $this->separator = ',';
73 $file_name = 'email_logs.csv';
74 header('Content-type: application/csv');
75 break;
76 }
77
78 header("Content-disposition: attachment; filename=\"$file_name\"");
79
80 $fields = array();
81 if(isset($_GET['common_information']) && is_array($_GET['common_information'])){
82 $fields = array_map('sanitize_text_field',$_GET['common_information']);
83 }
84
85 $status = 'All';
86 if(isset($_GET['export_status']) && !empty($_GET['export_status'])){
87 $status = sanitize_text_field($_GET['export_status']);
88 }
89
90 $export_date = 'all';
91 if(isset($_GET['export_date']) && !empty($_GET['export_date'])){
92 $export_date = sanitize_text_field($_GET['export_date']);
93 }
94
95 $from_date = date('Y-m-d 00:00:00');
96 $to_date = date('Y-m-d 23:59:59');
97
98 if($export_date == 'custom'){
99 if(isset($_GET['ck_mail_exp_from_date']) && !empty($_GET['ck_mail_exp_from_date'])){
100 $from_date = date('Y-m-d 00:00:00', strtotime(sanitize_text_field($_GET['ck_mail_exp_from_date'])));
101 }
102 if(isset($_GET['ck_mail_exp_to_date']) && !empty($_GET['ck_mail_exp_to_date'])){
103 $to_date = date('Y-m-d 23:59:59', strtotime(sanitize_text_field($_GET['ck_mail_exp_to_date'])));
104 }
105 }
106
107 $export_recipient = '';
108 if(isset($_GET['export_recipient']) && !empty($_GET['export_recipient'])){
109 $export_recipient = sanitize_text_field($_GET['export_recipient']);
110 }
111
112
113 if(!empty($fields)){
114 $logs = $this->ck_mail_generate_csv($fields, $status, $export_date, $from_date, $to_date, $export_recipient, $file_format);
115 echo $logs;
116 }
117
118 wp_die();
119 }
120
121 /**
122 * Generate email log
123 * @since 1.0.11
124 * */
125 public function ck_mail_generate_csv($fields, $status, $export_date, $from_date, $to_date, $export_recipient, $file_format){
126 global $wpdb;
127
128 $table_name = $wpdb->prefix.'check_email_log';
129
130 $query = $wpdb->prepare("SELECT * FROM $table_name");
131 if($status == 'All' && $export_date == 'all'){
132 if(!empty($export_recipient)){
133 $query = $wpdb->prepare("SELECT * FROM $table_name WHERE to_email = %s", $export_recipient);
134 }else{
135 $query = $wpdb->prepare("SELECT * FROM $table_name");
136 }
137 }else if($status == 'Success' && $export_date == 'all'){
138 if(!empty($export_recipient)){
139 $query = $wpdb->prepare("SELECT * FROM $table_name WHERE (error_message = %s OR error_message IS NULL) AND to_email = %s", $status, $export_recipient);
140 }else{
141 $query = $wpdb->prepare("SELECT * FROM $table_name WHERE error_message = %s OR error_message IS NULL", $status);
142 }
143 }else if($status == 'Fail' && $export_date == 'all'){
144 if(!empty($export_recipient)){
145 $query = $wpdb->prepare("SELECT * FROM $table_name WHERE error_message != %s AND to_email = %s", 'Success', $export_recipient);
146 }else{
147 $query = $wpdb->prepare("SELECT * FROM $table_name WHERE error_message != %s", 'Success');
148 }
149 }else if($status == 'All' && $export_date == 'custom'){
150 if(!empty($export_recipient)){
151 $query = $query = $wpdb->prepare("SELECT * FROM $table_name WHERE to_email = %s AND sent_date BETWEEN %s AND %s", $export_recipient, $from_date, $to_date);
152 }else{
153 $query = $query = $wpdb->prepare("SELECT * FROM $table_name WHERE sent_date BETWEEN %s AND %s", $from_date, $to_date);
154 }
155 }else if($status == 'Success' && $export_date == 'custom'){
156 if(!empty($export_recipient)){
157 $query = $wpdb->prepare("SELECT * FROM $table_name WHERE (error_message = %s OR error_message IS NULL) AND to_email = %s AND sent_date BETWEEN %s AND %s", $status, $export_recipient, $from_date, $to_date);
158 }else{
159 $query = $wpdb->prepare("SELECT * FROM $table_name WHERE (error_message = %s OR error_message IS NULL) AND sent_date BETWEEN %s AND %s", $status, $from_date, $to_date);
160 }
161 }else if($status == 'Fail' && $export_date == 'custom'){
162 if(!empty($export_recipient)){
163 $query = $wpdb->prepare("SELECT * FROM $table_name WHERE error_message != %s AND to_email = %s AND sent_date BETWEEN %s AND %s", 'Success', $export_recipient, $from_date, $to_date);
164 }else{
165 $query = $wpdb->prepare("SELECT * FROM $table_name WHERE error_message != %s AND sent_date BETWEEN %s AND %s", 'Success', $from_date, $to_date);
166 }
167 }
168
169 $results = $wpdb->get_results($query, ARRAY_A);
170
171 $logs_data = '';
172 $csv_headings = array('Sr No');
173 $csv_headings = array_merge($csv_headings, $fields);
174
175 $logs_data = implode($this->separator, $csv_headings);
176 if($file_format == 'txt'){
177 $logs_data = implode("\t \t", $csv_headings);
178 }
179 $logs_data .= "\n";
180
181 if( !empty($results) && is_array($results) && !empty($results) ){
182 $log_cnt = 1;
183 foreach ( $results as $l_key => $l_value ) {
184 if( !empty($l_value) && is_array($l_value) ){
185
186 $logs_data .= $log_cnt.$this->separator;
187
188 if(in_array("From", $csv_headings)){
189 $from = '';
190 if ( function_exists('imap_rfc822_parse_headers' ) ) {
191 $headers = imap_rfc822_parse_headers($l_value['headers']);
192
193 if (isset($headers->fromaddress) && !empty($headers->fromaddress)) {
194 $from = $headers->fromaddress;
195 $from = apply_filters( 'check_email_log_list_column_from_email', esc_html( $from ) );
196 }
197 }else {
198 $find_from = substr($l_value['headers'], strpos($l_value['headers'], 'From') + 5 );
199
200 if(!empty($find_from) && is_string($find_from)){
201 $find_from = explode("\n", $find_from);
202 if(is_array($find_from) && isset($find_from[1])){
203 $from = $find_from[1];
204 }
205 }
206 }
207
208 $logs_data .= $from.$this->separator;
209 }
210
211 if(in_array("To", $csv_headings)){
212 $logs_data .= $l_value['to_email'].$this->separator;
213 }
214
215 if(in_array("Subject", $csv_headings)){
216 $logs_data .= $l_value['subject'].$this->separator;
217 }
218
219 if(in_array("Message", $csv_headings)){
220 $message = str_replace(',', '', $l_value['message']);
221 $message = preg_replace('~[\r\n\t]+~', '', $message);
222 $logs_data .= $message.$this->separator;
223 }
224
225 if(in_array("Sent At", $csv_headings)){
226 $logs_data .= date('d-m-Y H:i:s', strtotime($l_value['sent_date'])).$this->separator;
227 }
228 if(in_array("Status", $csv_headings)){
229 $logs_data .= empty($l_value['error_message'])?'Success':$l_value['error_message'];
230 }
231
232 $log_cnt++;
233
234 $logs_data .= " \n ";
235 }
236 }
237 }
238 return $logs_data;
239 }
240
241 /**
242 * Template for email log options
243 * @since 1.0.11
244 * */
245 public function ck_email_export_filter_popup(){
246 if ( ! current_user_can( 'manage_check_email' ) ) {
247 wp_die( -1 );
248 }
249
250 if ( ! isset( $_GET['ck_mail_security_nonce'] ) ){
251 wp_die( '-1' );
252 }
253
254 if ( !wp_verify_nonce( $_GET['ck_mail_security_nonce'], 'ck_mail_ajax_check_nonce' ) ){
255 wp_die( '-1' );
256 }
257 ?>
258
259 <div id="ck-mail-elog-options">
260 <form id="ck-mail-export-form" type="GET" action="<?php echo esc_url(admin_url('admin-ajax.php')); ?>">
261 <div class="ck-mail-exp-row">
262 <div class="ck-mail-exp-col">
263 <div id="ck-mail-export-type">
264 <div class="ck-mail-logs-heading-wrapper">
265 <h2 class="ck-mail-export-h2"> <?php esc_html_e('File Format', 'check-email'); ?> </h2>
266 </div>
267
268 <div class="ck-mail-logs-contents">
269 <div class="ck-mail-log-exp-type ck-mail-export-options">
270 <label for="ck-mail-export-csv">
271 <input type="radio" name="export_type" class="ck-mail-export-type" id="ck-mail-export-csv" value="csv" checked>
272 <?php esc_html_e('Export in CSV (.csv)', 'check-email'); ?>
273 </label>
274 </div>
275
276 <div class="ck-mail-log-exp-type ck-mail-export-options">
277 <label for="ck-mail-export-xls">
278 <input type="radio" name="export_type" class="ck-mail-export-type" id="ck-mail-export-xls" value="xls">
279 <?php esc_html_e('Export in Microsoft Excel (.xls)', 'check-email'); ?>
280 </label>
281 </div>
282
283 <div class="ck-mail-log-exp-type ck-mail-export-options">
284 <label for="ck-mail-export-xlsx ck-mail-export-options">
285 <input type="radio" name="export_type" class="ck-mail-export-type" id="ck-mail-export-xlsx" value="xlsx">
286 <?php esc_html_e('Export in Microsoft Excel (.xlsx)', 'check-email'); ?>
287 </label>
288 </div>
289
290 <div class="ck-mail-log-exp-type ck-mail-export-options">
291 <label for="ck-mail-export-txt">
292 <input type="radio" name="export_type" class="ck-mail-export-type" id="ck-mail-export-txt" value="txt">
293 <?php esc_html_e('Export in Text (.txt)', 'check-email'); ?>
294 </label>
295 </div>
296
297 </div>
298 </div>
299 </div>
300
301 <div class="ck-mail-exp-col">
302 <div id="ck-mail-export-common-info">
303 <div class="ck-mail-logs-heading-wrapper">
304 <h2 class="ck-mail-export-h2"> <?php esc_html_e('Fields', 'check-email'); ?> </h2>
305 <p class="ck-mail-exp-error ck-mail-d-none" id="ck-mail-fields-error" style="color: red;"></p>
306 </div>
307
308 <div class="ck-mail-logs-contents">
309 <div class="ck-mail-log-exp-comm-info ck-mail-export-options">
310 <label for="ck-mail-comm-info-from">
311 <input type="checkbox" name="common_information[From]" class="ck-mail-comm-info-chk" id="ck-mail-comm-info-from" value="From" checked>
312 <?php esc_html_e('From', 'check-email'); ?>
313 </label>
314 </div>
315
316 <div class="ck-mail-log-exp-comm-info ck-mail-export-options">
317 <label for="ck-mail-comm-info-to">
318 <input type="checkbox" name="common_information[To]" class="ck-mail-comm-info-chk" id="ck-mail-comm-info-to" value="To" checked>
319 <?php esc_html_e('To', 'check-email'); ?>
320 </label>
321 </div>
322
323 <div class="ck-mail-log-exp-comm-info ck-mail-export-options">
324 <label for="ck-mail-comm-info-subject">
325 <input type="checkbox" name="common_information[Subject]" class="ck-mail-comm-info-chk" id="ck-mail-comm-info-subject" value="Subject" checked>
326 <?php esc_html_e('Subject', 'check-email'); ?>
327 </label>
328 </div>
329
330 <div class="ck-mail-log-exp-comm-info ck-mail-export-options">
331 <label for="ck-mail-comm-info-msg">
332 <input type="checkbox" name="common_information[Message]" class="ck-mail-comm-info-chk" id="ck-mail-comm-info-msg" value="Message" checked>
333 <?php esc_html_e('Message', 'check-email'); ?>
334 </label>
335 </div>
336
337 <div class="ck-mail-log-exp-comm-info ck-mail-export-options">
338 <label for="ck-mail-comm-info-date">
339 <input type="checkbox" name="common_information[Sent-At]" class="ck-mail-comm-info-chk" id="ck-mail-comm-info-date" value="Sent At" checked>
340 <?php esc_html_e('Sent At', 'check-email'); ?>
341 </label>
342 </div>
343
344 <div class="ck-mail-log-exp-comm-info ck-mail-export-options">
345 <label for="ck-mail-comm-info-status">
346 <input type="checkbox" name="common_information[Status]" class="ck-mail-comm-info-chk" id="ck-mail-comm-info-status" value="Status" checked>
347 <?php esc_html_e('Status', 'check-email'); ?>
348 </label>
349 </div>
350 </div>
351 </div>
352 </div>
353 </div> <!-- ck-mail-exp-row div end -->
354
355 <div class="ck-mail-exp-row">
356 <div class="ck-mail-exp-col">
357 <div id="ck-mail-export-by-status">
358 <div class="ck-mail-logs-heading-wrapper">
359 <h2> <?php esc_html_e('Status', 'check-email'); ?> </h2>
360 </div>
361
362 <div class="ck-mail-logs-contents">
363 <div class="ck-mail-log-exp-status ck-mail-export-options">
364 <label for="ck-mail-exp-status-all">
365 <input type="radio" name="export_status" class="ck-mail-exp-status-radio" id="ck-mail-exp-status-all" value="All" checked>
366 <?php esc_html_e('All', 'check-email'); ?>
367 </label>
368 </div>
369
370 <div class="ck-mail-log-exp-status ck-mail-export-options">
371 <label for="ck-mail-exp-status-success">
372 <input type="radio" name="export_status" class="ck-mail-exp-status-radio" id="ck-mail-exp-status-success" value="Success">
373 <?php esc_html_e('Success', 'check-email'); ?>
374 </label>
375 </div>
376
377 <div class="ck-mail-log-exp-status ck-mail-export-options">
378 <label for="ck-mail-exp-status-fail">
379 <input type="radio" name="export_status" class="ck-mail-exp-status-radio" id="ck-mail-exp-status-fail" value="Fail">
380 <?php esc_html_e('Failure', 'check-email'); ?>
381 </label>
382 </div>
383
384 </div>
385 </div>
386 </div>
387
388 <div class="ck-mail-exp-col">
389 <div id="ck-mail-export-by-rec">
390 <div class="ck-mail-logs-heading-wrapper">
391 <h2> <?php esc_html_e('Recipient', 'check-email'); ?> </h2>
392 </div>
393
394 <div class="ck-mail-logs-contents">
395 <div class="ck-mail-log-exp-recipient ck-mail-export-options">
396 <label for="ck-mail-export-recipient"> <?php esc_html_e('Enter Email id', 'check-email'); ?> </label>
397 <input type="text" name="export_recipient" class="ck-mail-export-recipient" id="ck-mail-export-recipient" placeholder="Enter Recipient Email">
398 </div>
399 </div>
400 </div>
401 </div>
402 </div> <!-- ck-mail-exp-row div end -->
403
404 <div style="clear: both;"></div>
405
406 <div class="ck-mail-exp-row">
407 <div class="ck-mail-exp-col">
408 <div id="ck-mail-export-by-date">
409 <div class="ck-mail-logs-heading-wrapper">
410 <h2> <?php esc_html_e('Date Range', 'check-email'); ?> </h2>
411 </div>
412
413 <div class="ck-mail-logs-contents">
414 <div class="ck-mail-log-exp-date ck-mail-export-options">
415 <label for="ck-mail-exp-date-all">
416 <input type="radio" name="export_date" class="ck-mail-exp-date-radio" id="ck-mail-exp-date-all" value="all" checked>
417 <?php esc_html_e('All', 'check-email'); ?>
418 </label>
419 </div>
420
421 <div class="ck-mail-log-exp-date ck-mail-export-options">
422 <label for="ck-mail-exp-date-custom">
423 <input type="radio" name="export_date" class="ck-mail-exp-date-radio" id="ck-mail-exp-date-custom" value="custom">
424 <?php esc_html_e('Custom', 'check-email'); ?>
425 </label>
426 <p class="ck-mail-exp-error ck-mail-d-none" id="ck-mail-exp-date-error"></p>
427 <div id="ck-mail-exp-c-date-wrapper" class="ck-mail-d-none">
428 <input type="search" id="ck-mail-exp-from-date" name="ck_mail_exp_from_date" value="<?php echo esc_attr(date('Y-m-d')); ?>" placeholder="<?php esc_attr_e( 'From Date', 'check-email' ); ?>" readonly />
429 <input type="search" id="ck-mail-exp-to-date" name="ck_mail_exp_to_date" value="<?php echo esc_attr(date('Y-m-d')); ?>" placeholder="<?php esc_attr_e( 'To Date', 'check-email' ); ?>" readonly />
430 </div>
431 </div>
432 </div>
433
434 </div>
435 </div>
436 </div> <!-- ck-mail-exp-row div end -->
437 <div style="clear: both;"></div>
438 <input type="hidden" name="ck_mail_export_nonce" value="<?php echo wp_create_nonce('ck_mail_ajax_check_nonce'); ?>">
439 <input type="hidden" name="action" value="ck_mail_export_logs">
440 <button type="button" class="button-primary button" id="ck-mail-export-logs-btn"> <?php esc_html_e('Export Logs', 'check-email'); ?> </button>
441 </form>
442 </div>
443
444 <?php
445
446 wp_die();
447 }
448 }
449