Completed
Push — 124-feature/notify-admins-on-e... ( 2e956d...ebe3a5 )
by Maria Daniel Deepak
06:33
created

EmailLogger::update_email_fail_status()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 20
ccs 0
cts 11
cp 0
crap 20
rs 9.9332
c 0
b 0
f 0
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
		 *
35
		 * @type string $to
36
		 * @type string $subject
37
		 * @type string $message
38
		 * @type string $headers
39
		 * @type string $attachment
40
		 * }
41
		 */
42 1
		$mail_info = apply_filters( 'el_wp_mail_log', $mail_info );
43
44
		// Sometimes the array passed to the `wp_mail` filter may not contain all the required keys.
45
		// See https://wordpress.org/support/topic/illegal-string-offset-attachments/
46 1
		$mail_info = wp_parse_args( $mail_info, array(
47 1
			'attachments' => array(),
48 1
			'to'          => '',
49 1
			'subject'     => '',
50 1
			'headers'     => '',
51 1
		) );
52
53
		$data = array(
54 1
			'attachments' => ( count( $mail_info['attachments'] ) > 0 ) ? 'true' : 'false',
55 1
			'to_email'    => is_array( $mail_info['to'] ) ? implode( ',', $mail_info['to'] ) : $mail_info['to'],
56 1
			'subject'     => $mail_info['subject'],
57 1
			'headers'     => is_array( $mail_info['headers'] ) ? implode( "\n", $mail_info['headers'] ) : $mail_info['headers'],
58 1
			'sent_date'   => current_time( 'mysql' ),
59 1
		);
60
61 1
		$message = '';
62
63 1
		if ( isset( $mail_info['message'] ) ) {
64
			$message = $mail_info['message'];
65
		} else {
66
			// wpmandrill plugin is changing "message" key to "html". See https://github.com/sudar/email-log/issues/20
67
			// Ideally this should be fixed in wpmandrill, but I am including this hack here till it is fixed by them.
68 1
			if ( isset( $mail_info['html'] ) ) {
69
				$message = $mail_info['html'];
70
			}
71
		}
72
73 1
		$data['message'] = $message;
74
75 1
		$email_log->table_manager->insert_log( $data );
76
77
		/**
78
		 * Fires the `el_email_log_inserted` action right after the log is inserted in to DB.
79
		 *
80
		 * @param array $data {
81
		 *      @type string $to
82
		 *      @type string $subject
83
		 *      @type string $message
84
		 *      @type string $headers
85
		 *      @type string $attachments
86
		 *      @type string $sent_date
87
		 * }
88
		 */
89 1
		do_action( 'el_email_log_inserted', $data );
90
91 1
		return $mail_info;
92
	}
93
94
	/**
95
	 * Updates the failed email in the DB.
96
	 *
97
	 * @since 2.3.0
98
	 *
99
	 * @param $wp_error
100
	 */
101
	public function update_email_fail_status( $wp_error ) {
102
		if ( ! ( $wp_error instanceof \WP_Error ) ) {
103
			return;
104
		}
105
106
		$email_log       = email_log();
107
		$mail_error_data = $wp_error->get_error_data( 'wp_mail_failed' );
108
109
		// $mail_error_data can be of type mixed.
110
		if ( ! is_array( $mail_error_data ) ) {
111
			return;
112
		}
113
114
		// @see wp-includes/pluggable.php#484
115
		$log_item_id = $email_log->table_manager->fetch_log_item_by_item_data( $mail_error_data );
116
		if ( empty( $log_item_id ) ) {
117
			return;
118
		}
119
120
		$email_log->table_manager->set_log_item_fail_status_by_id( $log_item_id );
121
	}
122
}
123