Completed
Push — master ( e85e3d...0703ae )
by Sudar
03:41
created

EmailLogger   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
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 46
ccs 14
cts 21
cp 0.6667
rs 10
wmc 7
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 3 1
B log_email() 0 29 6
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
		$data = array(
29 2
			'attachments' => ( count( $mail_info['attachments'] ) > 0 ) ? 'true' : 'false',
30 2
			'to_email'    => is_array( $mail_info['to'] ) ? implode( ',', $mail_info['to'] ) : $mail_info['to'],
31 2
			'subject'     => $mail_info['subject'],
32 2
			'headers'     => is_array( $mail_info['headers'] ) ? implode( "\n", $mail_info['headers'] ) : $mail_info['headers'],
33 2
			'sent_date'   => current_time( 'mysql' ),
34 2
		);
35
36 2
		$message = '';
37
38 2
		if ( isset( $mail_info['message'] ) ) {
39
			$message = $mail_info['message'];
40
		} else {
41
			// wpmandrill plugin is changing "message" key to "html". See https://github.com/sudar/email-log/issues/20
42
			// Ideally this should be fixed in wpmandrill, but I am including this hack here till it is fixed by them.
43 2
			if ( isset( $mail_info['html'] ) ) {
44
				$message = $mail_info['html'];
45
			}
46
		}
47
48 2
		$data['message'] = $message;
49
50 2
		$email_log->table_manager->insert_log( $data );
51
52 2
		return $mail_info;
53
	}
54
}