Skip to content

Commit 79d626a

Browse files
authored
Merge pull request #65 from SparkPost/FAD-3598
Transactional
2 parents 0830968 + b08c0ce commit 79d626a

File tree

6 files changed

+56
-4
lines changed

6 files changed

+56
-4
lines changed

admin.widget.class.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public function admin_page_init()
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');
134134
add_settings_field('template', 'Template', array($this, 'render_template_field'), 'sp-options', 'general');
135+
add_settings_field('transactional', 'Transactional', array($this, 'render_transactional_field'), 'sp-options', 'general');
135136

136137
add_settings_section('overrides', 'Overrides', null, 'sp-overrides');
137138
add_settings_field('from_name', 'From name', array($this, 'render_from_name_field'), 'sp-overrides', 'overrides');
@@ -199,8 +200,14 @@ public function sanitize($input)
199200
$new_input['enable_tracking'] = true;
200201
} else {
201202
$new_input['enable_tracking'] = false;
203+
}
202204

205+
if(!empty($input['transactional'])) {
206+
$new_input['transactional'] = true;
207+
} else {
208+
$new_input['transactional'] = false;
203209
}
210+
204211
return $new_input;
205212
}
206213

@@ -296,4 +303,12 @@ public function render_enable_debugging_field()
296303
{
297304
echo '<label><input type="checkbox" id="enable_debugging" name="enable_debugging" value="1" checked />Show email debugging messages</label>';
298305
}
306+
307+
public function render_transactional_field()
308+
{
309+
printf('<label><input type="checkbox" id="transactional" name="sp_settings[transactional]" value="1" %s />Mark emails as transactional</label>
310+
<br/><small>Upon checked, by default, it\'ll set mark all emails as transactional. It should be set false (using hooks) for non-transactional emails.</small>',
311+
$this->settings['transactional'] ? 'checked' : '');
312+
313+
}
299314
}

docs/hooks.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ Hook names are prefixed with `wpsp_`.
2727
| wpsp_reply_to | Filter |
2828
| wpsp_body_headers | Filter |
2929
| wpsp_smtp_msys_api | Filter |
30+
| wpsp_transactional | Filter | Set whether an email is transactional or not.

docs/transactional.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
# Transactional setting
3+
4+
## Background
5+
As of v2.5.0 (TODO: Fix version number before release), wordpress-sparkpost introduced [Transactional](https://www.sparkpost.com/blog/transactional-email-vs-mass-email) support for emails.
6+
7+
Emails created by WordPress are, generally, transactional in nature. Those are user registration, password reset emails etc. So, we wanted to set that flag on by default. Non-transactional emails are generated by third party plugins. In such case, that plugin should mark it as non-transactional. Because this option supports hook, it's easy to programmatically flip the value. We'll have an example below.
8+
9+
Also because this is quite a breaking change, we won't just change this value for the existing installations. Any current installations of this plugin upgraded to new version will have this unchecked by default.
10+
11+
## New Installations
12+
For new installations, Transactional will be enabled by default. In plugin's settings page, it'll be checked on. If you want to disable it, just uncheck and save the settings.
13+
14+
## Upgrades
15+
If you are upgrading to this new version from an existing installation, it won't be enabled by default. You must go to plugin's setting page and enable it from there.
16+
17+
## Programmatically modifying transactional setting
18+
If you know that you're sending a non-transactional email, you can set it to false before sending using a filter and then remove the filter after you sent the email. Here is an example:
19+
20+
```php
21+
function unsetTransactional() {
22+
return false;
23+
};
24+
25+
add_filter('wpsp_transactional', 'unsetTransactional');
26+
27+
// now send your email using wp_mail function
28+
29+
// as sending is done, let's remove the filter so other emails are not affected
30+
remove_filter('wpsp_transactional', 'unsetTransactional');
31+
```

mailer.http.class.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ protected function get_request_body()
8282
// enable engagement tracking
8383
$body['options'] = array(
8484
'open_tracking' => (bool) apply_filters('wpsp_open_tracking', $tracking_enabled),
85-
'click_tracking' => (bool) apply_filters('wpsp_click_tracking', $tracking_enabled)
85+
'click_tracking' => (bool) apply_filters('wpsp_click_tracking', $tracking_enabled),
86+
'transactional' => (bool) apply_filters('wpsp_transactional', $this->settings['transactional'])
8687
);
8788

8889
// pass through either stored template or inline content

mailer.smtp.class.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public function configure_phpmailer($phpmailer) {
2626
$x_msys_api = array(
2727
'options' => array (
2828
'open_tracking' => (bool) apply_filters('wpsp_open_tracking', $tracking_enabled),
29-
'click_tracking' => (bool) apply_filters('wpsp_click_tracking', $tracking_enabled)
29+
'click_tracking' => (bool) apply_filters('wpsp_click_tracking', $tracking_enabled),
30+
'transactional' => (bool) apply_filters('wpsp_transactional', $settings['transactional'])
3031
)
3132
);
3233

sparkpost.class.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class SparkPost
1717
'from_email' => '',
1818
'enable_sparkpost' => false,
1919
'enable_tracking' => true,
20-
'template' => ''
20+
'template' => '',
21+
'transactional' => false
2122
);
2223

2324
var $settings;
@@ -39,7 +40,9 @@ public function __construct()
3940

4041
public function sp_activate()
4142
{
42-
update_option('sp_settings', self::$settings_default);
43+
$settings = self::$settings_default;
44+
$settings['transactional'] = true; // setting it here to apply this default value to new installation only as this is breaking change
45+
update_option('sp_settings', $settings);
4346
}
4447

4548
public function sp_deactivate()

0 commit comments

Comments
 (0)