PluginProbe ʕ •ᴥ•ʔ
Check & Log Email – Easy Email Testing & Mail logging / 2.0
Check & Log Email – Easy Email Testing & Mail logging v2.0
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 1 year ago Request 1 year ago UI 1 year ago Check_Email_Admin_Capability_Giver.php 1 year ago Check_Email_Export_Log.php 1 year ago Check_Email_From_Handler.php 1 year ago Check_Email_Log.php 1 year ago Check_Email_Logger.php 1 year ago Check_Email_Review.php 1 year ago Loadie.php 1 year ago
Check_Email_Export_Log.php
456 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 = gmdate('Y-m-d 00:00:00');
96 $to_date = gmdate('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 = gmdate('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 = gmdate('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 esc_html($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 $cache_key = 'ck_mail_generate_csv'.$status;
128 $ck_mail_generate_csv = wp_cache_get( $cache_key );
129 if ( false === $ck_mail_generate_csv ) {
130 $table_name = $wpdb->prefix.'check_email_log';
131 // phpcs:disable -- prepared
132 $query = $wpdb->prepare("SELECT * FROM $table_name");
133 if($status == 'All' && $export_date == 'all'){
134 if(!empty($export_recipient)){
135 $query = $wpdb->prepare("SELECT * FROM $table_name WHERE to_email = %s", $export_recipient);
136 }else{
137 $query = $wpdb->prepare("SELECT * FROM $table_name");
138 }
139 }else if($status == 'Success' && $export_date == 'all'){
140 if(!empty($export_recipient)){
141 $query = $wpdb->prepare("SELECT * FROM $table_name WHERE (error_message = %s OR error_message IS NULL) AND to_email = %s", $status, $export_recipient);
142 }else{
143 $query = $wpdb->prepare("SELECT * FROM $table_name WHERE error_message = %s OR error_message IS NULL", $status);
144 }
145 }else if($status == 'Fail' && $export_date == 'all'){
146 if(!empty($export_recipient)){
147 $query = $wpdb->prepare("SELECT * FROM $table_name WHERE error_message != %s AND to_email = %s", 'Success', $export_recipient);
148 }else{
149 $query = $wpdb->prepare("SELECT * FROM $table_name WHERE error_message != %s", 'Success');
150 }
151 }else if($status == 'All' && $export_date == 'custom'){
152 if(!empty($export_recipient)){
153 $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);
154 }else{
155 $query = $query = $wpdb->prepare("SELECT * FROM $table_name WHERE sent_date BETWEEN %s AND %s", $from_date, $to_date);
156 }
157 }else if($status == 'Success' && $export_date == 'custom'){
158 if(!empty($export_recipient)){
159 $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);
160 }else{
161 $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);
162 }
163 }else if($status == 'Fail' && $export_date == 'custom'){
164 if(!empty($export_recipient)){
165 $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);
166 }else{
167 $query = $wpdb->prepare("SELECT * FROM $table_name WHERE error_message != %s AND sent_date BETWEEN %s AND %s", 'Success', $from_date, $to_date);
168 }
169 }
170
171 $results = $wpdb->get_results($query, ARRAY_A);
172 // phpcs:enable -- prepared
173 wp_cache_set( $cache_key, $results );
174 }else{
175 $results = $ck_mail_generate_csv;
176 }
177
178 $logs_data = '';
179 $csv_headings = array('Sr No');
180 $csv_headings = array_merge($csv_headings, $fields);
181
182 $logs_data = implode($this->separator, $csv_headings);
183 if($file_format == 'txt'){
184 $logs_data = implode("\t \t", $csv_headings);
185 }
186 $logs_data .= "\n";
187
188 if( !empty($results) && is_array($results) && !empty($results) ){
189 $log_cnt = 1;
190 foreach ( $results as $l_key => $l_value ) {
191 if( !empty($l_value) && is_array($l_value) ){
192
193 $logs_data .= $log_cnt.$this->separator;
194
195 if(in_array("From", $csv_headings)){
196 $from = '';
197 if ( function_exists('imap_rfc822_parse_headers' ) ) {
198 $headers = imap_rfc822_parse_headers($l_value['headers']);
199
200 if (isset($headers->fromaddress) && !empty($headers->fromaddress)) {
201 $from = $headers->fromaddress;
202 $from = apply_filters( 'check_email_log_list_column_from_email', esc_html( $from ) );
203 }
204 }else {
205 $find_from = substr($l_value['headers'], strpos($l_value['headers'], 'From') + 5 );
206
207 if(!empty($find_from) && is_string($find_from)){
208 $find_from = explode("\n", $find_from);
209 if(is_array($find_from) && isset($find_from[1])){
210 $from = $find_from[1];
211 }
212 }
213 }
214
215 $logs_data .= $from.$this->separator;
216 }
217
218 if(in_array("To", $csv_headings)){
219 $logs_data .= $l_value['to_email'].$this->separator;
220 }
221
222 if(in_array("Subject", $csv_headings)){
223 $logs_data .= $l_value['subject'].$this->separator;
224 }
225
226 if(in_array("Message", $csv_headings)){
227 $message = str_replace(',', '', $l_value['message']);
228 $message = preg_replace('~[\r\n\t]+~', '', $message);
229 $logs_data .= $message.$this->separator;
230 }
231
232 if(in_array("Sent At", $csv_headings)){
233 $logs_data .= gmdate('d-m-Y H:i:s', strtotime($l_value['sent_date'])).$this->separator;
234 }
235 if(in_array("Status", $csv_headings)){
236 $logs_data .= empty($l_value['error_message'])?'Success':$l_value['error_message'];
237 }
238
239 $log_cnt++;
240
241 $logs_data .= " \n ";
242 }
243 }
244 }
245 return $logs_data;
246 }
247
248 /**
249 * Template for email log options
250 * @since 1.0.11
251 * */
252 public function ck_email_export_filter_popup(){
253 if ( ! current_user_can( 'manage_check_email' ) ) {
254 wp_die( -1 );
255 }
256
257 if ( ! isset( $_GET['ck_mail_security_nonce'] ) ){
258 wp_die( '-1' );
259 }
260
261 if ( !wp_verify_nonce( $_GET['ck_mail_security_nonce'], 'ck_mail_ajax_check_nonce' ) ){
262 wp_die( '-1' );
263 }
264 ?>
265
266 <div id="ck-mail-elog-options">
267 <form id="ck-mail-export-form" type="GET" action="<?php echo esc_url(admin_url('admin-ajax.php')); ?>">
268 <div class="ck-mail-exp-row">
269 <div class="ck-mail-exp-col">
270 <div id="ck-mail-export-type">
271 <div class="ck-mail-logs-heading-wrapper">
272 <h2 class="ck-mail-export-h2"> <?php esc_html_e('File Format', 'check-email'); ?> </h2>
273 </div>
274
275 <div class="ck-mail-logs-contents">
276 <div class="ck-mail-log-exp-type ck-mail-export-options">
277 <label for="ck-mail-export-csv">
278 <input type="radio" name="export_type" class="ck-mail-export-type" id="ck-mail-export-csv" value="csv" checked>
279 <?php esc_html_e('Export in CSV (.csv)', '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-xls">
285 <input type="radio" name="export_type" class="ck-mail-export-type" id="ck-mail-export-xls" value="xls">
286 <?php esc_html_e('Export in Microsoft Excel (.xls)', '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-xlsx ck-mail-export-options">
292 <input type="radio" name="export_type" class="ck-mail-export-type" id="ck-mail-export-xlsx" value="xlsx">
293 <?php esc_html_e('Export in Microsoft Excel (.xlsx)', 'check-email'); ?>
294 </label>
295 </div>
296
297 <div class="ck-mail-log-exp-type ck-mail-export-options">
298 <label for="ck-mail-export-txt">
299 <input type="radio" name="export_type" class="ck-mail-export-type" id="ck-mail-export-txt" value="txt">
300 <?php esc_html_e('Export in Text (.txt)', 'check-email'); ?>
301 </label>
302 </div>
303
304 </div>
305 </div>
306 </div>
307
308 <div class="ck-mail-exp-col">
309 <div id="ck-mail-export-common-info">
310 <div class="ck-mail-logs-heading-wrapper">
311 <h2 class="ck-mail-export-h2"> <?php esc_html_e('Fields', 'check-email'); ?> </h2>
312 <p class="ck-mail-exp-error ck-mail-d-none" id="ck-mail-fields-error" style="color: red;"></p>
313 </div>
314
315 <div class="ck-mail-logs-contents">
316 <div class="ck-mail-log-exp-comm-info ck-mail-export-options">
317 <label for="ck-mail-comm-info-from">
318 <input type="checkbox" name="common_information[From]" class="ck-mail-comm-info-chk" id="ck-mail-comm-info-from" value="From" checked>
319 <?php esc_html_e('From', '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-to">
325 <input type="checkbox" name="common_information[To]" class="ck-mail-comm-info-chk" id="ck-mail-comm-info-to" value="To" checked>
326 <?php esc_html_e('To', '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-subject">
332 <input type="checkbox" name="common_information[Subject]" class="ck-mail-comm-info-chk" id="ck-mail-comm-info-subject" value="Subject" checked>
333 <?php esc_html_e('Subject', '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-msg">
339 <input type="checkbox" name="common_information[Message]" class="ck-mail-comm-info-chk" id="ck-mail-comm-info-msg" value="Message" checked>
340 <?php esc_html_e('Message', '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-date">
346 <input type="checkbox" name="common_information[Sent-At]" class="ck-mail-comm-info-chk" id="ck-mail-comm-info-date" value="Sent At" checked>
347 <?php esc_html_e('Sent At', 'check-email'); ?>
348 </label>
349 </div>
350
351 <div class="ck-mail-log-exp-comm-info ck-mail-export-options">
352 <label for="ck-mail-comm-info-status">
353 <input type="checkbox" name="common_information[Status]" class="ck-mail-comm-info-chk" id="ck-mail-comm-info-status" value="Status" checked>
354 <?php esc_html_e('Status', 'check-email'); ?>
355 </label>
356 </div>
357 </div>
358 </div>
359 </div>
360 </div> <!-- ck-mail-exp-row div end -->
361
362 <div class="ck-mail-exp-row">
363 <div class="ck-mail-exp-col">
364 <div id="ck-mail-export-by-status">
365 <div class="ck-mail-logs-heading-wrapper">
366 <h2> <?php esc_html_e('Status', 'check-email'); ?> </h2>
367 </div>
368
369 <div class="ck-mail-logs-contents">
370 <div class="ck-mail-log-exp-status ck-mail-export-options">
371 <label for="ck-mail-exp-status-all">
372 <input type="radio" name="export_status" class="ck-mail-exp-status-radio" id="ck-mail-exp-status-all" value="All" checked>
373 <?php esc_html_e('All', '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-success">
379 <input type="radio" name="export_status" class="ck-mail-exp-status-radio" id="ck-mail-exp-status-success" value="Success">
380 <?php esc_html_e('Success', 'check-email'); ?>
381 </label>
382 </div>
383
384 <div class="ck-mail-log-exp-status ck-mail-export-options">
385 <label for="ck-mail-exp-status-fail">
386 <input type="radio" name="export_status" class="ck-mail-exp-status-radio" id="ck-mail-exp-status-fail" value="Fail">
387 <?php esc_html_e('Failure', 'check-email'); ?>
388 </label>
389 </div>
390
391 </div>
392 </div>
393 </div>
394
395 <div class="ck-mail-exp-col">
396 <div id="ck-mail-export-by-rec">
397 <div class="ck-mail-logs-heading-wrapper">
398 <h2> <?php esc_html_e('Recipient', 'check-email'); ?> </h2>
399 </div>
400
401 <div class="ck-mail-logs-contents">
402 <div class="ck-mail-log-exp-recipient ck-mail-export-options">
403 <label for="ck-mail-export-recipient"> <?php esc_html_e('Enter Email id', 'check-email'); ?> </label>
404 <input type="text" name="export_recipient" class="ck-mail-export-recipient" id="ck-mail-export-recipient" placeholder="<?php esc_attr_e( 'Enter Recipient Email', 'check-email' ); ?>">
405 </div>
406 </div>
407 </div>
408 </div>
409 </div> <!-- ck-mail-exp-row div end -->
410
411 <div style="clear: both;"></div>
412
413 <div class="ck-mail-exp-row">
414 <div class="ck-mail-exp-col">
415 <div id="ck-mail-export-by-date">
416 <div class="ck-mail-logs-heading-wrapper">
417 <h2> <?php esc_html_e('Date Range', 'check-email'); ?> </h2>
418 </div>
419
420 <div class="ck-mail-logs-contents">
421 <div class="ck-mail-log-exp-date ck-mail-export-options">
422 <label for="ck-mail-exp-date-all">
423 <input type="radio" name="export_date" class="ck-mail-exp-date-radio" id="ck-mail-exp-date-all" value="all" checked>
424 <?php esc_html_e('All', 'check-email'); ?>
425 </label>
426 </div>
427
428 <div class="ck-mail-log-exp-date ck-mail-export-options">
429 <label for="ck-mail-exp-date-custom">
430 <input type="radio" name="export_date" class="ck-mail-exp-date-radio" id="ck-mail-exp-date-custom" value="custom">
431 <?php esc_html_e('Custom', 'check-email'); ?>
432 </label>
433 <p class="ck-mail-exp-error ck-mail-d-none" id="ck-mail-exp-date-error"></p>
434 <div id="ck-mail-exp-c-date-wrapper" class="ck-mail-d-none">
435 <input type="search" id="ck-mail-exp-from-date" name="ck_mail_exp_from_date" value="<?php echo esc_attr(gmdate('Y-m-d')); ?>" placeholder="<?php esc_attr_e( 'From Date', 'check-email' ); ?>" readonly />
436 <input type="search" id="ck-mail-exp-to-date" name="ck_mail_exp_to_date" value="<?php echo esc_attr(gmdate('Y-m-d')); ?>" placeholder="<?php esc_attr_e( 'To Date', 'check-email' ); ?>" readonly />
437 </div>
438 </div>
439 </div>
440
441 </div>
442 </div>
443 </div> <!-- ck-mail-exp-row div end -->
444 <div style="clear: both;"></div>
445 <input type="hidden" name="ck_mail_export_nonce" value="<?php echo esc_attr(wp_create_nonce('ck_mail_ajax_check_nonce')); ?>">
446 <input type="hidden" name="action" value="ck_mail_export_logs">
447 <button type="button" class="button-primary button" id="ck-mail-export-logs-btn"> <?php esc_html_e('Export Logs', 'check-email'); ?> </button>
448 </form>
449 </div>
450
451 <?php
452
453 wp_die();
454 }
455 }
456