Passed
Push — dev/2.3.0 ( 09ec36...292df5 )
by Sudar
03:15
created

EmailLogger::log_email()   B

Complexity

Conditions 7
Paths 36

Size

Total Lines 80
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 7.2269

Importance

Changes 0
Metric Value
cc 7
eloc 33
nc 36
nop 1
dl 0
loc 80
ccs 30
cts 36
cp 0.8333
crap 7.2269
rs 8.4586
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
		// ! empty() check on attachments handles both empty string and empty array.
53
		$data = array(
54 1
			'attachments'     => ( ! empty( $mail_info['attachments'] ) ) ? 'true' : 'false',
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
		$to = '';
65 1
		if ( empty( $mail_info['to'] ) ) {
66
			$to = '';
67 1
		} elseif ( is_array( $mail_info['to'] ) ) {
68 1
			$to = implode( ',', $mail_info['to'] );
69 1
		} else {
70
			$to = $mail_info['to'];
71
		}
72
73 1
		$data['to_email'] = $to;
74
75 1
		$message = '';
76
77 1
		if ( isset( $mail_info['message'] ) ) {
78
			$message = $mail_info['message'];
79
		} else {
80
			// wpmandrill plugin is changing "message" key to "html". See https://github.com/sudar/email-log/issues/20
81
			// Ideally this should be fixed in wpmandrill, but I am including this hack here till it is fixed by them.
82 1
			if ( isset( $mail_info['html'] ) ) {
83
				$message = $mail_info['html'];
84
			}
85
		}
86
87 1
		$data['message'] = $message;
88
89 1
		$email_log->table_manager->insert_log( $data );
90
91
		/**
92
		 * Fires the `el_email_log_inserted` action right after the log is inserted in to DB.
93
		 *
94
		 * @param array $data {
95
		 *      @type string $to
96
		 *      @type string $subject
97
		 *      @type string $message
98
		 *      @type string $headers
99
		 *      @type string $attachments
100
		 *      @type string $sent_date
101
		 * }
102
		 */
103 1
		do_action( 'el_email_log_inserted', $data );
104
105 1
		return $mail_info;
106
	}
107
108
	/**
109
	 * Updates the failed email in the DB.
110
	 *
111
	 * @since 2.3.0
112
	 *
113
	 * @param \WP_Error $wp_error The error instance.
114
	 */
115
	public function update_email_fail_status( $wp_error ) {
116
		if ( ! ( $wp_error instanceof \WP_Error ) ) {
0 ignored issues
show
introduced by
$wp_error is always a sub-type of WP_Error.
Loading history...
117
			return;
118
		}
119
120
		$email_log       = email_log();
121
		$mail_error_data = $wp_error->get_error_data( 'wp_mail_failed' );
122
123
		// $mail_error_data can be of type mixed.
124
		if ( ! is_array( $mail_error_data ) ) {
125
			return;
126
		}
127
128
		// @see wp-includes/pluggable.php#484
129
		$log_item_id = $email_log->table_manager->fetch_log_item_by_item_data( $mail_error_data );
130
		// Empty will handle 0 and return FALSE.
131
		if ( empty( $log_item_id ) ) {
132
			return;
133
		}
134
135
		$email_log->table_manager->set_log_item_fail_status_by_id( $log_item_id );
136
	}
137
}
138