Completed
Pull Request — master (#236)
by Sudar
07:24 queued 03:40
created

EmailLogger   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Test Coverage

Coverage 62.79%

Importance

Changes 13
Bugs 0 Features 0
Metric Value
eloc 40
c 13
b 0
f 0
dl 0
loc 129
ccs 27
cts 43
cp 0.6279
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 3 1
A update_email_fail_status() 0 21 4
A log_email() 0 82 2
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 $original_mail_info Information about email.
23
	 *
24
	 * @return array Information about email.
25
	 */
26 1
	public function log_email( $original_mail_info ) {
27
		/**
28
		 * Hook to modify wp_mail contents before Email Log plugin logs.
29
		 *
30
		 * @param array $original_mail_info {
31
		 *     @type string|array $to
32
		 *     @type string       $subject
33
		 *     @type string       $message
34
		 *     @type string|array $headers
35
		 *     @type string|array $attachment
36
		 * }
37
		 *
38
		 * @since 2.0.0
39
		 */
40 1
		$original_mail_info = apply_filters( 'el_wp_mail_log', $original_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(
45 1
			$original_mail_info,
46
			array(
47 1
				'to'          => '',
48 1
				'subject'     => '',
49 1
				'message'     => '',
50 1
				'headers'     => '',
51 1
				'attachments' => array(),
52
			)
53 1
		);
54
55
		$log = array(
56 1
			'to_email'        => \EmailLog\Util\stringify( $mail_info['to'] ),
57 1
			'subject'         => $mail_info['subject'],
58 1
			'message'         => $mail_info['message'],
59 1
			'headers'         => \EmailLog\Util\stringify( $mail_info['headers'], "\n" ),
60 1
			'attachment_name' => \EmailLog\Util\stringify( $mail_info['attachments'] ),
61 1
			'sent_date'       => current_time( 'mysql' ),
62 1
			'ip_address'      => $_SERVER['REMOTE_ADDR'],
63 1
			'result'          => 1,
64 1
		);
65
66 1
		if ( empty( $log['attachment_name'] ) ) {
67 1
			$log['attachments'] = 'false';
68 1
		} else {
69
			$log['attachments'] = 'true';
70
		}
71
72
		/**
73
		 * Filters the mail info right before inserting on the table.
74
		 *
75
		 * Masked fields would use this filter to avoid modifying the original data sent to
76
		 * `wp_mail() function`
77
		 *
78
		 * @param array $log                Email Log that is about to be inserted into db.
79
		 * @param array $original_mail_info Original mail info that was passed to `wp_mail` filter.
80
		 *
81
		 * @since 2.3.2
82
		 */
83 1
		$log = apply_filters( 'el_email_log_before_insert', $log, $original_mail_info );
84
85 1
		$email_log = email_log();
86 1
		$email_log->table_manager->insert_log( $log );
87
88
		/**
89
		 * Fires the `el_email_log_inserted` action right after the log is inserted in to DB.
90
		 *
91
		 * @since 2.3.0
92
		 *
93
		 * @param array $log {
94
		 *      @type string $to
95
		 *      @type string $subject
96
		 *      @type string $message
97
		 *      @type string $headers
98
		 *      @type string $attachments
99
		 *      @type string $attachment_name
100
		 *      @type string $sent_date
101
		 *      @type string $ip_address
102
		 *      @type bool   $result
103
		 * }
104
		 */
105 1
		do_action( 'el_email_log_inserted', $log );
106
107 1
		return $original_mail_info;
108
	}
109
110
	/**
111
	 * Updates the failed email in the DB.
112
	 *
113
	 * @since 2.3.0
114
	 *
115
	 * @param \WP_Error $wp_error The error instance.
116
	 */
117
	public function update_email_fail_status( $wp_error ) {
118
		if ( ! ( $wp_error instanceof \WP_Error ) ) {
0 ignored issues
show
introduced by
$wp_error is always a sub-type of WP_Error.
Loading history...
119
			return;
120
		}
121
122
		$email_log       = email_log();
123
		$mail_error_data = $wp_error->get_error_data( 'wp_mail_failed' );
124
125
		// $mail_error_data can be of type mixed.
126
		if ( ! is_array( $mail_error_data ) ) {
127
			return;
128
		}
129
130
		// @see wp-includes/pluggable.php#484
131
		$log_item_id = $email_log->table_manager->fetch_log_item_by_item_data( $mail_error_data );
132
		// Empty will handle 0 and return FALSE.
133
		if ( empty( $log_item_id ) ) {
134
			return;
135
		}
136
137
		$email_log->table_manager->set_log_item_fail_status_by_id( $log_item_id );
138
	}
139
}
140