Skip to content

Commit

Permalink
Merge pull request #98 from SparkPost/fix-template-id
Browse files Browse the repository at this point in the history
Fix template
  • Loading branch information
rajumsys authored Feb 16, 2017
2 parents b94f0af + abb1acd commit 2209375
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
5 changes: 4 additions & 1 deletion admin.widget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ public function set_html_content_type()
private function send_email($recipient)
{
add_filter('wp_mail_content_type', array($this, 'set_html_content_type'));
$headers = array();
$attachments= array(__DIR__ . '/sample.txt');
$result = wp_mail($recipient,
'SparkPost email test',
'<h3>Hurray!!</h3><p>You\'ve got mail! <br/><br> Regards,<br/><a href="https://www.sparkpost.com">SparkPost</a> WordPress plugin</p>',
''
$headers,
$attachments
);
remove_filter('wp_mail_content_type', array($this, 'set_html_content_type'));
return $result;
Expand Down
8 changes: 5 additions & 3 deletions mailer.http.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,18 @@ protected function get_request_body()
'transactional' => (bool) apply_filters('wpsp_transactional', $this->settings['transactional'])
);

$template_id = apply_filters('wpsp_template_id', $this->settings['template']);

// pass through either stored template or inline content
if (!empty($this->settings['template'])) {
if (!empty($template_id)) {
// stored template
$body['content']['template_id'] = apply_filters('wpsp_template_id', $this->settings['template']);
$body['content']['template_id'] = $template_id;

// supply substitution data so users can add variables to templates
$body['substitution_data']['content'] = $this->Body;
$body['substitution_data']['subject'] = $this->Subject;
$body['substitution_data']['from_name'] = $sender['name'];
$body['substitution_data']['from'] = $sender['name'] . ' <' . $sender['email'] . '>';
$body['substitution_data']['from'] = $sender['email'];
if ($replyTo) {
$body['substitution_data']['reply_to'] = $replyTo;
}
Expand Down
1 change: 1 addition & 0 deletions sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a sample file to be sent as an attachment.
26 changes: 24 additions & 2 deletions tests/specs/test-mailer.http.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ function test_get_request_body_without_template() {

NSA::setProperty($this->mailer, 'settings', [
'enable_tracking' => true,
'transactional' => false
'transactional' => false,
'template' => ''
]);

$header_to = 'abc <abc@xyz.com>';
Expand Down Expand Up @@ -259,6 +260,27 @@ function test_get_request_body_without_template() {
$this->assertTrue($expected_request_body == $actual);
}

function test_get_request_body_template_in_hook_but_not_in_settings() {
$this->mailer->addAddress('abc@xyz.com', 'abc');
$this->mailer->setFrom( 'me@hello.com', 'me');

$callback = function(){
return 'test-template';
};

add_filter('wpsp_template_id', $callback);

NSA::setProperty($this->mailer, 'settings', [
'enable_tracking' => true,
'transactional' => false,
'template' => ''
]);

$body = NSA::invokeMethod($this->mailer, 'get_request_body');
remove_filter('wpsp_template_id', $callback);
$this->assertTrue($body['content']['template_id'] == 'test-template');
}

function test_get_request_body_with_template() {
$this->mailer->addAddress('abc@xyz.com', 'abc');
$this->mailer->addBcc('bcc@xyz.com', 'bcc');
Expand Down Expand Up @@ -304,7 +326,7 @@ function test_get_request_body_with_template() {
'content' => '',
'subject' => '',
'from_name' => 'me',
'from' => 'me <me@hello.com>',
'from' => 'me@hello.com',
'from_localpart' => 'me'
]
];
Expand Down

0 comments on commit 2209375

Please sign in to comment.