Skip to content

Commit

Permalink
SparkPost completion. See: wpsharks/comment-mail#265
Browse files Browse the repository at this point in the history
  • Loading branch information
jaswsinc committed Nov 4, 2016
1 parent 30bdace commit f42a52f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 98 deletions.
6 changes: 3 additions & 3 deletions src/includes/classes/MenuPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,7 @@ protected function optionsX()
' <li>'.sprintf(__('Replies to comments via email will be functional for all types of notifications sent by %1$s (including digest notifications). However, there are a few things worth noting before you enable an RVE Handler. <a href="#" data-toggle="other" data-other=".pmp-rve-details">Click here to toggle important details</a>.', SLUG_TD), esc_html(NAME)).
' <ul class="pmp-rve-details" style="display:none;">'.
' <li>'.sprintf(__('All replies posted via email must be sent to the special <code>Reply-To</code> address that you configure below. Not to worry though, because once you configure the <code>Reply-To</code> for an RVE Handler, %1$s will automatically set the <code>Reply-To:</code> header in all email notifications that it sends. This way when somebody replies to a comment notification, their email program will reply to the address required for replies via email to work properly.', SLUG_TD), esc_html(NAME)).'</li>'.
' <li>'.sprintf(__('The <code>Reply-To</code> address that you configure below, will serve as a base for %1$s to work from. For instance, let\'s say you choose: <code>rve@sparkpost.%2$s</code>. This base address will be suffixed automatically (at runtime) with details specific to a particular notification that %1$s sends. Ultimately, <code>rve@sparkpost.%2$s</code> will look like: <code>rve<strong>+332-96-kgjdgxr4ldqpdrgjdgxr</strong>@sparkpost.%2$s</code>. In this example, the additional details (following the <code>+</code> sign) are there to help %1$s route the reply to the proper location, and to provide a means by which to identify the end-user that is posting a reply.', SLUG_TD), esc_html(NAME), esc_html($this->plugin->utils_url->currentHostBase())).'</li>'.
' <li>'.sprintf(__('The <code>Reply-To</code> address that you configure below, will serve as a base for %1$s to work from. For instance, let\'s say you choose: <code>rve@spark.%2$s</code>. This base address will be suffixed automatically (at runtime) with details specific to a particular notification that %1$s sends. Ultimately, <code>rve@spark.%2$s</code> will look like: <code>rve<strong>+332-96-kgjdgxr4ldqpdrgjdgxr</strong>@spark.%2$s</code>. In this example, the additional details (following the <code>+</code> sign) are there to help %1$s route the reply to the proper location, and to provide a means by which to identify the end-user that is posting a reply.', SLUG_TD), esc_html(NAME), esc_html($this->plugin->utils_url->currentHostBase())).'</li>'.
' <li>'.sprintf(__('For single-comment notifications; i.e., where a subscriber chooses delivery type <code>asap</code> (aka: instantly), there is just a single comment in each notification that a subscriber receives. This works best with replies via email, since the <code>Reply-To:</code> header (on its own) is enough for everything to work as expected. Someone replying via email need only hit the Reply button in their email program and start typing. Very simple.', SLUG_TD), esc_html(NAME)).'</li>'.
' <li>'.sprintf(__('For multi-comment notifications; i.e., where a subscriber chooses a delivery type that is not <code>asap</code> (e.g., <code>hourly</code>, <code>daily</code>, etc.); there can be more than a single comment in each notification they receive. If there is more than one comment in the notification, instructions will be provided to the end-user explaining how to reply. The special <code>Reply-To</code> address is still used in this case. However, they also need to specify which comment they want to reply to. To do this, the end-user must start their reply with a special marker provided by %1$s. Again, if there is more than one comment in the notification, instructions will be provided to the end-user explaining how to reply.', SLUG_TD), esc_html(NAME)).'</li>'.
' <li>'.sprintf(__('Comments posted via email are still piped through the same underlying WordPress handler that normal on-site comments go through (i.e., <code>/wp-comments-post.php</code>). This means that all of your existing WordPress Discussion Settings (and/or Akismet settings) will still apply to all comments, even if they are posted via email. <strong>With one exception.</strong> When an RVE Handler is enabled, any comments posted via email are allowed through without an end-user being logged-in. If your WordPress Discussion Settings require that users be logged-in to post comments, that will be overridden temporarily whenever a reply via email comes through. Please note that replies posted via email are generally from confirmed subscribers. Any reply via email that is not from a confirmed subscriber will be forced into moderation by %1$s anyway. Otherwise, whatever your current Discussion Settings are configured to allow, will be adhered to for replies via email also. For instance, if you require that all comments be moderated, that will continue to be the case for all replies via email. %1$s will never approve a comment on it\'s own. Approval of comments is always determined by your WP Discussion Settings.', SLUG_TD), esc_html(NAME)).'</li>'.
Expand All @@ -1154,7 +1154,7 @@ protected function optionsX()

