DB
1 year ago
Request
1 year ago
UI
1 year ago
Check_Email_Admin_Capability_Giver.php
1 year ago
Check_Email_Export_Log.php
1 year ago
Check_Email_From_Handler.php
1 year ago
Check_Email_Log.php
1 year ago
Check_Email_Logger.php
1 year ago
Check_Email_Review.php
1 year ago
Loadie.php
1 year ago
Check_Email_Logger.php
199 lines
| 1 | <?php namespace CheckEmail\Core; |
| 2 | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
| 3 | /** |
| 4 | * Log's emails sent through `wp_mail`. |
| 5 | */ |
| 6 | class Check_Email_Logger implements Loadie { |
| 7 | |
| 8 | public function load() { |
| 9 | add_filter( 'wp_mail', array( $this, 'log_email' ) ); |
| 10 | add_action( 'wp_mail_failed', array( $this, 'on_email_failed' ) ); |
| 11 | |
| 12 | /** |
| 13 | * These actions are required for logging BuddyPress emails, since BuddyPress does |
| 14 | * not use wp_mail for sending emails. |
| 15 | */ |
| 16 | add_action( 'bp_send_email_success', array( $this, 'log_buddy_press_email' ), 10, 2 ); |
| 17 | add_action( 'bp_send_email_failure', array( $this, 'log_buddy_press_email' ), 10, 2 ); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Logs email to database. |
| 22 | */ |
| 23 | public function log_email( $original_mail_info ) { |
| 24 | $option = get_option( 'check-email-log-core' ); |
| 25 | $original_mail_info = apply_filters( 'check_email_wp_mail_log', $original_mail_info ); |
| 26 | |
| 27 | $mail_info = wp_parse_args( |
| 28 | $original_mail_info, |
| 29 | array( |
| 30 | 'to' => '', |
| 31 | 'subject' => '', |
| 32 | 'message' => '', |
| 33 | 'headers' => '', |
| 34 | 'cc' => '', |
| 35 | 'attachments' => array(), |
| 36 | ) |
| 37 | ); |
| 38 | |
| 39 | $ip = ''; |
| 40 | if ( isset( $_SERVER['REMOTE_ADDR'] ) ) { |
| 41 | $ip = sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ); |
| 42 | } |
| 43 | |
| 44 | $backtrace_segment = array(); |
| 45 | $backtrace_segment = $this->ck_mail_get_backtrace(); |
| 46 | if(!empty($backtrace_segment) && is_array($backtrace_segment)){ |
| 47 | $backtrace_segment = wp_json_encode($backtrace_segment); |
| 48 | }else{ |
| 49 | $backtrace_segment = null; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | $log = array( |
| 54 | 'to_email' => \CheckEmail\Util\wp_chill_check_email_stringify( $mail_info['to'] ), |
| 55 | 'subject' => esc_html($mail_info['subject']), |
| 56 | 'backtrace_segment'=> $backtrace_segment, |
| 57 | 'headers' => \CheckEmail\Util\wp_chill_check_email_stringify( $mail_info['headers'], "\n" ), |
| 58 | 'attachment_name' => \CheckEmail\Util\wp_chill_check_email_stringify( $mail_info['attachments'] ), |
| 59 | 'sent_date' => current_time( 'mysql' ), |
| 60 | 'ip_address' => $ip, |
| 61 | 'result' => 1, |
| 62 | ); |
| 63 | |
| 64 | if(empty($option) || !isset( $option['log_email_content']) || (isset( $option['log_email_content'])) && $option['log_email_content']){ |
| 65 | $log['message'] = wp_kses_post($mail_info['message']); |
| 66 | } |
| 67 | |
| 68 | if ( empty( $log['attachment_name'] ) ) { |
| 69 | $log['attachments'] = 'false'; |
| 70 | } else { |
| 71 | $log['attachments'] = 'true'; |
| 72 | } |
| 73 | |
| 74 | if (isset($option['forward_email']) && !empty($option['forward_email'])) { |
| 75 | $forward_email_info = $original_mail_info; |
| 76 | |
| 77 | if (isset($option['forward_to']) && !empty($option['forward_to'])) { |
| 78 | $to = \CheckEmail\Util\wp_chill_check_email_stringify( $option['forward_to'] ); |
| 79 | $forward_email_info['to'] = $to; |
| 80 | $forward_header[] = 'Content-Type: text/html; charset=UTF-8'; |
| 81 | |
| 82 | $forward_header = []; |
| 83 | if (isset($option['forward_cc']) && !empty($option['forward_cc'])) { |
| 84 | $copy_to = explode(',',$option['forward_cc']); |
| 85 | foreach($copy_to as $email){ |
| 86 | $forward_header[] = 'Cc: '.$email; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (isset($option['forward_bcc']) && !empty($option['forward_bcc'])) { |
| 91 | $bcc_to = explode(',',$option['forward_bcc']); |
| 92 | foreach($bcc_to as $email){ |
| 93 | $forward_header[] = 'Bcc: '.$email; |
| 94 | } |
| 95 | } |
| 96 | $forward_email_info['headers'] = \CheckEmail\Util\wp_chill_check_email_stringify( $forward_header); |
| 97 | ck_mail_forward_mail($forward_email_info); |
| 98 | } |
| 99 | } |
| 100 | $log = apply_filters( 'check_email_email_log_before_insert', $log, $original_mail_info ); |
| 101 | $check_email = wpchill_check_email(); |
| 102 | $check_email->table_manager->insert_log( $log ); |
| 103 | |
| 104 | |
| 105 | |
| 106 | do_action( 'check_email_log_inserted' ); |
| 107 | |
| 108 | return $original_mail_info; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get the details of the method that originally triggered wp_mail |
| 113 | * |
| 114 | * @return array a single element of the debug_backtrace function |
| 115 | * @since 1.0.12 |
| 116 | */ |
| 117 | private function ck_mail_get_backtrace($functionName = 'wp_mail') |
| 118 | { |
| 119 | $backtraceSegment = null; |
| 120 | $backtrace = debug_backtrace(); |
| 121 | |
| 122 | foreach ($backtrace as $segment) { |
| 123 | if ($segment['function'] == $functionName) { |
| 124 | $backtraceSegment = $segment; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return $backtraceSegment; |
| 129 | } |
| 130 | |
| 131 | public function on_email_failed( $wp_error ) { |
| 132 | if ( ! is_wp_error( $wp_error ) ) { |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | $mail_error_data = $wp_error->get_error_data( 'wp_mail_failed' ); |
| 137 | $mail_error_message = $wp_error->get_error_message( 'wp_mail_failed' ); |
| 138 | |
| 139 | $this->mark_email_log_as_failed(apply_filters('wp_check_email_failed', $mail_error_data, $mail_error_message) ); |
| 140 | } |
| 141 | |
| 142 | public function log_buddy_press_email( $status, $bp_mail ) { |
| 143 | if ( ! class_exists( '\\BP_Email' ) ) { |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | if ( $bp_mail instanceof \BP_Email ) { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | $log = array( |
| 152 | 'to' => array_shift( $bp_mail->get_to() )->get_address(), |
| 153 | 'subject' => $bp_mail->get_subject( 'replace-tokens' ), |
| 154 | 'message' => $bp_mail->get_content( 'replace-tokens' ), |
| 155 | 'headers' => $bp_mail->get_headers( 'replace-tokens ' ), |
| 156 | ); |
| 157 | |
| 158 | $this->log_email( $log ); |
| 159 | |
| 160 | if ( ! $status ) { |
| 161 | $this->mark_email_log_as_failed( $log ); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | protected function mark_email_log_as_failed( $log, $error_message = '' ) { |
| 166 | if ( ! is_array( $log ) ) { |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | if ( ! isset( $log['to'], $log['subject'] ) ) { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | $check_email = wpchill_check_email(); |
| 175 | |
| 176 | $log_item_id = $check_email->table_manager->fetch_log_id_by_data( $log ); |
| 177 | |
| 178 | |
| 179 | if ( empty( $log_item_id ) ) { |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | $check_email->table_manager->mark_log_as_failed( $log_item_id, $error_message ); |
| 184 | |
| 185 | $data = $check_email->table_manager->fetch_log_items_by_id( [$log_item_id] ); |
| 186 | $data = $data[0]; |
| 187 | $data_to_insert = array( |
| 188 | 'check_email_log_id' => $log_item_id, |
| 189 | 'content' => $data['message'], |
| 190 | 'initiator' => $data['backtrace_segment'], |
| 191 | 'created_at' => $data['sent_date'], |
| 192 | ); |
| 193 | |
| 194 | ck_mail_insert_error_logs($data_to_insert); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | } |
| 199 |