Skip to content

Commit a0eb172

Browse files
committed
Merge pull request #22 from SparkPost/ISSUE-10
Add template support
2 parents 33cfc66 + 864ebe6 commit a0eb172

File tree

4 files changed

+59
-26
lines changed

4 files changed

+59
-26
lines changed

trunk/admin.widget.class.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public function admin_page_init()
131131
add_settings_field('sending_method', 'Method*', array($this, 'render_sending_method_field'), 'sp-options', 'general');
132132
add_settings_field('password', 'API Key*', array($this, 'render_password_field'), 'sp-options', 'general');
133133
add_settings_field('enable_tracking', 'Enable tracking*', array($this, 'render_enable_tracking_field'), 'sp-options', 'general');
134+
add_settings_field('template', 'Template', array($this, 'render_template_field'), 'sp-options', 'general');
134135

135136
add_settings_section('overrides', 'Overrides', null, 'sp-overrides');
136137
add_settings_field('from_name', 'From name', array($this, 'render_from_name_field'), 'sp-overrides', 'overrides');
@@ -147,11 +148,15 @@ public function sanitize($input)
147148
$new_input = array();
148149

149150
if (!empty($input['from_email'])) {
150-
$new_input['from_email'] = trim($input['from_email']);
151+
$new_input['from_email'] = sanitize_text_field($input['from_email']);
151152
}
152153

153154
if (!empty($input['from_name'])) {
154-
$new_input['from_name'] = trim($input['from_name']);
155+
$new_input['from_name'] = sanitize_text_field($input['from_name']);
156+
}
157+
158+
if (!empty($input['template'])) {
159+
$new_input['template'] = sanitize_text_field($input['template']);
155160
}
156161

157162
if (empty($input['password'])) {
@@ -160,7 +165,7 @@ public function sanitize($input)
160165
if(SparkPost::is_key_obfuscated(esc_attr($input['password']))) { //do not change password
161166
$new_input['password'] = $this->options['password'];
162167
} else {
163-
$new_input['password'] = trim(esc_attr($input['password']));
168+
$new_input['password'] = sanitize_text_field($input['password']);
164169
}
165170
}
166171

@@ -217,11 +222,28 @@ public function render_password_field()
217222

218223
printf(
219224
'<input type="text" id="password" name="sp_settings[password]" class="regular-text" value="%s" /><br/>
220-
<small><ul><li>For SMTP, use a SparkPost API key with <strong>Send via SMTP</strong> permission</li> <li>For HTTP API, use API Key with <strong>Transmissions: Read/Write</strong> permission</li><a href="https://support.sparkpost.com/customer/portal/articles/1933377-create-api-keys" target="_blank">Need help creating a SparkPost API key?</a></small>',
225+
<small><ul><li>For SMTP, set up an API key with the <strong>Send via SMTP</strong> permission</li> <li>For HTTP API, set up an API Key with the <strong>Transmissions: Read/Write</strong> permission</li><a href="https://support.sparkpost.com/customer/portal/articles/1933377-create-api-keys" target="_blank">Need help creating a SparkPost API key?</a></small>',
221226
isset($api_key) ? $api_key : ''
222227
);
223228
}
224229

230+
public function render_template_field()
231+
{
232+
?>
233+
<input type="text" id="template" name="sp_settings[template]" class="regular-text"
234+
value="<?php echo $this->options['template']; ?>"/><br/>
235+
<small>
236+
<ul>
237+
<li>- Templates can only be used with the HTTP API.</li>
238+
<li>- Leave this field blank to disable use of a template.</li>
239+
<li>- The template must have a variable in it named <code>{{{content}}}</code>. Note the triple curly braces, which are required to include non-escaped HTML.</li>
240+
<li>- Use <code>{{subject}}</code> and <code>{{from_name}}</code> in your template to allow substitution of Subject and From Name respectively.</li>
241+
<li>- From email override has no effect when using a template.</li>
242+
</ul>
243+
</small>
244+
<?php
245+
}
246+
225247
public function render_from_email_field()
226248
{
227249
$hint = 'Important: Domain must match with one of your verified sending domains.';

trunk/mailer.http.class.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ function sparkpostSend()
4848
protected function get_request_body()
4949
{
5050
$tracking_enabled = !!$this->options['enable_tracking'];
51-
51+
$sender = $this->get_sender();
5252
$body = array(
5353
'recipients' => $this->get_recipients(),
5454
'content' => array(
55-
'from' => $this->get_sender(),
55+
'from' => $sender,
5656
'subject' => $this->Subject,
5757
'headers' => $this->get_headers()
5858
),
@@ -62,17 +62,24 @@ protected function get_request_body()
6262
)
6363
);
6464

65-
switch ($this->ContentType) {
66-
case 'multipart/alternative':
67-
$body['content']['html'] = $this->Body;
68-
$body['content']['text'] = $this->AltBody;
69-
break;
70-
case 'text/plain':
71-
$body['content']['text'] = $this->Body;
72-
break;
73-
default:
74-
$body['content']['html'] = $this->Body;
75-
break;
65+
if (!empty($this->options['template'])) {
66+
$body['content']['template_id'] = $this->options['template'];
67+
$body['substitution_data']['content'] = $this->Body;
68+
$body['substitution_data']['subject'] = $this->Subject;
69+
$body['substitution_data']['from_name'] = $sender['name'];
70+
} else {
71+
switch($this->ContentType) {
72+
case 'multipart/alternative':
73+
$body['content']['html'] = $this->Body;
74+
$body['content']['text'] = $this->AltBody;
75+
break;
76+
case 'text/plain':
77+
$body['content']['text'] = $this->Body;
78+
break;
79+
default:
80+
$body['content']['html'] = $this->Body;
81+
break;
82+
}
7683
}
7784

7885
$replyTo = $this->get_reply_to();
@@ -242,4 +249,4 @@ protected function get_headers()
242249

243250
return $formatted_headers;
244251
}
245-
}
252+
}

trunk/sparkpost.class.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ class SparkPost
1616
'from_name' => '',
1717
'from_email' => '',
1818
'enable_sparkpost' => false,
19-
'enable_tracking' => true
19+
'enable_tracking' => true,
20+
'template' => ''
2021
);
2122

2223
var $options;
@@ -97,4 +98,13 @@ static function is_key_obfuscated($api_key)
9798
{
9899
return strpos($api_key, '*') !== false;
99100
}
101+
102+
public function init_sp_http_mailer($args)
103+
{
104+
global $phpmailer;
105+
if (!$phpmailer instanceof SparkPostHTTPMailer) {
106+
$phpmailer = new SparkPostHTTPMailer();
107+
}
108+
return $args;
109+
}
100110
}

trunk/wordpress-sparkpost.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,7 @@
3535
new SparkPostSMTPMailer();
3636
} else {
3737
require_once(WPSP_PLUGIN_DIR . 'mailer.http.class.php');
38-
add_filter('wp_mail', function ($args) {
39-
global $phpmailer;
40-
if (!$phpmailer instanceof SparkPostHTTPMailer) {
41-
$phpmailer = new SparkPostHTTPMailer();
42-
}
43-
return $args;
44-
});
38+
add_filter('wp_mail', array($sp, 'init_sp_http_mailer'));
4539
}
4640
}
4741

0 commit comments

Comments
 (0)