SendGrid - Version 1.7.5

Version Description

  • Fixed an issue with the reset password email from Wordpress
  • Updated validation for email addresses
  • Fixed an issue where some errors were not displayed on the settings page
  • Add substitutions functionality
Download this release

Release Info

Developer team-rs
Plugin Icon 128x128 SendGrid
Version 1.7.5
Comparing to
See all releases

Code changes from version 1.7.4 to 1.7.5

assets/screenshot-12.png ADDED
Binary file
lib/class-sendgrid-settings.php CHANGED
@@ -81,81 +81,100 @@ class Sendgrid_Settings {
81
  public static function show_settings_page()
82
  {
83
  $response = null;
 
 
84
  if ( 'POST' == $_SERVER['REQUEST_METHOD'] ) {
85
- $response = self::do_post($_POST);
 
 
 
86
  }
87
-
88
  $status = '';
89
  $message = '';
90
 
91
- $user = Sendgrid_Tools::get_username();
92
- $password = Sendgrid_Tools::get_password();
93
- $api_key = Sendgrid_Tools::get_api_key();
94
- $send_method = Sendgrid_Tools::get_send_method();
95
- $auth_method = Sendgrid_Tools::get_auth_method();
96
- $name = stripslashes( Sendgrid_Tools::get_from_name() );
97
- $email = Sendgrid_Tools::get_from_email();
98
- $reply_to = Sendgrid_Tools::get_reply_to();
99
- $categories = stripslashes( Sendgrid_Tools::get_categories() );
100
- $template = stripslashes( Sendgrid_Tools::get_template() );
101
- $port = Sendgrid_Tools::get_port();
102
- $content_type = Sendgrid_Tools::get_content_type();
103
- $stats_categories = stripslashes( Sendgrid_Tools::get_stats_categories() );
104
-
105
- $allowed_send_methods = array('API');
106
  if ( class_exists( 'Swift' ) ) {
107
  $allowed_send_methods[] = 'SMTP';
108
  }
109
 
110
- if ( ! in_array( strtoupper( $send_method ), $allowed_send_methods ) ) {
111
- $message = 'Invalid send method configured in the config file, available methods are: ' . join( ", ", $allowed_send_methods );
112
- $status = 'error';
113
- }
 
114
 
115
- if ( "apikey" == $auth_method and ! Sendgrid_Tools::check_api_key( $api_key, true ) ) {
116
- $message = 'API Key is invalid.';
117
- $status = 'error';
118
- } else if ( "credentials" == $auth_method and ! Sendgrid_Tools::check_username_password( $user, $password, true ) ) {
119
- $message = 'Username and password are invalid.';
 
 
 
 
 
120
  $status = 'error';
121
- }
 
 
 
 
 
122
 
123
- if ( $template and ! Sendgrid_Tools::check_template( $template ) ) {
124
- $message = 'Template not found.';
125
- $status = 'error';
126
- }
127
-
128
- if ( ! in_array( $port, Sendgrid_Tools::$allowed_ports ) ) {
129
- $message = 'Invalid port configured in the config file, available ports are: ' . join( ",", Sendgrid_Tools::$allowed_ports );
130
- $status = 'error';
131
- }
132
 
133
- if ( ! in_array( $auth_method, Sendgrid_Tools::$allowed_auth_methods ) ) {
134
- $message = 'Invalid authentication method configured in the config file, available options are: ' . join( ", ", Sendgrid_Tools::$allowed_auth_methods );
135
- $status = 'error';
136
- }
 
 
137
 
138
- $allowed_content_type = array('plaintext', 'html');
 
 
 
 
 
139
 
140
- if ( defined('SENDGRID_CONTENT_TYPE') ) {
141
- if ( ! in_array( SENDGRID_CONTENT_TYPE , $allowed_content_type ) ) {
142
- $message = 'Invalid content type, available content types are: "plaintext" or "html".';
143
- $status = 'error';
 
144
  }
145
  }
146
 
147
- $is_env_auth_method = defined('SENDGRID_AUTH_METHOD');
148
- $is_env_send_method = defined('SENDGRID_SEND_METHOD');
149
- $is_env_username = defined('SENDGRID_USERNAME');
150
- $is_env_password = defined('SENDGRID_PASSWORD');
151
- $is_env_api_key = defined('SENDGRID_API_KEY');
152
- $is_env_port = defined('SENDGRID_PORT');
153
- $is_env_content_type = defined('SENDGRID_CONTENT_TYPE');
154
 
155
  if ( $response && $status != 'error' ) {
156
- $message = $response['message'];
157
- $status = $response['status'];
158
- if( array_key_exists('error_type', $response) ) {
159
  $error_type = $response['error_type'];
160
  }
161
  }
@@ -172,29 +191,33 @@ class Sendgrid_Settings {
172
  }
173
 
174
  private static function save_settings( $params ) {
175
- if ( ! isset( $params['auth_method'] ) )
176
  $params['auth_method'] = Sendgrid_Tools::get_auth_method();
 
177
 
178
  switch ( $params['auth_method'] ) {
179
  case 'apikey':
180
- if ( ! isset( $params['sendgrid_apikey'] ) )
181
- break;
182
-
183
- if ( ! $params['sendgrid_apikey'] ) {
184
  $response = array(
185
- "message" => "API Key is empty.",
186
- "status" => "error"
187
  );
188
- } elseif ( ! Sendgrid_Tools::check_api_key( $params['sendgrid_apikey'], true ) ) {
 
 
 
 
 
 
189
  $response = array(
190
- "message" => "API Key is invalid or without permissions.",
191
- "status" => "error"
192
  );
193
 
194
  break;
195
  }
196
 
197
- Sendgrid_Tools::set_api_key($params['sendgrid_apikey']);
198
 
199
  break;
200
 
@@ -217,52 +240,68 @@ class Sendgrid_Settings {
217
 
218
  if ( ( isset( $params['sendgrid_username'] ) and ! $params['sendgrid_username'] ) or ( isset( $params['sendgrid_password'] ) and ! $params['sendgrid_password'] ) ) {
219
  $response = array(
220
- "message" => "Username or password is empty.",
221
- "status" => "error"
222
  );
223
  } elseif ( ! Sendgrid_Tools::check_username_password( $params['sendgrid_username'], $params['sendgrid_password'], true ) ) {
224
  $response = array(
225
- "message" => "Username and password are invalid.",
226
- "status" => "error"
227
  );
228
 
229
  break;
230
  }
231
 
232
- if ( $save_username )
233
- Sendgrid_Tools::set_username($params['sendgrid_username']);
 
234
 
235
- if ( $save_password )
236
- Sendgrid_Tools::set_password($params['sendgrid_password']);
 
237
 
238
  break;
239
  }
240
 
241
  if ( isset( $params['sendgrid_name'] ) ) {
242
- update_option('sendgrid_from_name', $params['sendgrid_name']);
243
  }
244
 
245
  if ( isset( $params['sendgrid_email'] ) ) {
246
- update_option('sendgrid_from_email', $params['sendgrid_email']);
 
 
 
 
 
 
 
247
  }
248
 
249
  if ( isset( $params['sendgrid_reply_to'] ) ) {
250
- update_option('sendgrid_reply_to', $params['sendgrid_reply_to']);
 
 
 
 
 
 
 
251
  }
252
 
253
  if ( isset( $params['sendgrid_categories'] ) ) {
254
- update_option('sendgrid_categories', $params['sendgrid_categories']);
255
  }
256
 
257
  if ( isset( $params['sendgrid_stats_categories'] ) ) {
258
- update_option('sendgrid_stats_categories', $params['sendgrid_stats_categories']);
259
  }
260
 
261
  if ( isset( $params['sendgrid_template'] ) ) {
262
  if ( ! Sendgrid_Tools::check_template( $params['sendgrid_template'] ) ) {
263
  $response = array(
264
- "message" => "Template not found.",
265
- "status" => "error"
266
  );
267
  } else {
268
  update_option( 'sendgrid_template', $params['sendgrid_template'] );
@@ -270,54 +309,63 @@ class Sendgrid_Settings {
270
  }
271
 
272
  if ( isset( $params['send_method'] ) ) {
273
- update_option('sendgrid_api', $params['send_method']);
274
  }
275
 
276
  if ( isset( $params['auth_method'] ) && in_array( $params['auth_method'], Sendgrid_Tools::$allowed_auth_methods ) ) {
277
- update_option('sendgrid_auth_method', $params['auth_method']);
278
  }
279
 
280
  if ( isset( $params['sendgrid_port'] ) ) {
281
- update_option('sendgrid_port', $params['sendgrid_port']);
282
  }
283
 
284
  if ( isset( $params['content_type'] ) ) {
285
  update_option( 'sendgrid_content_type', $params['content_type'] );
286
  }
287
 
288
- if( isset( $response ) and $response['status'] == 'error')
289
  return $response;
 
290
 
291
  return array(
292
- "message" => "Options are saved.",
293
- "status" => "updated"
294
  );
295
  }
296
 
297
  private static function send_test_email( $params ) {
298
- $to = $params['sendgrid_to'];
 
 
 
 
 
 
 
 
299
  $subject = stripslashes( $params['sendgrid_subj'] );
300
  $body = stripslashes( $params['sendgrid_body'] );
301
  $headers = $params['sendgrid_headers'];
302
 
303
- if ( preg_match( "/content-type:\s*text\/html/i", $headers ) ) {
304
  $body_br = nl2br( $body );
305
  } else {
306
- $body_br = $body;
307
  }
308
 
309
- $sent = wp_mail($to, $subject, $body_br, $headers);
310
  if ( true === $sent ) {
311
  return array(
312
- "message" => "Email was sent.",
313
- "status" => "updated"
314
  );
315
  }
316
 
317
  return array(
318
- "message" => "Email wasn't sent.",
319
- "status" => "updated",
320
- "error_type" => "sending"
321
  );
322
  }
323
  }
81
  public static function show_settings_page()
82
  {
83
  $response = null;
84
+ $error_from_update = false;
85
+
86
  if ( 'POST' == $_SERVER['REQUEST_METHOD'] ) {
87
+ $response = self::do_post( $_POST );
88
+ if( isset( $response['status'] ) and $response['status'] == 'error' ) {
89
+ $error_from_update = true;
90
+ }
91
  }
92
+
93
  $status = '';
94
  $message = '';
95
 
96
+ $user = Sendgrid_Tools::get_username();
97
+ $password = Sendgrid_Tools::get_password();
98
+ $api_key = Sendgrid_Tools::get_api_key();
99
+ $send_method = Sendgrid_Tools::get_send_method();
100
+ $auth_method = Sendgrid_Tools::get_auth_method();
101
+ $name = stripslashes( Sendgrid_Tools::get_from_name() );
102
+ $email = Sendgrid_Tools::get_from_email();
103
+ $reply_to = Sendgrid_Tools::get_reply_to();
104
+ $categories = stripslashes( Sendgrid_Tools::get_categories() );
105
+ $template = stripslashes( Sendgrid_Tools::get_template() );
106
+ $port = Sendgrid_Tools::get_port();
107
+ $content_type = Sendgrid_Tools::get_content_type();
108
+ $stats_categories = stripslashes( Sendgrid_Tools::get_stats_categories() );
109
+
110
+ $allowed_send_methods = array( 'API' );
111
  if ( class_exists( 'Swift' ) ) {
112
  $allowed_send_methods[] = 'SMTP';
113
  }
114
 
115
+ if ( ! $error_from_update ) {
116
+ if ( ! in_array( strtoupper( $send_method ), $allowed_send_methods ) ) {
117
+ $message = 'Invalid send method configured in the config file, available methods are: ' . join( ", ", $allowed_send_methods );
118
+ $status = 'error';
119
+ }
120
 
121
+ if ( 'apikey' == $auth_method and ! empty( $api_key ) and ! Sendgrid_Tools::check_api_key( $api_key, true ) ) {
122
+ $message = 'API Key is invalid or without permissions.';
123
+ $status = 'error';
124
+ } elseif ( 'credentials' == $auth_method and ! empty( $user ) and ! empty( $password ) and ! Sendgrid_Tools::check_username_password( $user, $password, true ) ) {
125
+ $message = 'Username and password are invalid.';
126
+ $status = 'error';
127
+ }
128
+
129
+ if ( $template and ! Sendgrid_Tools::check_template( $template ) ) {
130
+ $message = 'Template not found.';
131
  $status = 'error';
132
+ }
133
+
134
+ if ( ! in_array( $port, Sendgrid_Tools::$allowed_ports ) ) {
135
+ $message = 'Invalid port configured in the config file, available ports are: ' . join( ",", Sendgrid_Tools::$allowed_ports );
136
+ $status = 'error';
137
+ }
138
 
139
+ if ( ! in_array( $auth_method, Sendgrid_Tools::$allowed_auth_methods ) ) {
140
+ $message = 'Invalid authentication method configured in the config file, available options are: ' . join( ", ", Sendgrid_Tools::$allowed_auth_methods );
141
+ $status = 'error';
142
+ }
 
 
 
 
 
143
 
144
+ if ( defined( 'SENDGRID_CONTENT_TYPE' ) ) {
145
+ if ( ! in_array( SENDGRID_CONTENT_TYPE, Sendgrid_Tools::$allowed_content_type ) ) {
146
+ $message = 'Invalid content type, available content types are: "plaintext" or "html".';
147
+ $status = 'error';
148
+ }
149
+ }
150
 
151
+ if( defined( 'SENDGRID_FROM_EMAIL' ) ) {
152
+ if ( ! Sendgrid_Tools::is_valid_email( SENDGRID_FROM_EMAIL ) ) {
153
+ $message = 'Sending email address is not valid in config file.';
154
+ $status = 'error';
155
+ }
156
+ }
157
 
158
+ if( defined( 'SENDGRID_REPLY_TO' ) ) {
159
+ if ( ! Sendgrid_Tools::is_valid_email( SENDGRID_REPLY_TO ) ) {
160
+ $message = 'Reply email address is not valid in config file.';
161
+ $status = 'error';
162
+ }
163
  }
164
  }
165
 
166
+ $is_env_auth_method = defined( 'SENDGRID_AUTH_METHOD' );
167
+ $is_env_send_method = defined( 'SENDGRID_SEND_METHOD' );
168
+ $is_env_username = defined( 'SENDGRID_USERNAME' );
169
+ $is_env_password = defined( 'SENDGRID_PASSWORD' );
170
+ $is_env_api_key = defined( 'SENDGRID_API_KEY' );
171
+ $is_env_port = defined( 'SENDGRID_PORT' );
172
+ $is_env_content_type = defined( 'SENDGRID_CONTENT_TYPE' );
173
 
174
  if ( $response && $status != 'error' ) {
175
+ $message = $response['message'];
176
+ $status = $response['status'];
177
+ if( array_key_exists( 'error_type', $response ) ) {
178
  $error_type = $response['error_type'];
179
  }
180
  }
191
  }
192
 
193
  private static function save_settings( $params ) {
194
+ if ( ! isset( $params['auth_method'] ) ) {
195
  $params['auth_method'] = Sendgrid_Tools::get_auth_method();
196
+ }
197
 
198
  switch ( $params['auth_method'] ) {
199
  case 'apikey':
200
+ if ( ! isset( $params['sendgrid_apikey'] ) or empty( $params['sendgrid_apikey'] ) ) {
 
 
 
201
  $response = array(
202
+ 'message' => 'API Key is empty.',
203
+ 'status' => 'error'
204
  );
205
+
206
+ Sendgrid_Tools::set_api_key( '' );
207
+
208
+ break;
209
+ }
210
+
211
+ if ( ! Sendgrid_Tools::check_api_key( $params['sendgrid_apikey'], true ) ) {
212
  $response = array(
213
+ 'message' => 'API Key is invalid or without permissions.',
214
+ 'status' => 'error'
215
  );
216
 
217
  break;
218
  }
219
 
220
+ Sendgrid_Tools::set_api_key( $params['sendgrid_apikey'] );
221
 
222
  break;
223
 
240
 
241
  if ( ( isset( $params['sendgrid_username'] ) and ! $params['sendgrid_username'] ) or ( isset( $params['sendgrid_password'] ) and ! $params['sendgrid_password'] ) ) {
242
  $response = array(
243
+ 'message' => 'Username or password is empty.',
244
+ 'status' => 'error'
245
  );
246
  } elseif ( ! Sendgrid_Tools::check_username_password( $params['sendgrid_username'], $params['sendgrid_password'], true ) ) {
247
  $response = array(
248
+ 'message' => 'Username and password are invalid.',
249
+ 'status' => 'error'
250
  );
251
 
252
  break;
253
  }
254
 
255
+ if ( $save_username ) {
256
+ Sendgrid_Tools::set_username( $params['sendgrid_username'] );
257
+ }
258
 
259
+ if ( $save_password ) {
260
+ Sendgrid_Tools::set_password( $params['sendgrid_password'] );
261
+ }
262
 
263
  break;
264
  }
265
 
266
  if ( isset( $params['sendgrid_name'] ) ) {
267
+ update_option( 'sendgrid_from_name', $params['sendgrid_name'] );
268
  }
269
 
270
  if ( isset( $params['sendgrid_email'] ) ) {
271
+ if ( ! empty( $params['sendgrid_email'] ) and ! Sendgrid_Tools::is_valid_email( $params['sendgrid_email'] ) ) {
272
+ $response = array(
273
+ 'message' => 'Sending email address is not valid.',
274
+ 'status' => 'error'
275
+ );
276
+ } else {
277
+ update_option( 'sendgrid_from_email', $params['sendgrid_email'] );
278
+ }
279
  }
280
 
281
  if ( isset( $params['sendgrid_reply_to'] ) ) {
282
+ if ( ! empty( $params['sendgrid_reply_to'] ) and ! Sendgrid_Tools::is_valid_email( $params['sendgrid_reply_to'] ) ) {
283
+ $response = array(
284
+ 'message' => 'Reply email address is not valid.',
285
+ 'status' => 'error'
286
+ );
287
+ } else {
288
+ update_option( 'sendgrid_reply_to', $params['sendgrid_reply_to'] );
289
+ }
290
  }
291
 
292
  if ( isset( $params['sendgrid_categories'] ) ) {
293
+ update_option( 'sendgrid_categories', $params['sendgrid_categories'] );
294
  }
295
 
296
  if ( isset( $params['sendgrid_stats_categories'] ) ) {
297
+ update_option( 'sendgrid_stats_categories', $params['sendgrid_stats_categories'] );
298
  }
299
 
300
  if ( isset( $params['sendgrid_template'] ) ) {
301
  if ( ! Sendgrid_Tools::check_template( $params['sendgrid_template'] ) ) {
302
  $response = array(
303
+ 'message' => 'Template not found.',
304
+ 'status' => 'error'
305
  );
306
  } else {
307
  update_option( 'sendgrid_template', $params['sendgrid_template'] );
309
  }
310
 
311
  if ( isset( $params['send_method'] ) ) {
312
+ update_option( 'sendgrid_api', $params['send_method'] );
313
  }
314
 
315
  if ( isset( $params['auth_method'] ) && in_array( $params['auth_method'], Sendgrid_Tools::$allowed_auth_methods ) ) {
316
+ update_option( 'sendgrid_auth_method', $params['auth_method'] );
317
  }
318
 
319
  if ( isset( $params['sendgrid_port'] ) ) {
320
+ update_option( 'sendgrid_port', $params['sendgrid_port'] );
321
  }
322
 
323
  if ( isset( $params['content_type'] ) ) {
324
  update_option( 'sendgrid_content_type', $params['content_type'] );
325
  }
326
 
327
+ if( isset( $response ) and $response['status'] == 'error') {
328
  return $response;
329
+ }
330
 
331
  return array(
332
+ 'message' => 'Options are saved.',
333
+ 'status' => 'updated'
334
  );
335
  }
336
 
337
  private static function send_test_email( $params ) {
338
+ $to = $params['sendgrid_to'];
339
+ if ( ! Sendgrid_Tools::is_valid_email( $to ) ) {
340
+ return array(
341
+ 'message' => 'Email address in field "To" is not valid.',
342
+ 'status' => 'error',
343
+ 'error_type' => 'sending'
344
+ );
345
+ }
346
+
347
  $subject = stripslashes( $params['sendgrid_subj'] );
348
  $body = stripslashes( $params['sendgrid_body'] );
349
  $headers = $params['sendgrid_headers'];
350
 
351
+ if ( preg_match( '/content-type:\s*text\/html/i', $headers ) ) {
352
  $body_br = nl2br( $body );
353
  } else {
354
+ $body_br = $body;
355
  }
356
 
357
+ $sent = wp_mail( $to, $subject, $body_br, $headers );
358
  if ( true === $sent ) {
359
  return array(
360
+ 'message' => 'Email was sent.',
361
+ 'status' => 'updated'
362
  );
363
  }
364
 
365
  return array(
366
+ 'message' => 'Email wasn\'t sent.',
367
+ 'status' => 'updated',
368
+ 'error_type' => 'sending'
369
  );
370
  }
371
  }
lib/class-sendgrid-tools.php CHANGED
@@ -9,8 +9,9 @@ class Sendgrid_Tools
9
  const INVALID_CREDENTIALS_STATUS = "invalid";
10
 
11
  // used static variable because php 5.3 doesn't support array as constant
12
- public static $allowed_ports = array(Sendgrid_SMTP::TLS, Sendgrid_SMTP::TLS_ALTERNATIVE, Sendgrid_SMTP::SSL);
13
- public static $allowed_auth_methods = array('apikey', 'credentials');
 
14
 
15
  /**
16
  * Check username/password
@@ -21,26 +22,29 @@ class Sendgrid_Tools
21
  */
22
  public static function check_username_password( $username, $password, $clear_cache = false )
23
  {
24
- if ( !$username or !$password )
25
  return false;
 
26
 
27
- if ( $clear_cache )
28
- wp_cache_delete(self::CHECK_CREDENTIALS_CACHE_KEY, self::CACHE_GROUP);
 
29
 
30
- $valid_username_password = wp_cache_get(self::CHECK_CREDENTIALS_CACHE_KEY, self::CACHE_GROUP);
31
- if ( self::VALID_CREDENTIALS_STATUS == $valid_username_password )
32
  return true;
33
- elseif ( self::INVALID_CREDENTIALS_STATUS == $valid_username_password )
34
  return false;
 
35
 
36
  $url = 'https://api.sendgrid.com/api/profile.get.json?';
37
  $url .= "api_user=" . urlencode($username) . "&api_key=" . urlencode($password);
38
 
39
  $response = wp_remote_get( $url );
40
 
41
- if ( !is_array($response) or !isset( $response['body'] ) )
42
  {
43
- wp_cache_set(self::CHECK_CREDENTIALS_CACHE_KEY, self::INVALID_CREDENTIALS_STATUS, self::CACHE_GROUP, 60);
44
 
45
  return false;
46
  }
@@ -49,12 +53,12 @@ class Sendgrid_Tools
49
 
50
  if ( isset( $response['error'] ) )
51
  {
52
- wp_cache_set(self::CHECK_CREDENTIALS_CACHE_KEY, self::INVALID_CREDENTIALS_STATUS, self::CACHE_GROUP, 60);
53
 
54
  return false;
55
  }
56
 
57
- wp_cache_set(self::CHECK_CREDENTIALS_CACHE_KEY, self::VALID_CREDENTIALS_STATUS, self::CACHE_GROUP, 1800);
58
 
59
  return true;
60
  }
@@ -67,8 +71,9 @@ class Sendgrid_Tools
67
  */
68
  public static function check_api_key_scopes( $apikey, $scopes )
69
  {
70
- if ( ! $apikey or ! is_array( $scopes ) )
71
  return false;
 
72
 
73
  $url = 'https://api.sendgrid.com/v3/scopes';
74
 
@@ -80,22 +85,25 @@ class Sendgrid_Tools
80
  $response = wp_remote_get( $url, $args );
81
 
82
  if ( ! is_array( $response ) or ! isset( $response['body'] ) ) {
83
- wp_cache_set(self::CHECK_API_KEY_CACHE_KEY, self::INVALID_CREDENTIALS_STATUS, self::CACHE_GROUP, 60);
84
 
85
  return false;
86
  }
87
 
88
  $response = json_decode( $response['body'], true );
89
 
90
- if ( isset( $response['errors'] ) )
91
  return false;
 
92
 
93
- if( ! isset( $response['scopes'] ) )
94
  return false;
 
95
 
96
  foreach( $scopes as $scope ) {
97
- if( !in_array( $scope, $response['scopes'] ) )
98
  return false;
 
99
  }
100
 
101
  return true;
@@ -109,25 +117,28 @@ class Sendgrid_Tools
109
  */
110
  public static function check_api_key( $apikey, $clear_cache = false )
111
  {
112
- if ( ! $apikey )
113
  return false;
 
114
 
115
- if ( $clear_cache )
116
- wp_cache_delete(self::CHECK_API_KEY_CACHE_KEY, self::CACHE_GROUP);
 
117
 
118
- $valid_apikey = wp_cache_get(self::CHECK_API_KEY_CACHE_KEY, self::CACHE_GROUP);
119
- if ( self::VALID_CREDENTIALS_STATUS == $valid_apikey )
120
  return true;
121
- elseif ( self::INVALID_CREDENTIALS_STATUS == $valid_apikey )
122
  return false;
 
123
 
124
  if( ! Sendgrid_Tools::check_api_key_scopes( $apikey, array( "mail.send" ) ) ) {
125
- wp_cache_set(self::CHECK_API_KEY_CACHE_KEY, self::INVALID_CREDENTIALS_STATUS, self::CACHE_GROUP, 60);
126
 
127
  return false;
128
  }
129
 
130
- wp_cache_set(self::CHECK_API_KEY_CACHE_KEY, self::VALID_CREDENTIALS_STATUS, self::CACHE_GROUP, 1800);
131
 
132
  return true;
133
  }
@@ -140,24 +151,27 @@ class Sendgrid_Tools
140
  */
141
  public static function check_template( $template )
142
  {
143
- if ( '' == $template )
144
  return true;
 
145
 
146
  $url = 'v3/templates/' . $template;
147
 
148
- $parameters['auth_method'] = Sendgrid_Tools::get_auth_method();
149
- $parameters['api_username'] = Sendgrid_Tools::get_username();
150
- $parameters['api_password'] = Sendgrid_Tools::get_password();
151
- $parameters['apikey'] = Sendgrid_Tools::get_api_key();
152
 
153
  $response = Sendgrid_Tools::do_request( $url, $parameters );
154
 
155
- if ( ! $response )
156
  return false;
 
157
 
158
  $response = json_decode( $response, true );
159
- if ( isset( $response['error'] ) or ( isset( $response['errors'] ) && isset( $response['errors'][0]['message'] ) ) )
160
  return false;
 
161
 
162
  return true;
163
  }
@@ -173,7 +187,7 @@ class Sendgrid_Tools
173
  {
174
  $args = array();
175
  if ( "credentials" == $parameters['auth_method'] ) {
176
- $creds = base64_encode($parameters['api_username'] . ':' . $parameters['api_password']);
177
 
178
  $args = array(
179
  'headers' => array(
@@ -189,18 +203,17 @@ class Sendgrid_Tools
189
  );
190
  }
191
 
192
- unset($parameters['auth_method']);
193
- unset($parameters['api_username']);
194
- unset($parameters['api_password']);
195
- unset($parameters['apikey']);
196
 
197
  $data = urldecode( http_build_query( $parameters ) );
198
  $url = "https://api.sendgrid.com/$api?$data";
199
 
200
  $response = wp_remote_get( $url, $args );
201
 
202
- if ( !is_array($response) or !isset( $response['body'] ) )
203
- {
204
  return false;
205
  }
206
 
@@ -214,16 +227,16 @@ class Sendgrid_Tools
214
  */
215
  public static function get_username()
216
  {
217
- if ( defined('SENDGRID_USERNAME') ) {
218
  return SENDGRID_USERNAME;
219
  } else {
220
- $username = get_option('sendgrid_user');
221
  if( $username ) {
222
- delete_option('sendgrid_user');
223
- update_option('sendgrid_username', $username);
224
  }
225
 
226
- return get_option('sendgrid_username');
227
  }
228
  }
229
 
@@ -232,12 +245,13 @@ class Sendgrid_Tools
232
  * @param type string $username
233
  * @return bool
234
  */
235
- public static function set_username($username)
236
  {
237
- if( ! isset( $username ) )
238
- return update_option('sendgrid_username', '');
 
239
 
240
- return update_option('sendgrid_username', $username);
241
  }
242
 
243
  /**
@@ -247,17 +261,17 @@ class Sendgrid_Tools
247
  */
248
  public static function get_password()
249
  {
250
- if ( defined('SENDGRID_PASSWORD') ) {
251
  return SENDGRID_PASSWORD;
252
  } else {
253
- $password = get_option('sendgrid_pwd');
254
- $new_password = get_option('sendgrid_password');
255
- if( $new_password && ! $password ) {
256
- update_option('sendgrid_pwd', self::decrypt( $new_password, AUTH_KEY ) );
257
- delete_option('sendgrid_password');
258
  }
259
 
260
- $password = get_option('sendgrid_pwd');
261
  return $password;
262
  }
263
  }
@@ -267,9 +281,9 @@ class Sendgrid_Tools
267
  * @param type string $password
268
  * @return bool
269
  */
270
- public static function set_password($password)
271
  {
272
- return update_option('sendgrid_pwd', $password);
273
  }
274
 
275
  /**
@@ -279,17 +293,17 @@ class Sendgrid_Tools
279
  */
280
  public static function get_api_key()
281
  {
282
- if ( defined('SENDGRID_API_KEY') ) {
283
  return SENDGRID_API_KEY;
284
  } else {
285
- $apikey = get_option('sendgrid_api_key');
286
- $new_apikey = get_option('sendgrid_apikey');
287
- if( $new_apikey && ! $apikey ) {
288
- update_option('sendgrid_api_key', self::decrypt( $new_apikey, AUTH_KEY ));
289
- delete_option('sendgrid_apikey');
290
  }
291
 
292
- $apikey = get_option('sendgrid_api_key');
293
  return $apikey;
294
  }
295
  }
@@ -299,9 +313,9 @@ class Sendgrid_Tools
299
  * @param type string $apikey
300
  * @return bool
301
  */
302
- public static function set_api_key($apikey)
303
  {
304
- return update_option('sendgrid_api_key', $apikey);
305
  }
306
 
307
  /**
@@ -311,10 +325,10 @@ class Sendgrid_Tools
311
  */
312
  public static function get_send_method()
313
  {
314
- if ( defined('SENDGRID_SEND_METHOD') ) {
315
  return SENDGRID_SEND_METHOD;
316
- } elseif ( get_option('sendgrid_api') ) {
317
- return get_option('sendgrid_api');
318
  } else {
319
  return 'api';
320
  }
@@ -327,13 +341,13 @@ class Sendgrid_Tools
327
  */
328
  public static function get_auth_method()
329
  {
330
- if ( defined('SENDGRID_AUTH_METHOD') ) {
331
  return SENDGRID_AUTH_METHOD;
332
- } elseif ( get_option('sendgrid_auth_method') ) {
333
- $auth_method = get_option('sendgrid_auth_method');
334
  if ( 'username' == $auth_method ) {
335
  $auth_method = 'credentials';
336
- update_option('sendgrid_auth_method', $auth_method);
337
  }
338
 
339
  return $auth_method;
@@ -353,10 +367,10 @@ class Sendgrid_Tools
353
  */
354
  public static function get_port()
355
  {
356
- if ( defined('SENDGRID_PORT') ) {
357
  return SENDGRID_PORT;
358
  } else {
359
- return get_option('sendgrid_port', Sendgrid_SMTP::TLS);
360
  }
361
  }
362
 
@@ -367,10 +381,10 @@ class Sendgrid_Tools
367
  */
368
  public static function get_from_name()
369
  {
370
- if ( defined('SENDGRID_FROM_NAME') ) {
371
  return SENDGRID_FROM_NAME;
372
  } else {
373
- return get_option('sendgrid_from_name');
374
  }
375
  }
376
 
@@ -381,10 +395,10 @@ class Sendgrid_Tools
381
  */
382
  public static function get_from_email()
383
  {
384
- if ( defined('SENDGRID_FROM_EMAIL') ) {
385
  return SENDGRID_FROM_EMAIL;
386
  } else {
387
- return get_option('sendgrid_from_email');
388
  }
389
  }
390
 
@@ -395,10 +409,10 @@ class Sendgrid_Tools
395
  */
396
  public static function get_reply_to()
397
  {
398
- if ( defined('SENDGRID_REPLY_TO') ) {
399
  return SENDGRID_REPLY_TO;
400
  } else {
401
- return get_option('sendgrid_reply_to');
402
  }
403
  }
404
 
@@ -409,10 +423,10 @@ class Sendgrid_Tools
409
  */
410
  public static function get_categories()
411
  {
412
- if ( defined('SENDGRID_CATEGORIES') ) {
413
  return SENDGRID_CATEGORIES;
414
  } else {
415
- return get_option('sendgrid_categories');
416
  }
417
  }
418
 
@@ -423,10 +437,10 @@ class Sendgrid_Tools
423
  */
424
  public static function get_stats_categories()
425
  {
426
- if ( defined('SENDGRID_STATS_CATEGORIES') ) {
427
  return SENDGRID_STATS_CATEGORIES;
428
  } else {
429
- return get_option('sendgrid_stats_categories');
430
  }
431
  }
432
 
@@ -437,8 +451,8 @@ class Sendgrid_Tools
437
  */
438
  public static function get_categories_array()
439
  {
440
- $general_categories = Sendgrid_Tools::get_categories();
441
- $stats_categories = Sendgrid_Tools::get_stats_categories();
442
  $general_categories_array = $general_categories? explode( ',', trim( $general_categories ) ):array();
443
  $stats_categories_array = $stats_categories? explode( ',', trim( $stats_categories ) ):array();
444
  return array_unique( array_merge( $general_categories_array, $stats_categories_array ) );
@@ -451,7 +465,7 @@ class Sendgrid_Tools
451
  */
452
  public static function get_template()
453
  {
454
- if ( defined('SENDGRID_TEMPLATE') ) {
455
  return SENDGRID_TEMPLATE;
456
  } else {
457
  return get_option( 'sendgrid_template' );
@@ -465,7 +479,7 @@ class Sendgrid_Tools
465
  */
466
  public static function get_content_type()
467
  {
468
- if ( defined('SENDGRID_CONTENT_TYPE') ) {
469
  return SENDGRID_CONTENT_TYPE;
470
  } else {
471
  return get_option('sendgrid_content_type');
@@ -477,29 +491,33 @@ class Sendgrid_Tools
477
  *
478
  * @return string template
479
  */
480
- private static function decrypt($encrypted_input_string, $key) {
481
- if (!extension_loaded('mcrypt')) {
482
  return '';
483
  }
484
 
485
- $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
486
- if(false === $iv_size)
487
  return '';
 
488
 
489
- $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
490
- if(false === $iv)
491
  return '';
 
492
 
493
- $h_key = hash('sha256', $key, TRUE);
494
- $decoded = base64_decode($encrypted_input_string);
495
- if(false === $decoded)
496
  return '';
 
497
 
498
- $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $h_key, $decoded, MCRYPT_MODE_ECB, $iv);
499
- if(false === $decrypted)
500
  return '';
 
501
 
502
- return trim($decrypted);
503
  }
504
 
505
  /**
@@ -514,4 +532,24 @@ class Sendgrid_Tools
514
 
515
  return Sendgrid_Tools::check_api_key_scopes( $apikey, $required_scopes );
516
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
517
  }
9
  const INVALID_CREDENTIALS_STATUS = "invalid";
10
 
11
  // used static variable because php 5.3 doesn't support array as constant
12
+ public static $allowed_ports = array( Sendgrid_SMTP::TLS, Sendgrid_SMTP::TLS_ALTERNATIVE, Sendgrid_SMTP::SSL );
13
+ public static $allowed_auth_methods = array( 'apikey', 'credentials' );
14
+ public static $allowed_content_type = array( 'plaintext', 'html' );
15
 
16
  /**
17
  * Check username/password
22
  */
23
  public static function check_username_password( $username, $password, $clear_cache = false )
24
  {
25
+ if ( !$username or !$password ) {
26
  return false;
27
+ }
28
 
29
+ if ( $clear_cache ) {
30
+ wp_cache_delete( self::CHECK_CREDENTIALS_CACHE_KEY, self::CACHE_GROUP );
31
+ }
32
 
33
+ $valid_username_password = wp_cache_get( self::CHECK_CREDENTIALS_CACHE_KEY, self::CACHE_GROUP );
34
+ if ( self::VALID_CREDENTIALS_STATUS == $valid_username_password ) {
35
  return true;
36
+ } elseif ( self::INVALID_CREDENTIALS_STATUS == $valid_username_password ) {
37
  return false;
38
+ }
39
 
40
  $url = 'https://api.sendgrid.com/api/profile.get.json?';
41
  $url .= "api_user=" . urlencode($username) . "&api_key=" . urlencode($password);
42
 
43
  $response = wp_remote_get( $url );
44
 
45
+ if ( ! is_array( $response ) or ! isset( $response['body'] ) )
46
  {
47
+ wp_cache_set( self::CHECK_CREDENTIALS_CACHE_KEY, self::INVALID_CREDENTIALS_STATUS, self::CACHE_GROUP, 60 );
48
 
49
  return false;
50
  }
53
 
54
  if ( isset( $response['error'] ) )
55
  {
56
+ wp_cache_set( self::CHECK_CREDENTIALS_CACHE_KEY, self::INVALID_CREDENTIALS_STATUS, self::CACHE_GROUP, 60 );
57
 
58
  return false;
59
  }
60
 
61
+ wp_cache_set( self::CHECK_CREDENTIALS_CACHE_KEY, self::VALID_CREDENTIALS_STATUS, self::CACHE_GROUP, 1800 );
62
 
63
  return true;
64
  }
71
  */
72
  public static function check_api_key_scopes( $apikey, $scopes )
73
  {
74
+ if ( ! $apikey or ! is_array( $scopes ) ) {
75
  return false;
76
+ }
77
 
78
  $url = 'https://api.sendgrid.com/v3/scopes';
79
 
85
  $response = wp_remote_get( $url, $args );
86
 
87
  if ( ! is_array( $response ) or ! isset( $response['body'] ) ) {
88
+ wp_cache_set( self::CHECK_API_KEY_CACHE_KEY, self::INVALID_CREDENTIALS_STATUS, self::CACHE_GROUP, 60 );
89
 
90
  return false;
91
  }
92
 
93
  $response = json_decode( $response['body'], true );
94
 
95
+ if ( isset( $response['errors'] ) ) {
96
  return false;
97
+ }
98
 
99
+ if( ! isset( $response['scopes'] ) ) {
100
  return false;
101
+ }
102
 
103
  foreach( $scopes as $scope ) {
104
+ if( !in_array( $scope, $response['scopes'] ) ) {
105
  return false;
106
+ }
107
  }
108
 
109
  return true;
117
  */
118
  public static function check_api_key( $apikey, $clear_cache = false )
119
  {
120
+ if ( ! $apikey ) {
121
  return false;
122
+ }
123
 
124
+ if ( $clear_cache ) {
125
+ wp_cache_delete( self::CHECK_API_KEY_CACHE_KEY, self::CACHE_GROUP );
126
+ }
127
 
128
+ $valid_apikey = wp_cache_get( self::CHECK_API_KEY_CACHE_KEY, self::CACHE_GROUP );
129
+ if ( self::VALID_CREDENTIALS_STATUS == $valid_apikey ) {
130
  return true;
131
+ } elseif ( self::INVALID_CREDENTIALS_STATUS == $valid_apikey ) {
132
  return false;
133
+ }
134
 
135
  if( ! Sendgrid_Tools::check_api_key_scopes( $apikey, array( "mail.send" ) ) ) {
136
+ wp_cache_set( self::CHECK_API_KEY_CACHE_KEY, self::INVALID_CREDENTIALS_STATUS, self::CACHE_GROUP, 60 );
137
 
138
  return false;
139
  }
140
 
141
+ wp_cache_set( self::CHECK_API_KEY_CACHE_KEY, self::VALID_CREDENTIALS_STATUS, self::CACHE_GROUP, 1800 );
142
 
143
  return true;
144
  }
151
  */
152
  public static function check_template( $template )
153
  {
154
+ if ( '' == $template ) {
155
  return true;
156
+ }
157
 
158
  $url = 'v3/templates/' . $template;
159
 
160
+ $parameters['auth_method'] = Sendgrid_Tools::get_auth_method();
161
+ $parameters['api_username'] = Sendgrid_Tools::get_username();
162
+ $parameters['api_password'] = Sendgrid_Tools::get_password();
163
+ $parameters['apikey'] = Sendgrid_Tools::get_api_key();
164
 
165
  $response = Sendgrid_Tools::do_request( $url, $parameters );
166
 
167
+ if ( ! $response ) {
168
  return false;
169
+ }
170
 
171
  $response = json_decode( $response, true );
172
+ if ( isset( $response['error'] ) or ( isset( $response['errors'] ) and isset( $response['errors'][0]['message'] ) ) ) {
173
  return false;
174
+ }
175
 
176
  return true;
177
  }
187
  {
188
  $args = array();
189
  if ( "credentials" == $parameters['auth_method'] ) {
190
+ $creds = base64_encode( $parameters['api_username'] . ':' . $parameters['api_password'] );
191
 
192
  $args = array(
193
  'headers' => array(
203
  );
204
  }
205
 
206
+ unset( $parameters['auth_method'] );
207
+ unset( $parameters['api_username'] );
208
+ unset( $parameters['api_password'] );
209
+ unset( $parameters['apikey'] );
210
 
211
  $data = urldecode( http_build_query( $parameters ) );
212
  $url = "https://api.sendgrid.com/$api?$data";
213
 
214
  $response = wp_remote_get( $url, $args );
215
 
216
+ if ( ! is_array( $response ) or ! isset( $response['body'] ) ) {
 
217
  return false;
218
  }
219
 
227
  */
228
  public static function get_username()
229
  {
230
+ if ( defined( 'SENDGRID_USERNAME' ) ) {
231
  return SENDGRID_USERNAME;
232
  } else {
233
+ $username = get_option( 'sendgrid_user' );
234
  if( $username ) {
235
+ delete_option( 'sendgrid_user' );
236
+ update_option( 'sendgrid_username', $username );
237
  }
238
 
239
+ return get_option( 'sendgrid_username' );
240
  }
241
  }
242
 
245
  * @param type string $username
246
  * @return bool
247
  */
248
+ public static function set_username( $username )
249
  {
250
+ if( ! isset( $username ) ) {
251
+ return update_option( 'sendgrid_username', '' );
252
+ }
253
 
254
+ return update_option( 'sendgrid_username', $username );
255
  }
256
 
257
  /**
261
  */
262
  public static function get_password()
263
  {
264
+ if ( defined( 'SENDGRID_PASSWORD' ) ) {
265
  return SENDGRID_PASSWORD;
266
  } else {
267
+ $password = get_option( 'sendgrid_pwd' );
268
+ $new_password = get_option( 'sendgrid_password' );
269
+ if( $new_password and ! $password ) {
270
+ update_option( 'sendgrid_pwd', self::decrypt( $new_password, AUTH_KEY ) );
271
+ delete_option( 'sendgrid_password' );
272
  }
273
 
274
+ $password = get_option( 'sendgrid_pwd' );
275
  return $password;
276
  }
277
  }
281
  * @param type string $password
282
  * @return bool
283
  */
284
+ public static function set_password( $password )
285
  {
286
+ return update_option( 'sendgrid_pwd', $password );
287
  }
288
 
289
  /**
293
  */
294
  public static function get_api_key()
295
  {
296
+ if ( defined( 'SENDGRID_API_KEY' ) ) {
297
  return SENDGRID_API_KEY;
298
  } else {
299
+ $apikey = get_option( 'sendgrid_api_key' );
300
+ $new_apikey = get_option( 'sendgrid_apikey' );
301
+ if( $new_apikey and ! $apikey ) {
302
+ update_option( 'sendgrid_api_key', self::decrypt( $new_apikey, AUTH_KEY ) );
303
+ delete_option( 'sendgrid_apikey' );
304
  }
305
 
306
+ $apikey = get_option( 'sendgrid_api_key' );
307
  return $apikey;
308
  }
309
  }
313
  * @param type string $apikey
314
  * @return bool
315
  */
316
+ public static function set_api_key( $apikey )
317
  {
318
+ return update_option( 'sendgrid_api_key', $apikey );
319
  }
320
 
321
  /**
325
  */
326
  public static function get_send_method()
327
  {
328
+ if ( defined( 'SENDGRID_SEND_METHOD' ) ) {
329
  return SENDGRID_SEND_METHOD;
330
+ } elseif ( get_option( 'sendgrid_api' ) ) {
331
+ return get_option( 'sendgrid_api' );
332
  } else {
333
  return 'api';
334
  }
341
  */
342
  public static function get_auth_method()
343
  {
344
+ if ( defined( 'SENDGRID_AUTH_METHOD' ) ) {
345
  return SENDGRID_AUTH_METHOD;
346
+ } elseif ( get_option( 'sendgrid_auth_method' ) ) {
347
+ $auth_method = get_option( 'sendgrid_auth_method' );
348
  if ( 'username' == $auth_method ) {
349
  $auth_method = 'credentials';
350
+ update_option( 'sendgrid_auth_method', $auth_method );
351
  }
352
 
353
  return $auth_method;
367
  */
368
  public static function get_port()
369
  {
370
+ if ( defined( 'SENDGRID_PORT' ) ) {
371
  return SENDGRID_PORT;
372
  } else {
373
+ return get_option( 'sendgrid_port', Sendgrid_SMTP::TLS );
374
  }
375
  }
376
 
381
  */
382
  public static function get_from_name()
383
  {
384
+ if ( defined( 'SENDGRID_FROM_NAME' ) ) {
385
  return SENDGRID_FROM_NAME;
386
  } else {
387
+ return get_option( 'sendgrid_from_name' );
388
  }
389
  }
390
 
395
  */
396
  public static function get_from_email()
397
  {
398
+ if ( defined( 'SENDGRID_FROM_EMAIL' ) ) {
399
  return SENDGRID_FROM_EMAIL;
400
  } else {
401
+ return get_option( 'sendgrid_from_email' );
402
  }
403
  }
404
 
409
  */
410
  public static function get_reply_to()
411
  {
412
+ if ( defined( 'SENDGRID_REPLY_TO' ) ) {
413
  return SENDGRID_REPLY_TO;
414
  } else {
415
+ return get_option( 'sendgrid_reply_to' );
416
  }
417
  }
418
 
423
  */
424
  public static function get_categories()
425
  {
426
+ if ( defined( 'SENDGRID_CATEGORIES' ) ) {
427
  return SENDGRID_CATEGORIES;
428
  } else {
429
+ return get_option( 'sendgrid_categories' );
430
  }
431
  }
432
 
437
  */
438
  public static function get_stats_categories()
439
  {
440
+ if ( defined( 'SENDGRID_STATS_CATEGORIES' ) ) {
441
  return SENDGRID_STATS_CATEGORIES;
442
  } else {
443
+ return get_option( 'sendgrid_stats_categories' );
444
  }
445
  }
446
 
451
  */
452
  public static function get_categories_array()
453
  {
454
+ $general_categories = Sendgrid_Tools::get_categories();
455
+ $stats_categories = Sendgrid_Tools::get_stats_categories();
456
  $general_categories_array = $general_categories? explode( ',', trim( $general_categories ) ):array();
457
  $stats_categories_array = $stats_categories? explode( ',', trim( $stats_categories ) ):array();
458
  return array_unique( array_merge( $general_categories_array, $stats_categories_array ) );
465
  */
466
  public static function get_template()
467
  {
468
+ if ( defined( 'SENDGRID_TEMPLATE' ) ) {
469
  return SENDGRID_TEMPLATE;
470
  } else {
471
  return get_option( 'sendgrid_template' );
479
  */
480
  public static function get_content_type()
481
  {
482
+ if ( defined( 'SENDGRID_CONTENT_TYPE' ) ) {
483
  return SENDGRID_CONTENT_TYPE;
484
  } else {
485
  return get_option('sendgrid_content_type');
491
  *
492
  * @return string template
493
  */
494
+ private static function decrypt( $encrypted_input_string, $key ) {
495
+ if ( ! extension_loaded( 'mcrypt' ) ) {
496
  return '';
497
  }
498
 
499
+ $iv_size = mcrypt_get_iv_size( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB );
500
+ if( false === $iv_size ) {
501
  return '';
502
+ }
503
 
504
+ $iv = mcrypt_create_iv( $iv_size, MCRYPT_RAND );
505
+ if( false === $iv ) {
506
  return '';
507
+ }
508
 
509
+ $h_key = hash( 'sha256', $key, TRUE );
510
+ $decoded = base64_decode( $encrypted_input_string );
511
+ if( false === $decoded ) {
512
  return '';
513
+ }
514
 
515
+ $decrypted = mcrypt_decrypt( MCRYPT_RIJNDAEL_256, $h_key, $decoded, MCRYPT_MODE_ECB, $iv );
516
+ if( false === $decrypted ) {
517
  return '';
518
+ }
519
 
520
+ return trim( $decrypted );
521
  }
522
 
523
  /**
532
 
533
  return Sendgrid_Tools::check_api_key_scopes( $apikey, $required_scopes );
534
  }
535
+
536
+ /**
537
+ * Returns true if the email is valid, false otherwise
538
+ *
539
+ * @return bool
540
+ */
541
+ public static function is_valid_email( $email )
542
+ {
543
+ return filter_var( $email, FILTER_VALIDATE_EMAIL );
544
+ }
545
+
546
+ /**
547
+ * Returns the string content of the input with "<url>" replaced by "url"
548
+ *
549
+ * @return string
550
+ */
551
+ public static function remove_all_tag_urls( $content )
552
+ {
553
+ return preg_replace('/<(https?:\/\/[^>]*)>/im', '$1', $content);
554
+ }
555
  }
lib/sendgrid/sendgrid-wp-mail.php CHANGED
@@ -178,6 +178,20 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
178
  $mail->addCategory( $category );
179
  }
180
  break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
  default:
182
  // Add it to our grand headers array
183
  $headers[trim( $name )] = trim( $content );
@@ -286,7 +300,7 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array()
286
 
287
  // send HTML content
288
  if ( 'text/plain' !== $content_type ) {
289
- $mail->setHtml( $message );
290
  }
291
 
292
  // set from name
178
  $mail->addCategory( $category );
179
  }
180
  break;
181
+ case 'substitutions':
182
+ if ( false !== strpos( $content, ';' ) ) {
183
+ $substitutions = explode( ';', $content );
184
+ }
185
+ else {
186
+ $substitutions = (array) trim( $content );
187
+ }
188
+ foreach ( $substitutions as $substitution ) {
189
+ if ( false !== strpos( $content, '=' ) ) {
190
+ list( $key, $val ) = explode( '=', $substitution );
191
+ $mail->addSubstitution( '%' . trim( $key ) . '%', explode( ',', trim( $val ) ) );
192
+ }
193
+ }
194
+ break;
195
  default:
196
  // Add it to our grand headers array
197
  $headers[trim( $name )] = trim( $content );
300
 
301
  // send HTML content
302
  if ( 'text/plain' !== $content_type ) {
303
+ $mail->setHtml( Sendgrid_Tools::remove_all_tag_urls( $message ) );
304
  }
305
 
306
  // set from name
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: http://sendgrid.com/
4
  Tags: email, email reliability, email templates, sendgrid, smtp, transactional email, wp_mail,email infrastructure, email marketing, marketing email, deliverability, email deliverability, email delivery, email server, mail server, email integration, cloud email
5
  Requires at least: 3.3
6
  Tested up to: 4.4
7
- Stable tag: 1.7.4
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -64,6 +64,7 @@ $headers[] = 'Bcc: address5@sendgrid.com';
64
  $headers[] = 'unique-args:customer=mycustomer;location=mylocation'
65
  $headers[] = 'categories: category1, category2'
66
  $headers[] = 'template: templateID'
 
67
 
68
  $attachments = array('/tmp/img1.jpg', '/tmp/img2.jpg');
69
 
@@ -131,12 +132,18 @@ Create a SendGrid account at <a href="http://sendgrid.com/partner/wordpress" tar
131
  9. Now you are able to configure port number when using SMTP method.
132
  10. You are able to configure what template to use for sending emails.
133
  11. You are able to configure categories for which you would like to see your stats.
 
134
 
135
  == Changelog ==
136
 
 
 
 
 
 
137
  = 1.7.4 =
138
- * Fixed some failing requests during API Key checks.
139
- * Fixed an error that appeared on fresh installs regarding invalid port setting.
140
  = 1.7.3 =
141
  * Add global config for content-type
142
  * Validate send_method and port set in config file
@@ -223,9 +230,14 @@ Create a SendGrid account at <a href="http://sendgrid.com/partner/wordpress" tar
223
 
224
  == Upgrade notice ==
225
 
 
 
 
 
 
226
  = 1.7.4 =
227
- * Fixed some failing requests during API Key checks.
228
- * Fixed an error that appeared on fresh installs regarding invalid port setting.
229
  = 1.7.3 =
230
  * Add global config for content-type
231
  * Validate send_method and port set in config file
4
  Tags: email, email reliability, email templates, sendgrid, smtp, transactional email, wp_mail,email infrastructure, email marketing, marketing email, deliverability, email deliverability, email delivery, email server, mail server, email integration, cloud email
5
  Requires at least: 3.3
6
  Tested up to: 4.4
7
+ Stable tag: 1.7.5
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
64
  $headers[] = 'unique-args:customer=mycustomer;location=mylocation'
65
  $headers[] = 'categories: category1, category2'
66
  $headers[] = 'template: templateID'
67
+ $headers[] = 'substitutions:name=name1,name2;subject=subject1,subject2'
68
 
69
  $attachments = array('/tmp/img1.jpg', '/tmp/img2.jpg');
70
 
132
  9. Now you are able to configure port number when using SMTP method.
133
  10. You are able to configure what template to use for sending emails.
134
  11. You are able to configure categories for which you would like to see your stats.
135
+ 12. You can use substitutions for emails.
136
 
137
  == Changelog ==
138
 
139
+ = 1.7.5 =
140
+ * Fixed an issue with the reset password email from Wordpress
141
+ * Updated validation for email addresses
142
+ * Fixed an issue where some errors were not displayed on the settings page
143
+ * Add substitutions functionality
144
  = 1.7.4 =
145
+ * Fixed some failing requests during API Key checks
146
+ * Fixed an error that appeared on fresh installs regarding invalid port setting
147
  = 1.7.3 =
148
  * Add global config for content-type
149
  * Validate send_method and port set in config file
230
 
231
  == Upgrade notice ==
232
 
233
+ = 1.7.5 =
234
+ * Fixed an issue with the reset password email from Wordpress
235
+ * Updated validation for email addresses
236
+ * Fixed an issue where some errors were not displayed on the settings page
237
+ * Add substitutions functionality
238
  = 1.7.4 =
239
+ * Fixed some failing requests during API Key checks
240
+ * Fixed an error that appeared on fresh installs regarding invalid port setting
241
  = 1.7.3 =
242
  * Add global config for content-type
243
  * Validate send_method and port set in config file
view/sendgrid_settings.php CHANGED
@@ -94,14 +94,14 @@
94
  <tr valign="top">
95
  <th scope="row"><?php _e("Sending Address: "); ?></th>
96
  <td>
97
- <input type="email" name="sendgrid_email" value="<?php echo $email; ?>" size="20" class="regular-text" <?php disabled( defined('SENDGRID_FROM_EMAIL') ); ?>>
98
  <p class="description"><?php _e('Email address from which the message will be sent.') ?></p>
99
  </td>
100
  </tr>
101
  <tr valign="top">
102
  <th scope="row"><?php _e("Reply Address: "); ?></th>
103
  <td>
104
- <input type="email" name="sendgrid_reply_to" value="<?php echo $reply_to; ?>" size="20" class="regular-text" <?php disabled( defined('SENDGRID_REPLY_TO') ); ?>>
105
  <span><small><em><?php _e('Leave blank to use Sending Address.') ?></em></small></span>
106
  <p class="description"><?php _e('Email address where replies will be returned.') ?></p>
107
  </td>
@@ -171,7 +171,7 @@
171
  <tr valign="top">
172
  <th scope="row"><?php _e("To: "); ?></th>
173
  <td>
174
- <input type="email" name="sendgrid_to" required="true" value="<?php echo isset($success) ? '' : isset($to) ? $to : '' ; ?>" size="20" class="regular-text">
175
  </td>
176
  </tr>
177
  <tr valign="top">
94
  <tr valign="top">
95
  <th scope="row"><?php _e("Sending Address: "); ?></th>
96
  <td>
97
+ <input type="text" name="sendgrid_email" value="<?php echo $email; ?>" size="20" class="regular-text" <?php disabled( defined('SENDGRID_FROM_EMAIL') ); ?>>
98
  <p class="description"><?php _e('Email address from which the message will be sent.') ?></p>
99
  </td>
100
  </tr>
101
  <tr valign="top">
102
  <th scope="row"><?php _e("Reply Address: "); ?></th>
103
  <td>
104
+ <input type="text" name="sendgrid_reply_to" value="<?php echo $reply_to; ?>" size="20" class="regular-text" <?php disabled( defined('SENDGRID_REPLY_TO') ); ?>>
105
  <span><small><em><?php _e('Leave blank to use Sending Address.') ?></em></small></span>
106
  <p class="description"><?php _e('Email address where replies will be returned.') ?></p>
107
  </td>
171
  <tr valign="top">
172
  <th scope="row"><?php _e("To: "); ?></th>
173
  <td>
174
+ <input type="text" name="sendgrid_to" required="true" value="<?php echo isset($success) ? '' : isset($to) ? $to : '' ; ?>" size="20" class="regular-text">
175
  </td>
176
  </tr>
177
  <tr valign="top">
wpsendgrid.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: SendGrid
4
  Plugin URI: http://wordpress.org/plugins/sendgrid-email-delivery-simplified/
5
  Description: Email Delivery. Simplified. SendGrid's cloud-based email infrastructure relieves businesses of the cost and complexity of maintaining custom email systems. SendGrid provides reliable delivery, scalability and real-time analytics along with flexible APIs that make custom integration a breeze.
6
- Version: 1.7.4
7
  Author: SendGrid
8
  Author URI: http://sendgrid.com
9
  Text Domain: sendgrid-email-delivery-simplified
3
  Plugin Name: SendGrid
4
  Plugin URI: http://wordpress.org/plugins/sendgrid-email-delivery-simplified/
5
  Description: Email Delivery. Simplified. SendGrid's cloud-based email infrastructure relieves businesses of the cost and complexity of maintaining custom email systems. SendGrid provides reliable delivery, scalability and real-time analytics along with flexible APIs that make custom integration a breeze.
6
+ Version: 1.7.5
7
  Author: SendGrid
8
  Author URI: http://sendgrid.com
9
  Text Domain: sendgrid-email-delivery-simplified