Passed
Push — 124-feature/notify-admins-on-e... ( 07ca2e...c928e5 )
by Sudar
24:59 queued 21:36
created

EmailLogger::log_email()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 69
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 6.0944

Importance

Changes 0
Metric Value
cc 6
eloc 26
nc 24
nop 1
dl 0
loc 69
ccs 25
cts 29
cp 0.8621
crap 6.0944
rs 8.8817
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
	}
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
			'attachment_name' => implode( ',', $mail_info['attachments'] ),
58
			// TODO: Improve the Client's IP using https://www.virendrachandak.com/techtalk/getting-real-client-ip-address-in-php-2/
59 1
			'ip_address'      => $_SERVER['REMOTE_ADDR'],
60 1
			'result'          => 1,
61 1
		);
62
63 1
		$message = '';
64
65 1
		if ( isset( $mail_info['message'] ) ) {
66
			$message = $mail_info['message'];
67
		} else {
68
			// wpmandrill plugin is changing "message" key to "html". See https://github.com/sudar/email-log/issues/20
69
			// Ideally this should be fixed in wpmandrill, but I am including this hack here till it is fixed by them.
70 1
			if ( isset( $mail_info['html'] ) ) {
71
				$message = $mail_info['html'];
72
			}
73
		}
74
75 1
		$data['message'] = $message;
76
77 1
		$email_log->table_manager->insert_log( $data );
78
79
		/**
80
		 * Fires the `el_email_log_inserted` action right after the log is inserted in to DB.
81
		 *
82
		 * @param array $data {
83
		 *      @type string $to
84
		 *      @type string $subject
85
		 *      @type string $message
86
		 *      @type string $headers
87
		 *      @type string $attachments
88
		 *      @type string $sent_date
89
		 * }
90
		 */
91 1
		do_action( 'el_email_log_inserted', $data );
92
93 1
		return $mail_info;
94
	}
95
}
96