Completed
Push — master ( b9b169...bdebae )
by Sudar
14:50
created

EmailLogger::log_email()   C

Complexity

Conditions 7
Paths 36

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7.5375

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 20
c 1
b 0
f 0
nc 36
nop 1
dl 0
loc 34
ccs 14
cts 18
cp 0.7778
crap 7.5375
rs 6.7272
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 {
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 2
	public function log_email( $mail_info ) {
26 2
		$email_log = email_log();
27
28
		$headers = '';
29 2
		if ( isset( $mail_info['headers'] ) ) {
30 2
			$headers = is_array( $mail_info['headers'] ) ? implode( "\n", $mail_info['headers'] ) : $mail_info['headers'];
31 2
		}
32 2
33 2
		$data = array(
34 2
			'attachments' => ( count( $mail_info['attachments'] ) > 0 ) ? 'true' : 'false',
35
			'to_email'    => is_array( $mail_info['to'] ) ? implode( ',', $mail_info['to'] ) : $mail_info['to'],
36 2
			'subject'     => $mail_info['subject'],
37
			'headers'     => $headers,
38 2
			'sent_date'   => current_time( 'mysql' ),
39
		);
40
41
		$message = '';
42
43 2
		if ( isset( $mail_info['message'] ) ) {
44
			$message = $mail_info['message'];
45
		} else {
46
			// wpmandrill plugin is changing "message" key to "html". See https://github.com/sudar/email-log/issues/20
47
			// Ideally this should be fixed in wpmandrill, but I am including this hack here till it is fixed by them.
48 2
			if ( isset( $mail_info['html'] ) ) {
49
				$message = $mail_info['html'];
50 2
			}
51
		}
52 2
53
		$data['message'] = $message;
54
55
		$email_log->table_manager->insert_log( $data );
56
57
		return $mail_info;
58
	}
59
}
60