SendGrid - Version 1.11.0

Version Description

  • BREAKING CHANGE: DO NOT UPGRADE IF YOU USE PHP <
Download this release

Release Info

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

Code changes from version 1.10.9 to 1.11.0

assets/screenshot-10.png CHANGED
Binary file
assets/screenshot-11.png CHANGED
Binary file
assets/screenshot-12.png DELETED
Binary file
assets/screenshot-4.png CHANGED
Binary file
assets/screenshot-5.png CHANGED
Binary file
assets/screenshot-6.png CHANGED
Binary file
assets/screenshot-7.png CHANGED
Binary file
assets/screenshot-8.png CHANGED
Binary file
assets/screenshot-9.png CHANGED
Binary file
lib/class-sendgrid-nlvx.php CHANGED
@@ -13,7 +13,7 @@ class Sendgrid_NLVX
13
  */
14
  protected static function get_auth_header_value()
15
  {
16
- if ( "false" == Sendgrid_Tools::get_mc_opt_use_transactional() ) {
17
  $mc_api_key = Sendgrid_Tools::get_mc_api_key();
18
 
19
  if ( false != $mc_api_key ) {
@@ -21,20 +21,12 @@ class Sendgrid_NLVX
21
  }
22
  }
23
 
24
- $auth_method = Sendgrid_Tools::get_auth_method();
25
-
26
- if ( 'credentials' == $auth_method ) {
27
- $creds = base64_encode( Sendgrid_Tools::get_username() . ':' . Sendgrid_Tools::get_password() );
28
-
29
- return 'Basic ' . $creds;
30
- } else {
31
- $api_key = Sendgrid_Tools::get_api_key();
32
- if ( false == $api_key ) {
33
- return false;
34
- }
35
-
36
- return 'Bearer ' . $api_key;
37
  }
 
 
38
  }
39
 
40
  /**
13
  */
14
  protected static function get_auth_header_value()
15
  {
16
+ if ( 'false' == Sendgrid_Tools::get_mc_opt_use_transactional() ) {
17
  $mc_api_key = Sendgrid_Tools::get_mc_api_key();
18
 
19
  if ( false != $mc_api_key ) {
21
  }
22
  }
23
 
24
+ $api_key = Sendgrid_Tools::get_api_key();
25
+ if ( false == $api_key ) {
26
+ return false;
 
 
 
 
 
 
 
 
 
 
27
  }
28
+
29
+ return 'Bearer ' . $api_key;
30
  }
31
 
32
  /**
lib/class-sendgrid-settings.php CHANGED
@@ -143,11 +143,8 @@ class Sendgrid_Settings {
143
  $status = '';
144
  $message = '';
145
 
146
- $user = stripslashes( Sendgrid_Tools::get_username() );
147
- $password = stripslashes( Sendgrid_Tools::get_password() );
148
  $api_key = stripslashes( Sendgrid_Tools::get_api_key() );
149
  $send_method = stripslashes( Sendgrid_Tools::get_send_method() );
150
- $auth_method = stripslashes( Sendgrid_Tools::get_auth_method() );
151
  $name = stripslashes( Sendgrid_Tools::get_from_name() );
152
  $email = stripslashes( Sendgrid_Tools::get_from_email() );
153
  $reply_to = stripslashes( Sendgrid_Tools::get_reply_to() );
@@ -229,7 +226,7 @@ class Sendgrid_Settings {
229
  }
230
 
231
  $is_mc_api_key_valid = true;
232
- if ( 'true' == $mc_opt_use_transactional and 'apikey' == $auth_method and ! empty( $api_key ) ) {
233
  if ( ! Sendgrid_Tools::check_api_key_mc( $api_key ) ) {
234
  $is_mc_api_key_valid = false;
235
  }
@@ -251,7 +248,7 @@ class Sendgrid_Settings {
251
  $status = 'error';
252
  }
253
 
254
- if ( 'apikey' == $auth_method and ! empty( $api_key ) ) {
255
  if ( ! Sendgrid_Tools::check_api_key( $api_key, true ) ) {
256
  $message = 'API Key is invalid or without permissions.';
257
  $status = 'error';
@@ -261,13 +258,6 @@ class Sendgrid_Settings {
261
  } elseif ( 'error' != $status ) {
262
  $status = 'valid_auth';
263
  }
264
- } elseif ( 'credentials' == $auth_method and ! empty( $user ) and ! empty( $password ) ) {
265
- if ( ! Sendgrid_Tools::check_username_password( $user, $password, true ) ) {
266
- $message = 'Username and password are invalid.';
267
- $status = 'error';
268
- } elseif ( 'error' != $status ) {
269
- $status = 'valid_auth';
270
- }
271
  }
272
 
273
  if ( $template and ! Sendgrid_Tools::check_template( $template ) ) {
@@ -280,11 +270,6 @@ class Sendgrid_Settings {
280
  $status = 'error';
281
  }
282
 
283
- if ( ! in_array( $auth_method, Sendgrid_Tools::$allowed_auth_methods ) ) {
284
- $message = 'Invalid authentication method configured in the config file, available options are: ' . join( ", ", Sendgrid_Tools::$allowed_auth_methods );
285
- $status = 'error';
286
- }
287
-
288
  if ( defined( 'SENDGRID_CONTENT_TYPE' ) ) {
289
  if ( ! in_array( SENDGRID_CONTENT_TYPE, Sendgrid_Tools::$allowed_content_type ) ) {
290
  $message = 'Invalid content type, available content types are: "plaintext" or "html".';
@@ -310,7 +295,8 @@ class Sendgrid_Settings {
310
  // get unsubscribe groups
311
  $unsubscribe_groups = Sendgrid_Tools::get_all_unsubscribe_groups();
312
  $no_permission_on_unsubscribe_groups = false;
313
- if ( ( 'apikey' == $auth_method ) and ( 'true' != Sendgrid_Tools::get_asm_permission() ) ) {
 
314
  $no_permission_on_unsubscribe_groups = true;
315
  }
316
 
@@ -339,10 +325,7 @@ class Sendgrid_Settings {
339
  }
340
  $mc_signup_subscribe_label = stripslashes( $mc_signup_subscribe_label );
341
 
342
- $is_env_auth_method = defined( 'SENDGRID_AUTH_METHOD' );
343
  $is_env_send_method = defined( 'SENDGRID_SEND_METHOD' );
344
- $is_env_username = defined( 'SENDGRID_USERNAME' );
345
- $is_env_password = defined( 'SENDGRID_PASSWORD' );
346
  $is_env_api_key = defined( 'SENDGRID_API_KEY' );
347
  $is_env_port = defined( 'SENDGRID_PORT' );
348
  $is_env_content_type = defined( 'SENDGRID_CONTENT_TYPE' );
@@ -369,7 +352,7 @@ class Sendgrid_Settings {
369
  }
370
  }
371
 
372
- if ( $auth_method == 'apikey' and $api_key != '' and ! Sendgrid_Tools::check_api_key_stats( $api_key ) ) {
373
  $warning_message = 'The configured API key does not have statistics permissions. You will not be able to see the statistics page.';
374
  $warning_status = 'notice notice-warning';
375
  }
@@ -456,8 +439,8 @@ class Sendgrid_Settings {
456
  $use_transactional_key = ( 'true' == SENDGRID_MC_OPT_USE_TRANSACTIONAL ? true : false );
457
  }
458
 
459
- // If Use Transactional Is Set and auth is not through credentials, check the API key for MC scopes.
460
- if ( $use_transactional_key and 'apikey' == Sendgrid_Tools::get_auth_method() ) {
461
  $apikey = Sendgrid_Tools::get_api_key();
462
  if ( false == $apikey or empty( $apikey ) ) {
463
  $response = array(
@@ -637,91 +620,37 @@ class Sendgrid_Settings {
637
  * @return mixed response array with message and status
638
  */
639
  private static function save_general_settings( $params ) {
640
- if ( ! isset( $params['auth_method'] ) ) {
641
- $params['auth_method'] = Sendgrid_Tools::get_auth_method();
642
- }
643
-
644
- switch ( $params['auth_method'] ) {
645
- case 'apikey':
646
- if ( ! defined( 'SENDGRID_API_KEY' ) ) {
647
- if ( ! isset( $params['sendgrid_apikey'] ) or empty( $params['sendgrid_apikey'] ) ) {
648
- $response = array(
649
- 'message' => 'API Key is empty.',
650
- 'status' => 'error'
651
- );
652
-
653
- Sendgrid_Tools::set_api_key( '' );
654
-
655
- break;
656
- }
657
-
658
- $apikey = htmlspecialchars( $params['sendgrid_apikey'], ENT_QUOTES, 'UTF-8' );
659
-
660
- if ( ! Sendgrid_Tools::check_api_key( $apikey, true ) ) {
661
- $response = array(
662
- 'message' => 'API Key is invalid or without permissions.',
663
- 'status' => 'error'
664
- );
665
-
666
- break;
667
- }
668
-
669
- if ( 'true' == Sendgrid_Tools::get_mc_opt_use_transactional() and ! Sendgrid_Tools::check_api_key_mc( $apikey ) ) {
670
- $response = array(
671
- 'message' => 'This API key is also used for the Subscription Widget but does not have Marketing Campaigns permissions.',
672
- 'status' => 'error'
673
- );
674
- }
675
-
676
- Sendgrid_Tools::set_api_key( $apikey );
677
- }
678
-
679
- break;
680
-
681
- case 'credentials':
682
- if ( ! isset( $params['sendgrid_username'] ) and ! isset( $params['sendgrid_password'] ) ) {
683
- break;
684
- }
685
 
686
- $save_username = true;
687
- $save_password = true;
688
 
689
- if ( ! isset ( $params['sendgrid_username'] ) ) {
690
- $save_username = false;
691
- $params['sendgrid_username'] = Sendgrid_Tools::get_username();
692
- }
693
-
694
- if ( ! isset ( $params['sendgrid_password'] ) ) {
695
- $save_password = false;
696
- $params['sendgrid_password'] = Sendgrid_Tools::get_username();
697
- }
698
 
699
- $username = htmlspecialchars( $params['sendgrid_username'], ENT_QUOTES, 'UTF-8' );
700
- $password = htmlspecialchars( $params['sendgrid_password'], ENT_QUOTES, 'UTF-8' );
 
 
 
701
 
702
- if ( ( isset( $params['sendgrid_username'] ) and ! $params['sendgrid_username'] ) or ( isset( $params['sendgrid_password'] ) and ! $params['sendgrid_password'] ) ) {
703
- $response = array(
704
- 'message' => 'Username or password is empty.',
705
- 'status' => 'error'
706
- );
707
- } elseif ( ! Sendgrid_Tools::check_username_password( $username, $password, true ) ) {
708
- $response = array(
709
- 'message' => 'Username and password are invalid.',
710
- 'status' => 'error'
711
- );
712
 
713
- break;
714
- }
 
 
 
715
 
716
- if ( $save_username ) {
717
- Sendgrid_Tools::set_username( $username );
718
- }
719
-
720
- if ( $save_password ) {
721
- Sendgrid_Tools::set_password( $password );
722
- }
723
 
724
- break;
725
  }
726
 
727
  if ( isset( $params['sendgrid_name'] ) ) {
@@ -782,12 +711,6 @@ class Sendgrid_Settings {
782
  Sendgrid_Tools::set_send_method( $send_method );
783
  }
784
 
785
- if ( isset( $params['auth_method'] ) and in_array( $params['auth_method'], Sendgrid_Tools::$allowed_auth_methods ) ) {
786
- // Should be rejected by allowed auth method check, but just to be extra safe
787
- $auth_method = htmlspecialchars( $params['auth_method'], ENT_QUOTES, 'UTF-8' );
788
- Sendgrid_Tools::set_auth_method( $auth_method );
789
- }
790
-
791
  if ( isset( $params['sendgrid_port'] ) ) {
792
  $port = htmlspecialchars( $params['sendgrid_port'], ENT_QUOTES, 'UTF-8' );
793
  Sendgrid_Tools::set_port( $port );
@@ -878,35 +801,14 @@ class Sendgrid_Settings {
878
  'error_type' => 'upload'
879
  );
880
  }
881
-
882
- switch ( Sendgrid_Tools::get_auth_method() ) {
883
- case 'apikey':
884
- $apikey = Sendgrid_Tools::get_api_key();
885
- if ( ! Sendgrid_Tools::check_api_key( $apikey, true ) ) {
886
- return array(
887
- 'message' => 'API Key used for mail send is invalid or without permissions.',
888
- 'status' => 'error',
889
- 'error_type' => 'upload'
890
- );
891
- }
892
- break;
893
- case 'credentials':
894
- $username = Sendgrid_Tools::get_username();
895
- $password = Sendgrid_Tools::get_password();
896
- if ( ! Sendgrid_Tools::check_username_password( $params['sendgrid_username'], $params['sendgrid_password'], true ) ) {
897
- return array(
898
- 'message' => 'Credentials used for mail send are invalid.',
899
- 'status' => 'error',
900
- 'error_type' => 'upload'
901
- );
902
- }
903
- break;
904
- default:
905
- return array(
906
- 'message' => 'An error occured when trying to check your transactional credentials. Please check that they are correct on the General Settings tab.',
907
- 'status' => 'error',
908
- 'error_type' => 'upload'
909
- );
910
  }
911
 
912
  if ( false == Sendgrid_OptIn_API_Endpoint::send_confirmation_email( $email, '', '', true ) ) {
143
  $status = '';
144
  $message = '';
145
 
 
 
146
  $api_key = stripslashes( Sendgrid_Tools::get_api_key() );
147
  $send_method = stripslashes( Sendgrid_Tools::get_send_method() );
 
148
  $name = stripslashes( Sendgrid_Tools::get_from_name() );
149
  $email = stripslashes( Sendgrid_Tools::get_from_email() );
150
  $reply_to = stripslashes( Sendgrid_Tools::get_reply_to() );
226
  }
227
 
228
  $is_mc_api_key_valid = true;
229
+ if ( 'true' == $mc_opt_use_transactional and ! empty( $api_key ) ) {
230
  if ( ! Sendgrid_Tools::check_api_key_mc( $api_key ) ) {
231
  $is_mc_api_key_valid = false;
232
  }
248
  $status = 'error';
249
  }
250
 
251
+ if ( ! empty( $api_key ) ) {
252
  if ( ! Sendgrid_Tools::check_api_key( $api_key, true ) ) {
253
  $message = 'API Key is invalid or without permissions.';
254
  $status = 'error';
258
  } elseif ( 'error' != $status ) {
259
  $status = 'valid_auth';
260
  }
 
 
 
 
 
 
 
261
  }
262
 
263
  if ( $template and ! Sendgrid_Tools::check_template( $template ) ) {
270
  $status = 'error';
271
  }
272
 
 
 
 
 
 
273
  if ( defined( 'SENDGRID_CONTENT_TYPE' ) ) {
274
  if ( ! in_array( SENDGRID_CONTENT_TYPE, Sendgrid_Tools::$allowed_content_type ) ) {
275
  $message = 'Invalid content type, available content types are: "plaintext" or "html".';
295
  // get unsubscribe groups
296
  $unsubscribe_groups = Sendgrid_Tools::get_all_unsubscribe_groups();
297
  $no_permission_on_unsubscribe_groups = false;
298
+
299
+ if ( 'true' != Sendgrid_Tools::get_asm_permission() ) {
300
  $no_permission_on_unsubscribe_groups = true;
301
  }
302
 
325
  }
326
  $mc_signup_subscribe_label = stripslashes( $mc_signup_subscribe_label );
327
 
 
328
  $is_env_send_method = defined( 'SENDGRID_SEND_METHOD' );
 
 
329
  $is_env_api_key = defined( 'SENDGRID_API_KEY' );
330
  $is_env_port = defined( 'SENDGRID_PORT' );
331
  $is_env_content_type = defined( 'SENDGRID_CONTENT_TYPE' );
352
  }
353
  }
354
 
355
+ if ( $api_key != '' and ! Sendgrid_Tools::check_api_key_stats( $api_key ) ) {
356
  $warning_message = 'The configured API key does not have statistics permissions. You will not be able to see the statistics page.';
357
  $warning_status = 'notice notice-warning';
358
  }
439
  $use_transactional_key = ( 'true' == SENDGRID_MC_OPT_USE_TRANSACTIONAL ? true : false );
440
  }
