forked from SparkPost/wordpress-sparkpost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparkpost.class.php
217 lines (176 loc) · 5.82 KB
/
sparkpost.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
namespace WPSparkPost;
// If ABSPATH is defined, we assume WP is calling us.
// Otherwise, this could be an illicit direct request.
if (!defined('ABSPATH')) exit();
/**
* @package wp-sparkpost
*/
class SparkPost
{
protected static $settings_default = array(
'port' => 587,
'sending_method' => 'api',
'password' => '',
'from_name' => '',
'from_email' => '',
'enable_sparkpost' => false,
'enable_tracking' => true,
'template' => '',
'transactional' => false,
'log_emails' => false
);
var $settings, $db_version;
public function __construct()
{
register_activation_hook(WPSP_PLUGIN_PATH, array($this, 'sp_activate'));
register_deactivation_hook(WPSP_PLUGIN_PATH, array($this, 'sp_deactivate'));
add_filter('plugin_action_links_' . plugin_basename(WPSP_PLUGIN_PATH), array($this, 'add_settings_link'));
add_action('plugins_loaded', array($this, 'db_update_check'));
$this->settings = self::get_settings();
if (self::get_setting('enable_sparkpost')) { //no need to register this hooks if plugin is disabled
add_filter('wp_mail_from', array($this, 'set_from_email'));
add_filter('wp_mail_from_name', array($this, 'set_from_name'));
}
$this->db_version = '1.0.0';
}
public function sp_activate()
{
$settings = self::$settings_default;
$settings['transactional'] = true; // setting it here to apply this default value to new installation only as this is breaking change
update_option('sp_settings', $settings);
$this->install_db();
}
protected function install_email_log_table()
{
global $wpdb;
$table_name = $wpdb->prefix . 'sp_email_logs';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id SERIAL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
wp_mail_args text NOT NULL,
subject varchar(255) NOT NULL,
content text NOT NULL,
response text NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
$result = dbDelta($sql);
update_option('sp_db_version', $this->db_version);
return $result;
}
protected function install_db()
{
return $this->install_email_log_table();
}
function db_update_check()
{
if (get_site_option('sp_db_version') != $this->db_version) {
return $this->install_db();
}
return false;
}
public function sp_deactivate()
{
delete_option('sp_settings');
}
static function get_settings($apply_filter = true)
{
$settings = array_merge(
self::$settings_default,
get_option('sp_settings', array()),
get_option('sp_settings_basic', array()),
get_option('sp_settings_overrides', array())
);
if ($apply_filter) {
return apply_filters('wpsp_get_settings', $settings);
} else {
return $settings;
}
}
static function get_setting($setting)
{
$settings = self::get_settings();
return $settings[$setting];
}
public function add_settings_link($links)
{
$mylinks = array();
if (current_user_can('manage_options')) {
$mylinks[] = '<a href="' . esc_url(admin_url('options-general.php?page=wpsp-setting-admin')) . '">Settings</a>';
}
return array_merge($links, $mylinks);
}
public function set_from_name($name)
{
if (!empty($this->settings['from_name'])) {
$name = $this->settings['from_name'];
}
return apply_filters('wpsp_sender_name', $name);
}
public function set_from_email($email)
{
if (!empty($this->settings['from_email'])) {
$email = $this->settings['from_email'];
}
return apply_filters('wpsp_sender_email', $email);
}
static function obfuscate_api_key($api_key)
{
if (!empty($api_key)) {
return substr($api_key, 0, 4) . str_repeat('*', 36);
}
return $api_key;
}
static function is_key_obfuscated($api_key)
{
return strpos($api_key, '*') !== false;
}
public function init_sp_http_mailer($args)
{
global $phpmailer;
if (!$phpmailer instanceof SparkPostHTTPMailer) {
$phpmailer = new SparkPostHTTPMailer();
}
$phpmailer->wp_mail_args = $args;
return $args;
}
static function is_sandbox($email)
{
$email_splitted = array_slice(explode('@', $email), -1);
return $email_splitted[0] === 'sparkpostbox.com';
}
static function add_log($wp_mail_args, $content, $response)
{
if (!self::is_logging_enabled()) {
return false;
}
global $wpdb;
$wpdb->show_errors();
$content = json_decode($content);
//get subject
if (property_exists($content->content, 'subject')) {
$subject = $content->content->subject;
} else {
$subject = $content->substitution_data->subject;
}
return $wpdb->insert($wpdb->prefix . 'sp_email_logs', array(
'subject' => $subject,
'content' => json_encode($content),
'response' => json_encode($response),
'wp_mail_args' => json_encode($wp_mail_args)
));
}
static function clear_logs()
{
global $wpdb;
$wpdb->show_errors();
$wpdb->query('TRUNCATE ' . $wpdb->prefix . 'sp_email_logs');
}
static function is_logging_enabled()
{
$settings = self::get_settings();
return $settings['sending_method'] === 'api' && $settings['log_emails'];
}
}