statusMessage .= $message; } } } if ( ! class_exists( 'PostmanEmailLogService' ) ) { /** * This class creates the Custom Post Type for Email Logs and handles writing these posts. * * @author jasonhendriks */ class PostmanEmailLogService { /* * Private content is published only for your eyes, or the eyes of only those with authorization * permission levels to see private content. Normal users and visitors will not be aware of * private content. It will not appear in the article lists. If a visitor were to guess the URL * for your private post, they would still not be able to see your content. You will only see * the private content when you are logged into your WordPress blog. */ const POSTMAN_CUSTOM_POST_STATUS_PRIVATE = 'private'; // member variables private $logger; private $inst; public $new_logging = false; /** * Constructor */ private function __construct() { $this->logger = new PostmanLogger( get_class( $this ) ); $this->new_logging = get_option( 'postman_db_version' ); add_action('post_smtp_on_success', array( $this, 'write_success_log' ), 10, 4 ); add_action('post_smtp_on_failed', array( $this, 'write_failed_log' ), 10, 5 ); } /** * singleton instance */ public static function getInstance() { static $inst = null; if ( $inst === null ) { $inst = new PostmanEmailLogService(); } return $inst; } public function write_success_log($log, $message, $transcript, $transport) { $options = PostmanOptions::getInstance(); if ( $options->getRunMode() == PostmanOptions::RUN_MODE_PRODUCTION || $options->getRunMode() == PostmanOptions::RUN_MODE_LOG_ONLY ) { $this->writeSuccessLog( $log, $message, $transcript, $transport ); } } public function write_failed_log($log, $message, $transcript, $transport, $statusMessage) { $options = PostmanOptions::getInstance(); if ( $options->getRunMode() == PostmanOptions::RUN_MODE_PRODUCTION || $options->getRunMode() == PostmanOptions::RUN_MODE_LOG_ONLY ) { $this->writeFailureLog( $log, $message, $transcript, $transport, $statusMessage ); } } /** * Logs successful email attempts * * @param PostmanMessage $message * @param mixed $transcript * @param PostmanModuleTransport $transport */ public function writeSuccessLog( PostmanEmailLog $log, PostmanMessage $message, $transcript, PostmanModuleTransport $transport ) { if ( PostmanOptions::getInstance()->isMailLoggingEnabled() ) { $statusMessage = ''; $status = true; $subject = $message->getSubject(); if ( empty( $subject ) ) { $statusMessage = sprintf( '%s: %s', __( 'Warning', 'post-smtp' ), __( 'An empty subject line can result in delivery failure.', 'post-smtp' ) ); $status = 'WARN'; } $this->createLog( $log, $message, $transcript, $statusMessage, $status, $transport ); $this->writeToEmailLog( $log ); } } /** * Logs failed email attempts, requires more metadata so the email can be resent in the future * * @param PostmanMessage $message * @param mixed $transcript * @param PostmanModuleTransport $transport * @param mixed $statusMessage * @param mixed $originalTo * @param mixed $originalSubject * @param mixed $originalMessage * @param mixed $originalHeaders */ public function writeFailureLog( PostmanEmailLog $log, PostmanMessage $message = null, $transcript, PostmanModuleTransport $transport, $statusMessage ) { if ( PostmanOptions::getInstance()->isMailLoggingEnabled() ) { $this->createLog( $log, $message, $transcript, $statusMessage, false, $transport ); $this->writeToEmailLog( $log,$message ); } } /** * Writes an email sending attempt to the Email Log * * From http://wordpress.stackexchange.com/questions/8569/wp-insert-post-php-function-and-custom-fields */ private function writeToEmailLog( PostmanEmailLog $log, PostmanMessage $message = null ) { $options = PostmanOptions::getInstance(); $new_status = $log->statusMessage; if ( $options->is_fallback && empty( $log->statusMessage ) ) { $new_status = 'Sent ( ** Fallback ** )'; } if ( $options->is_fallback && ! empty( $log->statusMessage ) ) { $new_status = '( ** Fallback ** ) ' . $log->statusMessage; } $new_status = apply_filters( 'post_smtp_log_status', $new_status, $log, $message ); //If Table exists, Insert Log into Table if( $this->new_logging ) { $data = array(); $data['solution'] = apply_filters( 'post_smtp_log_solution', null, $new_status, $log, $message ); $data['success'] = empty( $new_status ) ? 1 : $new_status; $data['from_header'] = $log->sender; $data['to_header'] = !empty( $log->toRecipients ) ? $log->toRecipients : ''; $data['cc_header'] = !empty( $log->ccRecipients ) ? $log->ccRecipients : ''; $data['bcc_header'] = !empty( $log->bccRecipients ) ? $log->bccRecipients : ''; $data['reply_to_header'] = !empty( $log->replyTo ) ? $log->replyTo : ''; $data['transport_uri'] = !empty( $log->transportUri ) ? $log->transportUri : ''; $data['original_to'] = is_array( $log->originalTo ) ? implode( ',', $log->originalTo ) : $log->originalTo; $data['original_subject'] = !empty( $log->originalSubject ) ? $log->originalSubject : ''; $data['original_message'] = $log->originalMessage; $data['original_headers'] = is_array($log->originalHeaders) ? serialize($log->originalHeaders) : $log->originalHeaders; $data['session_transcript'] = $log->sessionTranscript; $email_logs = new PostmanEmailLogs(); /** * Filter the email log id * * @param string $log_id * @since 2.5.0 * @version 1.0.0 */ $log_id = apply_filters( 'post_smtp_update_email_log_id', '' ); $log_id = $email_logs->save( $data, $log_id ); /** * Fires after the email log is saved * * @param string $log_id * @since 2.5.0 * @version 1.0.0 */ do_action( 'post_smtp_after_email_log_saved', $log_id ); $this->logger->debug( sprintf( 'Saved message #%s to the database', $log_id ) ); $this->logger->trace( $log ); } //Do as previous else { // nothing here is sanitized as WordPress should take care of // making database writes safe $my_post = array( 'post_type' => PostmanEmailLogPostType::POSTMAN_CUSTOM_POST_TYPE_SLUG, 'post_title' => $log->subject, 'post_content' => $log->body, 'post_excerpt' => $new_status, 'post_status' => PostmanEmailLogService::POSTMAN_CUSTOM_POST_STATUS_PRIVATE, ); // Insert the post into the database (WordPress gives us the Post ID) $post_id = wp_insert_post( $my_post, true ); if ( is_wp_error( $post_id ) ) { add_action( 'admin_notices', function() use( $post_id ) { $class = 'notice notice-error'; $message = $post_id->get_error_message(); printf( '
%2$s