Skip to content

Commit 23ff711

Browse files
authored
Merge pull request #64 from SparkPost/FAD-3599
Add hooks
2 parents 22d1460 + 3f48444 commit 23ff711

File tree

5 files changed

+83
-61
lines changed

5 files changed

+83
-61
lines changed

admin.widget.class.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
*/
1010
class SparkPostAdmin
1111
{
12-
private $options;
12+
private $settings;
1313

1414
public function __construct()
1515
{
16-
$this->options = SparkPost::get_options();
16+
$this->settings = SparkPost::get_settings(false);
1717
add_action('admin_menu', array($this, 'add_plugin_page'));
1818
add_action('admin_init', array($this, 'admin_page_init'));
1919

@@ -80,7 +80,7 @@ public function test_email_sending($recipient, $debug = false)
8080
}
8181

8282
if ($result) {
83-
if (!$this->options['enable_sparkpost']) {
83+
if (!$this->settings['enable_sparkpost']) {
8484
$this->render_message('Test email sent successfully but not through SparkPost.<br/>Note: the SparkPost plugin is not enabled. To enable it, check "Send email using SparkPost" on the SparkPost settings page.', 'updated');
8585
} else {
8686
$this->render_message('Test email sent successfully', 'updated');
@@ -163,7 +163,7 @@ public function sanitize($input)
163163
add_settings_error('Password', esc_attr('password'), 'API Key is required', 'error');
164164
} else {
165165
if(SparkPost::is_key_obfuscated(esc_attr($input['password']))) { //do not change password
166-
$new_input['password'] = $this->options['password'];
166+
$new_input['password'] = $this->settings['password'];
167167
} else {
168168
$new_input['password'] = sanitize_text_field($input['password']);
169169
}
@@ -207,7 +207,7 @@ public function sanitize($input)
207207
public function render_enable_sparkpost_field()
208208
{
209209
printf(
210-
'<label><input type="checkbox" id="enable_sparkpost" name="sp_settings[enable_sparkpost]" value="1" %s />Send email using SparkPost</label>', $this->options['enable_sparkpost'] ? 'checked' : ''
210+
'<label><input type="checkbox" id="enable_sparkpost" name="sp_settings[enable_sparkpost]" value="1" %s />Send email using SparkPost</label>', $this->settings['enable_sparkpost'] ? 'checked' : ''
211211
);
212212
}
213213

@@ -218,7 +218,7 @@ public function render_username_field()
218218

219219
public function render_password_field()
220220
{
221-
$api_key = SparkPost::obfuscate_api_key($this->options['password']);
221+
$api_key = SparkPost::obfuscate_api_key($this->settings['password']);
222222

223223
printf(
224224
'<input type="text" id="password" name="sp_settings[password]" class="regular-text" value="%s" /><br/>
@@ -231,7 +231,7 @@ public function render_template_field()
231231
{
232232
?>
233233
<input type="text" id="template" name="sp_settings[template]" class="regular-text"
234-
value="<?php echo $this->options['template']; ?>"/><br/>
234+
value="<?php echo $this->settings['template']; ?>"/><br/>
235235
<small>
236236
<ul>
237237
<li>- Please see <a href="https://support.sparkpost.com/customer/portal/articles/2409547-using-templates-with-the-sparkpost-wordpress-plugin">this article</a> for detailed information about using templates with this plugin.</li>
@@ -245,30 +245,30 @@ public function render_template_field()
245245
public function render_from_email_field()
246246
{
247247
$hint = 'Important: Domain must match with one of your verified sending domains.';
248-
if(empty($this->options['from_email'])){
248+
if(empty($this->settings['from_email'])){
249249
$hostname = parse_url(get_bloginfo('url'), PHP_URL_HOST);
250250
$hint .= sprintf(' When left blank, <strong>%s</strong> will be used as email domain', $hostname);
251251
}
252252

253253
$hint = sprintf('<small>%s</small>', $hint);
254254
printf(
255255
'<input type="email" id="from_email" name="sp_settings[from_email]" class="regular-text" value="%s" /><br/>%s',
256-
isset($this->options['from_email']) ? esc_attr($this->options['from_email']) : '', $hint
256+
isset($this->settings['from_email']) ? esc_attr($this->settings['from_email']) : '', $hint
257257
);
258258
}
259259

260260
public function render_from_name_field()
261261
{
262262
printf(
263263
'<input type="text" id="from_name" name="sp_settings[from_name]" class="regular-text" value="%s" />',
264-
isset($this->options['from_name']) ? esc_attr($this->options['from_name']) : ''
264+
isset($this->settings['from_name']) ? esc_attr($this->settings['from_name']) : ''
265265
);
266266
}
267267

268268
public function render_sending_method_field()
269269
{
270-
$method = esc_attr($this->options['sending_method']);
271-
$port = esc_attr($this->options['port']);
270+
$method = esc_attr($this->settings['sending_method']);
271+
$port = esc_attr($this->settings['port']);
272272

273273
$selected_method = !empty($method) ? $method : 'api';
274274
$selected_port = !empty($port) ? $port : 587;
@@ -283,7 +283,7 @@ public function render_sending_method_field()
283283
public function render_enable_tracking_field()
284284
{
285285
printf(
286-
'<label><input type="checkbox" id="enable_tracking" name="sp_settings[enable_tracking]" value="1" %s />Track clicks/opens in SparkPost</label>', $this->options['enable_tracking'] ? 'checked' : ''
286+
'<label><input type="checkbox" id="enable_tracking" name="sp_settings[enable_tracking]" value="1" %s />Track clicks/opens in SparkPost</label>', $this->settings['enable_tracking'] ? 'checked' : ''
287287
);
288288
}
289289

mailer.http.class.php

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@
99
class SparkPostHTTPMailer extends PHPMailer
1010
{
1111
protected $endpoint = 'https://api.sparkpost.com/api/v1/transmissions';
12-
private $options;
12+
private $settings;
1313

1414
/**
1515
* Constructor.
1616
* @param boolean $exceptions Should we throw external exceptions?
1717
*/
1818
function __construct($exceptions = false)
1919
{
20-
$this->options = SparkPost::get_options();
20+
$this->settings = SparkPost::get_settings();
2121

2222
parent::__construct($exceptions);
23+
do_action('wpsp_init_mailer', $this);
2324
}
2425

2526
/**
@@ -46,43 +47,52 @@ function sparkpostSend()
4647
'body' => json_encode($this->get_request_body())
4748
);
4849

49-
$http = _wp_http_get_object();
50+
$http = apply_filters('wpsp_get_http_lib', _wp_http_get_object());
5051

5152
$this->edebug(sprintf('Request headers: %s', print_r($this->get_request_headers(true), true)));
5253
$this->edebug(sprintf('Request body: %s', $data['body']));
5354
$this->edebug(sprintf('Making HTTP POST request to %s', $this->endpoint));
55+
do_action('wpsp_before_send', $this->endpoint, $data);
5456
$result = $http->request($this->endpoint, $data);
57+
do_action('wpsp_after_send', $result);
5558
$this->edebug('Response received');
5659

57-
return $this->handle_response($result);
60+
$result = apply_filters('wpsp_handle_response', $result);
61+
if(is_bool($result)) { // it means, response been already processed by the hooked filter. so just return the value.
62+
$this->edebug('Skipping response processing');
63+
return $result;
64+
} else {
65+
return $this->handle_response($result);
66+
}
5867
}
5968

6069
/**
6170
* Build the request body to be sent to the SparkPost API.
6271
*/
6372
protected function get_request_body()
6473
{
65-
$tracking_enabled = !!$this->options['enable_tracking'];
74+
$tracking_enabled = !!$this->settings['enable_tracking'];
6675
$sender = $this->get_sender();
6776
$replyTo = $this->get_reply_to();
6877
$body = array();
6978

7079
// add recipients
71-
$body['recipients'] = $this->get_recipients();
80+
$body['recipients'] = apply_filters('wpsp_recipients', $this->get_recipients());
7281

7382
// enable engagement tracking
7483
$body['options'] = array(
75-
'open_tracking' => $tracking_enabled,
76-
'click_tracking' => $tracking_enabled
84+
'open_tracking' => (bool) apply_filters('wpsp_open_tracking', $tracking_enabled),
85+
'click_tracking' => (bool) apply_filters('wpsp_click_tracking', $tracking_enabled)
7786
);
7887

7988
// pass through either stored template or inline content
80-
if (!empty($this->options['template'])) {
89+
if (!empty($this->settings['template'])) {
8190
// stored template
82-
$body['content']['template_id'] = $this->options['template'];
91+
$body['content']['template_id'] = apply_filters('wpsp_template_id', $this->settings['template']);
8392

8493
// supply substitution data so users can add variables to templates
85-
$body['substitution_data']['content'] = $this->Body;
94+
$content_substitution_tag_name = apply_filters('wpsp_substitution_content_tag_name', 'content');
95+
$body['substitution_data'][$content_substitution_tag_name] = $this->Body;
8696
$body['substitution_data']['subject'] = $this->Subject;
8797
$body['substitution_data']['from_name'] = $sender['name'];
8898
$body['substitution_data']['from'] = $sender['name'] . ' <' . $sender['email'] . '>';
@@ -124,6 +134,8 @@ protected function get_request_body()
124134
$body['content']['attachments'] = $attachments;
125135
}
126136

137+
$body = apply_filters( 'wpsp_request_body', $body);
138+
127139
return $body;
128140
}
129141

@@ -173,7 +185,6 @@ public function isMail()
173185

174186
protected function handle_response($response)
175187
{
176-
177188
if (is_wp_error($response)) {
178189
$this->edebug('Request completed with error');
179190
$this->setError($response->get_error_messages()); //WP_Error implements this method
@@ -185,6 +196,8 @@ protected function handle_response($response)
185196
$this->edebug('Response body: ' . print_r($response['body'], true));
186197

187198
$body = json_decode($response['body']);
199+
do_action('wpsp_response_body', $body);
200+
188201
if (property_exists($body, 'errors')) {
189202
$this->edebug('Error in transmission');
190203
$this->setError($body->errors);
@@ -238,20 +251,21 @@ protected function get_recipients()
238251
// include cc to recipients, they need to included in recipients and in headers (refer to get_headers method)
239252
$recipients = array_merge($recipients, $this->get_cc($recipients_header_to));
240253

241-
return $recipients;
254+
return apply_filters('wpsp_recipients', $recipients);
242255
}
243256

244257
protected function get_request_headers($hide_api_key = false)
245258
{
246-
$api_key = $this->options['password'];
259+
$api_key = apply_filters('wpsp_api_key', $this->settings['password']);
247260
if ($hide_api_key) {
248261
$api_key = SparkPost::obfuscate_api_key($api_key);
249262
}
250-
return array(
263+
264+
return apply_filters('wpsp_request_headers', array(
251265
'User-Agent' => 'wordpress-sparkpost',
252266
'Content-Type' => 'application/json',
253267
'Authorization' => $api_key
254-
);
268+
));
255269
}
256270

257271
/**
@@ -291,7 +305,7 @@ protected function parse_reply_to()
291305
}
292306
}
293307

294-
return implode(',', $replyTos);
308+
return apply_filters('wpsp_reply_to', implode(',', $replyTos));
295309
}
296310

297311
protected function get_reply_to()
@@ -396,6 +410,6 @@ protected function get_headers()
396410
$formatted_headers['CC'] = $this->stringify_recipients($cc);
397411
}
398412

399-
return $formatted_headers;
413+
return apply_filters('spwp_body_headers', $formatted_headers);
400414
}
401415
}

mailer.smtp.class.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,29 @@ public function __construct()
1616
}
1717

1818
public function configure_phpmailer($phpmailer) {
19-
$options = SparkPost::get_options();
19+
$settings = SparkPost::get_settings();
2020

21-
if (!$options['enable_sparkpost'] || empty($options['password'])) {
21+
if (!$settings['enable_sparkpost'] || empty($settings['password'])) {
2222
return;
2323
}
24-
$tracking_enabled = !!$options['enable_tracking'];
24+
25+
$tracking_enabled = (bool) $settings['enable_tracking'];
2526
$x_msys_api = array(
2627
'options' => array (
27-
'open_tracking' => $tracking_enabled,
28-
'click_tracking' => $tracking_enabled
28+
'open_tracking' => (bool) apply_filters('wpsp_open_tracking', $tracking_enabled),
29+
'click_tracking' => (bool) apply_filters('wpsp_click_tracking', $tracking_enabled)
2930
)
3031
);
3132

3233
$phpmailer->isSMTP();
3334
$phpmailer->SMTPSecure = 'tls';
34-
$phpmailer->Port = !empty($options['port']) ? intval($options['port']) : 587;
35+
$phpmailer->Port = !empty($settings['port']) ? intval($settings['port']) : 587;
3536
$phpmailer->Host = 'smtp.sparkpostmail.com';
36-
3737
$phpmailer->SMTPAuth = true;
3838
$phpmailer->Username = 'SMTP_Injection';
39-
$phpmailer->Password = $options['password'];
39+
$phpmailer->Password = apply_filters('wpsp_api_key', $settings['password']);
4040

41-
$phpmailer->addCustomHeader('X-MSYS-API', json_encode($x_msys_api));
41+
$json_x_msys_api = apply_filters('wpsp_smtp_msys_api', json_encode($x_msys_api));
42+
$phpmailer->addCustomHeader('X-MSYS-API', $json_x_msys_api);
4243
}
4344
}

sparkpost.class.php

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class SparkPost
1010
{
1111

12-
protected static $options_default = array(
12+
protected static $settings_default = array(
1313
'port' => 587,
1414
'sending_method' => 'api',
1515
'password' => '',
@@ -20,7 +20,7 @@ class SparkPost
2020
'template' => ''
2121
);
2222

23-
var $options;
23+
var $settings;
2424

2525
public function __construct()
2626
{
@@ -29,33 +29,41 @@ public function __construct()
2929

3030
add_filter('plugin_action_links_' . plugin_basename(WPSP_PLUGIN_PATH), array($this, 'add_settings_link'));
3131

32-
$this->options = self::get_options();
32+
$this->settings = self::get_settings();
3333

34-
if (self::get_option('enable_sparkpost')) { //no need to register this hooks if plugin is disabled
34+
if (self::get_setting('enable_sparkpost')) { //no need to register this hooks if plugin is disabled
3535
add_filter('wp_mail_from', array($this, 'set_from_email'));
3636
add_filter('wp_mail_from_name', array($this, 'set_from_name'));
3737
}
3838
}
3939

4040
public function sp_activate()
4141
{
42-
update_option('sp_settings', self::$options_default);
42+
update_option('sp_settings', self::$settings_default);
4343
}
4444

4545
public function sp_deactivate()
4646
{
4747
delete_option('sp_settings');
4848
}
4949

50-
static function get_options()
50+
static function get_settings($apply_filter = true)
5151
{
52-
return array_merge(self::$options_default, get_option('sp_settings', array()));
52+
$settings = array_merge(self::$settings_default, get_option('sp_settings', array()));
53+
54+
if ($apply_filter) {
55+
return apply_filters('wpsp_get_settings', $settings);
56+
}
57+
else {
58+
return $settings;
59+
}
60+
5361
}
5462

55-
static function get_option($option)
63+
static function get_setting($setting)
5664
{
57-
$options = self::get_options();
58-
return $options[$option];
65+
$settings = self::get_settings();
66+
return $settings[$setting];
5967
}
6068

6169
public function add_settings_link($links)
@@ -69,20 +77,20 @@ public function add_settings_link($links)
6977

7078
public function set_from_name($name)
7179
{
72-
if (!empty($this->options['from_name'])) {
73-
return $this->options['from_name'];
74-
} else {
75-
return $name;
80+
if (!empty($this->settings['from_name'])) {
81+
$name = $this->settings['from_name'];
7682
}
83+
84+
return apply_filters('wpsp_sender_name', $name);
7785
}
7886

7987
public function set_from_email($email)
8088
{
81-
if (!empty($this->options['from_email'])) {
82-
return $this->options['from_email'];
83-
} else {
84-
return $email;
89+
if (!empty($this->settings['from_email'])) {
90+
$email = $this->settings['from_email'];
8591
}
92+
93+
return apply_filters('wpsp_sender_email', $email);
8694
}
8795

8896
static function obfuscate_api_key($api_key)

0 commit comments

Comments
 (0)