DB
2 years ago
Request
2 years ago
UI
2 years ago
Check_Email_Admin_Capability_Giver.php
2 years ago
Check_Email_From_Handler.php
2 years ago
Check_Email_Log.php
2 years ago
Check_Email_Logger.php
2 years ago
Check_Email_Review.php
2 years ago
Loadie.php
2 years ago
Check_Email_Logger.php
127 lines
| 1 | <?php namespace CheckEmail\Core; |
| 2 | |
| 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 | |
| 26 | if ( is_array( $option ) && array_key_exists( 'enable_logs', $option ) && 'true' === strtolower( $option['enable_logs'] ) ) { |
| 27 | $original_mail_info = apply_filters( 'check_email_wp_mail_log', $original_mail_info ); |
| 28 | |
| 29 | $mail_info = wp_parse_args( |
| 30 | $original_mail_info, |
| 31 | array( |
| 32 | 'to' => '', |
| 33 | 'subject' => '', |
| 34 | 'message' => '', |
| 35 | 'headers' => '', |
| 36 | 'attachments' => array(), |
| 37 | ) |
| 38 | ); |
| 39 | |
| 40 | $ip = ''; |
| 41 | if ( isset( $_SERVER['REMOTE_ADDR'] ) ) { |
| 42 | $ip = sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ); |
| 43 | } |
| 44 | |
| 45 | $log = array( |
| 46 | 'to_email' => \CheckEmail\Util\wp_chill_check_email_stringify( $mail_info['to'] ), |
| 47 | 'subject' => esc_html($mail_info['subject']), |
| 48 | 'message' => wp_kses_post($mail_info['message']), |
| 49 | 'headers' => \CheckEmail\Util\wp_chill_check_email_stringify( $mail_info['headers'], "\n" ), |
| 50 | 'attachment_name' => \CheckEmail\Util\wp_chill_check_email_stringify( $mail_info['attachments'] ), |
| 51 | 'sent_date' => current_time( 'mysql' ), |
| 52 | 'ip_address' => $ip, |
| 53 | 'result' => 1, |
| 54 | ); |
| 55 | |
| 56 | if ( empty( $log['attachment_name'] ) ) { |
| 57 | $log['attachments'] = 'false'; |
| 58 | } else { |
| 59 | $log['attachments'] = 'true'; |
| 60 | } |
| 61 | |
| 62 | $log = apply_filters( 'check_email_email_log_before_insert', $log, $original_mail_info ); |
| 63 | |
| 64 | $check_email = wpchill_check_email(); |
| 65 | $check_email->table_manager->insert_log( $log ); |
| 66 | |
| 67 | do_action( 'check_email_log_inserted' ); |
| 68 | } |
| 69 | |
| 70 | return $original_mail_info; |
| 71 | } |
| 72 | |
| 73 | public function on_email_failed( $wp_error ) { |
| 74 | if ( ! is_wp_error( $wp_error ) ) { |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | $mail_error_data = $wp_error->get_error_data( 'wp_mail_failed' ); |
| 79 | $mail_error_message = $wp_error->get_error_message( 'wp_mail_failed' ); |
| 80 | |
| 81 | $this->mark_email_log_as_failed( $mail_error_data, $mail_error_message ); |
| 82 | } |
| 83 | |
| 84 | public function log_buddy_press_email( $status, $bp_mail ) { |
| 85 | if ( ! class_exists( '\\BP_Email' ) ) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | if ( $bp_mail instanceof \BP_Email ) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | $log = array( |
| 94 | 'to' => array_shift( $bp_mail->get_to() )->get_address(), |
| 95 | 'subject' => $bp_mail->get_subject( 'replace-tokens' ), |
| 96 | 'message' => $bp_mail->get_content( 'replace-tokens' ), |
| 97 | 'headers' => $bp_mail->get_headers( 'replace-tokens ' ), |
| 98 | ); |
| 99 | |
| 100 | $this->log_email( $log ); |
| 101 | |
| 102 | if ( ! $status ) { |
| 103 | $this->mark_email_log_as_failed( $log ); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | protected function mark_email_log_as_failed( $log, $error_message = '' ) { |
| 108 | if ( ! is_array( $log ) ) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | if ( ! isset( $log['to'], $log['subject'] ) ) { |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | $check_email = wpchill_check_email(); |
| 117 | |
| 118 | $log_item_id = $check_email->table_manager->fetch_log_id_by_data( $log ); |
| 119 | |
| 120 | if ( empty( $log_item_id ) ) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | $check_email->table_manager->mark_log_as_failed( $log_item_id, $error_message ); |
| 125 | } |
| 126 | } |
| 127 |