|
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
|
|
|
add_action( 'wp_mail_failed', array( $this, 'update_email_fail_status' ) ); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Logs email to database. |
|
21
|
|
|
* |
|
22
|
|
|
* @param array $mail_info Information about email. |
|
23
|
|
|
* |
|
24
|
|
|
* @return array Information about email. |
|
25
|
|
|
*/ |
|
26
|
1 |
|
public function log_email( $mail_info ) { |
|
27
|
1 |
|
$email_log = email_log(); |
|
28
|
|
|
/** |
|
29
|
|
|
* Hook to modify wp_mail contents before Email Log plugin logs. |
|
30
|
|
|
* |
|
31
|
|
|
* @since Genesis |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $mail_info { |
|
34
|
|
|
* @type string $to |
|
35
|
|
|
* @type string $subject |
|
36
|
|
|
* @type string $message |
|
37
|
|
|
* @type string $headers |
|
38
|
|
|
* @type string $attachment |
|
39
|
|
|
* } |
|
40
|
|
|
*/ |
|
41
|
1 |
|
$mail_info = apply_filters( 'el_wp_mail_log', $mail_info ); |
|
42
|
|
|
|
|
43
|
|
|
// Sometimes the array passed to the `wp_mail` filter may not contain all the required keys. |
|
44
|
|
|
// See https://wordpress.org/support/topic/illegal-string-offset-attachments/ |
|
45
|
1 |
|
$mail_info = wp_parse_args( $mail_info, array( |
|
46
|
1 |
|
'attachments' => array(), |
|
47
|
1 |
|
'to' => '', |
|
48
|
1 |
|
'subject' => '', |
|
49
|
1 |
|
'headers' => '', |
|
50
|
1 |
|
) ); |
|
51
|
|
|
|
|
52
|
|
|
$data = array( |
|
53
|
1 |
|
'attachments' => ( count( $mail_info['attachments'] ) > 0 ) ? 'true' : 'false', |
|
54
|
1 |
|
'to_email' => is_array( $mail_info['to'] ) ? implode( ',', $mail_info['to'] ) : $mail_info['to'], |
|
55
|
1 |
|
'subject' => $mail_info['subject'], |
|
56
|
1 |
|
'headers' => is_array( $mail_info['headers'] ) ? implode( "\n", $mail_info['headers'] ) : $mail_info['headers'], |
|
57
|
1 |
|
'sent_date' => current_time( 'mysql' ), |
|
58
|
1 |
|
'attachment_name' => implode( ',', $mail_info['attachments'] ), |
|
59
|
|
|
// TODO: Improve the Client's IP using https://www.virendrachandak.com/techtalk/getting-real-client-ip-address-in-php-2/ |
|
60
|
1 |
|
'ip_address' => $_SERVER['REMOTE_ADDR'], |
|
61
|
1 |
|
'result' => 1, |
|
62
|
1 |
|
); |
|
63
|
|
|
|
|
64
|
1 |
|
$message = ''; |
|
65
|
|
|
|
|
66
|
1 |
|
if ( isset( $mail_info['message'] ) ) { |
|
67
|
|
|
$message = $mail_info['message']; |
|
68
|
|
|
} else { |
|
69
|
|
|
// wpmandrill plugin is changing "message" key to "html". See https://github.com/sudar/email-log/issues/20 |
|
70
|
|
|
// Ideally this should be fixed in wpmandrill, but I am including this hack here till it is fixed by them. |
|
71
|
1 |
|
if ( isset( $mail_info['html'] ) ) { |
|
72
|
|
|
$message = $mail_info['html']; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
$data['message'] = $message; |
|
77
|
|
|
|
|
78
|
1 |
|
$email_log->table_manager->insert_log( $data ); |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Fires the `el_email_log_inserted` action right after the log is inserted in to DB. |
|
82
|
|
|
* |
|
83
|
|
|
* @param array $data { |
|
84
|
|
|
* @type string $to |
|
85
|
|
|
* @type string $subject |
|
86
|
|
|
* @type string $message |
|
87
|
|
|
* @type string $headers |
|
88
|
|
|
* @type string $attachments |
|
89
|
|
|
* @type string $sent_date |
|
90
|
|
|
* } |
|
91
|
|
|
*/ |
|
92
|
1 |
|
do_action( 'el_email_log_inserted', $data ); |
|
93
|
|
|
|
|
94
|
1 |
|
return $mail_info; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Updates the failed email in the DB. |
|
99
|
|
|
* |
|
100
|
|
|
* @since 2.3.0 |
|
101
|
|
|
* |
|
102
|
|
|
* @param \WP_Error $wp_error The error instance. |
|
103
|
|
|
*/ |
|
104
|
|
|
public function update_email_fail_status( $wp_error ) { |
|
105
|
|
|
if ( ! ( $wp_error instanceof \WP_Error ) ) { |
|
|
|
|
|
|
106
|
|
|
return; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$email_log = email_log(); |
|
110
|
|
|
$mail_error_data = $wp_error->get_error_data( 'wp_mail_failed' ); |
|
111
|
|
|
|
|
112
|
|
|
// $mail_error_data can be of type mixed. |
|
113
|
|
|
if ( ! is_array( $mail_error_data ) ) { |
|
114
|
|
|
return; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// @see wp-includes/pluggable.php#484 |
|
118
|
|
|
$log_item_id = $email_log->table_manager->fetch_log_item_by_item_data( $mail_error_data ); |
|
119
|
|
|
if ( empty( $log_item_id ) ) { |
|
120
|
|
|
return; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$email_log->table_manager->set_log_item_fail_status_by_id( $log_item_id ); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|