Completed
Pull Request — dev/2.3.0 (#152)
by Sudar
27:25 queued 24:56
created

EmailLogger::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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