Skip to content

Commit

Permalink
Merge pull request #30 from soup-bowl/soup-bowl/issue14
Browse files Browse the repository at this point in the history
Log attachments
  • Loading branch information
soup-bowl committed Jan 31, 2021
2 parents 632cd8b + 09a0489 commit b233650
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 24 deletions.
51 changes: 39 additions & 12 deletions class-log.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace wpsimplesmtp;

use wpsimplesmtp\LogAttachment;

use stdClass;
use WP_Query;

Expand Down Expand Up @@ -40,26 +42,30 @@ public function register_log_storage() {
/**
* Creates a new log entry.
*
* @param string $recipients The person(s) who recieved the email.
* @param string $subject Subject headline from the email.
* @param string $content Whatever was inside the dispatched email.
* @param array $headers Email headers served alongside the dispatch.
* @param string $timestamp The time the email was sent.
* @param string $error Any errors encountered during the exchange.
* @param string $recipients The person(s) who recieved the email.
* @param string $subject Subject headline from the email.
* @param string $content Whatever was inside the dispatched email.
* @param array $headers Email headers served alongside the dispatch.
* @param string[] $attachments Location of attachments in the system.
* @param string $timestamp The time the email was sent.
* @param string $error Any errors encountered during the exchange.
* @return integer ID of the newly-inserted entry.
*/
public function new_log_entry( $recipients, $subject, $content, $headers, $timestamp, $error = null ) {
public function new_log_entry( $recipients, $subject, $content, $headers, $attachments = [], $timestamp = null, $error = null ) {
$timestamp = ( empty( $timestamp ) ) ? current_time( 'mysql' ) : $timestamp;

$post_id = wp_insert_post(
[
'post_title' => $subject,
'post_content' => $content,
'post_status' => 'publish',
'post_type' => $this->post_type,
'meta_input' => [
'recipients' => $recipients,
'headers' => $headers,
'timestamp' => $timestamp,
'error' => $error,
'recipients' => $recipients,
'headers' => $headers,
'attachments' => $attachments,
'timestamp' => $timestamp,
'error' => $error,
],
]
);
Expand Down Expand Up @@ -118,13 +124,34 @@ public function get_log_entry_pages( $limit ) {
$count = (int) wp_count_posts( $this->post_type )->publish;

if ( false !== $count ) {
$count = $count - intval(1);
$count = $count - intval( 1 );
return floor( $count / $limit );
} else {
return 1;
}
}

/**
* Gets an object collection of attachments, if the entry had them.
*
* @param integer $id ID of the email log entry.
* @return LogAttachment[]|null
*/
public function get_log_entry_attachments( $id ) {
$attachments = get_post_meta( $id, 'attachments', true );

if ( ! empty( $attachments ) ) {
$file_collection = [];
foreach ( $attachments as $attachment ) {
$file_collection[] = ( new LogAttachment() )->unpack( $attachment );
}

return $file_collection;
} else {
return null;
}
}

/**
* Deletes a log entry.
*
Expand Down
159 changes: 159 additions & 0 deletions class-logattachment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php
/**
* Simple email configuration within WordPress.
*
* @package sb-simple-smtp
* @author soup-bowl <code@soupbowl.io>
* @license MIT
*/

namespace wpsimplesmtp;

/**
* Object representation of an email system attachment.
*/
class LogAttachment {
/**
* File location on the filesystem.
*
* @var string
*/
protected $location;

/**
* Whether or not the supplied filepath actually exists.
*
* @var boolean
*/
protected $exists;

/**
* File name including extension.
*
* @var string
*/
protected $basename;

/**
* File name.
*
* @var string
*/
protected $filename;

/**
* File extension.
*
* @var string
*/
protected $extension;

/**
* Creates a new log attachment object.
*
* @param string $location The location of the tracked file.
* @return self
*/
public function new( $location ) {
$this->location = $location;

if ( file_exists( $this->location ) ) {
$this->exists = true;

$file = pathinfo( $this->location );
$this->basename = $file['basename'];
$this->filename = $file['filename'];
$this->extension = ( isset( $file['extension'] ) ) ? $file['extension'] : '';
} else {
$this->exists = false;
$this->basename = '';
$this->filename = '';
$this->extension = '';
}

return $this;
}

/**
* The file path of the file.
*
* @return string
*/
public function file_path() {
return $this->location;
}

/**
* Check for whether the file currently exists or not.
*
* @return boolean
*/
public function exists() {
return $this->exists;
}

/**
* Gets the file name.
*
* @return string
*/
public function filename() {
return $this->filename;
}

/**
* Returns the filename, plus extension.
*
* @return string
*/
public function basename() {
return $this->basename;
}

/**
* Gets the file extension.
*
* @return string
*/
public function extension() {
return $this->extension;
}

/**
* Takes the stored JSON and unpacks it back into the object.
*
* @param string $input The direct output of the to_string function.
* @return self
*/
public function unpack( $input ) {
$input = json_decode( $input );
$this->location = $input->location;
$this->basename = $input->basename;
$this->filename = $input->filename;
$this->extension = $input->extension;

if ( file_exists( $this->location ) ) {
$this->exists = true;
} else {
$this->exists = false;
}

return $this;
}

/**
* Returns the current object class as a JSON string.
*
* @return string
*/
public function to_string() {
return wp_json_encode(
[
'location' => $this->location,
'basename' => $this->basename,
'filename' => $this->filename,
'extension' => $this->extension,
]
);
}
}
2 changes: 1 addition & 1 deletion class-logtable.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function display( $page, $maximum_per_page = 5 ) {
$message = sprintf( __( 'Showing page %1$s of %2$s.', 'simple-smtp' ), $page_cu, $page_co );
$nav_buttons = $this->generate_table_buttons( $page, $pages );

if ( floatval(0) === $page_co ) {
if ( floatval( 0 ) === $page_co ) {
// Do not display navigation if 0 pages/entries.
return;
}
Expand Down
8 changes: 7 additions & 1 deletion class-mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use wpsimplesmtp\Options;
use wpsimplesmtp\Log;
use wpsimplesmtp\LogAttachment;

/**
* Configures PHPMailer to use our settings rather than the default.
Expand Down Expand Up @@ -128,12 +129,17 @@ public function preprocess_mail( $parameters ) {
if ( true === filter_var( $this->options->get( 'log' )->value, FILTER_VALIDATE_BOOLEAN ) ) {
$recipient_array = ( is_array( $parameters['to'] ) ) ? $parameters['to'] : [ $parameters['to'] ];

$attachments = [];
foreach ( $parameters['attachments'] as $attachment ) {
$attachments[] = ( new LogAttachment() )->new( $attachment )->to_string();
}

$wpss_mail_id = $this->log->new_log_entry(
wp_json_encode( $recipient_array ),
$parameters['subject'],
$parameters['message'],
wp_json_encode( $parameters['headers'] ),
current_time( 'mysql' )
$attachments
);
}

Expand Down
43 changes: 34 additions & 9 deletions class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function test_email_handler() {
// translators: %s is the website name.
sprintf( __( 'Test email from %s', 'simple-smtp' ), get_bloginfo( 'name' ) ),
$content,
[ 'x-test: WP SMTP', $content_type ]
[ 'x-test: WP SMTP', $content_type ],
);

wp_safe_redirect( admin_url( 'options-general.php?page=wpsimplesmtp' ) );
Expand All @@ -243,10 +243,18 @@ public function test_email_handler() {
* @return boolean
*/
public function resend_email( $email_id ) {
$email = $this->log->get_log_entry_by_id( $email_id );
$recipients = implode( ', ', json_decode( get_post_meta( $email->ID, 'recipients', true ) ) );
$headers = json_decode( get_post_meta( $email->ID, 'headers', true ) );
$opts = get_option( 'wpss_resent', [] );
$email = $this->log->get_log_entry_by_id( $email_id );
$attachments = $this->log->get_log_entry_attachments( $email_id );
$recipients = implode( ', ', json_decode( get_post_meta( $email->ID, 'recipients', true ) ) );
$headers = json_decode( get_post_meta( $email->ID, 'headers', true ) );
$opts = get_option( 'wpss_resent', [] );

$attachpaths = [];
foreach ( $attachments as $attachment ) {
if ( $attachment->exists() ) {
$attachpaths[] = $attachment->file_path();
}
}

if ( isset( $email ) && ! in_array( $email_id, $opts, true ) ) {
$opts[] = $email_id;
Expand All @@ -256,7 +264,8 @@ public function resend_email( $email_id ) {
$recipients,
$email->post_title,
$email->post_content,
$headers
$headers,
$attachpaths
);

return true;
Expand Down Expand Up @@ -414,9 +423,10 @@ private function render_settings() {
* @return void Prints to page.
*/
private function render_email_view( $id ) {
$log = $this->log->get_log_entry_by_id( $id );
$recset = ( in_array( (int) $id, get_option( 'wpss_resent', [] ), true ) ) ? ' disabled' : '';
$resend_url = add_query_arg(
$log = $this->log->get_log_entry_by_id( $id );
$attachments = $this->log->get_log_entry_attachments( $id );
$recset = ( in_array( (int) $id, get_option( 'wpss_resent', [] ), true ) ) ? ' disabled' : '';
$resend_url = add_query_arg(
[
'eid' => $id,
'ssnonce' => wp_create_nonce( 'wpss_action' ),
Expand Down Expand Up @@ -455,6 +465,21 @@ private function render_email_view( $id ) {
<div id="misc-publishing-actions">
<div class="misc-pub-section"><?php esc_html_e( 'Recipient(s)', 'simple-smtp' ); ?>: <strong><?php echo esc_html( $recipients ); ?></strong></div>
<div class="misc-pub-section"><?php esc_html_e( 'Date sent', 'simple-smtp' ); ?>: <strong><?php echo esc_html( $date ); ?></strong></div>
<?php if ( ! empty( $attachments ) ) : ?>
<div class="misc-pub-section">
<?php esc_html_e( 'Attachment(s)', 'simple-smtp' ); ?>:
<ol>
<?php foreach ( $attachments as $attachment ) : ?>
<li>
<?php echo esc_html( $attachment->basename() ); ?>
<?php if ( ! $attachment->exists() ) : ?>
<span class="wpsmtp-badge wpsmtp-badge-warning"><?php esc_html_e( 'File missing', 'simple-smtp' ); ?></span>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ol>
</div>
<?php endif; ?>
</div>
<div class="clear"></div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"class-options.php",
"class-mail.php",
"class-log.php",
"class-logtable.php"
"class-logtable.php",
"class-logattachment.php"
]
},
"require-dev": {
Expand Down
Loading

0 comments on commit b233650

Please sign in to comment.