$_panel_body .= '<div class="pmp-if-enabled-show pmp-if-nest"><hr />'.

'<a href="http://comment-mail.com/r/sparkpost-webhooks/" target="_blank">'.
'<a href="http://comment-mail.com/r/sparkpost/" target="_blank">'.
'<img src="'.esc_attr($this->plugin->utils_url->to('/src/client-s/images/sparkpost-rve.png')).'" class="pmp-right" style="margin-left:3em;" /></a>'.

' <table style="width:auto; margin-bottom:0;">'.
Expand Down Expand Up @@ -1199,7 +1199,7 @@ protected function optionsX()
[
'type' => 'email',
'label' => __('SparkPost <code>Reply-To</code> Address:', SLUG_TD),
'placeholder' => sprintf(__('e.g., rve@sparkpost.%1$s', SLUG_TD), $this->plugin->utils_url->currentHostBase()),
'placeholder' => sprintf(__('e.g., rve@spark.%1$s', SLUG_TD), $this->plugin->utils_url->currentHostBase()),
'name' => 'rve_sparkpost_reply_to_email',
'current_value' => $current_value_for('rve_sparkpost_reply_to_email'),
'notes_after' => '<p>'.sprintf(__('Please see %1$s for detailed instructions.', SLUG_TD), $this->plugin->utils_markup->xAnchor('http://comment-mail.com/kb-article/sparkpost-rve-handler/', __('this KB article', SLUG_TD))).'</p>',
Expand Down
139 changes: 44 additions & 95 deletions src/includes/classes/RveSparkPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ protected function maybeProcess()
*/
protected function collectMessage()
{
$response = file_get_contents('php://input');
$response = json_decode((string) $response);
$response = json_decode((string) file_get_contents('php://input'));

if (empty($response[0]->msys->relay_message)) {
return; // Nothing to do.
Expand All @@ -116,70 +115,55 @@ protected function collectMessage()
*/
protected function processMessage()
{
foreach ($this->events as $_event) {
// Iterate all events.

if (empty($_event->ts) || $_event->ts < strtotime('-7 days')) {
continue; // Missing timestamp; or it's very old.
}
if (empty($_event->event) || $_event->event !== 'inbound') {
continue; // Expecting an inbound event.
}
if (empty($_event->msg) || !($_event->msg instanceof \stdClass)) {
continue; // Expecting a msg object w/ properties.
}
$_reply_to_email = $this->issetOr($_event->msg->email, '', 'string');

$_from_name = $this->issetOr($_event->msg->from_name, '', 'string');
$_from_email = $this->issetOr($_event->msg->from_email, '', 'string');

$_subject = $this->issetOr($_event->msg->subject, '', 'string');

$_text_body = $this->issetOr($_event->msg->text, '', 'string');
$_html_body = $this->issetOr($_event->msg->html, '', 'string');
if (!$this->message) {
return; // Not possible.
} elseif (empty($this->message->protocol)) {
return; // Not possible.
} elseif (empty($this->message->webhook_id)) {
return; // Not possible.
} elseif ($this->message->protocol !== 'smtp') {
return; // Not possible.
} elseif ($this->message->webhook_id !== $this->plugin->options['rve_sparkpost_webhook_id']) {
return; // Webhook ID does not match.
} elseif (empty($this->message->content->headers)) {
return; // Not possible.
}
$headers = []; // Initialize array of all headers.

foreach ($this->message->content->headers as $_header) {
$headers = array_merge($headers, (array) $_header);
} // unset($_header); // Housekeeping.

if (empty($headers['To']) || empty($headers['From'])) {
return; // Not possible; missing headers.
} elseif (!($to_addresses = array_values($this->plugin->utils_mail->parseAddressesDeep($headers['To'])))) {
return; // Not possible; no recipient addresses.
} elseif (!($from_addresses = array_values($this->plugin->utils_mail->parseAddressesDeep($headers['From'])))) {
return; // Not possible; no from addresses.
}
$reply_to_email = $to_addresses[0]->email;

if (isset($_event->msg->spam_report->score)) {
$_spam_score = (float) $_event->msg->spam_report->score;
} else {
$_spam_score = 0.0; // Default value.
}
if (isset($_event->msg->spf->result)) {
$_spf_result = strtolower((string) $_event->msg->spf->result);
} else {
$_spf_result = 'none'; // Default value.
}
if (isset($_event->msg->dkim->signed)) {
$_dkim_signed = (bool) $_event->msg->dkim->signed;
} else {
$_dkim_signed = false; // Default value.
}
if (isset($_event->msg->dkim->valid)) {
$_dkim_valid = (bool) $_event->msg->dkim->valid;
} else {
$_dkim_valid = false; // Default value.
}
$this->maybeProcessCommentReply(
[
'reply_to_email' => $_reply_to_email,
$from_name = trim($from_addresses[0]->fname.' '.$from_addresses[0]->lname);
$from_email = $from_addresses[0]->email;

'from_name' => $_from_name,
'from_email' => $_from_email,
$subject = $this->message->content->subject;

'subject' => $_subject,
$text_body = $this->message->content->text;
$html_body = $this->message->content->html;

'text_body' => $_text_body,
'html_body' => $_html_body,
$this->maybeProcessCommentReply(
[
'reply_to_email' => $reply_to_email,

'spam_score' => $_spam_score,
'from_name' => $from_name,
'from_email' => $from_email,

'spf_result' => $_spf_result,
'subject' => $subject,

'dkim_signed' => $_dkim_signed,
'dkim_valid' => $_dkim_valid,
]
);
}
unset($_event); // Housekeeping.
'text_body' => $text_body,
'html_body' => $html_body,
]
);
}

/**
Expand All @@ -201,13 +185,6 @@ protected function maybeProcessCommentReply(array $args)

'text_body' => '',
'html_body' => '',

'spam_score' => 0.0,

'spf_result' => 'none',

'dkim_signed' => false,
'dkim_valid' => false,
];
$args = array_merge($default_args, $args);
$args = array_intersect_key($args, $default_args);
Expand All @@ -222,15 +199,6 @@ protected function maybeProcessCommentReply(array $args)
$text_body = trim((string) $args['text_body']);
$html_body = trim((string) $args['html_body']);

$spam_score = (float) $args['spam_score'];

$spf_result = trim(strtolower((string) $args['spf_result']));

$dkim_signed = (bool) $args['dkim_signed'];
$dkim_valid = (bool) $args['dkim_valid'];

$force_status = null; // Initialize.

if (!$reply_to_email) { // Must have this.
return; // Missing `Reply-To:` address.
}
Expand All @@ -240,23 +208,6 @@ protected function maybeProcessCommentReply(array $args)
if (!($rich_text_body = $this->coalesce($html_body, $text_body))) {
return; // Empty reply; nothing to do here.
}
if ($spam_score >= (float) $this->plugin->options['rve_mandrill_max_spam_score']) {
$force_status = 'spam'; // Force this to be considered `spam`.
}
if (($spf_check = (int) $this->plugin->options['rve_mandrill_spf_check_enable'])) {
if (($spf_check === 1 && !in_array($spf_result, ['pass', 'neutral', 'softfail', 'none'], true))
|| ($spf_check === 2 && !in_array($spf_result, ['pass', 'neutral', 'none'], true))
|| ($spf_check === 3 && !in_array($spf_result, ['pass', 'neutral'], true))
|| ($spf_check === 4 && !in_array($spf_result, ['pass'], true))
) {
$force_status = 'spam'; // Force this to be considered `spam`.
}
}
if (($dkim_check = (int) $this->plugin->options['rve_mandrill_dkim_check_enable'])) {
if (($dkim_check === 1 && $dkim_signed && !$dkim_valid) || ($dkim_check === 2 && (!$dkim_signed || !$dkim_valid))) {
$force_status = 'spam'; // Force this to be considered `spam`.
}
}
$post_comment_args = compact(
'reply_to_email',
//
Expand All @@ -265,9 +216,7 @@ protected function maybeProcessCommentReply(array $args)
//
'subject',
//
'rich_text_body',
//
'force_status'
'rich_text_body'
);
$this->plugin->utils_rve->maybePostComment($post_comment_args);
}
Expand Down

0 comments on commit f42a52f

Please sign in to comment.