441
 
442
+ // If Use Transactional Is Set check the API key for MC scopes.
443
+ if ( $use_transactional_key ) {
444
  $apikey = Sendgrid_Tools::get_api_key();
445
  if ( false == $apikey or empty( $apikey ) ) {
446
  $response = array(
620
  * @return mixed response array with message and status
621
  */
622
  private static function save_general_settings( $params ) {
623
+ if ( ! defined( 'SENDGRID_API_KEY' ) ) {
624
+ if ( ! isset( $params['sendgrid_apikey'] ) or empty( $params['sendgrid_apikey'] ) ) {
625
+ $response = array(
626
+ 'message' => 'API Key is empty.',
627
+ 'status' => 'error'
628
+ );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
629
 
630
+ Sendgrid_Tools::set_api_key( '' );
 
631
 
632
+ return $response;
633
+ }
 
 
 
 
 
 
 
634
 
635
+ if ( ! Sendgrid_Tools::check_api_key( $params['sendgrid_apikey'], true ) ) {
636
+ $response = array(
637
+ 'message' => 'API Key is invalid or without permissions.',
638
+ 'status' => 'error'
639
+ );
640
 
641
+ return $response;
642
+ }
 
 
 
 
 
 
 
 
643
 
644
+ if ( 'true' == Sendgrid_Tools::get_mc_opt_use_transactional() and ! Sendgrid_Tools::check_api_key_mc( $params['sendgrid_apikey'] ) ) {
645
+ $response = array(
646
+ 'message' => 'This API key is also used for the Subscription Widget but does not have Marketing Campaigns permissions.',
647
+ 'status' => 'error'
648
+ );
649
 
650
+ return $response;
651
+ }
 
 
 
 
 
652
 
653
+ Sendgrid_Tools::set_api_key( $params['sendgrid_apikey'] );
654
  }
655
 
656
  if ( isset( $params['sendgrid_name'] ) ) {
711
  Sendgrid_Tools::set_send_method( $send_method );
712
  }
713
 
 
 
 
 
 
 
714
  if ( isset( $params['sendgrid_port'] ) ) {
715
  $port = htmlspecialchars( $params['sendgrid_port'], ENT_QUOTES, 'UTF-8' );
716
  Sendgrid_Tools::set_port( $port );
801
  'error_type' => 'upload'
802
  );
803
  }
804
+
805
+ $apikey = Sendgrid_Tools::get_api_key();
806
+ if ( ! Sendgrid_Tools::check_api_key( $apikey, true ) ) {
807
+ return array(
808
+ 'message' => 'API Key used for mail send is invalid or without permissions.',
809
+ 'status' => 'error',
810
+ 'error_type' => 'upload'
811
+ );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
812
  }
813
 
814
  if ( false == Sendgrid_OptIn_API_Endpoint::send_confirmation_email( $email, '', '', true ) ) {
lib/class-sendgrid-statistics.php CHANGED
@@ -41,7 +41,7 @@ class Sendgrid_Statistics
41
  }
42
 
43
  /**
44
- * Verify if SendGrid username and password provided are correct and
45
  * initialize function for add widget in dashboard
46
  *
47
  * @return void
@@ -52,20 +52,9 @@ class Sendgrid_Statistics
52
  return;
53
  }
54
 
55
- switch ( Sendgrid_Tools::get_auth_method() )
56
- {
57
- case "apikey":
58
- $apikey = Sendgrid_Tools::get_api_key();
59
- if ( ! Sendgrid_Tools::check_api_key( $apikey ) or ! Sendgrid_Tools::check_api_key_stats( $apikey ) ) {
60
- return;
61
- }
62
- break;
63
-
64
- case "credentials":
65
- if ( ! Sendgrid_Tools::check_username_password( Sendgrid_Tools::get_username(), Sendgrid_Tools::get_password() ) ) {
66
- return;
67
- }
68
- break;
69
  }
70
 
71
  add_meta_box( 'sendgrid_statistics_widget', 'SendGrid Wordpress Statistics', array( __CLASS__, 'show_dashboard_widget' ),
@@ -89,20 +78,9 @@ class Sendgrid_Statistics
89
  */
90
  public static function add_statistics_menu()
91
  {
92
- switch ( Sendgrid_Tools::get_auth_method() )
93
- {
94
- case "apikey":
95
- $apikey = Sendgrid_Tools::get_api_key();
96
- if ( ! Sendgrid_Tools::check_api_key_stats( $apikey ) ) {
97
- return;
98
- }
99
- break;
100
-
101
- case "credentials":
102
- if ( ! Sendgrid_Tools::check_username_password( Sendgrid_Tools::get_username(), Sendgrid_Tools::get_password() ) ) {
103
- return;
104
- }
105
- break;
106
  }
107
 
108
  add_dashboard_page( "SendGrid Statistics", "SendGrid Statistics", "manage_options", "sendgrid-statistics",
@@ -115,20 +93,9 @@ class Sendgrid_Statistics
115
  * @return void
116
  */
117
  public static function add_network_statistics_menu() {
118
- switch ( Sendgrid_Tools::get_auth_method() )
119
- {
120
- case "apikey":
121
- $apikey = Sendgrid_Tools::get_api_key();
122
- if ( ! Sendgrid_Tools::check_api_key_stats( $apikey ) ) {
123
- return;
124
- }
125
- break;
126
-
127
- case "credentials":
128
- if ( ! Sendgrid_Tools::check_username_password( Sendgrid_Tools::get_username(), Sendgrid_Tools::get_password() ) ) {
129
- return;
130
- }
131
- break;
132
  }
133
 
134
  add_menu_page( __( 'SendGrid Stats' ), __( 'SendGrid Stats' ), 'manage_options', 'sendgrid-statistics',
@@ -143,7 +110,7 @@ class Sendgrid_Statistics
143
  public static function show_statistics_page()
144
  {
145
  $apikey = Sendgrid_Tools::get_api_key();
146
- if ( ( "apikey" == Sendgrid_Tools::get_auth_method() ) and isset( $apikey ) and ( $apikey != '' ) and ! Sendgrid_Tools::check_api_key_stats( $apikey, true ) )
147
  {
148
  $message = 'Your Api key does not have statistics permissions';
149
  $status = 'error';
@@ -195,12 +162,7 @@ class Sendgrid_Statistics
195
  }
196
 
197
  $parameters = array();
198
-
199
- $parameters['auth_method'] = Sendgrid_Tools::get_auth_method();
200
- $parameters['api_username'] = Sendgrid_Tools::get_username();
201
- $parameters['api_password'] = Sendgrid_Tools::get_password();
202
  $parameters['apikey'] = Sendgrid_Tools::get_api_key();
203
-
204
  $parameters['data_type'] = 'global';
205
 
206
  if ( array_key_exists( 'days', $_POST ) ) {
41
  }
42
 
43
  /**
44
+ * Verify if provided SendGrid API Key is correct and
45
  * initialize function for add widget in dashboard
46
  *
47
  * @return void
52
  return;
53
  }
54
 
55
+ $apikey = Sendgrid_Tools::get_api_key();
56
+ if ( ! Sendgrid_Tools::check_api_key( $apikey ) or ! Sendgrid_Tools::check_api_key_stats( $apikey ) ) {
57
+ return;
 
 
 
 
 
 
 
 
 
 
 
58
  }
59
 
60
  add_meta_box( 'sendgrid_statistics_widget', 'SendGrid Wordpress Statistics', array( __CLASS__, 'show_dashboard_widget' ),
78
  */
79
  public static function add_statistics_menu()
80
  {
81
+ $apikey = Sendgrid_Tools::get_api_key();
82
+ if ( ! Sendgrid_Tools::check_api_key( $apikey ) ) {
83
+ return;
 
 
 
 
 
 
 
 
 
 
 
84
  }
85
 
86
  add_dashboard_page( "SendGrid Statistics", "SendGrid Statistics", "manage_options", "sendgrid-statistics",
93
  * @return void
94
  */
95
  public static function add_network_statistics_menu() {
96
+ $apikey = Sendgrid_Tools::get_api_key();
97
+ if ( ! Sendgrid_Tools::check_api_key( $apikey ) ) {
98
+ return;
 
 
 
 
 
 
 
 
 
 
 
99
  }
100
 
101
  add_menu_page( __( 'SendGrid Stats' ), __( 'SendGrid Stats' ), 'manage_options', 'sendgrid-statistics',
110
  public static function show_statistics_page()
111
  {
112
  $apikey = Sendgrid_Tools::get_api_key();
113
+ if ( isset( $apikey ) and ( $apikey != '' ) and ! Sendgrid_Tools::check_api_key_stats( $apikey, true ) )
114
  {
115
  $message = 'Your Api key does not have statistics permissions';
116
  $status = 'error';
162
  }
163
 
164
  $parameters = array();
 
 
 
 
165
  $parameters['apikey'] = Sendgrid_Tools::get_api_key();
 
166
  $parameters['data_type'] = 'global';
167
 
168
  if ( array_key_exists( 'days', $_POST ) ) {
lib/class-sendgrid-tools.php CHANGED
@@ -10,7 +10,6 @@ class Sendgrid_Tools
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, Sendgrid_SMTP::TLS_ALTERNATIVE_2 );
13
- public static $allowed_auth_methods = array( 'apikey', 'credentials' );
14
  public static $allowed_content_type = array( 'plaintext', 'html' );
15
 
16
  /**
@@ -196,12 +195,7 @@ class Sendgrid_Tools
196
 
197
  $url = 'v3/templates/' . $template;
198
 
199
- $parameters['auth_method'] = Sendgrid_Tools::get_auth_method();
200
- $parameters['api_username'] = Sendgrid_Tools::get_username();
201
- $parameters['api_password'] = Sendgrid_Tools::get_password();
202
- $parameters['apikey'] = Sendgrid_Tools::get_api_key();
203
-
204
- $response = Sendgrid_Tools::do_request( $url, $parameters );
205
 
206
  if ( ! $response ) {
207
  return false;
@@ -225,30 +219,12 @@ class Sendgrid_Tools
225
  */
226
  public static function do_request( $api = 'v3/stats', $parameters = array() )
227
  {
228
- $args = array();
229
- if ( "credentials" == $parameters['auth_method'] ) {
230
- $creds = base64_encode( $parameters['api_username'] . ':' . $parameters['api_password'] );
231
-
232
- $args = array(
233
- 'headers' => array(
234
- 'Authorization' => 'Basic ' . $creds
235
- ),
236
- 'decompress' => false
237
- );
238
-
239
- } else {
240
- $args = array(
241
- 'headers' => array(
242
- 'Authorization' => 'Bearer ' . $parameters['apikey']
243
- ),
244
- 'decompress' => false
245
- );
246
- }
247
-
248
- unset( $parameters['auth_method'] );
249
- unset( $parameters['api_username'] );
250
- unset( $parameters['api_password'] );
251
- unset( $parameters['apikey'] );
252
 
253
  $data = urldecode( http_build_query( $parameters ) );
254
  $url = "https://api.sendgrid.com/$api?$data";
@@ -262,75 +238,6 @@ class Sendgrid_Tools
262
  return $response['body'];
263
  }
264
 
265
- /**
266
- * Return username from the database or global variable
267
- *
268
- * @return mixed username, false if the value is not found
269
- */
270
- public static function get_username()
271
- {
272
- if ( defined( 'SENDGRID_USERNAME' ) ) {
273
- return SENDGRID_USERNAME;
274
- } else {
275
- $username = Sendgrid_Tools::get_sendgrid_option( 'user' );
276
- if( $username ) {
277
- Sendgrid_Tools::delete_sendgrid_option( 'user' );
278
- Sendgrid_Tools::update_sendgrid_option( 'username', $username );
279
- }
280
-
281
- return Sendgrid_Tools::get_sendgrid_option( 'username' );
282
- }
283
- }
284
-
285
- /**
286
- * Sets username in the database
287
- *
288
- * @param type string $username
289
- *
290
- * @return bool
291
- */
292
- public static function set_username( $username )
293
- {
294
- if( ! isset( $username ) ) {
295
- return Sendgrid_Tools::update_sendgrid_option( 'username', '' );
296
- }
297
-
298
- return Sendgrid_Tools::update_sendgrid_option( 'username', $username );
299
- }
300
-
301
- /**
302
- * Return password from the database or global variable
303
- *
304
- * @return mixed password, false if the value is not found
305
- */
306
- public static function get_password()
307
- {
308
- if ( defined( 'SENDGRID_PASSWORD' ) ) {
309
- return SENDGRID_PASSWORD;
310
- } else {
311
- $password = Sendgrid_Tools::get_sendgrid_option( 'pwd' );
312
- $new_password = Sendgrid_Tools::get_sendgrid_option( 'password' );
313
- if ( $new_password and ! $password ) {
314
- Sendgrid_Tools::update_sendgrid_option( 'pwd', self::decrypt( $new_password, AUTH_KEY ) );
315
- Sendgrid_Tools::delete_sendgrid_option( 'password' );
316
- }
317
-
318
- return Sendgrid_Tools::get_sendgrid_option( 'pwd' );
319
- }
320
- }
321
-
322
- /**
323
- * Sets password in the database
324
- *
325
- * @param type string $password
326
- *
327
- * @return bool
328
- */
329
- public static function set_password( $password )
330
- {
331
- return Sendgrid_Tools::update_sendgrid_option( 'pwd', $password );
332
- }
333
-
334
  /**
335
  * Return api_key from the database or global variable
336
  *
@@ -697,44 +604,6 @@ class Sendgrid_Tools
697
  return Sendgrid_Tools::update_sendgrid_option( 'api', $method );
698
  }
699
 
700
- /**
701
- * Return auth method from the database or global variable
702
- *
703
- * @return string auth_method
704
- */
705
- public static function get_auth_method()
706
- {
707
- if ( defined( 'SENDGRID_AUTH_METHOD' ) ) {
708
- return SENDGRID_AUTH_METHOD;
709
- } elseif ( Sendgrid_Tools::get_sendgrid_option( 'auth_method', false ) ) {
710
- $auth_method = Sendgrid_Tools::get_sendgrid_option( 'auth_method' );
711
- if ( 'username' == $auth_method ) {
712
- $auth_method = 'credentials';
713
- Sendgrid_Tools::update_sendgrid_option( 'auth_method', $auth_method );
714
- }
715
-
716
- return $auth_method;
717
- } elseif ( Sendgrid_Tools::get_api_key() ) {
718
- return 'apikey';
719
- } elseif ( Sendgrid_Tools::get_username() and Sendgrid_Tools::get_password() ) {
720
- return 'credentials';
721
- } else {
722
- return 'apikey';
723
- }
724
- }
725
-
726
- /**
727
- * Sets the send method in the database
728
- *
729
- * @param type string $method
730
- *
731
- * @return bool
732
- */
733
- public static function set_auth_method( $method )
734
- {
735
- return Sendgrid_Tools::update_sendgrid_option( 'auth_method', $method );
736
- }
737
-
738
  /**
739
  * Return port from the database or global variable
740
  *
@@ -1002,16 +871,11 @@ class Sendgrid_Tools
1002
  {
1003
  $url = 'v3/asm/groups';
1004
 
1005
- $parameters['auth_method'] = Sendgrid_Tools::get_auth_method();
1006
- $parameters['api_username'] = Sendgrid_Tools::get_username();
1007
- $parameters['api_password'] = Sendgrid_Tools::get_password();
1008
- $parameters['apikey'] = Sendgrid_Tools::get_api_key();
1009
-
1010
- if ( ( 'apikey' == $parameters['auth_method'] ) and ( 'true' != self::get_asm_permission() ) ) {
1011
  return false;
1012
  }
1013
 
1014
- $response = Sendgrid_Tools::do_request( $url, $parameters );
1015
 
1016
  if ( ! $response ) {
1017
  return false;
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, Sendgrid_SMTP::TLS_ALTERNATIVE_2 );
 
13
  public static $allowed_content_type = array( 'plaintext', 'html' );
14
 
15
  /**
195
 
196
  $url = 'v3/templates/' . $template;
197
 
198
+ $response = Sendgrid_Tools::do_request( $url );
 
 
 
 
 
199
 
200
  if ( ! $response ) {
201
  return false;
219
  */
220
  public static function do_request( $api = 'v3/stats', $parameters = array() )
221
  {
222
+ $args = array(
223
+ 'headers' => array(
224
+ 'Authorization' => 'Bearer ' . self::get_api_key()
225
+ ),
226
+ 'decompress' => false
227
+ );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
228
 
229
  $data = urldecode( http_build_query( $parameters ) );
230
  $url = "https://api.sendgrid.com/$api?$data";
238
  return $response['body'];
239
  }
240
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
241
  /**
242
  * Return api_key from the database or global variable
243
  *
604
  return Sendgrid_Tools::update_sendgrid_option( 'api', $method );
605
  }
606
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
607
  /**
608
  * Return port from the database or global variable
609
  *
871
  {
872
  $url = 'v3/asm/groups';
873
 
874
+ if ( 'true' != self::get_asm_permission() ) {
 
 
 
 
 
875
  return false;
876
  }
877
 
878
+ $response = Sendgrid_Tools::do_request( $url );
879
 
880
  if ( ! $response ) {
881
  return false;
lib/sendgrid/class-sendgrid-api.php CHANGED
@@ -1,11 +1,11 @@
1
  <?php
2
 
3
  require_once plugin_dir_path( __FILE__ ) . 'interfaces/class-sendgrid-interface.php';
 
4
  require_once plugin_dir_path( __FILE__ ) . '../../vendor/autoload.php';
5
 
6
  class Sendgrid_API implements Sendgrid_Send {
7
-
8
- const URL = "https://api.sendgrid.com/api/mail.send.json";
9
 
10
  private $username;
11
  private $password;
@@ -17,42 +17,39 @@ class Sendgrid_API implements Sendgrid_Send {
17
  $this->method = "apikey";
18
  $this->apikey = $password_or_apikey;
19
  } else {
20
- $this->method = "credentials";
21
  $this->username = $username;
22
  $this->password = $password_or_apikey;
23
  }
24
  }
25
 
26
  public function send(SendGrid\Email $email) {
27
- $fields = $email->toWebFormat();
28
- $headers = array();
29
-
30
- if ( "credentials" == $this->method ) {
31
- $fields['api_user'] = $this->username;
32
- $fields['api_key'] = $this->password;
33
- } else {
34
- $headers = array(
35
  'Authorization' => 'Bearer ' . $this->apikey
36
- );
37
- }
38
-
39
- $files = preg_grep( '/files/', array_keys( $fields ) );
40
- foreach($files as $k => $file) {
41
- $fields[$file] = file_get_contents( substr( $fields[$file], 1 ) );
42
- }
43
-
44
-
45
- $data = array( 'body' => $fields, 'decompress' => false );
46
- if ( count( $headers ) ) {
47
- $data['headers'] = $headers;
48
- }
49
 
 
50
  $response = wp_remote_post( self::URL, $data );
51
- if ( !is_array( $response ) or !isset( $response['body'] ) )
52
- return false;
53
 
54
- if ( "success" == json_decode( $response['body'])->message )
 
 
 
 
 
 
 
 
 
 
55
  return true;
 
56
 
57
  return false;
58
  }
1
  <?php
2
 
3
  require_once plugin_dir_path( __FILE__ ) . 'interfaces/class-sendgrid-interface.php';
4
+ require_once plugin_dir_path( __FILE__ ) . 'class-sendgrid-translator.php';
5
  require_once plugin_dir_path( __FILE__ ) . '../../vendor/autoload.php';
6
 
7
  class Sendgrid_API implements Sendgrid_Send {
8
+ const URL = "https://api.sendgrid.com/v3/mail/send";
 
9
 
10
  private $username;
11
  private $password;
17
  $this->method = "apikey";
18
  $this->apikey = $password_or_apikey;
19
  } else {
20
+ $this->method = "credentials";
21
  $this->username = $username;
22
  $this->password = $password_or_apikey;
23
  }
24
  }
25
 
26
  public function send(SendGrid\Email $email) {
27
+ $data = array(
28
+ 'headers' => array(
29
+ 'Content-Type' => 'application/json',
30
+ 'User-Agent' => 'sendgrid/wordpress;php',
 
 
 
 
31
  'Authorization' => 'Bearer ' . $this->apikey
32
+ ),
33
+ 'body' => Sendgrid_Translator::to_api_v3( $email ),
34
+ 'decompress' => false
35
+ );
 
 
 
 
 
 
 
 
 
36
 
37
+ // Send the request
38
  $response = wp_remote_post( self::URL, $data );
 
 
39
 
40
+ // Check that the response fields are set
41
+ if ( !is_array( $response ) or
42
+ !isset( $response['response'] ) or
43
+ !isset( $response['response']['code'] ) ) {
44
+ return false;
45
+ }
46
+
47
+ // Check for success code range (200-299)
48
+ $response_code = (int) $response['response']['code'];
49
+ if ( $response_code >= 200 and
50
+ $response_code < 300 ) {
51
  return true;
52
+ }
53
 
54
  return false;
55
  }
lib/sendgrid/class-sendgrid-php.php CHANGED
@@ -11,36 +11,25 @@ class Sendgrid_WP {
11
 
12
  public static function get_instance() {
13
  $send_method = Sendgrid_Tools::get_send_method();
14
- $auth_method = Sendgrid_Tools::get_auth_method();
15
 
16
  switch ( $send_method ) {
17
  case 'api':
18
- return self::api_instance( $auth_method );
19
  break;
20
 
21
  case 'smtp':
22
- return self::smtp_instance( $auth_method );
23
  break;
24
  }
25
 
26
- return self::api_instance( $auth_method );
27
  }
28
 
29
- private static function api_instance( $auth_method ) {
30
- switch ( $auth_method ) {
31
- case 'apikey':
32
- return new Sendgrid_API( "apikey", Sendgrid_Tools::get_api_key() );
33
- break;
34
-
35
- case 'credentials':
36
- return new Sendgrid_API( Sendgrid_Tools::get_username(), Sendgrid_Tools::get_password() );
37
- break;
38
- }
39
-
40
- return null;
41
  }
42
 
43
- private static function smtp_instance( $auth_method )
44
  {
45
  if ( ! class_exists('Swift') ) {
46
  self::$error = array(
@@ -51,19 +40,7 @@ class Sendgrid_WP {
51
  return null;
52
  }
53
 
54
- switch ( $auth_method ) {
55
- case 'apikey':
56
- $smtp = new Sendgrid_SMTP( "apikey", Sendgrid_Tools::get_api_key() );
57
- break;
58
-
59
- case 'credentials':
60
- $smtp = new Sendgrid_SMTP( Sendgrid_Tools::get_username(), Sendgrid_Tools::get_password() );
61
- break;
62
-
63
- default:
64
- return null;
65
- break;
66
- }
67
 
68
  if ( Sendgrid_Tools::get_port() ) {
69
  if ( in_array( Sendgrid_Tools::get_port(), Sendgrid_Tools::$allowed_ports ) ) {
11
 
12
  public static function get_instance() {
13
  $send_method = Sendgrid_Tools::get_send_method();
 
14
 
15
  switch ( $send_method ) {
16
  case 'api':
17
+ return self::api_instance();
18
  break;
19
 
20
  case 'smtp':
21
+ return self::smtp_instance();
22
  break;
23
  }
24
 
25
+ return self::api_instance();
26
  }
27
 
28
+ private static function api_instance() {
29
+ return new Sendgrid_API( 'apikey', Sendgrid_Tools::get_api_key() );
 
 
 
 
 
 
 
 
 
 
30
  }
31
 
32
+ private static function smtp_instance( )
33
  {
34
  if ( ! class_exists('Swift') ) {
35
  self::$error = array(
40
  return null;
41
  }
42
 
43
+ $smtp = new Sendgrid_SMTP( "apikey", Sendgrid_Tools::get_api_key() );
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  if ( Sendgrid_Tools::get_port() ) {
46
  if ( in_array( Sendgrid_Tools::get_port(), Sendgrid_Tools::$allowed_ports ) ) {
lib/sendgrid/class-sendgrid-translator.php ADDED
@@ -0,0 +1,1028 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ require_once plugin_dir_path( __FILE__ ) . '../../vendor/autoload.php';
4
+ require_once plugin_dir_path( __FILE__ ) . 'sendgrid-api-v3.php';
5
+
6
+ class Sendgrid_Translator {
7
+ /**
8
+ * Checks if the specified variable holds a non-empty string
9
+ *
10
+ * @param type string $input_string
11
+ *
12
+ * @return bool
13
+ */
14
+ private static function is_valid_string( $input_string ) {
15
+ if ( is_string( $input_string ) and
16
+ strlen( trim( $input_string ) ) > 0 ) {
17
+ return true;
18
+ }
19
+
20
+ return false;
21
+ }
22
+
23
+ /**
24
+ * Returns an array of filter settings for the specified filter key from the SMTPAPI header of a v2 Email
25
+ *
26
+ * @param type SendGrid\Email $email_v2
27
+ * @param type string $filter_key
28
+ * @param type array $filter_settings
29
+ * @param type string $filter_enabled
30
+ *
31
+ * @return array
32
+ */
33
+ private static function get_smtp_filter_settings(
34
+ SendGrid\Email $email_v2,
35
+ $filter_key,
36
+ $filter_settings,
37
+ $filter_enabled = 'enable'
38
+ ) {
39
+ $filter_sub_label = 'settings';
40
+ $output_array = array();
41
+
42
+ if ( ! is_array( $filter_settings ) ) {
43
+ return $output_array;
44
+ }
45
+
46
+ // Check that the SMTPAPI header filter object is not malformed
47
+ if ( ! is_array( $email_v2->smtpapi->filters ) ) {
48
+ return $output_array;
49
+ }
50
+
51
+ // Check that the filter object exists
52
+ if ( ! isset( $email_v2->smtpapi->filters[ $filter_key ] ) ) {
53
+ return $output_array;
54
+ }
55
+
56
+ // Check that 'settings' exist under filter
57
+ if ( ! isset( $email_v2->smtpapi->filters[ $filter_key ][ $filter_sub_label ] ) ) {
58
+ return $output_array;
59
+ }
60
+
61
+ // Avoid PHP warning when foreaching for settings by making sure it's an array
62
+ if ( ! is_array( $email_v2->smtpapi->filters[ $filter_key ][ $filter_sub_label ] ) ) {
63
+ return $output_array;
64
+ }
65
+
66
+ // Make sure there is an enabled flag
67
+ if ( ! isset( $email_v2->smtpapi->filters[ $filter_key ][ $filter_sub_label ][ $filter_enabled ] ) ) {
68
+ return $output_array;
69
+ }
70
+
71
+ // If it's not enabled, return empty array, no need to make the payload bigger
72
+ if ( ! $email_v2->smtpapi->filters[ $filter_key ][ $filter_sub_label ][ $filter_enabled ] ) {
73
+ return $output_array;
74
+ }
75
+
76
+ foreach ( $email_v2->smtpapi->filters[ $filter_key ][ $filter_sub_label ] as $setting_key => $setting_value ) {
77
+ if ( in_array( $setting_key, $filter_settings ) ) {
78
+ $output_array[ $setting_key ] = $setting_value;
79
+ }
80
+ }
81
+
82
+ return $output_array;
83
+ }
84
+
85
+ /**
86
+ * Sets the From address and FromName (if set) to a V3 Email from a V2 Email
87
+ * - for API V3 the From email address is mandatory and it may not include Unicode encoding
88
+ * - for API V3 the FromName is optional
89
+ *
90
+ * @param type SendGridV3\Mail $email_v3
91
+ * @param type SendGrid\Email $email_v2
92
+ *
93
+ * @return void
94
+ */
95
+ private static function set_from_v3(
96
+ SendGridV3\Mail $email_v3,
97
+ SendGrid\Email $email_v2
98
+ ) {
99
+ $from_name = null;
100
+
101
+ if ( isset( $email_v2->fromName ) and self::is_valid_string( $email_v2->fromName ) ) {
102
+ $from_name = trim( $email_v2->fromName );
103
+ }
104
+
105
+ $from = new SendGridV3\Email( $from_name, trim( $email_v2->from ) );
106
+ $email_v3->setFrom( $from );
107
+ }
108
+
109
+ /**
110
+ * Sets the Subject (if set) to a V3 Email from a V2 Email
111
+ * - for API V3 the Subject field is optional
112
+ *
113
+ * @param type SendGridV3\Mail $email_v3
114
+ * @param type SendGrid\Email $email_v2
115
+ *
116
+ * @return void
117
+ */
118
+ private static function set_subject_v3(
119
+ SendGridV3\Mail $email_v3,
120
+ SendGrid\Email $email_v2
121
+ ) {
122
+ if ( isset( $email_v2->subject ) and self::is_valid_string( $email_v2->subject ) ) {
123
+ $email_v3->setSubject( $email_v2->subject );
124
+ }
125
+ }
126
+
127
+ /**
128
+ * Sets the plaintext content (if set) to a V3 Email from a V2 Email
129
+ * - for API V3 at least one content object must be present (either plaintext or html)
130
+ *
131
+ * @param type SendGridV3\Mail $email_v3
132
+ * @param type SendGrid\Email $email_v2
133
+ *
134
+ * @return void
135
+ */
136
+ private static function set_text_content_v3(
137
+ SendGridV3\Mail $email_v3,
138
+ SendGrid\Email $email_v2
139
+ ) {
140
+ if ( isset( $email_v2->text ) and self::is_valid_string( $email_v2->text ) ) {
141
+ $text_content = new SendGridV3\Content( 'text/plain', $email_v2->text );
142
+ $email_v3->addContent($text_content);
143
+ }
144
+ }
145
+
146
+ /**
147
+ * Sets the HTML content (if set) to a V3 Email from a V2 Email
148
+ * - for API V3 at least one content object must be present (either plaintext or html)
149
+ *
150
+ * @param type SendGridV3\Mail $email_v3
151
+ * @param type SendGrid\Email $email_v2
152
+ *
153
+ * @return void
154
+ */
155
+ private static function set_html_content_v3(
156
+ SendGridV3\Mail $email_v3,
157
+ SendGrid\Email $email_v2
158
+ ) {
159
+ if ( isset( $email_v2->html ) and self::is_valid_string( $email_v2->html ) ) {
160
+ $html_content = new SendGridV3\Content( 'text/html', $email_v2->html );
161
+ $email_v3->addContent($html_content);
162
+ }
163
+ }
164
+
165
+ /**
166
+ * Sets the To addresses and ToNames (if set) to a V3 Personalization Object from a V2 Email
167
+ * - for API V3 at least one recipient (To email address) must be present
168
+ * - for API V3 the To Name is optional
169
+ * - also adds substitutions, custom args and send each at, if present for each email
170
+ *
171
+ * @param type SendGridV3\Mail $email_v3
172
+ * @param type SendGrid\Email $email_v2
173
+ *
174
+ * @return void
175
+ */
176
+ private static function set_tos_v3(
177
+ SendGridV3\Mail $email_v3,
178
+ SendGrid\Email $email_v2
179
+ ) {
180
+ if ( ! is_array( $email_v2->to ) ) {
181
+ return;
182
+ }
183
+
184
+ foreach ( $email_v2->to as $index => $address ) {
185
+ if ( ! self::is_valid_string( $address ) ) {
186
+ continue;
187
+ }
188
+
189
+ $to_name = null;
190
+ $to_address = trim( $address );
191
+
192
+ if ( isset( $email_v2->toName[ $index ] ) and
193
+ self::is_valid_string( $email_v2->toName[ $index ] ) ) {
194
+ $to_name = trim( $email_v2->toName[ $index ] );
195
+ }
196
+
197
+ $recipient = new SendGridV3\Email( $to_name, $to_address );
198
+
199
+ // Create a new personalization for this To
200
+ $personalization = new SendGridV3\Personalization();
201
+
202
+ // Add the values
203
+ $personalization->addTo( $recipient );
204
+ self::set_substitutions_v3( $index, $personalization, $email_v2 );
205
+ self::set_custom_args_v3( $index, $personalization, $email_v2 );
206
+ self::set_send_each_at_v3( $index, $personalization, $email_v2 );
207
+
208
+ // Append the personalization to the email
209
+ $email_v3->addPersonalization( $personalization );
210
+ }
211
+ }
212
+
213
+ /**
214
+ * Sets the CC addresses and CCNames (if set) to a V3 Personalization Object from a V2 Email
215
+ * - for API V3 the CC addresses are optional
216
+ * - for API V3 the CC Name is optional for all CC addresses
217
+ *
218
+ * @param type SendGridV3\Personalization $personalization
219
+ * @param type SendGrid\Email $email_v2
220
+ *
221
+ * @return void
222
+ */
223
+ private static function set_ccs_v3(
224
+ SendGridV3\Personalization $personalization,
225
+ SendGrid\Email $email_v2
226
+ ) {
227
+ if ( ! is_array( $email_v2->cc ) ) {
228
+ return;
229
+ }
230
+
231
+ foreach ( $email_v2->cc as $index => $address ) {
232
+ // Check if "cc name" is set
233
+ $cc_name = null;
234
+ if ( self::is_valid_string( $email_v2->ccName[ $index ] ) ) {
235
+ $cc_name = trim( $email_v2->ccName[ $index ] );
236
+ }
237
+
238
+ $recipient = new SendGridV3\Email( $cc_name, $address );
239
+ $personalization->addCc( $recipient );
240
+ }
241
+ }
242
+
243
+ /**
244
+ * Sets the BCC addresses and BCCNames (if set) to a V3 Personalization Object from a V2 Email
245
+ * - for API V3 the BCC addresses are optional
246
+ * - for API V3 the BCC Name is optional for all BCC addresses
247
+ *
248
+ * @param type SendGridV3\Personalization $personalization
249
+ * @param type SendGrid\Email $email_v2
250
+ *
251
+ * @return void
252
+ */
253
+ private static function set_bccs_v3(
254
+ SendGridV3\Personalization $personalization,
255
+ SendGrid\Email $email_v2
256
+ ) {
257
+ if ( ! is_array( $email_v2->bcc ) ) {
258
+ return;
259
+ }
260
+
261
+ foreach ( $email_v2->bcc as $index => $address ) {
262
+ // Check if "bcc name" is set
263
+ $bcc_name = null;
264
+ if ( self::is_valid_string( $email_v2->bccName[ $index ] ) ) {
265
+ $bcc_name = trim( $email_v2->bccName[ $index ] );
266
+ }
267
+
268
+ $recipient = new SendGridV3\Email( $bcc_name, $address );
269
+ $personalization->addBcc( $recipient );
270
+ }
271
+ }
272
+
273
+ /**
274
+ * Sets the ReplyTo address (if set) to a V3 Email from a V2 Email
275
+ * - for API V3 the ReplyTo email address is optional and it may not include Unicode encoding
276
+ *
277
+ * @param type SendGridV3\Mail $email_v3
278
+ * @param type SendGrid\Email $email_v2
279
+ *
280
+ * @return void
281
+ */
282
+ private static function set_reply_to_v3(
283
+ SendGridV3\Mail $email_v3,
284
+ SendGrid\Email $email_v2
285
+ ) {
286
+ if ( isset( $email_v2->replyTo ) and self::is_valid_string( $email_v2->replyTo ) ) {
287
+ $email_v3->setReplyTo( new SendGridV3\Email( null, trim( $email_v2->replyTo ) ) );
288
+ }
289
+ }
290
+
291
+ /**
292
+ * Sets the Headers (if set) to a V3 Email from a V2 Email
293
+ * - for API V3 the CC addresses are optional
294
+ * - for API V3 the CC Name is optional for all CC addresses
295
+ *
296
+ * @param type SendGridV3\Mail $email_v3
297
+ * @param type SendGrid\Email $email_v2
298
+ *
299
+ * @return void
300
+ */
301
+ private static function set_headers_v3(
302
+ SendGridV3\Mail $email_v3,
303
+ SendGrid\Email $email_v2
304
+ ) {
305
+ if ( ! is_array( $email_v2->headers ) ) {
306
+ return;
307
+ }
308
+
309
+ foreach ( $email_v2->headers as $header => $value ) {
310
+ $email_v3->addHeader( $header, $value );
311
+ }
312
+ }
313
+
314
+ /**
315
+ * Sets the Attachments (if set) to a V3 Email from a V2 Email
316
+ * - only attaches file if it's present at specified path and readable
317
+ * - only the content and filename fields are mandatory
318
+ * - content field must be base64 encoded
319
+ *
320
+ * @param type SendGridV3\Mail $email_v3
321
+ * @param type SendGrid\Email $email_v2
322
+ *
323
+ * @return void
324
+ */
325
+ private static function set_attachments_v3(
326
+ SendGridV3\Mail $email_v3,
327
+ SendGrid\Email $email_v2
328
+ ) {
329
+ if ( ! is_array( $email_v2->attachments ) ) {
330
+ return;
331
+ }
332
+
333
+ foreach ( $email_v2->attachments as $index => $file_info ) {
334
+ if ( ! isset( $file_info[ 'file' ] ) or ! isset( $file_info[ 'basename' ] ) ) {
335
+ continue;
336
+ }
337
+
338
+ $file_contents = file_get_contents( $file_info[ 'file' ] );
339
+
340
+ // file_get_contents retuns a bool or non-bool which evaluates to false if it fails
341
+ if ( ! $file_contents ) {
342
+ continue;
343
+ }
344
+
345
+ $file_contents = base64_encode( $file_contents );
346
+
347
+ // base64_encode returns a bool or non-bool which evaluates to false if it fails
348
+ if ( ! $file_contents ) {
349
+ continue;
350
+ }
351
+
352
+ $attachment = new SendGridV3\Attachment();
353
+ $attachment->setContent( $file_contents );
354
+ $attachment->setFilename( $file_info[ 'basename' ] );
355
+
356
+ // Set the custom filename if specified
357
+ if ( isset( $file_info[ 'custom_filename' ] ) and
358
+ self::is_valid_string( $file_info[ 'custom_filename' ] ) ) {
359
+ $attachment->setFilename( trim( $file_info[ 'custom_filename' ] ) );
360
+ }
361
+
362
+ // Set the Content ID if specified
363
+ if ( isset( $file_info[ 'cid' ] ) and
364
+ self::is_valid_string( $file_info[ 'cid' ] ) ) {
365
+ $attachment->setContentID( trim( $file_info[ 'cid' ] ) );
366
+ }
367
+
368
+ $email_v3->addAttachment( $attachment );
369
+ }
370
+ }
371
+
372
+ /**
373
+ * Sets the Substitution (if set) to a V3 Personalization from a V2 Email
374
+ *
375
+ * @param type integer $index
376
+ * @param type SendGridV3\Personalization $personalization
377
+ * @param type SendGrid\Email $email_v2
378
+ *
379
+ * @return void
380
+ */
381
+ private static function set_substitutions_v3 (
382
+ $index,
383
+ SendGridV3\Personalization $personalization,
384
+ SendGrid\Email $email_v2
385
+ ) {
386
+ if ( ! is_array( $email_v2->smtpapi->sub ) ) {
387
+ return;
388
+ }
389
+
390
+ foreach ( $email_v2->smtpapi->sub as $key => $array_values ) {
391
+ if ( isset( $array_values[ $index ] ) ) {
392
+ $personalization->addSubstitution( $key, $array_values[ $index ] );
393
+ }
394
+ }
395
+ }
396
+
397
+ /**
398
+ * Sets the Custom Args (if set) to a V3 Personalization from a V2 Email
399
+ *
400
+ * @param type integer $index
401
+ * @param type SendGridV3\Personalization $personalization
402
+ * @param type SendGrid\Email $email_v2
403
+ *
404
+ * @return void
405
+ */
406
+ private static function set_custom_args_v3 (
407
+ $index,
408
+ SendGridV3\Personalization $personalization,
409
+ SendGrid\Email $email_v2
410
+ ) {
411
+ if ( ! is_array( $email_v2->smtpapi->unique_args ) ) {
412
+ return;
413
+ }
414
+
415
+ foreach ( $email_v2->smtpapi->unique_args as $key => $array_values ) {
416
+ if ( isset( $array_values[ $index ] ) ) {
417
+ $personalization->addCustomArg( $key, $array_values[ $index ] );
418
+ }
419
+ }
420
+ }
421
+
422
+ /**
423
+ * Sets the SendAt for each XSMTPAPI To (if set) to a V3 Personalization from a V2 Email
424
+ * - for API V3 the valus of send_at is a an integer (UNIX Timestamp)
425
+ *
426
+ * @param type integer $index
427
+ * @param type SendGridV3\Personalization $personalization
428
+ * @param type SendGrid\Email $email_v2
429
+ *
430
+ * @return void
431
+ */
432
+ private static function set_send_each_at_v3 (
433
+ $index,
434
+ SendGridV3\Personalization $personalization,
435
+ SendGrid\Email $email_v2
436
+ ) {
437
+ if ( ! is_array( $email_v2->smtpapi->send_each_at ) ) {
438
+ return;
439
+ }
440
+
441
+ if ( isset( $email_v2->smtpapi->send_each_at[ $index ] ) ) {
442
+
443
+ if( is_string( $email_v2->smtpapi->send_each_at[ $index ] ) ) {
444
+ $personalization->setSendAt( intval( trim( $email_v2->smtpapi->send_each_at[ $index ] ) ) );
445
+ } else {
446
+ $personalization->setSendAt( $email_v2->smtpapi->send_each_at[ $index ] );
447
+ }
448
+ }
449
+ }
450
+
451
+ /**
452
+ * Sets the SMTPAPI To addresses and ToNames (if set) to a V3 Personalization Object from a V2 Email
453
+ * - for API V3 at least one recipient (To email address) must be present
454
+ * - for API V3 the To Name is optional
455
+ * - SMTPAPI headers have the ToNames in <> brackets, they need to be extracted
456
+ * - will also set substitution per email
457
+ * - each SMTPAPI to will have it's own personalization
458
+ *
459
+ * @param type SendGridV3\Mail $email_v3
460
+ * @param type SendGrid\Email $email_v2
461
+ *
462
+ * @return void
463
+ */
464
+ private static function set_smtpapi_tos_v3 (
465
+ SendGridV3\Mail $email_v3,
466
+ SendGrid\Email $email_v2
467
+ ) {
468
+ if ( ! is_array( $email_v2->smtpapi->to ) ) {
469
+ return;
470
+ }
471
+
472
+ foreach ( $email_v2->smtpapi->to as $index => $address ) {
473
+ if ( ! self::is_valid_string( $address ) ) {
474
+ continue;
475
+ }
476
+
477
+ $to_name = null;
478
+ $to_address = trim( $address );
479
+
480
+ // If there is a ToName
481
+ if ( strstr( $address, '<' ) ) {
482
+ // Match for any string followed by any string between <> brackets
483
+ preg_match( '/(.*?)<([^>]+)>/', $address, $output_array );
484
+
485
+ // 3nd Grouping (position 2 in array) will be the email address
486
+ if ( isset( $output_array[ 2 ] ) ) {
487
+ $to_address = trim( $output_array[ 2 ] );
488
+ }
489
+
490
+ // 2rd Grouping (position 1 in array) will be the ToName
491
+ if ( isset( $output_array[ 1 ] ) ) {
492
+ $to_name = trim( $output_array[ 1 ] );
493
+ }
494
+ }
495
+
496
+ // If no <> brackets are found, there should only be one email address
497
+ $recipient = new SendGridV3\Email( $to_name, $to_address );
498
+
499
+ // Create a new personalization for this To
500
+ $personalization = new SendGridV3\Personalization();
501
+
502
+ // Add the SMTPAPI Values
503
+ $personalization->addTo( $recipient );
504
+ self::set_substitutions_v3( $index, $personalization, $email_v2 );
505
+ self::set_custom_args_v3( $index, $personalization, $email_v2 );
506
+ self::set_send_each_at_v3( $index, $personalization, $email_v2 );
507
+
508
+ // Append the personalization to the email
509
+ $email_v3->addPersonalization( $personalization );
510
+ }
511
+ }
512
+
513
+ /**
514
+ * Sets the Categories (if set) to a V3 Email from a V2 Email
515
+ * - for API V3 the Categories are optional
516
+ * - for API V3 each category must not exceed 255 characters
517
+ * - for API V3 you can have no more than 10 categories per request
518
+ *
519
+ * @param type SendGridV3\Mail $email_v3
520
+ * @param type SendGrid\Email $email_v2
521
+ *
522
+ * @return void
523
+ */
524
+ private static function set_categories_v3(
525
+ SendGridV3\Mail $email_v3,
526
+ SendGrid\Email $email_v2
527
+ ) {
528
+ if ( ! is_array( $email_v2->smtpapi->category ) ) {
529
+ return;
530
+ }
531
+
532
+ foreach ( $email_v2->smtpapi->category as $index => $category ) {
533
+ $email_v3->addCategory( trim( $category ) );
534
+ }
535
+ }
536
+
537
+ /**
538
+ * Sets the Sections (if set) to a V3 Email from a V2 Email
539
+ * - for API V3 the Sections are optional
540
+ *
541
+ * @param type SendGridV3\Mail $email_v3
542
+ * @param type SendGrid\Email $email_v2
543
+ *
544
+ * @return void
545
+ */
546
+ private static function set_sections_v3(
547
+ SendGridV3\Mail $email_v3,
548
+ SendGrid\Email $email_v2
549
+ ) {
550
+ if ( ! is_array( $email_v2->smtpapi->section ) ) {
551
+ return;
552
+ }
553
+
554
+ foreach ( $email_v2->smtpapi->section as $key => $section ) {
555
+ $email_v3->addSection( $key, $section );
556
+ }
557
+ }
558
+
559
+ /**
560
+ * Sets the SendAt (if set) to a V3 Email from a V2 Email
561
+ * - for API V3 send_at is an integer and is optional
562
+ *
563
+ * @param type SendGridV3\Mail $email_v3
564
+ * @param type SendGrid\Email $email_v2
565
+ *
566
+ * @return void
567
+ */
568
+ private static function set_send_at_v3(
569
+ SendGridV3\Mail $email_v3,
570
+ SendGrid\Email $email_v2
571
+ ) {
572
+ if ( ! $email_v2->smtpapi->send_at ) {
573
+ return;
574
+ }
575
+
576
+ if ( is_string( $email_v2->smtpapi->send_at ) ) {
577
+ $email_v3->setSendAt( intval( trim( $email_v2->smtpapi->send_at ) ) );
578
+ } else {
579
+ $email_v3->setSendAt( $email_v2->smtpapi->send_at );
580
+ }
581
+ }
582
+
583
+ /**
584
+ * Sets the ASM Group ID (if set) to a V3 Email from a V2 Email
585
+ * - for API V3 the ASM setting is an object and is optional
586
+ * - for API V3 the ASM group_id is mandatory for each object
587
+ *
588
+ * @param type SendGridV3\Mail $email_v3
589
+ * @param type SendGrid\Email $email_v2
590
+ *
591
+ * @return void
592
+ */
593
+ private static function set_asm_group_id_v3(
594
+ SendGridV3\Mail $email_v3,
595
+ SendGrid\Email $email_v2
596
+ ) {
597
+ if ( ! $email_v2->smtpapi->asm_group_id ) {
598
+ return;
599
+ }
600
+
601
+ $asm = new SendGridV3\ASM();
602
+ if ( is_string( $email_v2->smtpapi->asm_group_id ) ) {
603
+ $asm->setGroupId( intval( trim( $email_v2->smtpapi->asm_group_id ) ) );
604
+ } else {
605
+ $asm->setGroupId( $email_v2->smtpapi->asm_group_id );
606
+ }
607
+
608
+ $email_v3->setASM( $asm );
609
+ }
610
+
611
+ /**
612
+ * Sets the IP Pool Name (if set) to a V3 Email from a V2 Email
613
+ * - for API V3 the IP Pool Name is a string and is optional
614
+ * - for API V3 the IP Pool Name must be between 2 and 64 characters in length
615
+ *
616
+ * @param type SendGridV3\Mail $email_v3
617
+ * @param type SendGrid\Email $email_v2
618
+ *
619
+ * @return void
620
+ */
621
+ private static function set_ip_pool_v3(
622
+ SendGridV3\Mail $email_v3,
623
+ SendGrid\Email $email_v2
624
+ ) {
625
+ if ( ! is_string( $email_v2->smtpapi->ipPool ) ) {
626
+ return;
627
+ }
628
+
629
+ $email_v3->setIpPoolName( $email_v2->smtpapi->ipPool );
630
+ }
631
+
632
+ /**
633
+ * Sets the Template ID (if set) to a V3 Email from a V2 Email
634
+ * - for API V3 the Template ID is a string and is optional
635
+ *
636
+ * @param type SendGridV3\Mail $email_v3
637
+ * @param type SendGrid\Email $email_v2
638
+ *
639
+ * @return void
640
+ */
641
+ private static function set_template_id_v3(
642
+ SendGridV3\Mail $email_v3,
643
+ SendGrid\Email $email_v2
644
+ ) {
645
+ $filter_key = 'templates';
646
+ $filter_settings = array( 'template_id' );
647
+
648
+ $settings = self::get_smtp_filter_settings( $email_v2, $filter_key, $filter_settings );
649
+
650
+ // Fix for wrong label from V2 library
651
+ if ( ! count( $settings ) ) {
652
+ $settings = self::get_smtp_filter_settings( $email_v2, $filter_key, $filter_settings, 'enabled' );
653
+ }
654
+
655
+ if ( isset( $settings[ 'template_id' ] ) ) {
656
+ $email_v3->setTemplateId( $settings[ 'template_id' ] );
657
+ }
658
+ }
659
+
660
+ /**
661
+ * Sets the BCC Mail Setting (if set) to a V3 Email from a V2 Email
662
+ * - for API V3 the BCC Mail Setting is an object and is optional
663
+ *
664
+ * @param type SendGridV3\Mail $email_v3
665
+ * @param type SendGrid\Email $email_v2
666
+ *
667
+ * @return void
668
+ */
669
+ private static function set_bcc_setting_v3(
670
+ SendGridV3\Mail $email_v3,
671
+ SendGrid\Email $email_v2
672
+ ) {
673
+ $filter_key = 'bcc';
674
+ $filter_settings = array( 'email' );
675
+
676
+ $settings = self::get_smtp_filter_settings( $email_v2, $filter_key, $filter_settings );
677
+
678
+ if ( isset( $settings[ 'email' ] ) ) {
679
+ $bcc_settings = new SendGridV3\BccSettings();
680
+ $bcc_settings->setEnable( true );
681
+ $bcc_settings->setEmail( $settings[ 'email' ] );
682
+
683
+ if ( ! isset( $email_v3->mail_settings ) ) {
684
+ $mail_settings = new SendGridV3\MailSettings();
685
+ $email_v3->setMailSettings( $mail_settings );
686
+ }
687
+
688
+ $email_v3->getMailSettings()->setBccSettings( $bcc_settings );
689
+ }
690
+ }
691
+
692
+ /**
693
+ * Sets the Bypass List Management Mail Setting (if set) to a V3 Email from a V2 Email
694
+ * - for API V3 the Bypass List Management is an object and is optional
695
+ *
696
+ * @param type SendGridV3\Mail $email_v3
697
+ * @param type SendGrid\Email $email_v2
698
+ *
699
+ * @return void
700
+ */
701
+ private static function set_bypass_management_v3(
702
+ SendGridV3\Mail $email_v3,
703
+ SendGrid\Email $email_v2
704
+ ) {
705
+ $filter_key = 'bypass_list_management';
706
+ $filter_settings = array( 'enable' );
707
+
708
+ $settings = self::get_smtp_filter_settings( $email_v2, $filter_key, $filter_settings );
709
+
710
+ if ( isset( $settings[ 'enable' ] ) ) {
711
+ $bypass_settings = new SendGridV3\BypassListManagement();
712
+ $bypass_settings->setEnable( true );
713
+
714
+ if ( ! isset( $email_v3->mail_settings ) ) {
715
+ $mail_settings = new SendGridV3\MailSettings();
716
+ $email_v3->setMailSettings( $mail_settings );
717
+ }
718
+
719
+ $email_v3->getMailSettings()->setBypassListManagement( $bypass_settings );
720
+ }
721
+ }
722
+
723
+ /**
724
+ * Sets the Spam Check Mail Setting (if set) to a V3 Email from a V2 Email
725
+ * - for API V3 the Spam Check is an object and is optional
726
+ *
727
+ * @param type SendGridV3\Mail $email_v3
728
+ * @param type SendGrid\Email $email_v2
729
+ *
730
+ * @return void
731
+ */
732
+ private static function set_spam_check_v3(
733
+ SendGridV3\Mail $email_v3,
734
+ SendGrid\Email $email_v2
735
+ ) {
736
+ $filter_key = 'spamcheck';
737
+ $filter_settings = array( 'maxscore', 'url' );
738
+
739
+ $settings = self::get_smtp_filter_settings( $email_v2, $filter_key, $filter_settings );
740
+
741
+ if ( isset( $settings[ 'maxscore' ] ) or isset( $settings[ 'url' ] ) ) {
742
+ $spamcheck_settings = new SendGridV3\SpamCheck();
743
+ $spamcheck_settings->setEnable( true );
744
+
745
+ if ( isset( $settings[ 'maxscore' ] ) ) {
746
+ $spamcheck_settings->setThreshold( $settings[ 'maxscore' ] );
747
+ }
748
+
749
+ if ( isset( $settings[ 'url' ] ) ) {
750
+ $spamcheck_settings->setPostToUrl( $settings[ 'url' ] );
751
+ }
752
+
753
+ if ( ! isset( $email_v3->mail_settings ) ) {
754
+ $mail_settings = new SendGridV3\MailSettings();
755
+ $email_v3->setMailSettings( $mail_settings );
756
+ }
757
+
758
+ $email_v3->getMailSettings()->setSpamCheck( $spamcheck_settings );
759
+ }
760
+ }
761
+
762
+ /**
763
+ * Sets the Email Footer Setting (if set) to a V3 Email from a V2 Email
764
+ * - for API V3 the Email Footer Setting is an object and is optional
765
+ *
766
+ * @param type SendGridV3\Mail $email_v3
767
+ * @param type SendGrid\Email $email_v2
768
+ *
769
+ * @return void
770
+ */
771
+ private static function set_email_footer_v3(
772
+ SendGridV3\Mail $email_v3,
773
+ SendGrid\Email $email_v2
774
+ ) {
775
+ $filter_key = 'footer';
776
+ $filter_settings = array( 'text/html', 'text/plain' );
777
+
778
+ $settings = self::get_smtp_filter_settings( $email_v2, $filter_key, $filter_settings );
779
+
780
+ if ( isset( $settings[ 'text/html' ] ) or isset( $settings[ 'text/plain' ] ) ) {
781
+ $footer_settings = new SendGridV3\Footer();
782
+ $footer_settings->setEnable( true );
783
+
784
+ if ( isset( $settings[ 'text/html' ] ) ) {
785
+ $footer_settings->setHtml( $settings[ 'text/html' ] );
786
+ }
787
+
788
+ if ( isset( $settings[ 'text/plain' ] ) ) {
789
+ $footer_settings->setText( $settings[ 'text/plain' ] );
790
+ }
791
+
792
+ if ( ! isset( $email_v3->mail_settings ) ) {
793
+ $mail_settings = new SendGridV3\MailSettings();
794
+ $email_v3->setMailSettings( $mail_settings );
795
+ }
796
+
797
+ $email_v3->getMailSettings()->setFooter( $footer_settings );
798
+ }
799
+ }
800
+
801
+ /**
802
+ * Sets the Click Tracking Setting (if set) to a V3 Email from a V2 Email
803
+ * - for API V3 the Click Tracking Setting is an object and is optional
804
+ *
805
+ * @param type SendGridV3\Mail $email_v3
806
+ * @param type SendGrid\Email $email_v2
807
+ *
808
+ * @return void
809
+ */
810
+ private static function set_click_tracking_v3(
811
+ SendGridV3\Mail $email_v3,
812
+ SendGrid\Email $email_v2
813
+ ) {
814
+ $filter_key = 'clicktrack';
815
+ $filter_settings = array( 'enable' );
816
+
817
+ $settings = self::get_smtp_filter_settings( $email_v2, $filter_key, $filter_settings );
818
+
819
+ if ( isset( $settings[ 'enable' ] ) ) {
820
+ $click_tracking_settings = new SendGridV3\ClickTracking();
821
+ $click_tracking_settings->setEnable( true );
822
+ $click_tracking_settings->setEnableText( true );
823
+
824
+ if ( ! isset( $email_v3->tracking_settings ) ) {
825
+ $tracking_setings = new SendGridV3\TrackingSettings();
826
+ $email_v3->setTrackingSettings( $tracking_setings );
827
+ }
828
+
829
+ $email_v3->getTrackingSettings()->setClickTracking( $click_tracking_settings );
830
+ }
831
+ }
832
+
833
+ /**
834
+ * Sets the Open Tracking Setting (if set) to a V3 Email from a V2 Email
835
+ * - for API V3 the Open Tracking Setting is an object and is optional
836
+ *
837
+ * @param type SendGridV3\Mail $email_v3
838
+ * @param type SendGrid\Email $email_v2
839
+ *
840
+ * @return void
841
+ */
842
+ private static function set_open_tracking_v3(
843
+ SendGridV3\Mail $email_v3,
844
+ SendGrid\Email $email_v2
845
+ ) {
846
+ $filter_key = 'opentrack';
847
+ $filter_settings = array( 'enable', 'replace' );
848
+
849
+ $settings = self::get_smtp_filter_settings( $email_v2, $filter_key, $filter_settings );
850
+
851
+ if ( isset( $settings[ 'enable' ] ) and isset( $settings[ 'replace' ] ) ) {
852
+ $open_tracking_settings = new SendGridV3\OpenTracking();
853
+ if ( $settings[ 'enable' ] ) {
854
+ $open_tracking_settings->setEnable( true );
855
+ }
856
+
857
+ $open_tracking_settings->setSubstitutionTag( $settings[ 'replace' ] );
858
+
859
+ if ( ! isset( $email_v3->tracking_settings ) ) {
860
+ $tracking_setings = new SendGridV3\TrackingSettings();
861
+ $email_v3->setTrackingSettings( $tracking_setings );
862
+ }
863
+
864
+ $email_v3->getTrackingSettings()->setOpenTracking( $open_tracking_settings );
865
+ }
866
+ }
867
+
868
+ /**
869
+ * Sets the Subscription Tracking Setting (if set) to a V3 Email from a V2 Email
870
+ * - for API V3 the Subscription Tracking Setting is an object and is optional
871
+ *
872
+ * @param type SendGridV3\Mail $email_v3
873
+ * @param type SendGrid\Email $email_v2
874
+ *
875
+ * @return void
876
+ */
877
+ private static function set_subscription_tracking_v3(
878
+ SendGridV3\Mail $email_v3,
879
+ SendGrid\Email $email_v2
880
+ ) {
881
+ $filter_key = 'subscriptiontrack';
882
+ $filter_settings = array( 'enable', 'replace', 'text/html', 'text/plain' );
883
+
884
+ $settings = self::get_smtp_filter_settings( $email_v2, $filter_key, $filter_settings );
885
+
886
+ if ( isset( $settings[ 'enable' ] ) ) {
887
+ $subscription_tracking_settings = new SendGridV3\SubscriptionTracking();
888
+
889
+ if ( $settings[ 'enable' ] ) {
890
+ $subscription_tracking_settings->setEnable( true );
891
+ }
892
+
893
+ if( isset( $settings[ 'replace' ] ) ) {
894
+ $subscription_tracking_settings->setSubstitutionTag( $settings[ 'replace' ] );
895
+ }
896
+
897
+ if( isset( $settings[ 'text/html' ] ) ) {
898
+ $subscription_tracking_settings->setHtml( $settings[ 'text/html' ] );
899
+ }
900
+
901
+ if( isset( $settings[ 'text/plain' ] ) ) {
902
+ $subscription_tracking_settings->setText( $settings[ 'text/plain' ] );
903
+ }
904
+
905
+ if ( ! isset( $email_v3->tracking_settings ) ) {
906
+ $tracking_setings = new SendGridV3\TrackingSettings();
907
+ $email_v3->setTrackingSettings( $tracking_setings );
908
+ }
909
+
910
+ $email_v3->getTrackingSettings()->setSubscriptionTracking( $subscription_tracking_settings );
911
+ }
912
+ }
913
+
914
+ /**
915
+ * Sets the Google Analytics Tracking Setting (if set) to a V3 Email from a V2 Email
916
+ * - for API V3 the Google Analytics Tracking Setting is an object and is optional
917
+ *
918
+ * @param type SendGridV3\Mail $email_v3
919
+ * @param type SendGrid\Email $email_v2
920
+ *
921
+ * @return void
922
+ */
923
+ private static function set_ganalytics_v3(
924
+ SendGridV3\Mail $email_v3,
925
+ SendGrid\Email $email_v2
926
+ ) {
927
+ $filter_key = 'ganalytics';
928
+ $filter_settings = array( 'enable', 'utm_source', 'utm_medium', 'utm_term', 'utm_content', 'utm_campaign' );
929
+
930
+ $settings = self::get_smtp_filter_settings( $email_v2, $filter_key, $filter_settings );
931
+
932
+ if ( isset( $settings[ 'enable' ] ) ) {
933
+ $ganalytics_tracking_settings = new SendGridV3\Ganalytics();
934
+
935
+ if ( $settings[ 'enable' ] ) {
936
+ $ganalytics_tracking_settings->setEnable( true );
937
+ }
938
+
939
+ if( isset( $settings[ 'utm_source' ] ) ) {
940
+ $ganalytics_tracking_settings->setCampaignSource( $settings[ 'utm_source' ] );
941
+ }
942
+
943
+ if( isset( $settings[ 'utm_medium' ] ) ) {
944
+ $ganalytics_tracking_settings->setCampaignMedium( $settings[ 'utm_medium' ] );
945
+ }
946
+
947
+ if( isset( $settings[ 'utm_term' ] ) ) {
948
+ $ganalytics_tracking_settings->setCampaignTerm( $settings[ 'utm_term' ] );
949
+ }
950
+
951
+ if( isset( $settings[ 'utm_content' ] ) ) {
952
+ $ganalytics_tracking_settings->setCampaignContent( $settings[ 'utm_content' ] );
953
+ }
954
+
955
+ if( isset( $settings[ 'utm_campaign' ] ) ) {
956
+ $ganalytics_tracking_settings->setCampaignName( $settings[ 'utm_campaign' ] );
957
+ }
958
+
959
+ if ( ! isset( $email_v3->tracking_settings ) ) {
960
+ $tracking_setings = new SendGridV3\TrackingSettings();
961
+ $email_v3->setTrackingSettings( $tracking_setings );
962
+ }
963
+
964
+ $email_v3->getTrackingSettings()->setGanalytics( $ganalytics_tracking_settings );
965
+ }
966
+ }
967
+
968
+ /**
969
+ * Returns a JSON encoded object for an API V3 mail send request,
970
+ * from a V2 SendGrid Email object (v2 library).
971
+ *
972
+ * @param type SendGrid\Email $email_v2
973
+ *
974
+ * @return string
975
+ */
976
+ public static function to_api_v3( SendGrid\Email $email_v2 ) {
977
+ // Initialization
978
+ $email_v3 = new SendGridV3\Mail();
979
+
980
+ // Standard fields transformation
981
+ self::set_from_v3( $email_v3, $email_v2 );
982
+ self::set_subject_v3( $email_v3, $email_v2 );
983
+ self::set_text_content_v3( $email_v3, $email_v2 );
984
+ self::set_html_content_v3( $email_v3, $email_v2 );
985
+ self::set_reply_to_v3( $email_v3, $email_v2 );
986
+ self::set_headers_v3( $email_v3, $email_v2 );
987
+ self::set_attachments_v3( $email_v3, $email_v2 );
988
+
989
+ // XSMTPAPI Standard transformations
990
+ self::set_categories_v3( $email_v3, $email_v2 );
991
+ self::set_sections_v3( $email_v3, $email_v2 );
992
+ self::set_send_at_v3( $email_v3, $email_v2 );
993
+ self::set_asm_group_id_v3( $email_v3, $email_v2 );
994
+ self::set_ip_pool_v3( $email_v3, $email_v2 );
995
+
996
+ // Mail Settings
997
+ self::set_template_id_v3( $email_v3, $email_v2 );
998
+ self::set_bcc_setting_v3( $email_v3, $email_v2 );
999
+ self::set_bypass_management_v3( $email_v3, $email_v2 );
1000
+ self::set_spam_check_v3( $email_v3, $email_v2 );
1001
+ self::set_email_footer_v3( $email_v3, $email_v2 );
1002
+
1003
+ // Tracking settings
1004
+ self::set_click_tracking_v3( $email_v3, $email_v2 );
1005
+ self::set_open_tracking_v3( $email_v3, $email_v2 );
1006
+ self::set_subscription_tracking_v3( $email_v3, $email_v2 );
1007
+ self::set_ganalytics_v3( $email_v3, $email_v2 );
1008
+
1009
+ // Exclusive Tos
1010
+ self::set_smtpapi_tos_v3( $email_v3, $email_v2 );
1011
+
1012
+ // Personalization transformation
1013
+ if ( ! is_array( $email_v3->personalization ) or
1014
+ count( $email_v3->personalization ) == 0 ) {
1015
+ self::set_tos_v3( $email_v3, $email_v2 );
1016
+ }
1017
+
1018
+ // Set the CCs and BCCs to the first To
1019
+ if ( is_array( $email_v3->personalization ) and
1020
+ isset( $email_v3->personalization[0] ) ) {
1021
+ self::set_ccs_v3( $email_v3->personalization[0], $email_v2 );
1022
+ self::set_bccs_v3( $email_v3->personalization[0], $email_v2 );
1023
+ }
1024
+
1025
+ // Return API v3 formatted JSON
1026
+ return json_encode( $email_v3 );
1027
+ }
1028
+ }
lib/sendgrid/sendgrid-api-v3.php ADDED
@@ -0,0 +1,1124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * This helper builds the request body for a /mail/send API call.
4
+ *
5
+ * PHP version 5.3
6
+ *
7
+ * @author Elmer Thomas <dx@sendgrid.com>
8
+ * @copyright 2016 SendGrid
9
+ * @license https://opensource.org/licenses/MIT The MIT License
10
+ * @link http://packagist.org/packages/sendgrid/sendgrid
11
+ */
12
+ namespace SendGridV3;
13
+
14
+ class ReplyTo implements \JsonSerializable
15
+ {
16
+ private
17
+ $email;
18
+
19
+ public function __construct($email)
20
+ {
21
+ $this->email = $email;
22
+ }
23
+
24
+ public function setEmail($email)
25
+ {
26
+ $this->email = $email;
27
+ }
28
+
29
+ public function getEmail()
30
+ {
31
+ return $this->email;
32
+ }
33
+
34
+ public function jsonSerialize()
35
+ {
36
+ return array_filter(
37
+ [
38
+ 'email' => $this->getEmail()
39
+ ]
40
+ );
41
+ }
42
+ }
43
+
44
+ class ClickTracking implements \JsonSerializable
45
+ {
46
+ private
47
+ $enable,
48
+ $enable_text;
49
+
50
+ public function setEnable($enable)
51
+ {
52
+ $this->enable = $enable;
53
+ }
54
+
55
+ public function getEnable()
56
+ {
57
+ return $this->enable;
58
+ }
59
+
60
+ public function setEnableText($enable_text)
61
+ {
62
+ $this->enable_text = $enable_text;
63
+ }
64
+
65
+ public function getEnableText()
66
+ {
67
+ return $this->enable_text;
68
+ }
69
+
70
+ public function jsonSerialize()
71
+ {
72
+ return array_filter(
73
+ [
74
+ 'enable' => $this->getEnable(),
75
+ 'enable_text' => $this->getEnableText()
76
+ ]
77
+ );
78
+ }
79
+ }
80
+
81
+ class OpenTracking implements \JsonSerializable
82
+ {
83
+ private
84
+ $enable,
85
+ $substitution_tag;
86
+
87
+ public function setEnable($enable)
88
+ {
89
+ $this->enable = $enable;
90
+ }
91
+
92
+ public function getEnable()
93
+ {
94
+ return $this->enable;
95
+ }
96
+
97
+ public function setSubstitutionTag($substitution_tag)
98
+ {
99
+ $this->substitution_tag = $substitution_tag;
100
+ }
101
+
102
+ public function getSubstitutionTag()
103
+ {
104
+ return $this->substitution_tag;
105
+ }
106
+
107
+ public function jsonSerialize()
108
+ {
109
+ return array_filter(
110
+ [
111
+ 'enable' => $this->getEnable(),
112
+ 'substitution_tag' => $this->getSubstitutionTag()
113
+ ]
114
+ );
115
+ }
116
+ }
117
+
118
+ class SubscriptionTracking implements \JsonSerializable
119
+ {
120
+ private
121
+ $enable,
122
+ $text,
123
+ $html,
124
+ $substitution_tag;
125
+
126
+ public function setEnable($enable)
127
+ {
128
+ $this->enable = $enable;
129
+ }
130
+
131
+ public function getEnable()
132
+ {
133
+ return $this->enable;
134
+ }
135
+
136
+ public function setText($text)
137
+ {
138
+ $this->text = $text;
139
+ }
140
+
141
+ public function getText()
142
+ {
143
+ return $this->text;
144
+ }
145
+
146
+ public function setHtml($html)
147
+ {
148
+ $this->html = $html;
149
+ }
150
+
151
+ public function getHtml()
152
+ {
153
+ return $this->html;
154
+ }
155
+
156
+ public function setSubstitutionTag($substitution_tag)
157
+ {
158
+ $this->substitution_tag = $substitution_tag;
159
+ }
160
+
161
+ public function getSubstitutionTag()
162
+ {
163
+ return $this->substitution_tag;
164
+ }
165
+
166
+ public function jsonSerialize()
167
+ {
168
+ return array_filter(
169
+ [
170
+ 'enable' => $this->getEnable(),
171
+ 'text' => $this->getText(),
172
+ 'html' => $this->getHtml(),
173
+ 'substitution_tag' => $this->getSubstitutionTag()
174
+ ]
175
+ );
176
+ }
177
+ }
178
+
179
+ class Ganalytics implements \JsonSerializable
180
+ {
181
+ private
182
+ $enable,
183
+ $utm_source,
184
+ $utm_medium,
185
+ $utm_term,
186
+ $utm_content,
187
+ $utm_campaign;
188
+
189
+ public function setEnable($enable)
190
+ {
191
+ $this->enable = $enable;
192
+ }
193
+
194
+ public function getEnable()
195
+ {
196
+ return $this->enable;
197
+ }
198
+
199
+ public function setCampaignSource($utm_source)
200
+ {
201
+ $this->utm_source = $utm_source;
202
+ }
203
+
204
+ public function getCampaignSource()
205
+ {
206
+ return $this->utm_source;
207
+ }
208
+
209
+ public function setCampaignMedium($utm_medium)
210
+ {
211
+ $this->utm_medium = $utm_medium;
212
+ }
213
+
214
+ public function getCampaignMedium()
215
+ {
216
+ return $this->utm_medium;
217
+ }
218
+
219
+ public function setCampaignTerm($utm_term)
220
+ {
221
+ $this->utm_term = $utm_term;
222
+ }
223
+
224
+ public function getCampaignTerm()
225
+ {
226
+ return $this->utm_term;
227
+ }
228
+
229
+ public function setCampaignContent($utm_content)
230
+ {
231
+ $this->utm_content = $utm_content;
232
+ }
233
+
234
+ public function getCampaignContent()
235
+ {
236
+ return $this->utm_content;
237
+ }
238
+
239
+ public function setCampaignName($utm_campaign)
240
+ {
241
+ $this->utm_campaign = $utm_campaign;
242
+ }
243
+
244
+ public function getCampaignName()
245
+ {
246
+ return $this->utm_campaign;
247
+ }
248
+
249
+ public function jsonSerialize()
250
+ {
251
+ return array_filter(
252
+ [
253
+ 'enable' => $this->getEnable(),
254
+ 'utm_source' => $this->getCampaignSource(),
255
+ 'utm_medium' => $this->getCampaignMedium(),
256
+ 'utm_term' => $this->getCampaignTerm(),
257
+ 'utm_content' => $this->getCampaignContent(),
258
+ 'utm_campaign' => $this->getCampaignName()
259
+ ]
260
+ );
261
+ }
262
+ }
263
+
264
+ class TrackingSettings implements \JsonSerializable
265
+ {
266
+ private
267
+ $click_tracking,
268
+ $open_tracking,
269
+ $subscription_tracking,
270
+ $ganalytics;
271
+
272
+ public function setClickTracking($click_tracking)
273
+ {
274
+ $this->click_tracking = $click_tracking;
275
+ }
276
+
277
+ public function getClickTracking()
278
+ {
279
+ return $this->click_tracking;
280
+ }
281
+
282
+ public function setOpenTracking($open_tracking)
283
+ {
284
+ $this->open_tracking = $open_tracking;
285
+ }
286
+
287
+ public function getOpenTracking()
288
+ {
289
+ return $this->open_tracking;
290
+ }
291
+
292
+ public function setSubscriptionTracking($subscription_tracking)
293
+ {
294
+ $this->subscription_tracking = $subscription_tracking;
295
+ }
296
+
297
+ public function getSubscriptionTracking()
298
+ {
299
+ return $this->subscription_tracking;
300
+ }
301
+
302
+ public function setGanalytics($ganalytics)
303
+ {
304
+ $this->ganalytics = $ganalytics;
305
+ }
306
+
307
+ public function getGanalytics()
308
+ {
309
+ return $this->ganalytics;
310
+ }
311
+
312
+ public function jsonSerialize()
313
+ {
314
+ return array_filter(
315
+ [
316
+ 'click_tracking' => $this->getClickTracking(),
317
+ 'open_tracking' => $this->getOpenTracking(),
318
+ 'subscription_tracking' => $this->getSubscriptionTracking(),
319
+ 'ganalytics' => $this->getGanalytics()
320
+ ]
321
+ );
322
+ }
323
+ }
324
+
325
+ class BccSettings implements \JsonSerializable
326
+ {
327
+ private
328
+ $enable,
329
+ $email;
330
+
331
+ public function setEnable($enable)
332
+ {
333
+ $this->enable = $enable;
334
+ }
335
+
336
+ public function getEnable()
337
+ {
338
+ return $this->enable;
339
+ }
340
+
341
+ public function setEmail($email)
342
+ {
343
+ $this->email = $email;
344
+ }
345
+
346
+ public function getEmail()
347
+ {
348
+ return $this->email;
349
+ }
350
+
351
+ public function jsonSerialize()
352
+ {
353
+ return array_filter(
354
+ [
355
+ 'enable' => $this->getEnable(),
356
+ 'email' => $this->getEmail()
357
+ ]
358
+ );
359
+ }
360
+ }
361
+
362
+ class BypassListManagement implements \JsonSerializable
363
+ {
364
+ private
365
+ $enable;
366
+
367
+ public function setEnable($enable)
368
+ {
369
+ $this->enable = $enable;
370
+ }
371
+
372
+ public function getEnable()
373
+ {
374
+ return $this->enable;
375
+ }
376
+
377
+ public function jsonSerialize()
378
+ {
379
+ return array_filter(
380
+ [
381
+ 'enable' => $this->getEnable()
382
+ ]
383
+ );
384
+ }
385
+ }
386
+
387
+ class Footer implements \JsonSerializable
388
+ {
389
+ private
390
+ $enable,
391
+ $text,
392
+ $html;
393
+
394
+ public function setEnable($enable)
395
+ {
396
+ $this->enable = $enable;
397
+ }
398
+
399
+ public function getEnable()
400
+ {
401
+ return $this->enable;
402
+ }
403
+
404
+ public function setText($text)
405
+ {
406
+ $this->text = $text;
407
+ }
408
+
409
+ public function getText()
410
+ {
411
+ return $this->text;
412
+ }
413
+
414
+ public function setHtml($html)
415
+ {
416
+ $this->html = $html;
417
+ }
418
+
419
+ public function getHtml()
420
+ {
421
+ return $this->html;
422
+ }
423
+
424
+ public function jsonSerialize()
425
+ {
426
+ return array_filter(
427
+ [
428
+ 'enable' => $this->getEnable(),
429
+ 'text' => $this->getText(),
430
+ 'html' => $this->getHtml()
431
+ ]
432
+ );
433
+ }
434
+ }
435
+
436
+ class SandBoxMode implements \JsonSerializable
437
+ {
438
+ private
439
+ $enable;
440
+
441
+ public function setEnable($enable)
442
+ {
443
+ $this->enable = $enable;
444
+ }
445
+
446
+ public function getEnable()
447
+ {
448
+ return $this->enable;
449
+ }
450
+ public function jsonSerialize()
451
+ {
452
+ return array_filter(
453
+ [
454
+ 'enable' => $this->getEnable()
455
+ ]
456
+ );
457
+ }
458
+ }
459
+
460
+ class SpamCheck implements \JsonSerializable
461
+ {
462
+ private
463
+ $enable,
464
+ $threshold,
465
+ $post_to_url;
466
+
467
+ public function setEnable($enable)
468
+ {
469
+ $this->enable = $enable;
470
+ }
471
+
472
+ public function getEnable()
473
+ {
474
+ return $this->enable;
475
+ }
476
+
477
+ public function setThreshold($threshold)
478
+ {
479
+ $this->threshold = $threshold;
480
+ }
481
+
482
+ public function getThreshold()
483
+ {
484
+ return $this->threshold;
485
+ }
486
+
487
+ public function setPostToUrl($post_to_url)
488
+ {
489
+ $this->post_to_url = $post_to_url;
490
+ }
491
+
492
+ public function getPostToUrl()
493
+ {
494
+ return $this->post_to_url;
495
+ }
496
+
497
+ public function jsonSerialize()
498
+ {
499
+ return array_filter(
500
+ [
501
+ 'enable' => $this->getEnable(),
502
+ 'threshold' => $this->getThreshold(),
503
+ 'post_to_url' => $this->getPostToUrl()
504
+ ]
505
+ );
506
+ }
507
+ }
508
+
509
+ class MailSettings implements \JsonSerializable
510
+ {
511
+ private
512
+ $bcc,
513
+ $bypass_list_management,
514
+ $footer,
515
+ $sandbox_mode,
516
+ $spam_check;
517
+
518
+ public function setBccSettings($bcc)
519
+ {
520
+ $this->bcc = $bcc;
521
+ }
522
+
523
+ public function getBccSettings()
524
+ {
525
+ return $this->bcc;
526
+ }
527
+
528
+ public function setBypassListManagement($bypass_list_management)
529
+ {
530
+ $this->bypass_list_management = $bypass_list_management;
531
+ }
532
+
533
+ public function getBypassListManagement()
534
+ {
535
+ return $this->bypass_list_management;
536
+ }
537
+
538
+ public function setFooter($footer)
539
+ {
540
+ $this->footer = $footer;
541
+ }
542
+
543
+ public function getFooter()
544
+ {
545
+ return $this->footer;
546
+ }
547
+
548
+ public function setSandboxMode($sandbox_mode)
549
+ {
550
+ $this->sandbox_mode = $sandbox_mode;
551
+ }
552
+
553
+ public function getSandboxMode()
554
+ {
555
+ return $this->sandbox_mode;
556
+ }
557
+
558
+ public function setSpamCheck($spam_check)
559
+ {
560
+ $this->spam_check = $spam_check;
561
+ }
562
+
563
+ public function getSpamCheck()
564
+ {
565
+ return $this->spam_check;
566
+ }
567
+
568
+ public function jsonSerialize()
569
+ {
570
+ return array_filter(
571
+ [
572
+ 'bcc' => $this->getBccSettings(),
573
+ 'bypass_list_management' => $this->getBypassListManagement(),
574
+ 'footer' => $this->getFooter(),
575
+ 'sandbox_mode' => $this->getSandboxMode(),
576
+ 'spam_check' => $this->getSpamCheck()
577
+ ]
578
+ );
579
+ }
580
+ }
581
+
582
+ class ASM implements \JsonSerializable
583
+ {
584
+ private
585
+ $group_id,
586
+ $groups_to_display;
587
+
588
+ public function setGroupId($group_id)
589
+ {
590
+ $this->group_id = $group_id;
591
+ }
592
+
593
+ public function getGroupId()
594
+ {
595
+ return $this->group_id;
596
+ }
597
+
598
+ public function setGroupsToDisplay($group_ids)
599
+ {
600
+ $this->groups_to_display = $group_ids;
601
+ }
602
+
603
+ public function getGroupsToDisplay()
604
+ {
605
+ return $this->groups_to_display;
606
+ }
607
+
608
+ public function jsonSerialize()
609
+ {
610
+ return array_filter(
611
+ [
612
+ 'group_id' => $this->getGroupId(),
613
+ 'groups_to_display' => $this->getGroupsToDisplay()
614
+ ]
615
+ );
616
+ }
617
+ }
618
+
619
+ class Attachment implements \JsonSerializable
620
+ {
621
+ private
622
+ $content,
623
+ $type,
624
+ $filename,
625
+ $disposition,
626
+ $content_id;
627
+
628
+ public function setContent($content)
629
+ {
630
+ $this->content = $content;
631
+ }
632
+
633
+ public function getContent()
634
+ {
635
+ return $this->content;
636
+ }
637
+
638
+ public function setType($type)
639
+ {
640
+ $this->type = $type;
641
+ }
642
+
643
+ public function getType()
644
+ {
645
+ return $this->type;
646
+ }
647
+
648
+ public function setFilename($filename)
649
+ {
650
+ $this->filename = $filename;
651
+ }
652
+
653
+ public function getFilename()
654
+ {
655
+ return $this->filename;
656
+ }
657
+
658
+ public function setDisposition($disposition)
659
+ {
660
+ $this->disposition = $disposition;
661
+ }
662
+
663
+ public function getDisposition()
664
+ {
665
+ return $this->disposition;
666
+ }
667
+
668
+ public function setContentID($content_id)
669
+ {
670
+ $this->content_id = $content_id;
671
+ }
672
+
673
+ public function getContentID()
674
+ {
675
+ return $this->content_id;
676
+ }
677
+
678
+ public function jsonSerialize()
679
+ {
680
+ return array_filter(
681
+ [
682
+ 'content' => $this->getContent(),
683
+ 'type' => $this->getType(),
684
+ 'filename' => $this->getFilename(),
685
+ 'disposition' => $this->getDisposition(),
686
+ 'content_id' => $this->getContentID()
687
+ ]
688
+ );
689
+ }
690
+ }
691
+
692
+ class Content implements \JsonSerializable
693
+ {
694
+ private
695
+ $type,
696
+ $value;
697
+
698
+ public function __construct($type, $value)
699
+ {
700
+ $this->type = $type;
701
+ $this->value = $value;
702
+ }
703
+
704
+ public function setType($type)
705
+ {
706
+ $this->type = $type;
707
+ }
708
+
709
+ public function getType()
710
+ {
711
+ return $this->type;
712
+ }
713
+
714
+ public function setValue($value)
715
+ {
716
+ $this->value = $value;
717
+ }
718
+
719
+ public function getValue()
720
+ {
721
+ return $this->value;
722
+ }
723
+
724
+ public function jsonSerialize()
725
+ {
726
+ return array_filter(
727
+ [
728
+ 'type' => $this->getType(),
729
+ 'value' => $this->getValue()
730
+ ]
731
+ );
732
+ }
733
+ }
734
+
735
+ class Personalization implements \JsonSerializable
736
+ {
737
+ private
738
+ $tos,
739
+ $ccs,
740
+ $bccs,
741
+ $subject,
742
+ $headers,
743
+ $substitutions,
744
+ $custom_args,
745
+ $send_at;
746
+
747
+ public function addTo($email)
748
+ {
749
+ $this->tos[] = $email;
750
+ }
751
+
752
+ public function getTos()
753
+ {
754
+ return $this->tos;
755
+ }
756
+
757
+ public function addCc($email)
758
+ {
759
+ $this->ccs[] = $email;
760
+ }
761
+
762
+ public function getCcs()
763
+ {
764
+ return $this->ccs;
765
+ }
766
+
767
+ public function addBcc($email)
768
+ {
769
+ $this->bccs[] = $email;
770
+ }
771
+
772
+ public function getBccs()
773
+ {
774
+ return $this->bccs;
775
+ }
776
+
777
+ public function setSubject($subject)
778
+ {
779
+ $this->subject = $subject;
780
+ }
781
+
782
+ public function getSubject()
783
+ {
784
+ return $this->subject;
785
+ }
786
+
787
+ public function addHeader($key, $value)
788
+ {
789
+ $this->headers[$key] = $value;
790
+ }
791
+
792
+ public function getHeaders()
793
+ {
794
+ return $this->headers;
795
+ }
796
+
797
+ public function addSubstitution($key, $value)
798
+ {
799
+ $this->substitutions[$key] = $value;
800
+ }
801
+
802
+ public function getSubstitutions()
803
+ {
804
+ return $this->substitutions;
805
+ }
806
+
807
+ public function addCustomArg($key, $value)
808
+ {
809
+ $this->custom_args[$key] = $value;
810
+ }
811
+
812
+ public function getCustomArgs()
813
+ {
814
+ return $this->custom_args;
815
+ }
816
+
817
+ public function setSendAt($send_at)
818
+ {
819
+ $this->send_at = $send_at;
820
+ }
821
+
822
+ public function getSendAt()
823
+ {
824
+ return $this->send_at;
825
+ }
826
+
827
+ public function jsonSerialize()
828
+ {
829
+ return array_filter(
830
+ [
831
+ 'to' => $this->getTos(),
832
+ 'cc' => $this->getCcs(),
833
+ 'bcc' => $this->getBccs(),
834
+ 'subject' => $this->subject,
835
+ 'headers' => $this->getHeaders(),
836
+ 'substitutions' => $this->getSubstitutions(),
837
+ 'custom_args' => $this->getCustomArgs(),
838
+ 'send_at' => $this->getSendAt()
839
+ ]
840
+ );
841
+ }
842
+ }
843
+
844
+ class Email implements \JsonSerializable
845
+ {
846
+ private
847
+ $name,
848
+ $email;
849
+
850
+ public function __construct($name, $email)
851
+ {
852
+ $this->name = $name;
853
+ $this->email = $email;
854
+ }
855
+
856
+ public function setName($name)
857
+ {
858
+ $this->name = $name;
859
+ }
860
+
861
+ public function getName()
862
+ {
863
+ return $this->name;
864
+ }
865
+
866
+ public function setEmail($email)
867
+ {
868
+ $this->email = $email;
869
+ }
870
+
871
+ public function getEmail()
872
+ {
873
+ return $this->email;
874
+ }
875
+
876
+ public function jsonSerialize()
877
+ {
878
+ return array_filter(
879
+ [
880
+ 'name' => $this->getName(),
881
+ 'email' => $this->getEmail()
882
+ ]
883
+ );
884
+ }
885
+ }
886
+
887
+ /**
888
+ * The final request body object
889
+ */
890
+ class Mail implements \JsonSerializable
891
+ {
892
+ const VERSION = '1.0.0';
893
+
894
+ protected
895
+ $namespace = 'SendGrid';
896
+
897
+ public
898
+ $from,
899
+ $personalization,
900
+ $subject,
901
+ $contents,
902
+ $attachments,
903
+ $template_id,
904
+ $sections,
905
+ $headers,
906
+ $categories,
907
+ $custom_args,
908
+ $send_at,
909
+ $batch_id,
910
+ $asm,
911
+ $ip_pool_name,
912
+ $mail_settings,
913
+ $tracking_settings,
914
+ $reply_to;
915
+
916
+ public function __construct($from = null, $subject = null, $to = null, $content = null)
917
+ {
918
+ if (!empty($from) && !empty($subject) && !empty($to) && !empty($content))
919
+ {
920
+ $this->setFrom($from);
921
+ $personalization = new Personalization();
922
+ $personalization->addTo($to);
923
+ $this->addPersonalization($personalization);
924
+ $this->setSubject($subject);
925
+ $this->addContent($content);
926
+ }
927
+
928
+ }
929
+
930
+ public function setFrom($email)
931
+ {
932
+ $this->from = $email;
933
+ }
934
+
935
+ public function getFrom()
936
+ {
937
+ return $this->from;
938
+ }
939
+
940
+ public function addPersonalization($personalization)
941
+ {
942
+ $this->personalization[] = $personalization;
943
+ }
944
+
945
+ public function getPersonalizations()
946
+ {
947
+ return $this->personalization;
948
+ }
949
+
950
+ public function setSubject($subject)
951
+ {
952
+ $this->subject = $subject;
953
+ }
954
+
955
+ public function getSubject()
956
+ {
957
+ return $this->subject;
958
+ }
959
+
960
+ public function addContent($content)
961
+ {
962
+ $this->contents[] = $content;
963
+ }
964
+
965
+ public function getContents()
966
+ {
967
+ return $this->contents;
968
+ }
969
+
970
+ public function addAttachment($attachment)
971
+ {
972
+ $this->attachments[] = $attachment;
973
+ }
974
+
975
+ public function getAttachments()
976
+ {
977
+ return $this->attachments;
978
+ }
979
+
980
+ public function setTemplateId($template_id)
981
+ {
982
+ $this->template_id = $template_id;
983
+ }
984
+
985
+ public function getTemplateId()
986
+ {
987
+ return $this->template_id;
988
+ }
989
+
990
+ public function addSection($key, $value)
991
+ {
992
+ $this->sections[$key] = $value;
993
+ }
994
+
995
+ public function getSections()
996
+ {
997
+ return $this->sections;
998
+ }
999
+
1000
+ public function addHeader($key, $value)
1001
+ {
1002
+ $this->headers[$key] = $value;
1003
+ }
1004
+
1005
+ public function getHeaders()
1006
+ {
1007
+ return $this->headers;
1008
+ }
1009
+
1010
+ public function addCategory($category)
1011
+ {
1012
+ $this->categories[] = $category;
1013
+ }
1014
+
1015
+ public function getCategories()
1016
+ {
1017
+ return $this->categories;
1018
+ }
1019
+
1020
+ public function addCustomArg($key, $value)
1021
+ {
1022
+ $this->custom_args[$key] = $value;
1023
+ }
1024
+
1025
+ public function getCustomArgs()
1026
+ {
1027
+ return $this->custom_args;
1028
+ }
1029
+
1030
+ public function setSendAt($send_at)
1031
+ {
1032
+ $this->send_at = $send_at;
1033
+ }
1034
+
1035
+ public function getSendAt()
1036
+ {
1037
+ return $this->send_at;
1038
+ }
1039
+
1040
+ public function setBatchId($batch_id)
1041
+ {
1042
+ $this->batch_id = $batch_id;
1043
+ }
1044
+
1045
+ public function getBatchId()
1046
+ {
1047
+ return $this->batch_id;
1048
+ }
1049
+
1050
+ public function setASM($asm)
1051
+ {
1052
+ $this->asm = $asm;
1053
+ }
1054
+
1055
+ public function getASM()
1056
+ {
1057
+ return $this->asm;
1058
+ }
1059
+
1060
+ public function setIpPoolName($ip_pool_name)
1061
+ {
1062
+ $this->ip_pool_name = $ip_pool_name;
1063
+ }
1064
+
1065
+ public function getIpPoolName()
1066
+ {
1067
+ return $this->ip_pool_name;
1068
+ }
1069
+
1070
+ public function setMailSettings($mail_settings)
1071
+ {
1072
+ $this->mail_settings = $mail_settings;
1073
+ }
1074
+
1075
+ public function getMailSettings()
1076
+ {
1077
+ return $this->mail_settings;
1078
+ }
1079
+
1080
+ public function setTrackingSettings($tracking_settings)
1081
+ {
1082
+ $this->tracking_settings = $tracking_settings;
1083
+ }
1084
+
1085
+ public function getTrackingSettings()
1086
+ {
1087
+ return $this->tracking_settings;
1088
+ }
1089
+
1090
+ public function setReplyTo($reply_to)
1091
+ {
1092
+ $this->reply_to = $reply_to;
1093
+ }
1094
+
1095
+ public function getReplyTo()
1096
+ {
1097
+ return $this->reply_to;
1098
+ }
1099
+
1100
+ public function jsonSerialize()
1101
+ {
1102
+ return array_filter(
1103
+ [
1104
+ 'from' => $this->getFrom(),
1105
+ 'personalizations' => $this->getPersonalizations(),
1106
+ 'subject' => $this->getSubject(),
1107
+ 'content' => $this->getContents(),
1108
+ 'attachments' => $this->getAttachments(),
1109
+ 'template_id' => $this->getTemplateId(),
1110
+ 'sections' => $this->getSections(),
1111
+ 'headers' => $this->getHeaders(),
1112
+ 'categories' => $this->getCategories(),
1113
+ 'custom_args' => $this->getCustomArgs(),
1114
+ 'send_at' => $this->getSendAt(),
1115
+ 'batch_id' => $this->getBatchId(),
1116
+ 'asm' => $this->getASM(),
1117
+ 'ip_pool_name' => $this->getIpPoolName(),
1118
+ 'mail_settings' => $this->getMailSettings(),
1119
+ 'tracking_settings' => $this->getTrackingSettings(),
1120
+ 'reply_to' => $this->getReplyTo()
1121
+ ]
1122
+ );
1123
+ }
1124
+ }
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: 4.6
6
  Tested up to: 4.7
7
- Stable tag: 1.10.9
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -42,7 +42,8 @@ If you already had the plugin installed in a Multisite environment and you updat
42
 
43
  Requirements:
44
 
45
- 1. PHP version >= 5.3.0. PHP 7 is not yet supported.
 
46
  2. To send emails through SMTP you need to also install the 'Swift Mailer' plugin. ( https://wordpress.org/plugins/swift-mailer/ )
47
  3. If wp_mail() function has been declared by another plugin that you have installed, you won't be able to use the SendGrid plugin
48
 
@@ -75,10 +76,7 @@ For Multisite:
75
 
76
  SendGrid settings can optionally be defined as global variables (wp-config.php):
77
 
78
- 1. Set credentials (You can use credentials or API key. If using credentials, both need to be set in order to get credentials from variables and not from the database. If using API key you need to make sure you set the Mail Send permissions to FULL ACCESS, Stats to READ ACCESS and Template Engine to READ or FULL ACCESS when you created the api key on SendGrid side, so you can send emails and see statistics on wordpress):
79
- * Auth method ('apikey' or 'credentials'): define('SENDGRID_AUTH_METHOD', 'apikey');
80
- * Username: define('SENDGRID_USERNAME', 'sendgrid_username');
81
- * Password: define('SENDGRID_PASSWORD', 'sendgrid_password');
82
  * API key: define('SENDGRID_API_KEY', 'sendgrid_api_key');
83
 
84
  2. Set email related settings:
@@ -150,6 +148,12 @@ Note that all HTML emails sent through our plugin also contain the HTML body in
150
 
151
  Yes. You can find it here : https://sendgrid.com/docs/Integrate/Tutorials/WordPress/index.html
152
 
 
 
 
 
 
 
153
  = What credentials do I need to add on settings page ? =
154
 
155
  Create a SendGrid account at <a href="http://sendgrid.com/partner/wordpress" target="_blank">https://sendgrid.com/partner/wordpress</a> and generate a new API key on <https://app.sendgrid.com/settings/api_keys>.
@@ -242,7 +246,7 @@ You need to enable the use of the First Name and Last Name fields from the setti
242
 
243
  Yes. This plugin has basic Multisite support. You need to Network Activate this plugin.
244
 
245
- The settings for all sites in the network can be configured only by the Network Admin in the Network Admin Dashboard.
246
 
247
  Since 1.10.5 the Network Admin can delegate the configuration for each subsite to their respective owners. This will allow any subsite to use it's own SendGrid Plugin configuration.
248
 
@@ -250,19 +254,25 @@ Since 1.10.5 the Network Admin can delegate the configuration for each subsite t
250
 
251
  1. Go to Admin Panel, section Plugins and activate the SendGrid plugin. If you want to send emails through SMTP you need to install also the 'Swift Mailer' plugin.
252
  2. After activation "Settings" link will appear.
253
- 3. Go to settings page and provide your SendGrid credentials by choosing the authentication method which default is Api Key. On this page you can set also the default "Name", "Sending Address" and "Reply Address".
254
- 4. If you want to use your username and password for authentication, switch to Username&Password authentication method.
255
- 5. If you provide valid credentials, a form which can be used to send test emails will appear. Here you can test the plugin sending some emails.
256
- 6. Header provided in the send test email form.
257
- 7. If you click in the right corner from the top of the page on the "Help" button, a popup window with more information will appear.
258
- 8. Select the time interval for which you want to see SendGrid statistics and charts.
259
- 9. You can configure the port number when using SMTP method.
260
- 10. You can configure categories for which you would like to see your stats.
261
- 11. You can use substitutions for emails using X-SMTPAPI headers.
262
- 12. You can configure the subscription widget.
263
 
264
  == Changelog ==
265
 
 
 
 
 
 
 
 
266
  = 1.10.9 =
267
  * Added pagination on multisite settings page
268
  * Fixed an FAQ link
@@ -416,6 +426,13 @@ Since 1.10.5 the Network Admin can delegate the configuration for each subsite t
416
 
417
  == Upgrade notice ==
418
 
 
 
 
 
 
 
 
419
  = 1.10.9 =
420
  * Added pagination on multisite settings page
421
  * Fixed an FAQ link
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: 4.6
6
  Tested up to: 4.7
7
+ Stable tag: 1.11.0
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
42
 
43
  Requirements:
44
 
45
+ 1. PHP version >= 5.6.0. PHP 7 is not yet supported. Installing this plugin on PHP versions 5.3 and earlier will cause your website to break.
46
+ Installation on PHP versions 5.4 and 5.5 will work but it is not recommended.
47
  2. To send emails through SMTP you need to also install the 'Swift Mailer' plugin. ( https://wordpress.org/plugins/swift-mailer/ )
48
  3. If wp_mail() function has been declared by another plugin that you have installed, you won't be able to use the SendGrid plugin
49
 
76
 
77
  SendGrid settings can optionally be defined as global variables (wp-config.php):
78
 
79
+ 1. Set the API key. You need to make sure you set the Mail Send permissions to FULL ACCESS, Stats to READ ACCESS and Template Engine to READ or FULL ACCESS when you created the api key on SendGrid side, so you can send emails and see statistics on wordpress):
 
 
 
80
  * API key: define('SENDGRID_API_KEY', 'sendgrid_api_key');
81
 
82
  2. Set email related settings:
148
 
149
  Yes. You can find it here : https://sendgrid.com/docs/Integrate/Tutorials/WordPress/index.html
150
 
151
+ = What PHP versions are supported ? =
152
+
153
+ Plugin versions 1.11.x were tested and confirmed to work on PHP 5.4, 5.5, 5.6, 7.0, 7.1. It DOES NOT work on PHP 5.3 and earlier.
154
+
155
+ Plugin versions 1.10.x were tested and confirmed to work on PHP 5.3, 5.4, 5.5 and 5.6. It DOES NOT work on PHP 7.0 and later.
156
+
157
  = What credentials do I need to add on settings page ? =
158
 
159
  Create a SendGrid account at <a href="http://sendgrid.com/partner/wordpress" target="_blank">https://sendgrid.com/partner/wordpress</a> and generate a new API key on <https://app.sendgrid.com/settings/api_keys>.
246
 
247
  Yes. This plugin has basic Multisite support. You need to Network Activate this plugin.
248
 
249
+ The settings for all sites in the network can be configured only by the Network Admin in the Network Admin Dashboard.
250
 
251
  Since 1.10.5 the Network Admin can delegate the configuration for each subsite to their respective owners. This will allow any subsite to use it's own SendGrid Plugin configuration.
252
 
254
 
255
  1. Go to Admin Panel, section Plugins and activate the SendGrid plugin. If you want to send emails through SMTP you need to install also the 'Swift Mailer' plugin.
256
  2. After activation "Settings" link will appear.
257
+ 3. Go to settings page and provide your SendGrid API Key. On this page you can set also the default "Name", "Sending Address" and "Reply Address".
258
+ 4. If you provide valid credentials, a form which can be used to send test emails will appear. Here you can test the plugin sending some emails.
259
+ 5. Header provided in the send test email form.
260
+ 6. If you click in the right corner from the top of the page on the "Help" button, a popup window with more information will appear.
261
+ 7. Select the time interval for which you want to see SendGrid statistics and charts.
262
+ 8. You can configure the port number when using SMTP method.
263
+ 9. You can configure categories for which you would like to see your stats.
264
+ 10. You can use substitutions for emails using X-SMTPAPI headers.
265
+ 11. You can configure the subscription widget.
 
266
 
267
  == Changelog ==
268
 
269
+ = 1.11.0 =
270
+ * BREAKING CHANGE: DO NOT UPGRADE IF YOU USE PHP <= 5.3. Only PHP 5.4 and later versions are supported.
271
+ * BREAKING CHANGE: Username & Password is no longer supported. Change your settings to use an API Key before updating
272
+ * API Mail Send was changed to use the V3 SendGrid API
273
+ * Emails sent with the V2 Email Object will now be translated to V3
274
+ * BREAKING CHANGE: The date parameter on the V2 object is no longer supported
275
+ * BREAKING CHANGE: When using the V2 object with SMTPAPI Tos, the BCC and CCs will only be applied to the first address
276
  = 1.10.9 =
277
  * Added pagination on multisite settings page
278
  * Fixed an FAQ link
426
 
427
  == Upgrade notice ==
428
 
429
+ = 1.11.0 =
430
+ * BREAKING CHANGE: DO NOT UPGRADE IF YOU USE PHP <= 5.3. Only PHP 5.4 and later versions are supported.
431
+ * BREAKING CHANGE: Username & Password is no longer supported. Change your settings to use an API Key before updating
432
+ * API Mail Send was changed to use the V3 SendGrid API
433
+ * Emails sent with the V2 Email Object will now be translated to V3
434
+ * BREAKING CHANGE: The date parameter on the V2 object is no longer supported
435
+ * BREAKING CHANGE: When using the V2 object with SMTPAPI Tos, the BCC and CCs will only be applied to the first address
436
  = 1.10.9 =
437
  * Added pagination on multisite settings page
438
  * Fixed an FAQ link
view/js/sendgrid.settings-v1.7.3.js CHANGED
@@ -6,16 +6,6 @@ jQuery(document).ready(function($) {
6
  var select_page_placeholder = 'Select a page';
7
  var no_pages_placeholder = 'Please create a page to select';
8
 
9
- if ( $('#auth_method').find( 'option:selected' ).val() == 'apikey' ) {
10
- $('.apikey').show();
11
- $('.credentials').hide();
12
- $('.send_method').show();
13
- } else if ( $('#auth_method').find( 'option:selected' ).val() == 'credentials' ) {
14
- $('.apikey').hide();
15
- $('.credentials').show();
16
- $('.send_method').show();
17
- }
18
-
19
  if ( $('#send_method').find( 'option:selected' ).val() == 'api' ) {
20
  $('.port').hide();
21
  } else if ( $('#send_method').find( 'option:selected' ).val() == 'smtp' ) {
6
  var select_page_placeholder = 'Select a page';
7
  var no_pages_placeholder = 'Please create a page to select';
8
 
 
 
 
 
 
 
 
 
 
 
9
  if ( $('#send_method').find( 'option:selected' ).val() == 'api' ) {
10
  $('.port').hide();
11
  } else if ( $('#send_method').find( 'option:selected' ).val() == 'smtp' ) {
view/sendgrid_settings_general.php CHANGED
@@ -7,37 +7,13 @@
7
  <h3 class="sendgrid-settings-top-header"><?php echo _e('SendGrid Credentials') ?></h3>
8
  </td>
9
  </tr>
10
- <tr valign="top">
11
- <th scope="row"><?php _e("Authentication method: "); ?></th>
12
- <td>
13
- <select name="auth_method" class="sendgrid-settings-select" id="auth_method" <?php disabled( $is_env_auth_method ); ?> >
14
- <option value="apikey" id="apikey" <?php echo ( 'apikey' == $auth_method ) ? 'selected' : '' ?>><?php _e('Api Key') ?></option>
15
- <option value="credentials" id="credentials" <?php echo ( 'credentials' == $auth_method ) ? 'selected' : '' ?>><?php _e('Username&Password') ?></option>
16
- <?php if ( ! in_array( $auth_method, Sendgrid_Tools::$allowed_auth_methods ) ) { ?>
17
- <option value="<?php echo $auth_method; ?>" id="<?php echo $auth_method; ?>" selected><?php echo $auth_method; ?></option>
18
- <?php } ?>
19
- </select>
20
- </td>
21
- </tr>
22
- <tr valign="top" class="apikey" style="display: none;">
23
  <th scope="row"><?php _e("API Key: "); ?></th>
24
  <td>
25
  <input type="password" id="sendgrid_general_apikey" name="sendgrid_apikey" class="sendgrid-settings-key" value="<?php echo ( $is_env_api_key ? "************" : $api_key ); ?>" <?php disabled( $is_env_api_key ); ?>>
26
  </td>
27
  </tr>
28
- <tr valign="top" class="credentials" style="display: none;">
29
- <th scope="row"><?php _e("Username: "); ?></th>
30
- <td>
31
- <input type="text" name="sendgrid_username" value="<?php echo $user; ?>" size="20" class="regular-text" <?php disabled( $is_env_username ); ?>>
32
- </td>
33
- </tr>
34
- <tr valign="top" class="credentials" style="display: none;">
35
- <th scope="row"><?php _e("Password: "); ?></th>
36
- <td>
37
- <input type="password" name="sendgrid_password" value="<?php echo ( $is_env_password ? "******" : $password ); ?>" size="20" class="regular-text" <?php disabled( $is_env_password ); ?>>
38
- </td>
39
- </tr>
40
- <tr valign="top" class="send_method" style="display: none;">
41
  <th scope="row"><?php _e("Send Mail with: "); ?></th>
42
  <td>
43
  <select name="send_method" class="sendgrid-settings-select" id="send_method" <?php disabled( defined('SENDGRID_SEND_METHOD') ); ?>>
@@ -63,7 +39,7 @@
63
  </select>
64
  </td>
65
  </tr>
66
- <?php if ( $is_env_auth_method or $is_env_send_method or $is_env_api_key or $is_env_username or $is_env_password or $is_env_port ) : ?>
67
  <tr valign="top">
68
  <td colspan="2">
69
  <p>
7
  <h3 class="sendgrid-settings-top-header"><?php echo _e('SendGrid Credentials') ?></h3>
8
  </td>
9
  </tr>
10
+ <tr valign="top" class="apikey">
 
 
 
 
 
 
 
 
 
 
 
 
11
  <th scope="row"><?php _e("API Key: "); ?></th>
12
  <td>
13
  <input type="password" id="sendgrid_general_apikey" name="sendgrid_apikey" class="sendgrid-settings-key" value="<?php echo ( $is_env_api_key ? "************" : $api_key ); ?>" <?php disabled( $is_env_api_key ); ?>>
14
  </td>
15
  </tr>
16
+ <tr valign="top" class="send_method">
 
 
 
 
 
 
 
 
 
 
 
 
17
  <th scope="row"><?php _e("Send Mail with: "); ?></th>
18
  <td>
19
  <select name="send_method" class="sendgrid-settings-select" id="send_method" <?php disabled( defined('SENDGRID_SEND_METHOD') ); ?>>
39
  </select>
40
  </td>
41
  </tr>
42
+ <?php if ( $is_env_send_method or $is_env_api_key or $is_env_port ) : ?>
43
  <tr valign="top">
44
  <td colspan="2">
45
  <p>
view/sendgrid_settings_test_email.php CHANGED
@@ -11,25 +11,25 @@
11
  <tr valign="top">
12
  <th scope="row"><?php _e("To: "); ?></th>
13
  <td>
14
- <input type="text" name="sendgrid_to" required="true" value="<?php echo isset($success) ? '' : isset($to) ? $to : '' ; ?>" size="20" class="regular-text">
15
  </td>
16
  </tr>
17
  <tr valign="top">
18
  <th scope="row"><?php _e("Subject: "); ?></th>
19
  <td>
20
- <input type="text" name="sendgrid_subj" required="true" value="<?php echo isset($success) ? '' : isset($subject) ? $subject : '' ; ?>" size="20" class="regular-text">
21
  </td>
22
  </tr>
23
  <tr valign="top">
24
  <th scope="row"><?php _e("Body: "); ?></th>
25
  <td>
26
- <textarea name="sendgrid_body" rows="5" class="large-text"><?php echo isset($success) ? '' : isset($body) ? $body : '' ; ?></textarea>
27
  </td>
28
  </tr>
29
  <tr valign="top">
30
  <th scope="row"><?php _e("Headers: "); ?></th>
31
  <td>
32
- <textarea name="sendgrid_headers" rows="3" class="large-text"><?php echo isset($success) ? '' : isset($headers) ? $headers : ''; ?></textarea>
33
  </td>
34
  </tr>
35
  </table>
11
  <tr valign="top">
12
  <th scope="row"><?php _e("To: "); ?></th>
13
  <td>
14
+ <input type="text" name="sendgrid_to" required="true" value="<?php echo isset( $success ) ? '' : isset( $to ) ? $to : '' ; ?>" size="20" class="regular-text">
15
  </td>
16
  </tr>
17
  <tr valign="top">
18
  <th scope="row"><?php _e("Subject: "); ?></th>
19
  <td>
20
+ <input type="text" name="sendgrid_subj" required="true" value="<?php echo isset( $success ) ? '' : isset( $subject ) ? $subject : '' ; ?>" size="20" class="regular-text">
21
  </td>
22
  </tr>
23
  <tr valign="top">
24
  <th scope="row"><?php _e("Body: "); ?></th>
25
  <td>
26
+ <textarea name="sendgrid_body" rows="5" class="large-text"><?php echo isset( $success ) ? '' : isset( $body ) ? $body : '' ; ?></textarea>
27
  </td>
28
  </tr>
29
  <tr valign="top">
30
  <th scope="row"><?php _e("Headers: "); ?></th>
31
  <td>
32
+ <textarea name="sendgrid_headers" rows="3" class="large-text"><?php echo isset( $success ) ? '' : isset( $headers ) ? $headers : ''; ?></textarea>
33
  </td>
34
  </tr>
35
  </table>
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.10.9
7
  Author: SendGrid
8
  Author URI: http://sendgrid.com
9
  Text Domain: sendgrid-email-delivery-simplified
@@ -15,7 +15,7 @@ define( 'SENDGRID_CATEGORY', 'wp_sendgrid_plugin' );
15
  define( 'SENDGRID_PLUGIN_SETTINGS', 'settings_page_sendgrid-settings' );
16
  define( 'SENDGRID_PLUGIN_STATISTICS', 'dashboard_page_sendgrid-statistics' );
17
 
18
- if ( version_compare( phpversion(), '5.3.0', '<' ) ) {
19
  add_action( 'admin_notices', 'php_version_error' );
20
 
21
  /**
@@ -25,7 +25,7 @@ if ( version_compare( phpversion(), '5.3.0', '<' ) ) {
25
  */
26
  function php_version_error()
27
  {
28
- echo '<div class="error"><p>' . __('SendGrid: Plugin requires PHP >= 5.3.0.') . '</p></div>';
29
  }
30
 
31
  return;
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.11.0
7
  Author: SendGrid
8
  Author URI: http://sendgrid.com
9
  Text Domain: sendgrid-email-delivery-simplified
15
  define( 'SENDGRID_PLUGIN_SETTINGS', 'settings_page_sendgrid-settings' );
16
  define( 'SENDGRID_PLUGIN_STATISTICS', 'dashboard_page_sendgrid-statistics' );
17
 
18
+ if ( version_compare( phpversion(), '5.4.0', '<' ) ) {
19
  add_action( 'admin_notices', 'php_version_error' );
20
 
21
  /**
25
  */
26
  function php_version_error()
27
  {
28
+ echo '<div class="error"><p>' . __('SendGrid: Plugin requires PHP >= 5.4.0.') . '</p></div>';
29
  }
30
 
31
  return;