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

EmailLogger   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 66.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 51
ccs 14
cts 21
cp 0.6667
rs 10
wmc 8
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 3 1
C log_email() 0 34 7
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