1
|
|
|
<?php namespace EmailLog\Core; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Log's emails sent through `wp_mail`. |
5
|
|
|
* |
6
|
|
|
* @package EmailLog\Core |
7
|
|
|
* @since 2.0 |
8
|
|
|
*/ |
9
|
|
|
class EmailLogger implements Loadie { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Load the logger. |
13
|
|
|
*/ |
14
|
|
|
public function load() { |
15
|
|
|
add_filter( 'wp_mail', array( $this, 'log_email' ) ); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Logs email to database. |
20
|
|
|
* |
21
|
|
|
* @param array $mail_info Information about email. |
22
|
|
|
* |
23
|
|
|
* @return array Information about email. |
24
|
|
|
*/ |
25
|
1 |
|
public function log_email( $mail_info ) { |
26
|
1 |
|
$email_log = email_log(); |
27
|
|
|
/** |
28
|
|
|
* Hook to modify wp_mail contents before Email Log plugin logs. |
29
|
|
|
* |
30
|
|
|
* @since Genesis |
31
|
|
|
* |
32
|
|
|
* @param array $mail_info { |
33
|
|
|
* @type string $to |
34
|
|
|
* @type string $subject |
35
|
|
|
* @type string $message |
36
|
|
|
* @type string $headers |
37
|
|
|
* @type string $attachment |
38
|
|
|
* } |
39
|
|
|
*/ |
40
|
1 |
|
$mail_info = apply_filters( 'el_wp_mail_log', $mail_info ); |
41
|
|
|
|
42
|
|
|
// Sometimes the array passed to the `wp_mail` filter may not contain all the required keys. |
43
|
|
|
// See https://wordpress.org/support/topic/illegal-string-offset-attachments/ |
44
|
1 |
|
$mail_info = wp_parse_args( $mail_info, array( |
45
|
1 |
|
'attachments' => array(), |
46
|
1 |
|
'to' => '', |
47
|
1 |
|
'subject' => '', |
48
|
1 |
|
'headers' => '', |
49
|
1 |
|
) ); |
50
|
|
|
|
51
|
|
|
$data = array( |
52
|
1 |
|
'attachments' => ( count( $mail_info['attachments'] ) > 0 ) ? 'true' : 'false', |
53
|
1 |
|
'to_email' => is_array( $mail_info['to'] ) ? implode( ',', $mail_info['to'] ) : $mail_info['to'], |
54
|
1 |
|
'subject' => $mail_info['subject'], |
55
|
1 |
|
'headers' => is_array( $mail_info['headers'] ) ? implode( "\n", $mail_info['headers'] ) : $mail_info['headers'], |
56
|
1 |
|
'sent_date' => current_time( 'mysql' ), |
57
|
1 |
|
); |
58
|
|
|
|
59
|
1 |
|
$message = ''; |
60
|
|
|
|
61
|
1 |
|
if ( isset( $mail_info['message'] ) ) { |
62
|
|
|
$message = $mail_info['message']; |
63
|
|
|
} else { |
64
|
|
|
// wpmandrill plugin is changing "message" key to "html". See https://github.com/sudar/email-log/issues/20 |
65
|
|
|
// Ideally this should be fixed in wpmandrill, but I am including this hack here till it is fixed by them. |
66
|
1 |
|
if ( isset( $mail_info['html'] ) ) { |
67
|
|
|
$message = $mail_info['html']; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
$data['message'] = $message; |
72
|
|
|
|
73
|
1 |
|
$email_log->table_manager->insert_log( $data ); |
74
|
|
|
|
75
|
1 |
|
return $mail_info; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|