PluginProbe ʕ •ᴥ•ʔ
Check & Log Email – Easy Email Testing & Mail logging / trunk
Check & Log Email – Easy Email Testing & Mail logging vtrunk
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 month ago Request 1 month ago UI 1 month ago Auth.php 1 month ago Check_Email_Admin_Capability_Giver.php 1 month ago Check_Email_Export_Log.php 1 month ago Check_Email_From_Handler.php 1 month ago Check_Email_Log.php 1 month ago Check_Email_Logger.php 1 month ago Check_Email_Multisite.php 1 month ago Check_Email_Review.php 1 month ago Loadie.php 1 month ago
Check_Email_Export_Log.php
457 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( sanitize_text_field( wp_unslash( $_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( wp_unslash( $_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', wp_unslash($_GET['common_information']));
83 }
84
85 $status = 'All';
86 if(isset($_GET['export_status']) && !empty($_GET['export_status'])){
87 $status = sanitize_text_field( wp_unslash( $_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( wp_unslash( $_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( wp_unslash( $_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( wp_unslash( $_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( wp_unslash( $_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 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
203 $from = apply_filters( 'check_email_log_list_column_from_email', esc_html( $from ) );
204 }
205 }else {
206 $find_from = substr($l_value['headers'], strpos($l_value['headers'], 'From') + 5 );
207
208 if(!empty($find_from) && is_string($find_from)){
209 $find_from = explode("\n", $find_from);
210 if(is_array($find_from) && isset($find_from[1])){
211 $from = $find_from[1];
212 }
213 }
214 }
215
216 $logs_data .= $from.$this->separator;
217 }
218
219 if(in_array("To", $csv_headings)){
220 $logs_data .= $l_value['to_email'].$this->separator;
221 }
222
223 if(in_array("Subject", $csv_headings)){
224 $logs_data .= $l_value['subject'].$this->separator;
225 }
226
227 if(in_array("Message", $csv_headings)){
228 $message = str_replace(',', '', $l_value['message']);
229 $message = preg_replace('~[\r\n\t]+~', '', $message);
230 $logs_data .= $message.$this->separator;
231 }
232
233 if(in_array("Sent At", $csv_headings)){
234 $logs_data .= gmdate('d-m-Y H:i:s', strtotime($l_value['sent_date'])).$this->separator;
235 }
236 if(in_array("Status", $csv_headings)){
237 $logs_data .= $l_value['result'] == 0 ?'Failed : '.$l_value['error_message'] : 'Success';
238 }
239
240 $log_cnt++;
241
242 $logs_data .= " \n ";
243 }
244 }
245 }
246 return $logs_data;
247 }
248
249 /**
250 * Template for email log options
251 * @since 1.0.11
252 * */
253 public function ck_email_export_filter_popup(){
254 if ( ! current_user_can( 'manage_check_email' ) ) {
255 wp_die( -1 );
256 }
257
258 if ( ! isset( $_GET['ck_mail_security_nonce'] ) ){
259 wp_die( '-1' );
260 }
261
262 if ( !wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['ck_mail_security_nonce'] ) ), 'ck_mail_ajax_check_nonce' ) ){
263 wp_die( '-1' );
264 }
265 ?>
266
267 <div id="ck-mail-elog-options">
268 <form id="ck-mail-export-form" type="GET" action="<?php echo esc_url(admin_url('admin-ajax.php')); ?>">
269 <div class="ck-mail-exp-row">
270 <div class="ck-mail-exp-col">
271 <div id="ck-mail-export-type">
272 <div class="ck-mail-logs-heading-wrapper">
273 <h2 class="ck-mail-export-h2"> <?php esc_html_e('File Format', 'check-email'); ?> </h2>
274 </div>
275
276 <div class="ck-mail-logs-contents">
277 <div class="ck-mail-log-exp-type ck-mail-export-options">
278 <label for="ck-mail-export-csv">
279 <input type="radio" name="export_type" class="ck-mail-export-type" id="ck-mail-export-csv" value="csv" checked>
280 <?php esc_html_e('Export in CSV (.csv)', 'check-email'); ?>
281 </label>
282 </div>
283
284 <div class="ck-mail-log-exp-type ck-mail-export-options">
285 <label for="ck-mail-export-xls">
286 <input type="radio" name="export_type" class="ck-mail-export-type" id="ck-mail-export-xls" value="xls">
287 <?php esc_html_e('Export in Microsoft Excel (.xls)', 'check-email'); ?>
288 </label>
289 </div>
290
291 <div class="ck-mail-log-exp-type ck-mail-export-options">
292 <label for="ck-mail-export-xlsx ck-mail-export-options">
293 <input type="radio" name="export_type" class="ck-mail-export-type" id="ck-mail-export-xlsx" value="xlsx">
294 <?php esc_html_e('Export in Microsoft Excel (.xlsx)', 'check-email'); ?>
295 </label>
296 </div>
297
298 <div class="ck-mail-log-exp-type ck-mail-export-options">
299 <label for="ck-mail-export-txt">
300 <input type="radio" name="export_type" class="ck-mail-export-type" id="ck-mail-export-txt" value="txt">
301 <?php esc_html_e('Export in Text (.txt)', 'check-email'); ?>
302 </label>
303 </div>
304
305 </div>
306 </div>
307 </div>
308
309 <div class="ck-mail-exp-col">
310 <div id="ck-mail-export-common-info">
311 <div class="ck-mail-logs-heading-wrapper">
312 <h2 class="ck-mail-export-h2"> <?php esc_html_e('Fields', 'check-email'); ?> </h2>
313 <p class="ck-mail-exp-error ck-mail-d-none" id="ck-mail-fields-error" style="color: red;"></p>
314 </div>
315
316 <div class="ck-mail-logs-contents">
317 <div class="ck-mail-log-exp-comm-info ck-mail-export-options">
318 <label for="ck-mail-comm-info-from">
319 <input type="checkbox" name="common_information[From]" class="ck-mail-comm-info-chk" id="ck-mail-comm-info-from" value="From" checked>
320 <?php esc_html_e('From', 'check-email'); ?>
321 </label>
322 </div>
323
324 <div class="ck-mail-log-exp-comm-info ck-mail-export-options">
325 <label for="ck-mail-comm-info-to">
326 <input type="checkbox" name="common_information[To]" class="ck-mail-comm-info-chk" id="ck-mail-comm-info-to" value="To" checked>
327 <?php esc_html_e('To', 'check-email'); ?>
328 </label>
329 </div>
330
331 <div class="ck-mail-log-exp-comm-info ck-mail-export-options">
332 <label for="ck-mail-comm-info-subject">
333 <input type="checkbox" name="common_information[Subject]" class="ck-mail-comm-info-chk" id="ck-mail-comm-info-subject" value="Subject" checked>
334 <?php esc_html_e('Subject', 'check-email'); ?>
335 </label>
336 </div>
337
338 <div class="ck-mail-log-exp-comm-info ck-mail-export-options">
339 <label for="ck-mail-comm-info-msg">
340 <input type="checkbox" name="common_information[Message]" class="ck-mail-comm-info-chk" id="ck-mail-comm-info-msg" value="Message" checked>
341 <?php esc_html_e('Message', 'check-email'); ?>
342 </label>
343 </div>
344
345 <div class="ck-mail-log-exp-comm-info ck-mail-export-options">
346 <label for="ck-mail-comm-info-date">
347 <input type="checkbox" name="common_information[Sent-At]" class="ck-mail-comm-info-chk" id="ck-mail-comm-info-date" value="Sent At" checked>
348 <?php esc_html_e('Sent At', 'check-email'); ?>
349 </label>
350 </div>
351
352 <div class="ck-mail-log-exp-comm-info ck-mail-export-options">
353 <label for="ck-mail-comm-info-status">
354 <input type="checkbox" name="common_information[Status]" class="ck-mail-comm-info-chk" id="ck-mail-comm-info-status" value="Status" checked>
355 <?php esc_html_e('Status', 'check-email'); ?>
356 </label>
357 </div>
358 </div>
359 </div>
360 </div>
361 </div> <!-- ck-mail-exp-row div end -->
362
363 <div class="ck-mail-exp-row">
364 <div class="ck-mail-exp-col">
365 <div id="ck-mail-export-by-status">
366 <div class="ck-mail-logs-heading-wrapper">
367 <h2> <?php esc_html_e('Status', 'check-email'); ?> </h2>
368 </div>
369
370 <div class="ck-mail-logs-contents">
371 <div class="ck-mail-log-exp-status ck-mail-export-options">
372 <label for="ck-mail-exp-status-all">
373 <input type="radio" name="export_status" class="ck-mail-exp-status-radio" id="ck-mail-exp-status-all" value="All" checked>
374 <?php esc_html_e('All', 'check-email'); ?>
375 </label>
376 </div>
377
378 <div class="ck-mail-log-exp-status ck-mail-export-options">
379 <label for="ck-mail-exp-status-success">
380 <input type="radio" name="export_status" class="ck-mail-exp-status-radio" id="ck-mail-exp-status-success" value="Success">
381 <?php esc_html_e('Success', 'check-email'); ?>
382 </label>
383 </div>
384
385 <div class="ck-mail-log-exp-status ck-mail-export-options">
386 <label for="ck-mail-exp-status-fail">
387 <input type="radio" name="export_status" class="ck-mail-exp-status-radio" id="ck-mail-exp-status-fail" value="Fail">
388 <?php esc_html_e('Failure', 'check-email'); ?>
389 </label>
390 </div>
391
392 </div>
393 </div>
394 </div>
395
396 <div class="ck-mail-exp-col">
397 <div id="ck-mail-export-by-rec">
398 <div class="ck-mail-logs-heading-wrapper">
399 <h2> <?php esc_html_e('Recipient', 'check-email'); ?> </h2>
400 </div>
401
402 <div class="ck-mail-logs-contents">
403 <div class="ck-mail-log-exp-recipient ck-mail-export-options">
404 <label for="ck-mail-export-recipient"> <?php esc_html_e('Enter Email id', 'check-email'); ?> </label>
405 <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' ); ?>">
406 </div>
407 </div>
408 </div>
409 </div>
410 </div> <!-- ck-mail-exp-row div end -->
411
412 <div style="clear: both;"></div>
413
414 <div class="ck-mail-exp-row">
415 <div class="ck-mail-exp-col">
416 <div id="ck-mail-export-by-date">
417 <div class="ck-mail-logs-heading-wrapper">
418 <h2> <?php esc_html_e('Date Range', 'check-email'); ?> </h2>
419 </div>
420
421 <div class="ck-mail-logs-contents">
422 <div class="ck-mail-log-exp-date ck-mail-export-options">
423 <label for="ck-mail-exp-date-all">
424 <input type="radio" name="export_date" class="ck-mail-exp-date-radio" id="ck-mail-exp-date-all" value="all" checked>
425 <?php esc_html_e('All', 'check-email'); ?>
426 </label>
427 </div>
428
429 <div class="ck-mail-log-exp-date ck-mail-export-options">
430 <label for="ck-mail-exp-date-custom">
431 <input type="radio" name="export_date" class="ck-mail-exp-date-radio" id="ck-mail-exp-date-custom" value="custom">
432 <?php esc_html_e('Custom', 'check-email'); ?>
433 </label>
434 <p class="ck-mail-exp-error ck-mail-d-none" id="ck-mail-exp-date-error"></p>
435 <div id="ck-mail-exp-c-date-wrapper" class="ck-mail-d-none">
436 <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 />
437 <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 />
438 </div>
439 </div>
440 </div>
441
442 </div>
443 </div>
444 </div> <!-- ck-mail-exp-row div end -->
445 <div style="clear: both;"></div>
446 <input type="hidden" name="ck_mail_export_nonce" value="<?php echo esc_attr(wp_create_nonce('ck_mail_ajax_check_nonce')); ?>">
447 <input type="hidden" name="action" value="ck_mail_export_logs">
448 <button type="button" class="button-primary button" id="ck-mail-export-logs-btn"> <?php esc_html_e('Export Logs', 'check-email'); ?> </button>
449 </form>
450 </div>
451
452 <?php
453
454 wp_die();
455 }
456 }
457