Mailgun for WordPress - Version 1.7.2

Version Description

(2020-10-07): = - Test plugin with PHP 7.4. - Test plugin up to WordPress 5.5.1.

Download this release

Release Info

Developer Mailgun
Plugin Icon 128x128 Mailgun for WordPress
Version 1.7.2
Comparing to
See all releases

Code changes from version 1.5.10 to 1.7.2

.gitignore CHANGED
@@ -1 +1,3 @@
1
  svn/
 
 
1
  svn/
2
+ .idea/
3
+ docker-compose.yaml
CHANGELOG.md CHANGED
@@ -1,6 +1,55 @@
1
  Changelog
2
  =========
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  1.5.10 (2017-11-22):
5
  - Fix back to settings link on lists page (https://github.com/mailgun/wordpress-plugin/pull/65)
6
  - Fix a bug causing `text/html` emails to send as both `text/plain` *and* `text/html` parts
1
  Changelog
2
  =========
3
 
4
+ 1.7.1 (2019-02-07)
5
+ - Reinstall settings page for multisites.
6
+
7
+ 1.7 (2019-01-21)
8
+ - Remove settings page for multisites.
9
+ - Simplify admin notifications.
10
+ - Test plugin with PHP 7.2.
11
+ - Test plugin up to WordPress 5.0.3.
12
+
13
+ 1.6.1 (2018-10-08)
14
+ - Restore Settings page form for all install types.
15
+
16
+ 1.6 (2018-9-21)
17
+ - Refactor admin notifications
18
+ - Enable Settings page for all WordPress install types
19
+ - Enable Test Configuration for all WordPress install types
20
+ - Test plugin up to WordPress 4.9.8.
21
+
22
+ 1.5.14 (2018-09-11)
23
+ - Force SSL-secured SMTP connections to use port 465 (SMTPS) to connect, 587 for plain and TLS
24
+ - Support region endpoint switching for SMTP
25
+
26
+ 1.5.13.1 (2018-08-15)
27
+ - Fix line breaks in Test Configuration email
28
+
29
+ 1.5.13 (2018-08-14)
30
+ - Default to US region if no region is configured in settings
31
+ - Add admin notification about region configuration
32
+ - Log an error message when an email is sent with no explicit region configured
33
+
34
+ 1.5.12.3 (2018-08-09)
35
+ - Fix Region select menu default when wp-config.php variable is set
36
+ - Fix front end email input validation
37
+
38
+ 1.5.12.2 (2018-08-09)
39
+ - Fix plugin not saving after update
40
+
41
+ 1.5.12.1 (2018-08-06)
42
+ - Fix for backwards compatibility
43
+
44
+ 1.5.12 (2018-08-02)
45
+ - Add EU endpoint for Mailgun HTTP API
46
+ - Fix broken logo image on Lists page
47
+ - Test plugin up to Wordpress 4.9.7
48
+
49
+ 1.5.11 (2018-05-30):
50
+ - Fix an issue with authentication failing for newer API keys
51
+ - Test plugin up to Wordpress 4.9.6
52
+
53
  1.5.10 (2017-11-22):
54
  - Fix back to settings link on lists page (https://github.com/mailgun/wordpress-plugin/pull/65)
55
  - Fix a bug causing `text/html` emails to send as both `text/plain` *and* `text/html` parts
includes/admin.php CHANGED
@@ -1,441 +1,493 @@
1
  <?php
2
 
3
- /*
4
- * mailgun-wordpress-plugin - Sending mail from Wordpress using Mailgun
5
- * Copyright (C) 2016 Mailgun, et al.
6
- *
7
- * This program is free software; you can redistribute it and/or modify
8
- * it under the terms of the GNU General Public License as published by
9
- * the Free Software Foundation; either version 2 of the License, or
10
- * (at your option) any later version.
11
- *
12
- * This program is distributed in the hope that it will be useful,
13
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- * GNU General Public License for more details.
16
- *
17
- * You should have received a copy of the GNU General Public License along
18
- * with this program; if not, write to the Free Software Foundation, Inc.,
19
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
- */
21
-
22
- class MailgunAdmin extends Mailgun
23
- {
24
- /**
25
- * @var array Array of "safe" option defaults.
26
- */
27
- private $defaults;
28
-
29
- /**
30
- * Setup backend functionality in WordPress.
31
- *
32
- * @return none
33
- *
34
- * @since 0.1
35
- */
36
- public function __construct()
37
- {
38
- Mailgun::__construct();
39
-
40
- // Load localizations if available
41
- load_plugin_textdomain('mailgun', false, 'mailgun/languages');
42
-
43
- // Activation hook
44
- register_activation_hook($this->plugin_file, array(&$this, 'init'));
45
-
46
- if (!defined('MAILGUN_USEAPI') || !MAILGUN_USEAPI) {
47
- // Hook into admin_init and register settings and potentially register an admin_notice
48
- add_action('admin_init', array(&$this, 'admin_init'));
49
-
50
- // Activate the options page
51
- add_action('admin_menu', array(&$this, 'admin_menu'));
52
- }
53
-
54
- // Register an AJAX action for testing mail sending capabilities
55
- add_action('wp_ajax_mailgun-test', array(&$this, 'ajax_send_test'));
56
- }
57
-
58
- /**
59
- * Initialize the default options during plugin activation.
60
- *
61
- * @return none
62
- *
63
- * @since 0.1
64
- */
65
- public function init()
66
- {
67
- $sitename = strtolower($_SERVER['SERVER_NAME']);
68
- if (substr($sitename, 0, 4) == 'www.') {
69
- $sitename = substr($sitename, 4);
70
- }
71
-
72
- $this->defaults = array(
73
- 'useAPI' => '1',
74
- 'apiKey' => '',
75
- 'domain' => '',
76
- 'username' => '',
77
- 'password' => '',
78
- 'secure' => '1',
79
- 'sectype' => 'tls',
80
- 'track-clicks' => '',
81
- 'track-opens' => '',
82
- 'campaign-id' => '',
83
- 'override-from' => '0',
84
- 'tag' => $sitename,
85
- );
86
- if (!$this->options) {
87
- $this->options = $this->defaults;
88
- add_option('mailgun', $this->options);
89
- }
90
- }
91
-
92
- /**
93
- * Add the options page.
94
- *
95
- * @return none
96
- *
97
- * @since 0.1
98
- */
99
- public function admin_menu()
100
- {
101
- if (current_user_can('manage_options')) {
102
- $this->hook_suffix = add_options_page(__('Mailgun', 'mailgun'), __('Mailgun', 'mailgun'), 'manage_options', 'mailgun', array(&$this, 'options_page'));
103
- add_options_page(__('Mailgun Lists', 'mailgun'), __('Mailgun Lists', 'mailgun'), 'manage_options', 'mailgun-lists', array(&$this, 'lists_page'));
104
- add_action("admin_print_scripts-{$this->hook_suffix}", array(&$this, 'admin_js'));
105
- add_filter("plugin_action_links_{$this->plugin_basename}", array(&$this, 'filter_plugin_actions'));
106
- add_action("admin_footer-{$this->hook_suffix}", array(&$this, 'admin_footer_js'));
107
- }
108
- }
109
-
110
- /**
111
- * Enqueue javascript required for the admin settings page.
112
- *
113
- * @return none
114
- *
115
- * @since 0.1
116
- */
117
- public function admin_js()
118
- {
119
- wp_enqueue_script('jquery');
120
- }
121
-
122
- /**
123
- * Output JS to footer for enhanced admin page functionality.
124
- *
125
- * @since 0.1
126
- */
127
- public function admin_footer_js()
128
- {
129
- ?>
130
- <script type="text/javascript">
131
- /* <![CDATA[ */
132
- var mailgunApiOrNot = function() {
133
- if (jQuery("#mailgun-api").val() == 1) {
134
- jQuery(".mailgun-smtp").hide();
135
- jQuery(".mailgun-api").show();
 
 
 
 
136
  } else {
137
- jQuery(".mailgun-api").hide();
138
- jQuery(".mailgun-smtp").show();
139
  }
140
 
141
- }
142
- var formModified = false;
143
- jQuery().ready(function() {
144
- mailgunApiOrNot();
145
- jQuery('#mailgun-api').change(function() {
146
- mailgunApiOrNot();
147
- });
148
- jQuery('#mailgun-test').click(function(e) {
149
- e.preventDefault();
150
- if ( formModified ) {
151
- var doTest = confirm('<?php _e('The Mailgun plugin configuration has changed since you last saved. Do you wish to test anyway?\n\nClick "Cancel" and then "Save Changes" if you wish to save your changes.', 'mailgun'); ?>');
152
- if ( ! doTest ) {
153
- return false;
154
- }
155
  }
156
- jQuery(this).val('<?php _e('Testing...', 'mailgun'); ?>');
157
- jQuery("#mailgun-test-result").text('');
158
- jQuery.get(
159
- ajaxurl,
160
- {
161
- action: 'mailgun-test',
162
- _wpnonce: '<?php echo wp_create_nonce(); ?>'
163
- }
164
- )
165
- .complete(function() {
166
- jQuery("#mailgun-test").val('<?php _e('Test Configuration', 'mailgun'); ?>');
 
 
 
 
 
 
 
167
  })
168
- .success(function(data) {
169
- alert(
170
- 'Mailgun ' + data.method + ' Test ' + data.message
171
- + '; status "' + data.error + '"'
172
- );
173
  })
174
- .error(function() {
175
- alert('Mailgun Test <?php _e('Failure', 'mailgun'); ?>');
176
- });
177
- });
178
- jQuery("#mailgun-form").change(function() {
179
- formModified = true;
180
- });
181
- });
182
- /* ]]> */
183
- </script>
184
- <?php
185
-
186
- }
187
-
188
- /**
189
- * Output the options page.
190
- *
191
- * @return none
192
- *
193
- * @since 0.1
194
- */
195
- public function options_page()
196
- {
197
- if (!@include 'options-page.php') {
198
- printf(__('<div id="message" class="updated fade"><p>The options page for the <strong>Mailgun</strong> plugin cannot be displayed. The file <strong>%s</strong> is missing. Please reinstall the plugin.</p></div>', 'mailgun'), dirname(__FILE__).'/options-page.php');
199
- }
200
- }
201
-
202
- /**
203
- * Output the lists page.
204
- *
205
- * @return none
206
- *
207
- * @since 0.1
208
- */
209
- public function lists_page()
210
- {
211
- if (!@include 'lists-page.php') {
212
- printf(__('<div id="message" class="updated fade"><p>The lists page for the <strong>Mailgun</strong> plugin cannot be displayed. The file <strong>%s</strong> is missing. Please reinstall the plugin.</p></div>', 'mailgun'), dirname(__FILE__).'/lists-page.php');
213
- }
214
- }
215
-
216
- // /options-general.php?page=mailgun-lists
217
-
218
- /**
219
- * Wrapper function hooked into admin_init to register settings
220
- * and potentially register an admin notice if the plugin hasn't
221
- * been configured yet.
222
- *
223
- * @return none
224
- *
225
- * @since 0.1
226
- */
227
- public function admin_init()
228
- {
229
- $this->register_settings();
230
- $apiKey = $this->get_option('apiKey');
231
- $useAPI = $this->get_option('useAPI');
232
- $password = $this->get_option('password');
233
-
234
- add_action('admin_notices', array(&$this, 'admin_notices'));
235
- }
236
-
237
- /**
238
- * Whitelist the mailgun options.
239
- *
240
- * @since 0.1
241
- *
242
- * @return none
243
- */
244
- public function register_settings()
245
- {
246
- register_setting('mailgun', 'mailgun', array(&$this, 'validation'));
247
- }
248
-
249
- /**
250
- * Data validation callback function for options.
251
- *
252
- * @param array $options An array of options posted from the options page
253
- *
254
- * @return array
255
- *
256
- * @since 0.1
257
- */
258
- public function validation($options)
259
- {
260
- $apiKey = trim($options['apiKey']);
261
- $username = trim($options['username']);
262
- if (!empty($apiKey)) {
263
- $pos = strpos($apiKey, 'key-');
264
- if ($pos === false || $pos > 4) {
265
- $apiKey = "key-{$apiKey}";
266
- }
267
-
268
- $pos = strpos($apiKey, 'api:');
269
- if ($pos !== false && $pos == 0) {
270
- $apiKey = substr($apiKey, 4);
271
- }
272
- $options['apiKey'] = $apiKey;
273
- }
274
-
275
- if (!empty($username)) {
276
- $username = preg_replace('/@.+$/', '', $username);
277
- $options['username'] = $username;
278
- }
279
-
280
- foreach ($options as $key => $value) {
281
- $options[$key] = trim($value);
282
- }
283
-
284
- if (empty($options['override-from'])) {
285
- $options['override-from'] = $this->defaults['override-from'];
286
- }
287
-
288
- if (empty($options['sectype'])) {
289
- $options['sectype'] = $this->defaults['sectype'];
290
- }
291
- // alternatively:
292
- // foreach ($defaults as $key => $value) {
293
- // if (empty($options[$key])) {
294
- // $options[$key] = $value;
295
- // }
296
- // }
297
-
298
- $this->options = $options;
299
-
300
- return $options;
301
- }
302
-
303
- /**
304
- * Function to output an admin notice when the plugin has not
305
- * been configured yet.
306
- *
307
- * @return none
308
- *
309
- * @since 0.1
310
- */
311
- public function admin_notices()
312
- {
313
- $screen = get_current_screen();
314
- if (!current_user_can('manage_options') || $screen->id == $this->hook_suffix
315
- ) {
316
- return;
317
- }
318
-
319
- if ((!$this->get_option('apiKey') && $this->get_option('useAPI') === '1')
320
- || (!$this->get_option('password') && $this->get_option('useAPI') === '0')
321
- ) {
322
- ?>
323
- <div id='mailgun-warning' class='notice notice-warning fade'><p><strong><?php _e('Mailgun is almost ready. ', 'mailgun'); ?></strong><?php printf(__('You must <a href="%1$s">configure Mailgun</a> for it to work.', 'mailgun'), menu_page_url('mailgun', false)); ?></p></div>
324
  <?php
325
-
326
- }
327
-
328
- if ($this->get_option('override-from') === '1'
329
- && (!$this->get_option('from-name')
330
- || !$this->get_option('from-address'))
331
- ) {
332
- ?>
333
- <div id='mailgun-warning' class='notice notice-warning fade'><p><strong><?php _e('Mailgun is almost ready. ', 'mailgun'); ?></strong><?php printf(__('"Override From" option requires that "From Name" and "From Address" be set to work properly! <a href="%1$s">Configure Mailgun now</a>.', 'mailgun'), menu_page_url('mailgun', false)); ?></p></div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
334
  <?php
335
-
336
- }
337
- }
338
-
339
- /**
340
- * Add a settings link to the plugin actions.
341
- *
342
- * @param array $links Array of the plugin action links
343
- *
344
- * @return array
345
- *
346
- * @since 0.1
347
- */
348
- public function filter_plugin_actions($links)
349
- {
350
- $settings_link = '<a href="'.menu_page_url('mailgun', false).'">'.__('Settings', 'mailgun').'</a>';
351
- array_unshift($links, $settings_link);
352
-
353
- return $links;
354
- }
355
-
356
- /**
357
- * AJAX callback function to test mail sending functionality.
358
- *
359
- * @return string
360
- *
361
- * @since 0.1
362
- */
363
- public function ajax_send_test()
364
- {
365
- nocache_headers();
366
- header('Content-Type: application/json');
367
-
368
- if (!current_user_can('manage_options') || !wp_verify_nonce($_GET['_wpnonce'])) {
369
- die(
370
- json_encode(
371
- array(
372
- 'message' => __('Unauthorized', 'mailgun'),
373
- 'method' => null,
374
- 'error' => __('Unauthorized', 'mailgun'),
375
- )
376
- )
377
- );
378
- }
379
-
380
- $useAPI = (defined('MAILGUN_USEAPI') && MAILGUN_USEAPI) ? MAILGUN_USEAPI : $this->get_option('useAPI');
381
- $secure = (defined('MAILGUN_SECURE') && MAILGUN_SECURE) ? MAILGUN_SECURE : $this->get_option('secure');
382
- $sectype = (defined('MAILGUN_SECTYPE') && MAILGUN_SECTYPE) ? MAILGUN_SECTYPE : $this->get_option('sectype');
383
-
384
- if ((bool) $useAPI) {
385
- $method = __('HTTP API', 'mailgun');
386
- } else {
387
- $method = ((bool) $secure) ? __('Secure SMTP', 'mailgun') : __('SMTP', 'mailgun');
388
- if ((bool) $secure) {
389
- $method = $method . sprintf(__(' via %s', $sectype));
390
- }
391
- }
392
-
393
- $admin_email = get_option('admin_email');
394
- $result = wp_mail(
395
- $admin_email,
396
- __('Mailgun WordPress Plugin Test', 'mailgun'),
397
- sprintf(__("This is a test email generated by the Mailgun WordPress plugin.\n\nIf you have received this message, the requested test has succeeded.\n\nThe method used to send this email was: %s.", 'mailgun'), $method),
398
- array('Content-Type: text/plain')
399
- );
400
-
401
- if ((bool) $useAPI) {
402
- if (!function_exists('mg_api_last_error')) {
403
- if (!include dirname(__FILE__).'/wp-mail-api.php') {
404
- self::deactivate_and_die(dirname(__FILE__).'/wp-mail-api.php');
405
- }
406
- }
407
-
408
- $error_msg = mg_api_last_error();
409
- } else {
410
- if (!function_exists('mg_smtp_last_error')) {
411
- if (!include dirname(__FILE__).'/wp-mail-smtp.php') {
412
- self::deactivate_and_die(dirname(__FILE__).'/wp-mail-smtp.php');
413
- }
414
- }
415
-
416
- $error_msg = mg_smtp_last_error();
417
- }
418
-
419
- if ($result) {
420
- die(
421
- json_encode(
422
- array(
423
- 'message' => __('Success', 'mailgun'),
424
- 'method' => $method,
425
- 'error' => __('Success', 'mailgun'),
426
- )
427
- )
428
- );
429
- } else {
430
- die(
431
- json_encode(
432
- array(
433
- 'message' => __('Failure', 'mailgun'),
434
- 'method' => $method,
435
- 'error' => $error_msg,
436
- )
437
- )
438
- );
439
- }
440
- }
441
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  <?php
2
 
3
+ /*
4
+ * mailgun-wordpress-plugin - Sending mail from Wordpress using Mailgun
5
+ * Copyright (C) 2016 Mailgun, et al.
6
+ *
7
+ * This program is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version.
11
+ *
12
+ * This program is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ * GNU General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU General Public License along
18
+ * with this program; if not, write to the Free Software Foundation, Inc.,
19
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
+ */
21
+
22
+ class MailgunAdmin extends Mailgun
23
+ {
24
+ /**
25
+ * @var array Array of "safe" option defaults.
26
+ */
27
+ private $defaults;
28
+
29
+ /**
30
+ * Setup backend functionality in WordPress.
31
+ *
32
+ * @return void
33
+ *
34
+ * @since 0.1
35
+ */
36
+ public function __construct()
37
+ {
38
+ Mailgun::__construct();
39
+
40
+ // Load localizations if available
41
+ load_plugin_textdomain('mailgun', false, 'mailgun/languages');
42
+
43
+ // Activation hook
44
+ register_activation_hook($this->plugin_file, array(&$this, 'init'));
45
+
46
+ // Hook into admin_init and register settings and potentially register an admin_notice
47
+ add_action('admin_init', array(&$this, 'admin_init'));
48
+
49
+ // Activate the options page
50
+ add_action('admin_menu', array(&$this, 'admin_menu'));
51
+
52
+ // Register an AJAX action for testing mail sending capabilities
53
+ add_action('wp_ajax_mailgun-test', array(&$this, 'ajax_send_test'));
54
+ }
55
+
56
+ /**
57
+ * Initialize the default options during plugin activation.
58
+ *
59
+ * @return void
60
+ *
61
+ * @since 0.1
62
+ */
63
+ public function init()
64
+ {
65
+ $sitename = strtolower($_SERVER[ 'SERVER_NAME' ]);
66
+ if (substr($sitename, 0, 4) == 'www.'):
67
+ $sitename = substr($sitename, 4);
68
+ endif;
69
+
70
+ $region = (defined('MAILGUN_REGION') && MAILGUN_REGION) ? MAILGUN_REGION : $this->get_option('region');
71
+ $regionDefault = $region ?: 'us';
72
+
73
+ $this->defaults = array(
74
+ 'region' => $regionDefault,
75
+ 'useAPI' => '1',
76
+ 'apiKey' => '',
77
+ 'domain' => '',
78
+ 'username' => '',
79
+ 'password' => '',
80
+ 'secure' => '1',
81
+ 'sectype' => 'tls',
82
+ 'track-clicks' => '',
83
+ 'track-opens' => '',
84
+ 'campaign-id' => '',
85
+ 'override-from' => '0',
86
+ 'tag' => $sitename,
87
+ );
88
+ if (!$this->options):
89
+ $this->options = $this->defaults;
90
+ add_option('mailgun', $this->options);
91
+ endif;
92
+ }
93
+
94
+ /**
95
+ * Add the options page.
96
+ *
97
+ * @return void
98
+ *
99
+ * @since 0.1
100
+ */
101
+ public function admin_menu()
102
+ {
103
+ if (current_user_can('manage_options')):
104
+ $this->hook_suffix = add_options_page(__('Mailgun', 'mailgun'), __('Mailgun', 'mailgun'),
105
+ 'manage_options', 'mailgun', array(&$this, 'options_page'));
106
+ add_options_page(__('Mailgun Lists', 'mailgun'), __('Mailgun Lists', 'mailgun'), 'manage_options',
107
+ 'mailgun-lists', array(&$this, 'lists_page'));
108
+ add_action("admin_print_scripts-{$this->hook_suffix}", array(&$this, 'admin_js'));
109
+ add_filter("plugin_action_links_{$this->plugin_basename}", array(&$this, 'filter_plugin_actions'));
110
+ add_action("admin_footer-{$this->hook_suffix}", array(&$this, 'admin_footer_js'));
111
+ endif;
112
+ }
113
+
114
+ /**
115
+ * Enqueue javascript required for the admin settings page.
116
+ *
117
+ * @return void
118
+ *
119
+ * @since 0.1
120
+ */
121
+ public function admin_js()
122
+ {
123
+ wp_enqueue_script('jquery');
124
+ }
125
+
126
+ /**
127
+ * Output JS to footer for enhanced admin page functionality.
128
+ *
129
+ * @since 0.1
130
+ */
131
+ public function admin_footer_js()
132
+ {
133
+ ?>
134
+ <script type="text/javascript">
135
+ /* <![CDATA[ */
136
+ var mailgunApiOrNot = function () {
137
+ if (jQuery('#mailgun-api').val() == 1) {
138
+ jQuery('.mailgun-smtp').hide()
139
+ jQuery('.mailgun-api').show()
140
  } else {
141
+ jQuery('.mailgun-api').hide()
142
+ jQuery('.mailgun-smtp').show()
143
  }
144
 
145
+ }
146
+ var formModified = false
147
+ jQuery().ready(function () {
148
+ mailgunApiOrNot()
149
+ jQuery('#mailgun-api').change(function () {
150
+ mailgunApiOrNot()
151
+ })
152
+ jQuery('#mailgun-test').click(function (e) {
153
+ e.preventDefault()
154
+ if (formModified) {
155
+ var doTest = confirm('<?php _e('The Mailgun plugin configuration has changed since you last saved. Do you wish to test anyway?\n\nClick "Cancel" and then "Save Changes" if you wish to save your changes.',
156
+ 'mailgun'); ?>')
157
+ if (!doTest) {
158
+ return false
159
  }
160
+ }
161
+ jQuery(this).val('<?php _e('Testing...', 'mailgun'); ?>')
162
+ jQuery('#mailgun-test-result').text('')
163
+ jQuery.get(
164
+ ajaxurl,
165
+ {
166
+ action: 'mailgun-test',
167
+ _wpnonce: '<?php echo wp_create_nonce(); ?>'
168
+ }
169
+ )
170
+ .complete(function () {
171
+ jQuery('#mailgun-test').val('<?php _e('Test Configuration', 'mailgun'); ?>')
172
+ })
173
+ .success(function (data) {
174
+ alert(
175
+ 'Mailgun ' + data.method + ' Test ' + data.message
176
+ + '; status "' + data.error + '"'
177
+ )
178
  })
179
+ .error(function () {
180
+ alert('Mailgun Test <?php _e('Failure', 'mailgun'); ?>')
 
 
 
181
  })
182
+ })
183
+ jQuery('#mailgun-form').change(function () {
184
+ formModified = true
185
+ })
186
+ })
187
+ /* ]]> */
188
+ </script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  <?php
190
+ }
191
+
192
+ /**
193
+ * Output the options page.
194
+ *
195
+ * @return void
196
+ *
197
+ * @since 0.1
198
+ */
199
+ public function options_page()
200
+ {
201
+ if (!@include 'options-page.php'):
202
+ printf(__('<div id="message" class="updated fade"><p>The options page for the <strong>Mailgun</strong> plugin cannot be displayed. The file <strong>%s</strong> is missing. Please reinstall the plugin.</p></div>',
203
+ 'mailgun'), dirname(__FILE__) . '/options-page.php');
204
+ endif;
205
+ }
206
+
207
+ /**
208
+ * Output the lists page.
209
+ *
210
+ * @return void
211
+ *
212
+ * @since 0.1
213
+ */
214
+ public function lists_page()
215
+ {
216
+ if (!@include 'lists-page.php'):
217
+ printf(__('<div id="message" class="updated fade"><p>The lists page for the <strong>Mailgun</strong> plugin cannot be displayed. The file <strong>%s</strong> is missing. Please reinstall the plugin.</p></div>',
218
+ 'mailgun'), dirname(__FILE__) . '/lists-page.php');
219
+ endif;
220
+ }
221
+
222
+ /**
223
+ * Wrapper function hooked into admin_init to register settings
224
+ * and potentially register an admin notice if the plugin hasn't
225
+ * been configured yet.
226
+ *
227
+ * @return void
228
+ *
229
+ * @since 0.1
230
+ */
231
+ public function admin_init()
232
+ {
233
+ $this->register_settings();
234
+ $region = $this->get_option('region');
235
+ $apiKey = $this->get_option('apiKey');
236
+ $useAPI = $this->get_option('useAPI');
237
+ $password = $this->get_option('password');
238
+
239
+ add_action('admin_notices', array(&$this, 'admin_notices'));
240
+ }
241
+
242
+ /**
243
+ * Whitelist the mailgun options.
244
+ *
245
+ * @return void
246
+ *
247
+ * @since 0.1
248
+ */
249
+ public function register_settings()
250
+ {
251
+ register_setting('mailgun', 'mailgun', array(&$this, 'validation'));
252
+ }
253
+
254
+ /**
255
+ * Data validation callback function for options.
256
+ *
257
+ * @param array $options An array of options posted from the options page
258
+ *
259
+ * @return array
260
+ *
261
+ * @since 0.1
262
+ */
263
+ public function validation($options)
264
+ {
265
+ $apiKey = trim($options[ 'apiKey' ]);
266
+ $username = trim($options[ 'username' ]);
267
+ if (!empty($apiKey)):
268
+ $pos = strpos($apiKey, 'api:');
269
+ if ($pos !== false && $pos == 0):
270
+ $apiKey = substr($apiKey, 4);
271
+ endif;
272
+
273
+ if (1 === preg_match('(\w{32}-\w{8}-\w{8})', $apiKey)):
274
+ $options[ 'apiKey' ] = $apiKey;
275
+ else:
276
+ $pos = strpos($apiKey, 'key-');
277
+ if ($pos === false || $pos > 4):
278
+ $apiKey = "key-{$apiKey}";
279
+ endif;
280
+
281
+ $options[ 'apiKey' ] = $apiKey;
282
+ endif;
283
+ endif;
284
+
285
+ if (!empty($username)):
286
+ $username = preg_replace('/@.+$/', '', $username);
287
+ $options[ 'username' ] = $username;
288
+ endif;
289
+
290
+ foreach ($options as $key => $value) {
291
+ $options[ $key ] = trim($value);
292
+ }
293
+
294
+ if (empty($options[ 'override-from' ])):
295
+ $options[ 'override-from' ] = $this->defaults[ 'override-from' ];
296
+ endif;
297
+
298
+ if (empty($options[ 'sectype' ])):
299
+ $options[ 'sectype' ] = $this->defaults[ 'sectype' ];
300
+ endif;
301
+
302
+ $this->options = $options;
303
+
304
+ return $options;
305
+ }
306
+
307
+ /**
308
+ * Function to output an admin notice
309
+ * when plugin settings or constants need to be configured
310
+ *
311
+ * @return void
312
+ *
313
+ * @since 0.1
314
+ */
315
+ public function admin_notices()
316
+ {
317
+ $screen = get_current_screen();
318
+ if (!current_user_can('manage_options') || $screen->id == $this->hook_suffix):
319
+ return;
320
+ endif;
321
+
322
+ $smtpPasswordUndefined = ( !$this->get_option('password') && ( !defined('MAILGUN_PASSWORD') || !MAILGUN_PASSWORD ) );
323
+ $smtpActiveNotConfigured = ( $this->get_option('useAPI') === '0' && $smtpPasswordUndefined );
324
+ $apiRegionUndefined = ( !$this->get_option('region') && ( !defined('MAILGUN_REGION') || !MAILGUN_REGION ) );
325
+ $apiKeyUndefined = ( !$this->get_option('apiKey') && ( !defined('MAILGUN_APIKEY') || !MAILGUN_APIKEY ));
326
+ $apiActiveNotConfigured = ( $this->get_option('useAPI') === '1' && ( $apiRegionUndefined || $apiKeyUndefined ) );
327
+
328
+ if ($apiActiveNotConfigured || $smtpActiveNotConfigured):
329
+ ?>
330
+ <div id='mailgun-warning' class='notice notice-warning is-dismissible'>
331
+ <p>
332
+ <?php
333
+ printf(
334
+ __('Mailgun now supports multiple regions! The U.S. region will be used by default, but you can choose the EU region. You can configure your Mailgun settings in your wp-config.php file or <a href="%1$s">here</a>',
335
+ 'mailgun'),
336
+ menu_page_url('mailgun', false)
337
+ );
338
+ ?>
339
+ </p>
340
+ </div>
341
  <?php
342
+ endif;
343
+
344
+ if ($this->get_option('override-from') === '1' &&
345
+ (!$this->get_option('from-name') || !$this->get_option('from-address'))
346
+ ):
347
+ ?>
348
+ <div id='mailgun-warning' class='notice notice-warning is-dismissible'>
349
+ <p>
350
+ <strong>
351
+ <?php _e('Mailgun is almost ready. ', 'mailgun'); ?>
352
+ </strong>
353
+ <?php
354
+ printf(
355
+ __('"Override From" option requires that "From Name" and "From Address" be set to work properly! <a href="%1$s">Configure Mailgun now</a>.',
356
+ 'mailgun'),
357
+ menu_page_url('mailgun', false)
358
+ );
359
+ ?>
360
+ </p>
361
+ </div>
362
+ <?php
363
+ endif;
364
+ }
365
+
366
+ /**
367
+ * Add a settings link to the plugin actions.
368
+ *
369
+ * @param array $links Array of the plugin action links
370
+ *
371
+ * @return array
372
+ *
373
+ * @since 0.1
374
+ */
375
+ public function filter_plugin_actions($links)
376
+ {
377
+ $settings_link = '<a href="' . menu_page_url('mailgun', false) . '">' . __('Settings', 'mailgun') . '</a>';
378
+ array_unshift($links, $settings_link);
379
+
380
+ return $links;
381
+ }
382
+
383
+ /**
384
+ * AJAX callback function to test mail sending functionality.
385
+ *
386
+ * @return string
387
+ *
388
+ * @since 0.1
389
+ */
390
+ public function ajax_send_test()
391
+ {
392
+ nocache_headers();
393
+ header('Content-Type: application/json');
394
+
395
+ if (!current_user_can('manage_options') || !wp_verify_nonce($_GET[ '_wpnonce' ])):
396
+ die(
397
+ json_encode(
398
+ array(
399
+ 'message' => __('Unauthorized', 'mailgun'),
400
+ 'method' => null,
401
+ 'error' => __('Unauthorized', 'mailgun'),
402
+ )
403
+ )
404
+ );
405
+ endif;
406
+
407
+ $getRegion = (defined('MAILGUN_REGION') && MAILGUN_REGION) ? MAILGUN_REGION : $this->get_option('region');
408
+ $useAPI = (defined('MAILGUN_USEAPI') && MAILGUN_USEAPI) ? MAILGUN_USEAPI : $this->get_option('useAPI');
409
+ $secure = (defined('MAILGUN_SECURE') && MAILGUN_SECURE) ? MAILGUN_SECURE : $this->get_option('secure');
410
+ $sectype = (defined('MAILGUN_SECTYPE') && MAILGUN_SECTYPE) ? MAILGUN_SECTYPE : $this->get_option('sectype');
411
+
412
+ if ((bool) !$getRegion):
413
+ mg_api_last_error(__("Region has not been selected", "mailgun"));
414
+ else:
415
+ if ($getRegion === 'us'):
416
+ $region = __("U.S./North America", "mailgun");
417
+ endif;
418
+
419
+ if ($getRegion === "eu"):
420
+ $region = __("Europe", "mailgun");
421
+ endif;
422
+ endif;
423
+
424
+ if ((bool) $useAPI):
425
+ $method = __('HTTP API', 'mailgun');
426
+ else:
427
+ $method = ((bool) $secure) ? __('Secure SMTP', 'mailgun') : __('Insecure SMTP', 'mailgun');
428
+ if ((bool) $secure):
429
+ $method = $method . sprintf(__(' via %s', 'mailgun'), $sectype);
430
+ endif;
431
+ endif;
432
+
433
+ $admin_email = get_option('admin_email');
434
+ $result = wp_mail(
435
+ $admin_email,
436
+ __('Mailgun WordPress Plugin Test', 'mailgun'),
437
+ sprintf(__("This is a test email generated by the Mailgun WordPress plugin.\n\nIf you have received this message, the requested test has succeeded.\n\nThe sending region is set to %s.\n\nThe method used to send this email was: %s.",
438
+ 'mailgun'), $region, $method),
439
+ array('Content-Type: text/plain')
440
+ );
441
+
442
+ if ((bool) $useAPI):
443
+ if (!function_exists('mg_api_last_error')):
444
+ if (!include dirname(__FILE__) . '/wp-mail-api.php'):
445
+ self::deactivate_and_die(dirname(__FILE__) . '/wp-mail-api.php');
446
+ endif;
447
+ endif;
448
+
449
+ $error_msg = mg_api_last_error();
450
+ else:
451
+ if (!function_exists('mg_smtp_last_error')):
452
+ if (!include dirname(__FILE__) . '/wp-mail-smtp.php'):
453
+ self::deactivate_and_die(dirname(__FILE__) . '/wp-mail-smtp.php');
454
+ endif;
455
+ endif;
456
+
457
+ $error_msg = mg_smtp_last_error();
458
+ endif;
459
+
460
+ // Admin Email is used as 'to' parameter, but in case of 'Test Configuration' this message is not clear for the user, so replaced with more appropriate one
461
+ if (false !== strpos($error_msg, "'to'") && false !== strpos($error_msg, 'is not a valid')):
462
+ $error_msg = sprintf(
463
+ "Administration Email Address (%s) is not valid and can't be used for test, you can change it at General Setting page",
464
+ $admin_email
465
+ );
466
+ endif;
467
+
468
+ if ($result):
469
+ die(
470
+ json_encode(
471
+ array(
472
+ 'message' => __('Success', 'mailgun'),
473
+ 'method' => $method,
474
+ 'error' => __('Success', 'mailgun'),
475
+ )
476
+ )
477
+ );
478
+ else:
479
+ // Error message will always be returned in case of failure, if not - connection wasn't successful
480
+ $error_msg = $error_msg ? $error_msg : "Can't connect to Mailgun";
481
+
482
+ die(
483
+ json_encode(
484
+ array(
485
+ 'message' => __('Failure', 'mailgun'),
486
+ 'method' => $method,
487
+ 'error' => $error_msg,
488
+ )
489
+ )
490
+ );
491
+ endif;
492
+ }
493
+ }
includes/lists-page.php CHANGED
@@ -23,15 +23,15 @@ global $mailgun;
23
 
24
  // check mailgun domain & api key
25
  $missing_error = '';
26
- $api_key = $this->get_option('apiKey');
27
- $mailgun_domain = $this->get_option('domain');
28
- if ($api_key != '') {
29
- if ($mailgun_domain == '') {
30
  $missing_error = '<strong style="color:red;">Missing or invalid Mailgun Domain</strong>. ';
31
- }
32
- } else {
33
  $missing_error = '<strong style="color:red;">Missing or invalid API Key</strong>. ';
34
- }
35
 
36
  // import available lists
37
  $lists_arr = $mailgun->get_lists();
@@ -44,7 +44,7 @@ $lists_arr = $mailgun->get_lists();
44
 
45
  <span class="alignright">
46
  <a target="_blank" href="http://www.mailgun.com/">
47
- <img src="https://www.mailgun.com/static/img/mailgun.svg" alt="Mailgun" style="width: 10em;"/>
48
  </a>
49
  </span>
50
 
@@ -85,8 +85,8 @@ $lists_arr = $mailgun->get_lists();
85
  <h3>Multi-list subscription</h3>
86
  <p>
87
  <?php _e('To allow users to subscribe to multiple lists on a single form, comma-separate the Mailgun list ids.', 'mailgun'); ?></p>
88
- <p class="description">
89
- <?php _e('<strong>Example:</strong> [mailgun id="list1@mydomain.com,list2@mydomain.com"]'); ?>
90
  </p>
91
 
92
  <?php endif; ?>
23
 
24
  // check mailgun domain & api key
25
  $missing_error = '';
26
+ $api_key = (defined('MAILGUN_APIKEY') && MAILGUN_APIKEY) ? MAILGUN_APIKEY : $this->get_option('apiKey');
27
+ $mailgun_domain = (defined('MAILGUN_DOMAIN') && MAILGUN_DOMAIN) ? MAILGUN_DOMAIN : $this->get_option('domain');
28
+ if ($api_key != ''):
29
+ if ($mailgun_domain == ''):
30
  $missing_error = '<strong style="color:red;">Missing or invalid Mailgun Domain</strong>. ';
31
+ endif;
32
+ else:
33
  $missing_error = '<strong style="color:red;">Missing or invalid API Key</strong>. ';
34
+ endif;
35
 
36
  // import available lists
37
  $lists_arr = $mailgun->get_lists();
44
 
45
  <span class="alignright">
46
  <a target="_blank" href="http://www.mailgun.com/">
47
+ <img src="https://assets.mailgun.com/img/mailgun.svg" alt="Mailgun" style="width: 10em;"/>
48
  </a>
49
  </span>
50
 
85
  <h3>Multi-list subscription</h3>
86
  <p>
87
  <?php _e('To allow users to subscribe to multiple lists on a single form, comma-separate the Mailgun list ids.', 'mailgun'); ?></p>
88
+ <p>
89
+ <?php _e('<strong>Example:</strong> <code>[mailgun id="list1@mydomain.com,list2@mydomain.com"]</code>'); ?>
90
  </p>
91
 
92
  <?php endif; ?>
includes/mg-filter.php CHANGED
@@ -19,15 +19,16 @@
19
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
  */
21
 
 
22
  /**
23
  * Tries several methods to get the MIME Content-Type of a file.
24
  *
25
- * @param string $filepath
26
- * @param string $default_type If all methods fail, fallback to $default_type
27
  *
28
- * @return string Content-Type
29
  *
30
- * @since 1.5.4
31
  */
32
  function get_mime_content_type($filepath, $default_type = 'text/plain')
33
  {
@@ -58,9 +59,9 @@ function get_mime_content_type($filepath, $default_type = 'text/plain')
58
  * `$from_addr` before being returned. The filtered result is null-tested
59
  * before being returned.
60
  *
61
- * @return string
62
  *
63
- * @since 1.5.8
64
  */
65
  function mg_detect_from_name($from_name_header = null)
66
  {
@@ -123,13 +124,11 @@ function mg_detect_from_name($from_name_header = null)
123
  * might appear to be another option but some hosts may refuse to
124
  * relay mail from an unknown domain.
125
  *
126
- * @link http://trac.wordpress.org/ticket/5007.
127
- *
128
- * @param string $from_addr_header From address given by a header.
129
  *
130
- * @return string
131
  *
132
- * @since 1.5.8
133
  */
134
  function mg_detect_from_address($from_addr_header = null)
135
  {
@@ -194,11 +193,11 @@ function mg_detect_from_address($from_addr_header = null)
194
  * )
195
  * )
196
  *
197
- * @param string|array $headers
198
  *
199
- * @return array
200
  *
201
- * @since 1.5.8
202
  */
203
  function mg_parse_headers($headers = array())
204
  {
@@ -259,11 +258,11 @@ function mg_parse_headers($headers = array())
259
  * Takes a header array in the format produced by mg_parse_headers and
260
  * dumps them down in to a submittable header format.
261
  *
262
- * @param array $headers Headers to dump
263
  *
264
- * @return string String of \r\n separated headers
265
  *
266
- * @since 1.5.8
267
  */
268
  function mg_dump_headers($headers = null)
269
  {
@@ -287,3 +286,40 @@ function mg_dump_headers($headers = null)
287
  return $header_string;
288
  }
289
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
  */
21
 
22
+
23
  /**
24
  * Tries several methods to get the MIME Content-Type of a file.
25
  *
26
+ * @param string $filepath
27
+ * @param string $default_type If all methods fail, fallback to $default_type
28
  *
29
+ * @return string Content-Type
30
  *
31
+ * @since 1.5.4
32
  */
33
  function get_mime_content_type($filepath, $default_type = 'text/plain')
34
  {
59
  * `$from_addr` before being returned. The filtered result is null-tested
60
  * before being returned.
61
  *
62
+ * @return string
63
  *
64
+ * @since 1.5.8
65
  */
66
  function mg_detect_from_name($from_name_header = null)
67
  {
124
  * might appear to be another option but some hosts may refuse to
125
  * relay mail from an unknown domain.
126
  *
127
+ * @link http://trac.wordpress.org/ticket/5007.
 
 
128
  *
129
+ * @return string
130
  *
131
+ * @since 1.5.8
132
  */
133
  function mg_detect_from_address($from_addr_header = null)
134
  {
193
  * )
194
  * )
195
  *
196
+ * @param string|array $headers
197
  *
198
+ * @return array
199
  *
200
+ * @since 1.5.8
201
  */
202
  function mg_parse_headers($headers = array())
203
  {
258
  * Takes a header array in the format produced by mg_parse_headers and
259
  * dumps them down in to a submittable header format.
260
  *
261
+ * @param array $headers Headers to dump
262
  *
263
+ * @return string String of \r\n separated headers
264
  *
265
+ * @since 1.5.8
266
  */
267
  function mg_dump_headers($headers = null)
268
  {
286
  return $header_string;
287
  }
288
 
289
+ /**
290
+ * Set the API endpoint based on the region selected.
291
+ * Value can be "0" if not selected, "us" or "eu"
292
+ *
293
+ * @param string $getRegion Region value set either in config or Mailgun plugin settings.
294
+ *
295
+ * @return bool|string
296
+ *
297
+ * @since 1.5.12
298
+ */
299
+ function mg_api_get_region($getRegion)
300
+ {
301
+ switch ($getRegion) {
302
+ case 'us': return 'https://api.mailgun.net/v3/';
303
+ case 'eu': return 'https://api.eu.mailgun.net/v3/';
304
+ default: return false;
305
+ }
306
+ }
307
+
308
+ /**
309
+ * Set the SMTP endpoint based on the region selected.
310
+ * Value can be "0" if not selected, "us" or "eu"
311
+ *
312
+ * @param string $getRegion Region value set either in config or Mailgun plugin settings.
313
+ *
314
+ * @return bool|string
315
+ *
316
+ * @since 1.5.12
317
+ */
318
+ function mg_smtp_get_region($getRegion)
319
+ {
320
+ switch ($getRegion) {
321
+ case 'us': return 'smtp.mailgun.org';
322
+ case 'eu': return 'smtp.eu.mailgun.org';
323
+ default: return false;
324
+ }
325
+ }
includes/options-page.php CHANGED
@@ -1,201 +1,450 @@
1
  <?php
2
 
3
- /*
4
- * mailgun-wordpress-plugin - Sending mail from Wordpress using Mailgun
5
- * Copyright (C) 2016 Mailgun, et al.
6
- *
7
- * This program is free software; you can redistribute it and/or modify
8
- * it under the terms of the GNU General Public License as published by
9
- * the Free Software Foundation; either version 2 of the License, or
10
- * (at your option) any later version.
11
- *
12
- * This program is distributed in the hope that it will be useful,
13
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- * GNU General Public License for more details.
16
- *
17
- * You should have received a copy of the GNU General Public License along
18
- * with this program; if not, write to the Free Software Foundation, Inc.,
19
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
- */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  ?>
23
- <div class="wrap">
24
- <div id="icon-options-general" class="icon32"><br /></div>
25
- <span class="alignright"><a target="_blank" href="http://www.mailgun.com/"><img src="https://assets.mailgun.com/img/mailgun.svg" alt="Mailgun" style="width: 10em;"/></a></span>
26
- <h2><?php _e('Mailgun', 'mailgun'); ?></h2>
27
- <p>A <a target="_blank" href="http://www.mailgun.com/">Mailgun</a> account is required to use this plugin and the Mailgun service.</p>
28
- <p>If you need to register for an account, you can do so at <a target="_blank" href="http://www.mailgun.com/">http://www.mailgun.com/</a>.</p>
29
- <form id="mailgun-form" action="options.php" method="post">
30
- <?php settings_fields('mailgun'); ?>
31
- <h3><?php _e('Configuration', 'mailgun'); ?></h3>
32
- <table class="form-table">
33
- <tr valign="top">
34
- <th scope="row">
35
- <?php _e('Use HTTP API', 'mailgun'); ?>
36
- </th>
37
- <td>
38
- <select id="mailgun-api" name="mailgun[useAPI]">
39
- <option value="1"<?php selected('1', $this->get_option('useAPI')); ?>><?php _e('Yes', 'mailgun'); ?></option>
40
- <option value="0"<?php selected('0', $this->get_option('useAPI')); ?>><?php _e('No', 'mailgun'); ?></option>
41
- </select>
42
- <p class="description"><?php _e('Set this to "No" if your server cannot make outbound HTTP connections or if emails are not being delivered. "No" will cause this plugin to use SMTP. Default "Yes".', 'mailgun'); ?></p>
43
- </td>
44
- </tr>
45
- <tr valign="top">
46
- <th scope="row">
47
- <?php _e('Mailgun Domain Name', 'mailgun'); ?>
48
- </th>
49
- <td>
50
- <input type="text" class="regular-text" name="mailgun[domain]" value="<?php esc_attr_e($this->get_option('domain')); ?>" placeholder="samples.mailgun.org" />
51
- <p class="description"><?php _e('Your Mailgun Domain Name.', 'mailgun'); ?></p>
52
- </td>
53
- </tr>
54
- <tr valign="top" class="mailgun-api">
55
- <th scope="row">
56
- <?php _e('API Key', 'mailgun'); ?>
57
- </th>
58
- <td>
59
- <input type="text" class="regular-text" name="mailgun[apiKey]" value="<?php esc_attr_e($this->get_option('apiKey')); ?>" placeholder="key-3ax6xnjp29jd6fds4gc373sgvjxteol0" />
60
- <p class="description"><?php _e('Your Mailgun API key, that starts with and includes "key-". Only valid for use with the API.', 'mailgun'); ?></p>
61
- </td>
62
- </tr>
63
- <tr valign="top" class="mailgun-smtp">
64
- <th scope="row">
65
- <?php _e('Username', 'mailgun'); ?>
66
- </th>
67
- <td>
68
- <input type="text" class="regular-text" name="mailgun[username]" value="<?php esc_attr_e($this->get_option('username')); ?>" placeholder="postmaster" />
69
- <p class="description"><?php _e('Your Mailgun SMTP username. Only valid for use with SMTP.', 'mailgun'); ?></p>
70
- </td>
71
- </tr>
72
- <tr valign="top" class="mailgun-smtp">
73
- <th scope="row">
74
- <?php _e('Password', 'mailgun'); ?>
75
- </th>
76
- <td>
77
- <input type="text" class="regular-text" name="mailgun[password]" value="<?php esc_attr_e($this->get_option('password')); ?>" placeholder="my-password" />
78
- <p class="description"><?php _e('Your Mailgun SMTP password that goes with the above username. Only valid for use with SMTP.', 'mailgun'); ?></p>
79
- </td>
80
- </tr>
81
- <tr valign="top" class="mailgun-smtp">
82
- <th scope="row">
83
- <?php _e('Use Secure SMTP', 'mailgun'); ?>
84
- </th>
85
- <td>
86
- <select name="mailgun[secure]">
87
- <option value="1"<?php selected('1', $this->get_option('secure')); ?>><?php _e('Yes', 'mailgun'); ?></option>
88
- <option value="0"<?php selected('0', $this->get_option('secure')); ?>><?php _e('No', 'mailgun'); ?></option>
89
- </select>
90
- <p class="description"><?php _e('Set this to "No" if your server cannot establish SSL SMTP connections or if emails are not being delivered. If you set this to "No" your password will be sent in plain text. Only valid for use with SMTP. Default "Yes".', 'mailgun'); ?></p>
91
- </td>
92
- </tr>
93
- <tr valign="top" class="mailgun-smtp">
94
- <th scope="row">
95
- <?php _e('Security Type', 'mailgun'); ?>
96
- </th>
97
- <td>
98
- <select name="mailgun[sectype]">
99
- <option value="ssl"<?php selected('ssl', $this->get_option('sectype')); ?>>SSL</option>
100
- <option value="tls"<?php selected('tls', $this->get_option('sectype')); ?>>TLS</option>
101
- </select>
102
- <p class="description"><php _e('Leave this at "TLS" unless mail sending fails. This option only matters for Secure SMTP. Default "TLS".', 'mailgun'); ?></p>
103
- </td>
104
- </tr>
105
- <tr valign="top">
106
- <th scope="row">
107
- <?php _e('Click Tracking', 'mailgun'); ?>
108
- </th>
109
- <td>
110
- <select name="mailgun[track-clicks]">
111
- <option value="htmlonly"<?php selected('htmlonly', $this->get_option('track-clicks')); ?>><?php _e('HTML Only', 'mailgun'); ?></option>
112
- <option value="yes"<?php selected('yes', $this->get_option('track-clicks')); ?>><?php _e('Yes', 'mailgun'); ?></option>
113
- <option value="no"<?php selected('no', $this->get_option('track-clicks')); ?>><?php _e('No', 'mailgun'); ?></option>
114
- </select>
115
- <p class="description"><?php _e('If enabled, Mailgun will and track links.', 'mailgun'); ?> <a href="http://documentation.mailgun.com/user_manual.html#tracking-clicks" target="_blank">Click Tracking Documentation</a></p>
116
- </td>
117
- </tr>
118
- <tr valign="top">
119
- <th scope="row">
120
- <?php _e('Open Tracking', 'mailgun'); ?>
121
- </th>
122
- <td>
123
- <select name="mailgun[track-opens]">
124
- <option value="1"<?php selected('1', $this->get_option('track-opens')); ?>><?php _e('Yes', 'mailgun'); ?></option>
125
- <option value="0"<?php selected('0', $this->get_option('track-opens')); ?>><?php _e('No', 'mailgun'); ?></option>
126
- </select>
127
- <p class="description"><?php _e('If enabled, HTML messages will include an open tracking beacon.', 'mailgun'); ?> <a href="http://documentation.mailgun.com/user_manual.html#tracking-opens" target="_blank">Open Tracking Documentation</a></p>
128
- </td>
129
- </tr>
130
- <tr valign="top">
131
- <th scope="row">
132
- <?php _e('From Address', 'mailgun'); ?>
133
- </th>
134
- <td>
135
- <input type="text" class="regular-text" name="mailgun[from-address]" value="<?php esc_attr_e($this->get_option('from-address')); ?>" placeholder="wordpress@mydomain.com" />
136
- <p class="description"><?php _e('The <address@mydomain.com> part of the sender information (<code>"Excited User &lt;user@samples.mailgun.org&gt;"</code>). This address will appear as the `From` address on sent mail. <strong>It is recommended that the @mydomain portion matches your Mailgun sending domain.</strong>', 'mailgun'); ?></p>
137
- </td>
138
- </tr>
139
- <tr valign="top">
140
- <th scope="row">
141
- <?php _e('From Name', 'mailgun'); ?>
142
- </th>
143
- <td>
144
- <input type="text" class="regular-text" name="mailgun[from-name]" value="<?php esc_attr_e($this->get_option('from-name')); ?>" placeholder="WordPress" />
145
- <p class="description"><?php _e('The "User Name" part of the sender information (<code>"Excited User &lt;user@samples.mailgun.org&gt;"</code>).', 'mailgun'); ?></p>
146
- </td>
147
- </tr>
148
- <tr valign="top">
149
- <th scope="row">
150
- <?php _e('Override "From" Details', 'mailgun'); ?>
151
- </th>
152
- <td>
153
- <select name="mailgun[override-from]">
154
- <option value="1"<?php selected('1', $this->get_option('override-from', null, '0')); ?>><?php _e('Yes', 'mailgun'); ?></option>
155
- <option value="0"<?php selected('0', $this->get_option('override-from', null, '0')); ?>><?php _e('No', 'mailgun'); ?></option>
156
- </select>
157
- <p class="description"><?php _e('If enabled, all emails will be sent with the above "From Name" and "From Address", regardless of values set by other plugins. Useful for cases where other plugins don\'t play nice with our "From Name" / "From Address" setting.', 'mailgun'); ?></p>
158
- </td>
159
- </tr>
160
- <tr valign="top">
161
- <th scope="row">
162
- <?php _e('Tag', 'mailgun'); ?>
163
- </th>
164
- <td>
165
- <input type="text" class="regular-text" name="mailgun[campaign-id]" value="<?php esc_attr_e($this->get_option('campaign-id')); ?>" placeholder="tag" />
166
- <p class="description"><?php _e('If added, this tag will exist on every outbound message. Statistics will be populated in the Mailgun Control Panel. Use a comma to define multiple tags.', 'mailgun'); ?> <?php _e('Learn more about', 'mailgun'); ?> <a href="https://documentation.mailgun.com/user_manual.html#tracking-messages" target="_blank">Tracking</a> <?php _e('and', 'mailgun'); ?> <a href="https://documentation.mailgun.com/user_manual.html#tagging" target="_blank">Tagging</a>.</p>
167
- </td>
168
- </tr>
169
- </table>
170
- <h3><?php _e('Lists', 'mailgun'); ?></h3>
171
- <table class="form-table">
172
- <tr valign="top">
173
- <th scope="row">
174
- <?php _e('Shortcode', 'mailgun'); ?>
175
- </th>
176
- <td>
177
- <div>
178
- <strong>[mailgun id="<em>{mailgun list id}</em>" collect_name="true"]</strong>
179
- </div>
180
- <div>
181
- <p class="description"><?php _e('Use the shortcode above to associate a widget instance with a mailgun list', 'mailgun'); ?></p>
182
- </div>
183
- </td>
184
- </tr>
185
- <tr valign="top">
186
- <th scope="row">
187
- <?php _e('Lists', 'mailgun'); ?>
188
- </th>
189
- <td>
190
- <a href="?page=mailgun-lists">View available lists</a>
191
- </td>
192
- </tr>
193
- </table>
194
-
195
- <p><?php _e('Before attempting to test the configuration, please click "Save Changes".', 'mailgun'); ?></p>
196
- <p class="submit">
197
- <input type="submit" class="button-primary" value="<?php _e('Save Changes', 'mailgun'); ?>" />
198
- <input type="button" id="mailgun-test" class="button-secondary" value="<?php _e('Test Configuration', 'mailgun'); ?>" />
199
- </p>
200
- </form>
201
- </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  <?php
2
 
3
+ /*
4
+ * mailgun-wordpress-plugin - Sending mail from Wordpress using Mailgun
5
+ * Copyright (C) 2016 Mailgun, et al.
6
+ *
7
+ * This program is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version.
11
+ *
12
+ * This program is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ * GNU General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU General Public License along
18
+ * with this program; if not, write to the Free Software Foundation, Inc.,
19
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
+ */
21
+
22
+ $mailgun_domain_const = ((defined('MAILGUN_DOMAIN') && MAILGUN_DOMAIN) ? MAILGUN_DOMAIN : null);
23
+ $mailgun_domain = $mailgun_domain_const ? $mailgun_domain_const : $this->get_option('domain');
24
+
25
+ $mailgun_region_const = ((defined('MAILGUN_REGION') && MAILGUN_REGION) ? MAILGUN_REGION : null);
26
+ $mailgun_region = $mailgun_region_const ? $mailgun_region_const : $this->get_option('region');
27
+
28
+ $mailgun_api_key_const = ((defined('MAILGUN_APIKEY') && MAILGUN_APIKEY) ? MAILGUN_APIKEY : null);
29
+ $mailgun_api_key = $mailgun_api_key_const ? $mailgun_api_key_const : $this->get_option('apiKey');
30
+
31
+ $mailgun_username_const = ((defined('MAILGUN_USERNAME') && MAILGUN_USERNAME) ? MAILGUN_USERNAME : null);
32
+ $mailgun_username = $mailgun_username_const ? $mailgun_username_const : $this->get_option('username');
33
+
34
+ $mailgun_password_const = ((defined('MAILGUN_PASSWORD') && MAILGUN_PASSWORD) ? MAILGUN_PASSWORD : null);
35
+ $mailgun_password = $mailgun_password_const ? $mailgun_password_const : $this->get_option('password');
36
+
37
+ $mailgun_sectype_const = ((defined('MAILGUN_SECTYPE') && MAILGUN_SECTYPE) ? MAILGUN_SECTYPE : null);
38
+ $mailgun_sectype = $mailgun_sectype_const ? $mailgun_sectype_const : $this->get_option('sectype');
39
+
40
+ $mailgun_from_name_const = ((defined('MAILGUN_FROM_NAME') && MAILGUN_FROM_NAME) ? MAILGUN_FROM_NAME : null);
41
+ $mailgun_from_name = $mailgun_from_name_const ? $mailgun_from_name_const : $this->get_option('from-name');
42
+
43
+ $mailgun_from_address_const = ((defined('MAILGUN_FROM_ADDRESS') && MAILGUN_FROM_ADDRESS) ? MAILGUN_FROM_ADDRESS : null);
44
+ $mailgun_from_address = $mailgun_from_address_const ? $mailgun_from_address_const : $this->get_option('from-address');
45
+
46
+ $mailgun_secure_const = (defined('MAILGUN_SECURE') ? MAILGUN_SECURE : null);
47
+ $mailgun_secure = !is_null($mailgun_secure_const) ? ((string)(1 * $mailgun_secure_const)) : $this->get_option('secure');
48
+
49
+ $mailgun_use_api_const = (defined('MAILGUN_USEAPI') ? MAILGUN_USEAPI : null);
50
+ $mailgun_use_api = !is_null($mailgun_use_api_const) ? ((string)(1 * $mailgun_use_api_const)) : $this->get_option('useAPI');
51
+
52
 
53
  ?>
54
+ <div class="wrap">
55
+ <div id="icon-options-general" class="icon32"><br/></div>
56
+ <span class="alignright">
57
+ <a target="_blank" href="http://www.mailgun.com/">
58
+ <img src="https://assets.mailgun.com/img/mailgun.svg" alt="Mailgun" style="width:10em;"/>
59
+ </a>
60
+ </span>
61
+ <h2><?php _e('Mailgun', 'mailgun'); ?></h2>
62
+
63
+ <p>
64
+ <?php
65
+ $url = 'https://www.mailgun.com';
66
+ $link = sprintf(
67
+ wp_kses(
68
+ __('A <a href="%1$s" target="%2$s">Mailgun</a> account is required to use this plugin and the Mailgun service.', 'mailgun'),
69
+ array('a' => array(
70
+ 'href' => array(),
71
+ 'target' => array()
72
+ )
73
+ )
74
+ ), esc_url($url), '_blank'
75
+ );
76
+ echo $link;
77
+ ?>
78
+ </p>
79
+
80
+ <p>
81
+ <?php
82
+ $url = 'https://signup.mailgun.com/new/signup';
83
+ $link = sprintf(
84
+ wp_kses(
85
+ __('If you need to register for an account, you can do so at <a href="%1$s" target="%2$s">Mailgun.com</a>.', 'mailgun'),
86
+ array('a' => array(
87
+ 'href' => array(),
88
+ 'target' => array()
89
+ )
90
+ )
91
+ ), esc_url($url), '_blank'
92
+ );
93
+ echo $link;
94
+ ?>
95
+ </p>
96
+
97
+ <h3><?php _e('Configuration', 'mailgun'); ?></h3>
98
+ <form id="mailgun-form" action="options.php" method="post">
99
+ <?php settings_fields('mailgun'); ?>
100
+
101
+ <table class="form-table">
102
+ <tr valign="top">
103
+ <th scope="row">
104
+ <?php _e('Select Your Region', 'mailgun'); ?>
105
+ </th>
106
+ <td>
107
+ <?php if ($mailgun_region_const): ?>
108
+ <input type="hidden" name="mailgun[region]" value="<?php echo $mailgun_region ?>">
109
+ <?php endif ?>
110
+
111
+ <select id="mailgun-region" name="mailgun[region]" <?php echo $mailgun_region_const ? 'disabled="disabled"' : '' ?>>
112
+ <option value="us"<?php selected('us', $mailgun_region); ?>><?php _e('U.S./North America', 'mailgun') ?></option>
113
+ <option value="eu"<?php selected('eu', $mailgun_region); ?>><?php _e('Europe', 'mailgun') ?></option>
114
+ </select>
115
+ <p class="description">
116
+ <?php
117
+ _e('Choose a region - U.S./North America or Europe - from which to send email, and to store your customer data. Please note that your sending domain must be set up in whichever region you choose.', 'mailgun');
118
+ ?>
119
+ </p>
120
+ </td>
121
+ </tr>
122
+ <tr valign="top">
123
+ <th scope="row">
124
+ <?php _e('Use HTTP API', 'mailgun'); ?>
125
+ </th>
126
+ <td>
127
+ <?php if (!is_null($mailgun_use_api_const)): ?>
128
+ <input type="hidden" name="mailgun[useAPI]" value="<?php echo $mailgun_use_api ?>">
129
+ <?php endif ?>
130
+
131
+ <select id="mailgun-api" name="mailgun[useAPI]" <?php echo !is_null($mailgun_use_api_const) ? 'disabled="disabled"' : '' ?>>
132
+ <option value="1"<?php selected('1', $mailgun_use_api); ?>><?php _e('Yes', 'mailgun'); ?></option>
133
+ <option value="0"<?php selected('0', $mailgun_use_api); ?>><?php _e('No', 'mailgun'); ?></option>
134
+ </select>
135
+ <p class="description">
136
+ <?php
137
+ _e('Set this to "No" if your server cannot make outbound HTTP connections or if emails are not being delivered. "No" will cause this plugin to use SMTP. Default "Yes".', 'mailgun');
138
+ ?>
139
+ </p>
140
+ </td>
141
+ </tr>
142
+ <tr valign="top">
143
+ <th scope="row">
144
+ <?php _e('Mailgun Domain Name', 'mailgun'); ?>
145
+ </th>
146
+ <td>
147
+ <input type="text" class="regular-text"
148
+ name="mailgun[domain]"
149
+ value="<?php esc_attr_e($mailgun_domain); ?>"
150
+ placeholder="samples.mailgun.org"
151
+ <?php echo $mailgun_domain_const ? 'readonly="readonly"' : '' ?>
152
+ />
153
+ <p class="description">
154
+ <?php _e('Your Mailgun Domain Name.', 'mailgun'); ?>
155
+ </p>
156
+ </td>
157
+ </tr>
158
+ <tr valign="top" class="mailgun-api">
159
+ <th scope="row">
160
+ <?php _e('API Key', 'mailgun'); ?>
161
+ </th>
162
+ <td>
163
+ <input type="text" class="regular-text" name="mailgun[apiKey]"
164
+ value="<?php esc_attr_e($mailgun_api_key); ?>"
165
+ placeholder="key-3ax6xnjp29jd6fds4gc373sgvjxteol0"
166
+ <?php echo $mailgun_api_key_const ? 'readonly="readonly"' : '' ?>
167
+ />
168
+ <p class="description">
169
+ <?php
170
+ _e('Your Mailgun API key. Only valid for use with the API.', 'mailgun');
171
+ ?>
172
+ </p>
173
+ </td>
174
+ </tr>
175
+ <tr valign="top" class="mailgun-smtp">
176
+ <th scope="row">
177
+ <?php _e('Username', 'mailgun'); ?>
178
+ </th>
179
+ <td>
180
+ <input type="text" class="regular-text"
181
+ name="mailgun[username]"
182
+ value="<?php esc_attr_e($mailgun_username); ?>"
183
+ placeholder="postmaster"
184
+ <?php echo $mailgun_username_const ? 'readonly="readonly"' : '' ?>
185
+ />
186
+ <p class="description">
187
+ <?php
188
+ _e('Your Mailgun SMTP username. Only valid for use with SMTP.', 'mailgun');
189
+ ?>
190
+ </p>
191
+ </td>
192
+ </tr>
193
+ <tr valign="top" class="mailgun-smtp">
194
+ <th scope="row">
195
+ <?php _e('Password', 'mailgun'); ?>
196
+ </th>
197
+ <td>
198
+ <input type="text" class="regular-text"
199
+ name="mailgun[password]"
200
+ value="<?php esc_attr_e($mailgun_password); ?>"
201
+ placeholder="my-password"
202
+ <?php echo $mailgun_password_const ? 'readonly="readonly"' : '' ?>
203
+ />
204
+ <p class="description">
205
+ <?php
206
+ _e('Your Mailgun SMTP password that goes with the above username. Only valid for use with SMTP.', 'mailgun');
207
+ ?>
208
+ </p>
209
+ </td>
210
+ </tr>
211
+ <tr valign="top" class="mailgun-smtp">
212
+ <th scope="row">
213
+ <?php _e('Use Secure SMTP', 'mailgun'); ?>
214
+ </th>
215
+ <td>
216
+ <?php if (!is_null($mailgun_secure_const)): ?>
217
+ <input type="hidden" name="mailgun[secure]" value="<?php echo $mailgun_secure ?>">
218
+ <?php endif ?>
219
+
220
+ <select name="mailgun[secure]" <?php echo !is_null($mailgun_secure_const) ? 'disabled="disabled"' : '' ?>>
221
+ <option value="1"<?php selected('1', $mailgun_secure); ?>><?php _e('Yes', 'mailgun'); ?></option>
222
+ <option value="0"<?php selected('0', $mailgun_secure); ?>><?php _e('No', 'mailgun'); ?></option>
223
+ </select>
224
+ <p class="description">
225
+ <?php
226
+ _e('Set this to "No" if your server cannot establish SSL SMTP connections or if emails are not being delivered. If you set this to "No" your password will be sent in plain text. Only valid for use with SMTP. Default "Yes".', 'mailgun');
227
+ ?>
228
+ </p>
229
+ </td>
230
+ </tr>
231
+ <tr valign="top" class="mailgun-smtp">
232
+ <th scope="row">
233
+ <?php _e('Security Type', 'mailgun'); ?>
234
+ </th>
235
+ <td>
236
+ <?php if ($mailgun_sectype_const): ?>
237
+ <input type="hidden" name="mailgun[sectype]" value="<?php echo $mailgun_sectype ?>">
238
+ <?php endif ?>
239
+
240
+ <select name="mailgun[sectype]" <?php echo $mailgun_sectype_const ? 'disabled="disabled"' : '' ?>>
241
+ <option value="ssl"<?php selected('ssl', $mailgun_sectype); ?>>SSL</option>
242
+ <option value="tls"<?php selected('tls', $mailgun_sectype); ?>>TLS</option>
243
+ </select>
244
+ <p class="description">
245
+ <?php
246
+ _e('Leave this at "TLS" unless mail sending fails. This option only matters for Secure SMTP. Default "TLS".', 'mailgun');
247
+ ?>
248
+ </p>
249
+ </td>
250
+ </tr>
251
+ <tr valign="top">
252
+ <th scope="row">
253
+ <?php _e('Click Tracking', 'mailgun'); ?>
254
+ </th>
255
+ <td>
256
+ <select name="mailgun[track-clicks]">
257
+ <option value="htmlonly"<?php selected('htmlonly', $this->get_option('track-clicks')); ?>><?php _e('HTML Only', 'mailgun'); ?></option>
258
+ <option value="yes"<?php selected('yes', $this->get_option('track-clicks')); ?>><?php _e('Yes', 'mailgun'); ?></option>
259
+ <option value="no"<?php selected('no', $this->get_option('track-clicks')); ?>><?php _e('No', 'mailgun'); ?></option>
260
+ </select>
261
+ <p class="description">
262
+ <?php
263
+ $url = 'http://documentation.mailgun.com/user_manual.html#tracking-clicks';
264
+ $link = sprintf(
265
+ wp_kses(
266
+ __('If enabled, Mailgun will track links. <a href="%1$s" target="%2$s">Open Tracking Documentation</a>.', 'mailgun'),
267
+ array('a' => array(
268
+ 'href' => array(),
269
+ 'target' => array()
270
+ )
271
+ )
272
+ ), esc_url($url), '_blank'
273
+ );
274
+ echo $link;
275
+ ?>
276
+ </p>
277
+ </td>
278
+ </tr>
279
+ <tr valign="top">
280
+ <th scope="row">
281
+ <?php _e('Open Tracking', 'mailgun'); ?>
282
+ </th>
283
+ <td>
284
+ <select name="mailgun[track-opens]">
285
+ <option value="1"<?php selected('1', $this->get_option('track-opens')); ?>><?php _e('Yes', 'mailgun'); ?></option>
286
+ <option value="0"<?php selected('0', $this->get_option('track-opens')); ?>><?php _e('No', 'mailgun'); ?></option>
287
+ </select>
288
+ <p class="description">
289
+ <?php
290
+ $url = 'http://documentation.mailgun.com/user_manual.html#tracking-opens';
291
+ $link = sprintf(
292
+ wp_kses(
293
+ __('If enabled, HTML messages will include an open tracking beacon. <a href="%1$s" target="%2$s">Open Tracking Documentation</a>.', 'mailgun'),
294
+ array('a' => array(
295
+ 'href' => array(),
296
+ 'target' => array()
297
+ )
298
+ )
299
+ ), esc_url($url), '_blank'
300
+ );
301
+ echo $link;
302
+ ?>
303
+ </p>
304
+ </td>
305
+ </tr>
306
+ <tr valign="top">
307
+ <th scope="row">
308
+ <?php _e('From Address', 'mailgun'); ?>
309
+ </th>
310
+ <td>
311
+ <input type="text"
312
+ class="regular-text"
313
+ name="mailgun[from-address]"
314
+ value="<?php esc_attr_e($mailgun_from_address); ?>"
315
+ placeholder="wordpress@mydomain.com"
316
+ <?php echo $mailgun_from_address_const ? 'readonly="readonly"' : '' ?>
317
+ />
318
+ <p class="description">
319
+ <?php
320
+ _e('The &lt;address@mydomain.com&gt; part of the sender information (<code>"Excited User &lt;user@samples.mailgun.org&gt;"</code>). This address will appear as the `From` address on sent mail. <strong>It is recommended that the @mydomain portion matches your Mailgun sending domain.</strong>', 'mailgun');
321
+ ?>
322
+ </p>
323
+ </td>
324
+ </tr>
325
+ <tr valign="top">
326
+ <th scope="row">
327
+ <?php _e('From Name', 'mailgun'); ?>
328
+ </th>
329
+ <td>
330
+ <input type="text" class="regular-text"
331
+ name="mailgun[from-name]"
332
+ value="<?php esc_attr_e($mailgun_from_name); ?>"
333
+ placeholder="WordPress"
334
+ <?php echo $mailgun_from_name_const ? 'readonly="readonly"' : '' ?>
335
+ />
336
+ <p class="description">
337
+ <?php
338
+ _e('The "User Name" part of the sender information (<code>"Excited User &lt;user@samples.mailgun.org&gt;"</code>).', 'mailgun');
339
+ ?>
340
+ </p>
341
+ </td>
342
+ </tr>
343
+ <tr valign="top">
344
+ <th scope="row">
345
+ <?php _e('Override "From" Details', 'mailgun'); ?>
346
+ </th>
347
+ <td>
348
+ <select name="mailgun[override-from]">
349
+ <option value="1"<?php selected('1', $this->get_option('override-from', null, '0')); ?>><?php _e('Yes', 'mailgun'); ?></option>
350
+ <option value="0"<?php selected('0', $this->get_option('override-from', null, '0')); ?>><?php _e('No', 'mailgun'); ?></option>
351
+ </select>
352
+ <p class="description">
353
+ <?php
354
+ _e('If enabled, all emails will be sent with the above "From Name" and "From Address", regardless of values set by other plugins. Useful for cases where other plugins don\'t play nice with our "From Name" / "From Address" setting.', 'mailgun');
355
+ ?>
356
+ </p>
357
+ </td>
358
+ </tr>
359
+ <tr valign="top">
360
+ <th scope="row">
361
+ <?php _e('Tag', 'mailgun'); ?>
362
+ </th>
363
+ <td>
364
+ <input type="text" class="regular-text"
365
+ name="mailgun[campaign-id]"
366
+ value="<?php esc_attr_e($this->get_option('campaign-id')); ?>"
367
+ placeholder="tag"
368
+ />
369
+ <p class="description">
370
+ <?php
371
+ _e('If added, this tag will exist on every outbound message. Statistics will be populated in the Mailgun Control Panel. Use a comma to define multiple tags. ', 'mailgun');
372
+ _e('Learn more about', 'mailgun');
373
+
374
+ $url1 = 'https://documentation.mailgun.com/user_manual.html#tracking-messages';
375
+ $url2 = 'https://documentation.mailgun.com/user_manual.html#tagging';
376
+ $link = sprintf(
377
+ wp_kses(
378
+ __('<a href="%1$s" target="%3$s">Tracking</a> and <a href="%2$s" target="%3$s">Tagging</a>', 'mailgun'),
379
+ array('a' => array(
380
+ 'href' => array(),
381
+ 'target' => array()
382
+ )
383
+ )
384
+ ), esc_url($url1), esc_url($url2), '_blank'
385
+ );
386
+ echo $link;
387
+ ?>
388
+ </p>
389
+ </td>
390
+ </tr>
391
+ </table>
392
+ <h3><?php _e('Lists', 'mailgun'); ?></h3>
393
+ <table class="form-table">
394
+ <tr valign="top">
395
+ <th scope="row">
396
+ <?php _e('Shortcode', 'mailgun'); ?>
397
+ </th>
398
+ <td>
399
+ <div>
400
+ <code>[mailgun id="<em>{mailgun list id}</em>" collect_name="true"]</code>
401
+ </div>
402
+ <div>
403
+ <p class="description">
404
+ <?php
405
+ _e('Use the shortcode above to associate a widget instance with a mailgun list', 'mailgun');
406
+ ?>
407
+ </p>
408
+ </div>
409
+ </td>
410
+ </tr>
411
+ <tr valign="top">
412
+ <th scope="row">
413
+ <?php _e('Lists', 'mailgun'); ?>
414
+ </th>
415
+ <td>
416
+ <?php
417
+ $url = '?page=mailgun-lists';
418
+
419
+ $link = sprintf(
420
+ wp_kses(
421
+ __('<a href="%1$s" target="%2$s">View available lists</a>.', 'mailgun'),
422
+ array('a' => array(
423
+ 'href' => array(),
424
+ )
425
+ )
426
+ ), esc_url($url)
427
+ );
428
+ echo $link;
429
+ ?>
430
+ </td>
431
+ </tr>
432
+ </table>
433
+ <p>
434
+ <?php
435
+ _e('Before attempting to test the configuration, please click "Save Changes".', 'mailgun');
436
+ ?>
437
+ </p>
438
+ <p class="submit">
439
+ <input type="submit"
440
+ class="button-primary"
441
+ value="<?php _e('Save Changes', 'mailgun'); ?>"
442
+ />
443
+ <input type="button"
444
+ id="mailgun-test"
445
+ class="button-secondary"
446
+ value="<?php _e('Test Configuration', 'mailgun'); ?>"
447
+ />
448
+ </p>
449
+ </form>
450
+ </div>
includes/wp-mail-api.php CHANGED
@@ -28,11 +28,11 @@ if (!include dirname(__FILE__).'/mg-filter.php') {
28
  * mg_api_last_error is a compound getter/setter for the last error that was
29
  * encountered during a Mailgun API call.
30
  *
31
- * @param string $error OPTIONAL
32
  *
33
- * @return string Last error that occurred.
34
  *
35
- * @since 1.5.0
36
  */
37
  function mg_api_last_error($error = null)
38
  {
@@ -98,22 +98,26 @@ function mg_mutate_to_rcpt_vars_cb($to_addrs)
98
  * Based off of the core wp_mail function, but with modifications required to
99
  * send email using the Mailgun HTTP API
100
  *
101
- * @param string|array $to Array or comma-separated list of email addresses to send message.
102
- * @param string $subject Email subject
103
- * @param string $message Message contents
104
- * @param string|array $headers Optional. Additional headers.
105
- * @param string|array $attachments Optional. Files to attach.
106
  *
107
- * @return bool Whether the email contents were sent successfully.
108
  *
109
- * @since 0.1
 
 
110
  */
 
111
  function wp_mail($to, $subject, $message, $headers = '', $attachments = array())
112
  {
113
  // Compact the input, apply the filters, and extract them back out
114
  extract(apply_filters('wp_mail', compact('to', 'subject', 'message', 'headers', 'attachments')));
115
 
116
  $mailgun = get_option('mailgun');
 
117
  $apiKey = (defined('MAILGUN_APIKEY') && MAILGUN_APIKEY) ? MAILGUN_APIKEY : $mailgun['apiKey'];
118
  $domain = (defined('MAILGUN_DOMAIN') && MAILGUN_DOMAIN) ? MAILGUN_DOMAIN : $mailgun['domain'];
119
 
@@ -121,6 +125,12 @@ function wp_mail($to, $subject, $message, $headers = '', $attachments = array())
121
  return false;
122
  }
123
 
 
 
 
 
 
 
124
  if (!is_array($attachments)) {
125
  $attachments = explode("\n", str_replace("\r\n", "\n", $attachments));
126
  }
@@ -289,6 +299,39 @@ function wp_mail($to, $subject, $message, $headers = '', $attachments = array())
289
  $body['html'] = $message;
290
  }
291
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
292
  // If we don't have a charset from the input headers
293
  if (!isset($charset)) {
294
  $charset = get_bloginfo('charset');
@@ -341,7 +384,7 @@ function wp_mail($to, $subject, $message, $headers = '', $attachments = array())
341
  // // }
342
  // }
343
 
344
- // Allow other plugins to apply attachent changes before writing to the payload.
345
  $attachments = apply_filters('mg_mutate_attachments', $attachments);
346
  if ( ($attachment_payload = mg_build_attachments_payload($attachments, $boundary)) != null ) {
347
  $payload .= $attachment_payload;
@@ -357,7 +400,9 @@ function wp_mail($to, $subject, $message, $headers = '', $attachments = array())
357
  ),
358
  );
359
 
360
- $url = "https://api.mailgun.net/v3/{$domain}/messages";
 
 
361
 
362
  // TODO: Mailgun only supports 1000 recipients per request, since we are
363
  // overriding this function, let's add looping here to handle that
@@ -375,7 +420,7 @@ function wp_mail($to, $subject, $message, $headers = '', $attachments = array())
375
  // Mailgun API should *always* return a `message` field, even when
376
  // $response_code != 200, so a lack of `message` indicates something
377
  // is broken.
378
- if ((int) $response_code != 200 && !isset($response_body->message)) {
379
  // Store response code and HTTP response message in last error.
380
  $response_message = wp_remote_retrieve_response_message($response);
381
  $errmsg = "$response_code - $response_message";
@@ -393,6 +438,7 @@ function wp_mail($to, $subject, $message, $headers = '', $attachments = array())
393
 
394
  return true;
395
  }
 
396
 
397
  function mg_build_payload_from_body($body, $boundary) {
398
  $payload = '';
28
  * mg_api_last_error is a compound getter/setter for the last error that was
29
  * encountered during a Mailgun API call.
30
  *
31
+ * @param string $error OPTIONAL
32
  *
33
+ * @return string Last error that occurred.
34
  *
35
+ * @since 1.5.0
36
  */
37
  function mg_api_last_error($error = null)
38
  {
98
  * Based off of the core wp_mail function, but with modifications required to
99
  * send email using the Mailgun HTTP API
100
  *
101
+ * @param string|array $to Array or comma-separated list of email addresses to send message.
102
+ * @param string $subject Email subject
103
+ * @param string $message Message contents
104
+ * @param string|array $headers Optional. Additional headers.
105
+ * @param string|array $attachments Optional. Files to attach.
106
  *
107
+ * @return bool Whether the email contents were sent successfully.
108
  *
109
+ * @global PHPMailer\PHPMailer\PHPMailer $phpmailer
110
+ *
111
+ * @since 0.1
112
  */
113
+ if (!function_exists('wp_mail')) {
114
  function wp_mail($to, $subject, $message, $headers = '', $attachments = array())
115
  {
116
  // Compact the input, apply the filters, and extract them back out
117
  extract(apply_filters('wp_mail', compact('to', 'subject', 'message', 'headers', 'attachments')));
118
 
119
  $mailgun = get_option('mailgun');
120
+ $region = (defined('MAILGUN_REGION') && MAILGUN_REGION) ? MAILGUN_REGION : $mailgun['region'];
121
  $apiKey = (defined('MAILGUN_APIKEY') && MAILGUN_APIKEY) ? MAILGUN_APIKEY : $mailgun['apiKey'];
122
  $domain = (defined('MAILGUN_DOMAIN') && MAILGUN_DOMAIN) ? MAILGUN_DOMAIN : $mailgun['domain'];
123
 
125
  return false;
126
  }
127
 
128
+ // If a region is not set via defines or through the options page, default to US region.
129
+ if (!((bool) $region)) {
130
+ error_log('[Mailgun] No region configuration was found! Defaulting to US region.');
131
+ $region = 'us';
132
+ }
133
+
134
  if (!is_array($attachments)) {
135
  $attachments = explode("\n", str_replace("\r\n", "\n", $attachments));
136
  }
299
  $body['html'] = $message;
300
  }
301
 
302
+ // Some plugins, such as WooCommerce (@see WC_Email::handle_multipart()), to handle multipart/alternative with html
303
+ // and plaintext messages hooks into phpmailer_init action to override AltBody property directly in $phpmailer,
304
+ // so we should allow them to do this, and then get overridden plain text body from $phpmailer.
305
+ // Partly, this logic is taken from original wp_mail function.
306
+ if (false !== stripos($content_type, 'multipart')) {
307
+ global $phpmailer;
308
+
309
+ // (Re)create it, if it's gone missing.
310
+ if (!($phpmailer instanceof PHPMailer\PHPMailer\PHPMailer)) {
311
+ require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php';
312
+ require_once ABSPATH . WPINC . '/PHPMailer/SMTP.php';
313
+ require_once ABSPATH . WPINC . '/PHPMailer/Exception.php';
314
+ $phpmailer = new PHPMailer\PHPMailer\PHPMailer(true);
315
+
316
+ $phpmailer::$validator = static function ($email) {
317
+ return (bool)is_email($email);
318
+ };
319
+ }
320
+
321
+ /**
322
+ * Fires after PHPMailer is initialized.
323
+ *
324
+ * @param PHPMailer $phpmailer The PHPMailer instance (passed by reference).
325
+ */
326
+ do_action_ref_array('phpmailer_init', array(&$phpmailer));
327
+
328
+ $plainTextMessage = $phpmailer->AltBody;
329
+
330
+ if ($plainTextMessage) {
331
+ $body['text'] = $plainTextMessage;
332
+ }
333
+ }
334
+
335
  // If we don't have a charset from the input headers
336
  if (!isset($charset)) {
337
  $charset = get_bloginfo('charset');
384
  // // }
385
  // }
386
 
387
+ // Allow other plugins to apply attachment changes before writing to the payload.
388
  $attachments = apply_filters('mg_mutate_attachments', $attachments);
389
  if ( ($attachment_payload = mg_build_attachments_payload($attachments, $boundary)) != null ) {
390
  $payload .= $attachment_payload;
400
  ),
401
  );
402
 
403
+ $endpoint = mg_api_get_region($region);
404
+ $endpoint = ($endpoint) ? $endpoint : 'https://api.mailgun.net/v3/';
405
+ $url = $endpoint."{$domain}/messages";
406
 
407
  // TODO: Mailgun only supports 1000 recipients per request, since we are
408
  // overriding this function, let's add looping here to handle that
420
  // Mailgun API should *always* return a `message` field, even when
421
  // $response_code != 200, so a lack of `message` indicates something
422
  // is broken.
423
+ if ((int) $response_code != 200 || !isset($response_body->message)) {
424
  // Store response code and HTTP response message in last error.
425
  $response_message = wp_remote_retrieve_response_message($response);
426
  $errmsg = "$response_code - $response_message";
438
 
439
  return true;
440
  }
441
+ }
442
 
443
  function mg_build_payload_from_body($body, $boundary) {
444
  $payload = '';
languages/mailgun-template.po CHANGED
@@ -61,10 +61,6 @@ msgstr ""
61
  msgid "Mailgun is almost ready. "
62
  msgstr ""
63
 
64
- #: includes/admin.php:301
65
- msgid "You must <a href=\"%1$s\">configure Mailgun</a> for it to work."
66
- msgstr ""
67
-
68
  #: includes/admin.php:309
69
  msgid ""
70
  "\"Override From\" option requires that \"From Name\" and \"From Address\" be "
61
  msgid "Mailgun is almost ready. "
62
  msgstr ""
63
 
 
 
 
 
64
  #: includes/admin.php:309
65
  msgid ""
66
  "\"Override From\" option requires that \"From Name\" and \"From Address\" be "
mailgun.php CHANGED
@@ -1,457 +1,486 @@
1
  <?php
2
 
3
- /**
4
- * Plugin Name: Mailgun
5
- * Plugin URI: http://wordpress.org/extend/plugins/mailgun/
6
- * Description: Mailgun integration for WordPress
7
- * Version: 1.5.10
8
- * Author: Mailgun
9
- * Author URI: http://www.mailgun.com/
10
- * License: GPLv2 or later
11
- * Text Domain: mailgun
12
- * Domain Path: /languages/.
13
- */
14
-
15
- /*
16
- * mailgun-wordpress-plugin - Sending mail from Wordpress using Mailgun
17
- * Copyright (C) 2016 Mailgun, et al.
18
- *
19
- * This program is free software; you can redistribute it and/or modify
20
- * it under the terms of the GNU General Public License as published by
21
- * the Free Software Foundation; either version 2 of the License, or
22
- * (at your option) any later version.
23
- *
24
- * This program is distributed in the hope that it will be useful,
25
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
- * GNU General Public License for more details.
28
- *
29
- * You should have received a copy of the GNU General Public License along
30
- * with this program; if not, write to the Free Software Foundation, Inc.,
31
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32
- */
33
-
34
- /**
35
- * Entrypoint for the Mailgun plugin. Sets up the mailing "strategy" -
36
- * either API or SMTP.
37
- *
38
- * Registers handlers for later actions and sets up config variables with
39
- * Wordpress.
40
- */
41
- class Mailgun
42
- {
43
- /**
44
- * Setup shared functionality for Admin and Front End.
45
- *
46
- * @return none
47
- *
48
- * @since 0.1
49
- */
50
- public function __construct()
51
- {
52
- $this->options = get_option('mailgun');
53
- $this->plugin_file = __FILE__;
54
- $this->plugin_basename = plugin_basename($this->plugin_file);
55
- $this->api_endpoint = 'https://api.mailgun.net/v3/';
56
-
57
- // Either override the wp_mail function or configure PHPMailer to use the
58
- // Mailgun SMTP servers
59
- // When using SMTP, we also need to inject a `wp_mail` filter to make "from" settings
60
- // work properly. Fixes issues with 1.5.7+
61
- if ($this->get_option('useAPI') || (defined('MAILGUN_USEAPI') && MAILGUN_USEAPI)) {
62
- if (!function_exists('wp_mail')) {
63
- if (!include dirname(__FILE__).'/includes/wp-mail-api.php') {
64
- self::deactivate_and_die(dirname(__FILE__).'/includes/wp-mail-api.php');
65
- }
66
- }
67
- } else {
68
- // Using SMTP, include the SMTP filter
69
- if (!function_exists('mg_smtp_mail_filter')) {
70
- if (!include dirname(__FILE__).'/includes/wp-mail-smtp.php') {
71
- self::deactivate_and_die(dirname(__FILE__).'/includes/wp-mail-smtp.php');
72
- }
73
- }
74
- add_filter('wp_mail', 'mg_smtp_mail_filter');
75
- add_action('phpmailer_init', array(&$this, 'phpmailer_init'));
76
- add_action('wp_mail_failed', 'wp_mail_failed');
77
- }
78
- }
79
-
80
- /**
81
- * Get specific option from the options table.
82
- *
83
- * @param string $option Name of option to be used as array key for retrieving the specific value
84
- *
85
- * @return mixed
86
- *
87
- * @since 0.1
88
- */
89
- public function get_option($option, $options = null, $default = false)
90
- {
91
- if (is_null($options)) {
92
- $options = &$this->options;
93
- }
94
- if (isset($options[$option])) {
95
- return $options[$option];
96
- } else {
97
- return $default;
98
- }
99
- }
100
-
101
- /**
102
- * Hook into phpmailer to override SMTP based configurations
103
- * to use the Mailgun SMTP server.
104
- *
105
- * @param object $phpmailer The PHPMailer object to modify by reference
106
- *
107
- * @return none
108
- *
109
- * @since 0.1
110
- */
111
- public function phpmailer_init(&$phpmailer)
112
- {
113
- $username = (defined('MAILGUN_USERNAME') && MAILGUN_USERNAME) ? MAILGUN_USERNAME : $this->get_option('username');
114
- $domain = (defined('MAILGUN_DOMAIN') && MAILGUN_DOMAIN) ? MAILGUN_DOMAIN : $this->get_option('domain');
115
- $username = preg_replace('/@.+$/', '', $username)."@{$domain}";
116
- $secure = (defined('MAILGUN_SECURE') && MAILGUN_SECURE) ? MAILGUN_SECURE : $this->get_option('secure');
117
- $sectype = (defined('MAILGUN_SECTYPE') && MAILGUN_SECTYPE) ? MAILGUN_SECTYPE : $this->get_option('sectype');
118
- $password = (defined('MAILGUN_PASSWORD') && MAILGUN_PASSWORD) ? MAILGUN_PASSWORD : $this->get_option('password');
119
-
120
- $phpmailer->Mailer = 'smtp';
121
- $phpmailer->Host = 'smtp.mailgun.org';
122
- $phpmailer->Port = (bool) $secure ? 465 : 587;
123
- $phpmailer->SMTPAuth = true;
124
- $phpmailer->Username = $username;
125
- $phpmailer->Password = $password;
126
-
127
- $phpmailer->SMTPSecure = (bool) $secure ? $sectype : 'none';
128
- // Without this line... wp_mail for SMTP-only will always return false. But why? :(
129
- $phpmailer->Debugoutput = 'mg_smtp_debug_output';
130
- $phpmailer->SMTPDebug = 2;
131
- }
132
-
133
- /**
134
- * Deactivate this plugin and die.
135
- *
136
- * Used to deactivate the plugin when files critical to it's operation can not be loaded
137
- *
138
- * @since 0.1
139
- *
140
- * @return none
141
- */
142
- public function deactivate_and_die($file)
143
- {
144
- load_plugin_textdomain('mailgun', false, 'mailgun/languages');
145
- $message = sprintf(__('Mailgun has been automatically deactivated because the file <strong>%s</strong> is missing. Please reinstall the plugin and reactivate.'), $file);
146
- if (!function_exists('deactivate_plugins')) {
147
- include ABSPATH.'wp-admin/includes/plugin.php';
148
- }
149
- deactivate_plugins(__FILE__);
150
- wp_die($message);
151
- }
152
-
153
- /**
154
- * Make a Mailgun api call.
155
- *
156
- * @param string $endpoint The Mailgun endpoint uri
157
- *
158
- * @return array
159
- *
160
- * @since 0.1
161
- */
162
- public function api_call($uri, $params = array(), $method = 'POST')
163
- {
164
- $options = get_option('mailgun');
165
- $apiKey = (defined('MAILGUN_APIKEY') && MAILGUN_APIKEY) ? MAILGUN_APIKEY : $options['apiKey'];
166
- $domain = (defined('MAILGUN_DOMAIN') && MAILGUN_DOMAIN) ? MAILGUN_DOMAIN : $options['domain'];
167
-
168
- $time = time();
169
- $url = $this->api_endpoint.$uri;
170
- $headers = array(
171
- 'Authorization' => 'Basic '.base64_encode("api:{$apiKey}"),
172
- );
173
-
174
- switch ($method) {
175
- case 'GET':
176
- $params['sess'] = '';
177
- $querystring = http_build_query($params);
178
- $url = $url.'?'.$querystring;
179
- $params = '';
180
- break;
181
- case 'POST':
182
- case 'PUT':
183
- case 'DELETE':
184
- $params['sess'] = '';
185
- $params['time'] = $time;
186
- $params['hash'] = sha1(date('U'));
187
- break;
188
- }
189
-
190
- // make the request
191
- $args = array(
192
- 'method' => $method,
193
- 'body' => $params,
194
- 'headers' => $headers,
195
- 'sslverify' => true,
196
- );
197
-
198
- // make the remote request
199
- $result = wp_remote_request($url, $args);
200
- if (!is_wp_error($result)) {
201
- return $result['body'];
202
- } else {
203
- return $result->get_error_message();
204
- }
205
- }
206
-
207
- /**
208
- * Get account associated lists.
209
- *
210
- * @return array
211
- *
212
- * @since 0.1
213
- */
214
- public function get_lists()
215
- {
216
- $results = array();
217
-
218
- $lists_json = $this->api_call('lists', array(), 'GET');
219
- $lists_arr = json_decode($lists_json, true);
220
- if (isset($lists_arr['items']) && !empty($lists_arr['items'])) {
221
- $results = $lists_arr['items'];
222
- }
223
-
224
- return $results;
225
- }
226
-
227
- /**
228
- * Handle add list ajax post.
229
- *
230
- * @return string json
231
- *
232
- * @since 0.1
233
- */
234
- public function add_list()
235
- {
236
- $response = array();
237
-
238
- $name = isset($_POST['name']) ? $_POST['name'] : null;
239
- $email = isset($_POST['email']) ? $_POST['email'] : null;
240
-
241
- $list_addresses = $_POST['addresses'];
242
-
243
- if (!empty($list_addresses)) {
244
- foreach ($list_addresses as $address => $val) {
245
- $response[] = $this->api_call(
246
- "lists/{$address}/members",
247
- array(
248
- 'address' => $email,
249
- 'name' => $name,
250
- )
251
- );
252
- }
253
-
254
- echo json_encode(array('status' => 200, 'message' => 'Thank you!'));
255
- } else {
256
- echo json_encode(array('status' => 500, 'message' => 'Uh oh. We weren\'t able to add you to the list'.count($list_addresses) ? 's.' : '. Please try again.'));
257
- }
258
-
259
- wp_die();
260
- }
261
-
262
- /**
263
- * Frontend List Form.
264
- *
265
- * @param string $list_address Mailgun address list id
266
- * @param array $args widget arguments
267
- * @param string $instance widget instance params
268
- *
269
- * @since 0.1
270
- */
271
- public function list_form($list_address, $args = array(), $instance = array())
272
- {
273
- $widget_class_id = "mailgun-list-widget-{$args['widget_id']}";
274
- $form_class_id = "list-form-{$args['widget_id']}";
275
-
276
- // List addresses from the plugin config
277
- $list_addresses = array_map('trim', explode(',', $list_address));
278
-
279
- // All list info from the API; used for list info when more than one list is available to subscribe to
280
- $all_list_addresses = $this->get_lists(); ?>
281
- <div class="mailgun-list-widget-front <?php echo $widget_class_id; ?> widget">
282
- <form class="list-form <?php echo $form_class_id; ?>">
283
- <div class="mailgun-list-widget-inputs">
284
- <?php if (isset($args['list_title'])) : ?>
285
- <div class="mailgun-list-title">
286
- <h4 class="widget-title">
287
- <span><?php echo $args['list_title']; ?></span>
288
- </h4>
289
- </div>
290
- <?php endif; ?>
291
- <?php if (isset($args['list_description'])) : ?>
292
- <div class="mailgun-list-description">
293
- <p class="widget-description">
294
- <span><?php echo $args['list_description']; ?></span>
295
- </p>
296
- </div>
297
- <?php endif; ?>
298
- <?php if (isset($args['collect_name']) && intval($args['collect_name']) === 1) : ?>
299
- <p class="mailgun-list-widget-name">
300
- <strong>Name:</strong>
301
- <input type="text" name="name" />
302
- </p>
303
- <?php endif; ?>
304
- <p class="mailgun-list-widget-email">
305
- <strong>Email:</strong>
306
- <input type="text" name="email" />
307
- </p>
308
- </div>
309
-
310
- <?php if (count($list_addresses) > 1) : ?>
311
- <ul class="mailgun-lists" style="list-style: none;">
312
- <?php foreach ($all_list_addresses as $la) : ?>
313
- <?php if (!in_array($la['address'], $list_addresses)) {
314
- continue;
315
- } ?>
316
- <li>
317
- <input type="checkbox" class="mailgun-list-name" name="addresses[<?php echo $la['address']; ?>]" /> <?php echo $la['name']; ?>
318
- </li>
319
- <?php endforeach; ?>
320
- </ul>
321
- <?php else : ?>
322
- <input type="hidden" name="addresses[<?php echo $list_addresses[0]; ?>]" value="on" />
323
- <?php endif; ?>
324
-
325
- <input class="mailgun-list-submit-button" data-form-id="<?php echo $form_class_id; ?>" type="button" value="Subscribe" />
326
- <input type="hidden" name="mailgun-submission" value="1" />
327
-
328
- </form>
329
- <div class="widget-list-panel result-panel" style="display:none;">
330
- <span>Thank you for subscribing!</span>
331
- </div>
332
- </div>
333
-
334
- <script>
335
-
336
- jQuery(document).ready(function(){
337
-
338
- jQuery('.mailgun-list-submit-button').on('click', function() {
339
-
340
- var form_id = jQuery(this).data('form-id');
341
-
342
- if(jQuery('.mailgun-list-name').length > 0 && jQuery('.'+form_id+' .mailgun-list-name:checked').length < 1) {
343
- alert('Please select a list to subscribe to.');
344
- return;
345
- }
346
-
347
- if(jQuery('.'+form_id+' .mailgun-list-widget-name input') && jQuery('.'+form_id+' .mailgun-list-widget-name input').val() === '') {
348
- alert('Please enter your subscription name.');
349
- return;
350
- }
351
-
352
- if(jQuery('.'+form_id+' .mailgun-list-widget-email input').val() === '') {
353
- alert('Please enter your subscription email.');
354
- return;
355
- }
356
-
357
- jQuery.ajax({
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
358
  url: '<?php echo admin_url('admin-ajax.php?action=add_list'); ?>',
359
- action:'add_list',
360
  type: 'post',
361
  dataType: 'json',
362
- data: jQuery('.'+form_id+'').serialize(),
363
- success: function(data) {
364
-
365
- data_msg = data.message;
366
- already_exists = false;
367
- if(data_msg !== undefined){
368
- already_exists = data_msg.indexOf('Address already exists') > -1;
369
- }
370
-
371
- // success
372
- if((data.status === 200)) {
373
- jQuery('.<?php echo $widget_class_id; ?> .widget-list-panel').css('display', 'none');
374
- jQuery('.<?php echo $widget_class_id; ?> .list-form').css('display', 'none');
375
- jQuery('.<?php echo $widget_class_id; ?> .result-panel').css('display', 'block');
376
  // error
377
- } else {
378
- alert(data_msg);
379
- }
380
  }
381
- });
382
- });
383
- });
384
-
385
- </script>
386
-
387
- <?php
388
-
389
- }
390
-
391
- /**
392
- * Initialize List Form.
393
- *
394
- * @param array $atts Form attributes
395
- *
396
- * @since 0.1
397
- */
398
- public function build_list_form($atts)
399
- {
400
- if (isset($atts['id']) && $atts['id'] != '') {
401
- $args['widget_id'] = md5(rand(10000, 99999) + $atts['id']);
402
-
403
- if (isset($atts['collect_name'])) {
404
- $args['collect_name'] = true;
405
- }
406
-
407
- if (isset($atts['title'])) {
408
- $args['list_title'] = $atts['title'];
409
- }
410
-
411
- if (isset($atts['description'])) {
412
- $args['list_description'] = $atts['description'];
413
- }
414
-
415
- ob_start();
416
- $this->list_form($atts['id'], $args);
417
- $output_string = ob_get_contents();
418
- ob_end_clean();
419
-
420
- return $output_string;
421
- } else {
422
- ?>
423
- <span>Mailgun list ID needed to render form!</span>
424
- <br />
425
- <strong>Example :</strong> [mailgun id="[your list id]"]
426
- <?php
427
-
428
- }
429
- }
430
-
431
- /**
432
- * Initialize List Widget.
433
- *
434
- * @since 0.1
435
- */
436
- public function load_list_widget()
437
- {
438
- register_widget('list_widget');
439
- add_shortcode('mailgun', array(&$this, 'build_list_form'));
440
- }
441
- }
442
-
443
- $mailgun = new Mailgun();
444
-
445
- if (@include dirname(__FILE__).'/includes/widget.php') {
446
- add_action('widgets_init', array(&$mailgun, 'load_list_widget'));
447
- add_action('wp_ajax_nopriv_add_list', array(&$mailgun, 'add_list'));
448
- add_action('wp_ajax_add_list', array(&$mailgun, 'add_list'));
449
- }
450
-
451
- if (is_admin()) {
452
- if (@include dirname(__FILE__).'/includes/admin.php') {
453
- $mailgunAdmin = new MailgunAdmin();
454
- } else {
455
- Mailgun::deactivate_and_die(dirname(__FILE__).'/includes/admin.php');
456
- }
457
- }
1
  <?php
2
 
3
+ /**
4
+ * Plugin Name: Mailgun
5
+ * Plugin URI: http://wordpress.org/extend/plugins/mailgun/
6
+ * Description: Mailgun integration for WordPress
7
+ * Version: 1.7.1
8
+ * Author: Mailgun
9
+ * Author URI: http://www.mailgun.com/
10
+ * License: GPLv2 or later
11
+ * Text Domain: mailgun
12
+ * Domain Path: /languages/.
13
+ */
14
+
15
+ /*
16
+ * mailgun-wordpress-plugin - Sending mail from Wordpress using Mailgun
17
+ * Copyright (C) 2016 Mailgun, et al.
18
+ *
19
+ * This program is free software; you can redistribute it and/or modify
20
+ * it under the terms of the GNU General Public License as published by
21
+ * the Free Software Foundation; either version 2 of the License, or
22
+ * (at your option) any later version.
23
+ *
24
+ * This program is distributed in the hope that it will be useful,
25
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
+ * GNU General Public License for more details.
28
+ *
29
+ * You should have received a copy of the GNU General Public License along
30
+ * with this program; if not, write to the Free Software Foundation, Inc.,
31
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32
+ */
33
+
34
+ /**
35
+ * Entrypoint for the Mailgun plugin. Sets up the mailing "strategy" -
36
+ * either API or SMTP.
37
+ *
38
+ * Registers handlers for later actions and sets up config variables with
39
+ * Wordpress.
40
+ */
41
+ class Mailgun
42
+ {
43
+ /**
44
+ * Setup shared functionality for Admin and Front End.
45
+ *
46
+ * @since 0.1
47
+ */
48
+ public function __construct()
49
+ {
50
+ $this->options = get_option('mailgun');
51
+ $this->plugin_file = __FILE__;
52
+ $this->plugin_basename = plugin_basename($this->plugin_file);
53
+
54
+ // Either override the wp_mail function or configure PHPMailer to use the
55
+ // Mailgun SMTP servers
56
+ // When using SMTP, we also need to inject a `wp_mail` filter to make "from" settings
57
+ // work properly. Fixes issues with 1.5.7+
58
+ if ($this->get_option('useAPI') || (defined('MAILGUN_USEAPI') && MAILGUN_USEAPI)):
59
+ if (!function_exists('wp_mail')):
60
+ if (!include dirname(__FILE__) . '/includes/wp-mail-api.php'):
61
+ self::deactivate_and_die(dirname(__FILE__) . '/includes/wp-mail-api.php');
62
+ endif;
63
+ endif;
64
+ else:
65
+ // Using SMTP, include the SMTP filter
66
+ if (!function_exists('mg_smtp_mail_filter')):
67
+ if (!include dirname(__FILE__) . '/includes/wp-mail-smtp.php'):
68
+ self::deactivate_and_die(dirname(__FILE__) . '/includes/wp-mail-smtp.php');
69
+ endif;
70
+ endif;
71
+ add_filter('wp_mail', 'mg_smtp_mail_filter');
72
+ add_action('phpmailer_init', array(&$this, 'phpmailer_init'));
73
+ add_action('wp_mail_failed', 'wp_mail_failed');
74
+ endif;
75
+ }
76
+
77
+ /**
78
+ * Get specific option from the options table.
79
+ *
80
+ * @param string $option Name of option to be used as array key for retrieving the specific value
81
+ * @param array $options Array to iterate over for specific values
82
+ * @param bool $default False if no options are set
83
+ *
84
+ * @return mixed
85
+ *
86
+ * @since 0.1
87
+ */
88
+ public function get_option($option, $options = null, $default = false)
89
+ {
90
+ if (is_null($options)):
91
+ $options = &$this->options;
92
+ endif;
93
+ if (isset($options[ $option ])):
94
+ return $options[ $option ];
95
+ else:
96
+ return $default;
97
+ endif;
98
+ }
99
+
100
+ /**
101
+ * Hook into phpmailer to override SMTP based configurations
102
+ * to use the Mailgun SMTP server.
103
+ *
104
+ * @param object $phpmailer The PHPMailer object to modify by reference
105
+ *
106
+ * @return void
107
+ *
108
+ * @since 0.1
109
+ */
110
+ public function phpmailer_init(&$phpmailer)
111
+ {
112
+ $username = (defined('MAILGUN_USERNAME') && MAILGUN_USERNAME) ? MAILGUN_USERNAME : $this->get_option('username');
113
+ $domain = (defined('MAILGUN_DOMAIN') && MAILGUN_DOMAIN) ? MAILGUN_DOMAIN : $this->get_option('domain');
114
+ $username = preg_replace('/@.+$/', '', $username) . "@{$domain}";
115
+ $secure = (defined('MAILGUN_SECURE') && MAILGUN_SECURE) ? MAILGUN_SECURE : $this->get_option('secure');
116
+ $sectype = (defined('MAILGUN_SECTYPE') && MAILGUN_SECTYPE) ? MAILGUN_SECTYPE : $this->get_option('sectype');
117
+ $password = (defined('MAILGUN_PASSWORD') && MAILGUN_PASSWORD) ? MAILGUN_PASSWORD : $this->get_option('password');
118
+ $region = (defined('MAILGUN_REGION') && MAILGUN_REGION) ? MAILGUN_REGION : $this->get_option('region');
119
+
120
+ $smtp_endpoint = mg_smtp_get_region($region);
121
+ $smtp_endpoint = (bool) $smtp_endpoint ? $smtp_endpoint : 'smtp.mailgun.org';
122
+
123
+ $phpmailer->Mailer = 'smtp';
124
+ $phpmailer->Host = $smtp_endpoint;
125
+
126
+ if ('ssl' === $sectype):
127
+ // For SSL-only connections, use 465
128
+ $phpmailer->Port = 465;
129
+ else:
130
+ // Otherwise, use 587.
131
+ $phpmailer->Port = 587;
132
+ endif;
133
+
134
+ $phpmailer->SMTPAuth = true;
135
+ $phpmailer->Username = $username;
136
+ $phpmailer->Password = $password;
137
+
138
+ $phpmailer->SMTPSecure = (bool) $secure ? $sectype : '';
139
+ // Without this line... wp_mail for SMTP-only will always return false. But why? :(
140
+ $phpmailer->Debugoutput = 'mg_smtp_debug_output';
141
+ $phpmailer->SMTPDebug = 2;
142
+
143
+ // Emit some logging for SMTP connection
144
+ mg_smtp_debug_output(sprintf("PHPMailer configured to send via %s:%s", $phpmailer->Host, $phpmailer->Port),
145
+ 'DEBUG');
146
+ }
147
+
148
+ /**
149
+ * Deactivate this plugin and die.
150
+ * Deactivate the plugin when files critical to it's operation cannot be loaded
151
+ *
152
+ * @param $file Files critical to plugin functionality
153
+ *
154
+ * @return void
155
+ *
156
+ * @since 0.1
157
+ */
158
+ public function deactivate_and_die($file)
159
+ {
160
+ load_plugin_textdomain('mailgun', false, 'mailgun/languages');
161
+ $message = sprintf(__('Mailgun has been automatically deactivated because the file <strong>%s</strong> is missing. Please reinstall the plugin and reactivate.'),
162
+ $file);
163
+ if (!function_exists('deactivate_plugins')):
164
+ include ABSPATH . 'wp-admin/includes/plugin.php';
165
+ endif;
166
+ deactivate_plugins(__FILE__);
167
+ wp_die($message);
168
+ }
169
+
170
+ /**
171
+ * Make a Mailgun api call.
172
+ *
173
+ * @param string $uri The endpoint for the Mailgun API
174
+ * @param array $params Array of parameters passed to the API
175
+ * @param string $method The form request type
176
+ *
177
+ * @return array
178
+ *
179
+ * @since 0.1
180
+ */
181
+ public function api_call($uri, $params = array(), $method = 'POST')
182
+ {
183
+ $options = get_option('mailgun');
184
+ $getRegion = (defined('MAILGUN_REGION') && MAILGUN_REGION) ? MAILGUN_REGION : $options[ 'region' ];
185
+ $apiKey = (defined('MAILGUN_APIKEY') && MAILGUN_APIKEY) ? MAILGUN_APIKEY : $options[ 'apiKey' ];
186
+ $domain = (defined('MAILGUN_DOMAIN') && MAILGUN_DOMAIN) ? MAILGUN_DOMAIN : $options[ 'domain' ];
187
+
188
+ $region = mg_api_get_region($getRegion);
189
+ $this->api_endpoint = ($region) ? $region : 'https://api.mailgun.net/v3/';
190
+
191
+ $time = time();
192
+ $url = $this->api_endpoint . $uri;
193
+ $headers = array(
194
+ 'Authorization' => 'Basic ' . base64_encode("api:{$apiKey}"),
195
+ );
196
+
197
+ switch ($method) {
198
+ case 'GET':
199
+ $params[ 'sess' ] = '';
200
+ $querystring = http_build_query($params);
201
+ $url = $url . '?' . $querystring;
202
+ $params = '';
203
+ break;
204
+ case 'POST':
205
+ case 'PUT':
206
+ case 'DELETE':
207
+ $params[ 'sess' ] = '';
208
+ $params[ 'time' ] = $time;
209
+ $params[ 'hash' ] = sha1(date('U'));
210
+ break;
211
+ }
212
+
213
+ // make the request
214
+ $args = array(
215
+ 'method' => $method,
216
+ 'body' => $params,
217
+ 'headers' => $headers,
218
+ 'sslverify' => true,
219
+ );
220
+
221
+ // make the remote request
222
+ $result = wp_remote_request($url, $args);
223
+ if (!is_wp_error($result)):
224
+ return $result[ 'body' ];
225
+ else:
226
+ return $result->get_error_message();
227
+ endif;
228
+ }
229
+
230
+ /**
231
+ * Get account associated lists.
232
+ *
233
+ * @return array
234
+ *
235
+ * @since 0.1
236
+ */
237
+ public function get_lists()
238
+ {
239
+ $results = array();
240
+
241
+ $lists_json = $this->api_call('lists', array(), 'GET');
242
+ $lists_arr = json_decode($lists_json, true);
243
+ if (isset($lists_arr[ 'items' ]) && !empty($lists_arr[ 'items' ])):
244
+ $results = $lists_arr[ 'items' ];
245
+ endif;
246
+
247
+ return $results;
248
+ }
249
+
250
+ /**
251
+ * Handle add list ajax post.
252
+ *
253
+ * @return string json
254
+ *
255
+ * @since 0.1
256
+ */
257
+ public function add_list()
258
+ {
259
+ $response = array();
260
+
261
+ $name = isset($_POST[ 'name' ]) ? $_POST[ 'name' ] : null;
262
+ $email = isset($_POST[ 'email' ]) ? $_POST[ 'email' ] : null;
263
+
264
+ $list_addresses = $_POST[ 'addresses' ];
265
+
266
+ if (!empty($list_addresses)):
267
+ foreach ($list_addresses as $address => $val):
268
+ $response[] = $this->api_call(
269
+ "lists/{$address}/members",
270
+ array(
271
+ 'address' => $email,
272
+ 'name' => $name,
273
+ )
274
+ );
275
+ endforeach;
276
+
277
+ echo json_encode(array('status' => 200, 'message' => 'Thank you!'));
278
+ else:
279
+ echo json_encode(array(
280
+ 'status' => 500,
281
+ 'message' => 'Uh oh. We weren\'t able to add you to the list' . count($list_addresses) ? 's.' : '. Please try again.'
282
+ ));
283
+ endif;
284
+
285
+ wp_die();
286
+ }
287
+
288
+ /**
289
+ * Frontend List Form.
290
+ *
291
+ * @param string $list_address Mailgun address list id
292
+ * @param array $args widget arguments
293
+ * @param array $instance widget instance params
294
+ *
295
+ * @since 0.1
296
+ */
297
+ public function list_form($list_address, $args = array(), $instance = array())
298
+ {
299
+ $widget_class_id = "mailgun-list-widget-{$args['widget_id']}";
300
+ $form_class_id = "list-form-{$args['widget_id']}";
301
+
302
+ // List addresses from the plugin config
303
+ $list_addresses = array_map('trim', explode(',', $list_address));
304
+
305
+ // All list info from the API; used for list info when more than one list is available to subscribe to
306
+ $all_list_addresses = $this->get_lists();
307
+ ?>
308
+ <div class="mailgun-list-widget-front <?php echo $widget_class_id; ?> widget">
309
+ <form class="list-form <?php echo $form_class_id; ?>">
310
+ <div class="mailgun-list-widget-inputs">
311
+ <?php if (isset($args[ 'list_title' ])): ?>
312
+ <div class="mailgun-list-title">
313
+ <h4 class="widget-title">
314
+ <span><?php echo $args[ 'list_title' ]; ?></span>
315
+ </h4>
316
+ </div>
317
+ <?php endif; ?>
318
+ <?php if (isset($args[ 'list_description' ])): ?>
319
+ <div class="mailgun-list-description">
320
+ <p class="widget-description">
321
+ <span><?php echo $args[ 'list_description' ]; ?></span>
322
+ </p>
323
+ </div>
324
+ <?php endif; ?>
325
+ <?php if (isset($args[ 'collect_name' ]) && intval($args[ 'collect_name' ]) === 1): ?>
326
+ <p class="mailgun-list-widget-name">
327
+ <strong>Name:</strong>
328
+ <input type="text" name="name"/>
329
+ </p>
330
+ <?php endif; ?>
331
+ <p class="mailgun-list-widget-email">
332
+ <strong>Email:</strong>
333
+ <input type="text" name="email"/>
334
+ </p>
335
+ </div>
336
+
337
+ <?php if (count($list_addresses) > '1'): ?>
338
+ <ul class="mailgun-lists" style="list-style: none;">
339
+ <?php
340
+ foreach ($all_list_addresses as $la):
341
+ if (!in_array($la[ 'address' ], $list_addresses)):
342
+ continue;
343
+ endif;
344
+ ?>
345
+ <li>
346
+ <input type="checkbox" class="mailgun-list-name"
347
+ name="addresses[<?php echo $la[ 'address' ]; ?>]"/> <?php echo $la[ 'name' ]; ?>
348
+ </li>
349
+ <?php endforeach; ?>
350
+ </ul>
351
+ <?php else: ?>
352
+ <input type="hidden" name="addresses[<?php echo $list_addresses[ 0 ]; ?>]" value="on"/>
353
+ <?php endif; ?>
354
+
355
+ <input class="mailgun-list-submit-button" data-form-id="<?php echo $form_class_id; ?>" type="button"
356
+ value="Subscribe"/>
357
+ <input type="hidden" name="mailgun-submission" value="1"/>
358
+
359
+ </form>
360
+ <div class="widget-list-panel result-panel" style="display:none;">
361
+ <span>Thank you for subscribing!</span>
362
+ </div>
363
+ </div>
364
+
365
+ <script>
366
+ jQuery(document).ready(function () {
367
+
368
+ jQuery('.mailgun-list-submit-button').on('click', function () {
369
+
370
+ var form_id = jQuery(this).data('form-id')
371
+
372
+ if (jQuery('.mailgun-list-name').length > 0 && jQuery('.' + form_id + ' .mailgun-list-name:checked').length < 1) {
373
+ alert('Please select a list to subscribe to.')
374
+ return
375
+ }
376
+
377
+ if (jQuery('.' + form_id + ' .mailgun-list-widget-name input') && jQuery('.' + form_id + ' .mailgun-list-widget-name input').val() === '') {
378
+ alert('Please enter your subscription name.')
379
+ return
380
+ }
381
+
382
+ if (jQuery('.' + form_id + ' .mailgun-list-widget-email input').val() === '') {
383
+ alert('Please enter your subscription email.')
384
+ return
385
+ }
386
+
387
+ jQuery.ajax({
388
  url: '<?php echo admin_url('admin-ajax.php?action=add_list'); ?>',
389
+ action: 'add_list',
390
  type: 'post',
391
  dataType: 'json',
392
+ data: jQuery('.' + form_id + '').serialize(),
393
+ success: function (data) {
394
+
395
+ data_msg = data.message
396
+ already_exists = false
397
+ if (data_msg !== undefined) {
398
+ already_exists = data_msg.indexOf('Address already exists') > -1
399
+ }
400
+
401
+ // success
402
+ if ((data.status === 200)) {
403
+ jQuery('.<?php echo $widget_class_id; ?> .widget-list-panel').css('display', 'none')
404
+ jQuery('.<?php echo $widget_class_id; ?> .list-form').css('display', 'none')
405
+ jQuery('.<?php echo $widget_class_id; ?> .result-panel').css('display', 'block')
406
  // error
407
+ } else {
408
+ alert(data_msg)
409
+ }
410
  }
411
+ })
412
+ })
413
+ })
414
+ </script>
415
+
416
+ <?php
417
+ }
418
+
419
+ /**
420
+ * Initialize List Form.
421
+ *
422
+ * @param array $atts Form attributes
423
+ *
424
+ * @return string
425
+ *
426
+ * @since 0.1
427
+ */
428
+ public function build_list_form($atts)
429
+ {
430
+ if (isset($atts[ 'id' ]) && $atts[ 'id' ] != ''):
431
+ $args[ 'widget_id' ] = md5(rand(10000, 99999) + $atts[ 'id' ]);
432
+
433
+ if (isset($atts[ 'collect_name' ])):
434
+ $args[ 'collect_name' ] = true;
435
+ endif;
436
+
437
+ if (isset($atts[ 'title' ])):
438
+ $args[ 'list_title' ] = $atts[ 'title' ];
439
+ endif;
440
+
441
+ if (isset($atts[ 'description' ])):
442
+ $args[ 'list_description' ] = $atts[ 'description' ];
443
+ endif;
444
+
445
+ ob_start();
446
+ $this->list_form($atts[ 'id' ], $args);
447
+ $output_string = ob_get_contents();
448
+ ob_end_clean();
449
+
450
+ return $output_string;
451
+ else:
452
+ ?>
453
+ <span>Mailgun list ID needed to render form!</span>
454
+ <br/>
455
+ <strong>Example :</strong> [mailgun id="[your list id]"]
456
+ <?php
457
+ endif;
458
+ }
459
+
460
+ /**
461
+ * Initialize List Widget.
462
+ *
463
+ * @since 0.1
464
+ */
465
+ public function load_list_widget()
466
+ {
467
+ register_widget('list_widget');
468
+ add_shortcode('mailgun', array(&$this, 'build_list_form'));
469
+ }
470
+ }
471
+
472
+ $mailgun = new Mailgun();
473
+
474
+ if (@include dirname(__FILE__) . '/includes/widget.php'):
475
+ add_action('widgets_init', array(&$mailgun, 'load_list_widget'));
476
+ add_action('wp_ajax_nopriv_add_list', array(&$mailgun, 'add_list'));
477
+ add_action('wp_ajax_add_list', array(&$mailgun, 'add_list'));
478
+ endif;
479
+
480
+ if (is_admin()):
481
+ if (@include dirname(__FILE__) . '/includes/admin.php'):
482
+ $mailgunAdmin = new MailgunAdmin();
483
+ else:
484
+ Mailgun::deactivate_and_die(dirname(__FILE__) . '/includes/admin.php');
485
+ endif;
486
+ endif;
 
readme.txt CHANGED
@@ -4,8 +4,8 @@ Mailgun for WordPress
4
  Contributors: Mailgun, sivel, lookahead.io, m35dev
5
  Tags: mailgun, smtp, http, api, mail, email
6
  Requires at least: 3.3
7
- Tested up to: 4.8
8
- Stable tag: 1.5.10
9
  License: GPLv2 or later
10
 
11
 
@@ -18,7 +18,7 @@ Easily send email from your WordPress site through Mailgun using the HTTP API or
18
 
19
  One particularly useful feature of this plugin is that it provides you with a way to send email when the server you are on does not support SMTP or where outbound SMTP is restricted since the plug-in uses the Mailgun HTTP API for sending email by default. All you need to use the plugin is a [Mailgun account](http://www.mailgun.com/). Mailgun has a free account that lets you send up to 200 emails per day, which is great for testing. Paid subscriptions are available for increased limits.
20
 
21
- The latest version of this plugin adds support for Mailgun list subscription. Using the shortcode, you can place a form on an article or page to allow the visitor to subscribe to one or more lists. Using the widget, you can provide subscription functionality in sidebars or anywhere widgets are supported e.g. footers.
22
 
23
  The current version of this plugin only handles sending emails, tracking and tagging and list subscription.
24
 
@@ -53,12 +53,14 @@ define( 'MG_DEBUG_SMTP', true );
53
  Yes, using the following constants that can be placed in wp-config.php:
54
 
55
  `
56
- MAILGUN_USEAPI Type: boolean
 
 
57
  MAILGUN_APIKEY Type: string
58
  MAILGUN_DOMAIN Type: string
59
  MAILGUN_USERNAME Type: string
60
  MAILGUN_PASSWORD Type: string
61
- MAILGUN_SECURE Type: boolean
62
  MAILGUN_SECTYPE Type: string Choices: 'ssl' or 'tls'
63
  MAILGUN_FROM_NAME Type: string
64
  MAILGUN_FROM_ADDRESS Type: string
@@ -70,7 +72,7 @@ MAILGUN_FROM_ADDRESS Type: string
70
  Mutates messages to use recipient variables syntax - see
71
  https://documentation.mailgun.com/user_manual.html#batch-sending for more info.
72
 
73
- Should accept a list of `To` addressses.
74
 
75
  Should *only* return `true` or `false`.
76
 
@@ -95,7 +97,7 @@ MAILGUN_FROM_ADDRESS Type: string
95
  Mutates messages to use recipient variables syntax - see
96
  https://documentation.mailgun.com/user_manual.html#batch-sending for more info.
97
 
98
- Should accept a list of `To` addressses.
99
 
100
  Should *only* return `true` or `false`.
101
 
@@ -127,6 +129,59 @@ MAILGUN_FROM_ADDRESS Type: string
127
 
128
  == Changelog ==
129
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  = 1.5.10 (2017-11-22): =
131
  - Fix back to settings link on lists page (https://github.com/mailgun/wordpress-plugin/pull/65)
132
  - Fix a bug causing `text/html` emails to send as both `text/plain` *and* `text/html` parts
4
  Contributors: Mailgun, sivel, lookahead.io, m35dev
5
  Tags: mailgun, smtp, http, api, mail, email
6
  Requires at least: 3.3
7
+ Tested up to: 5.5.1
8
+ Stable tag: 1.7.2
9
  License: GPLv2 or later
10
 
11
 
18
 
19
  One particularly useful feature of this plugin is that it provides you with a way to send email when the server you are on does not support SMTP or where outbound SMTP is restricted since the plug-in uses the Mailgun HTTP API for sending email by default. All you need to use the plugin is a [Mailgun account](http://www.mailgun.com/). Mailgun has a free account that lets you send up to 200 emails per day, which is great for testing. Paid subscriptions are available for increased limits.
20
 
21
+ The latest version of this plugin now supports regions. Select either U.S./North America or Europe to choose the API endpoint from which you will send email.
22
 
23
  The current version of this plugin only handles sending emails, tracking and tagging and list subscription.
24
 
53
  Yes, using the following constants that can be placed in wp-config.php:
54
 
55
  `
56
+ MAILGUN_REGION Type: string Choices: 'us' or 'eu'
57
+ ex. define('MAILGUN_REGION', 'us');
58
+ MAILGUN_USEAPI Type: boolean Choices: '0' or '1' (0 = false/no)
59
  MAILGUN_APIKEY Type: string
60
  MAILGUN_DOMAIN Type: string
61
  MAILGUN_USERNAME Type: string
62
  MAILGUN_PASSWORD Type: string
63
+ MAILGUN_SECURE Type: boolean Choices: '0' or '1' (0 = false/no)
64
  MAILGUN_SECTYPE Type: string Choices: 'ssl' or 'tls'
65
  MAILGUN_FROM_NAME Type: string
66
  MAILGUN_FROM_ADDRESS Type: string
72
  Mutates messages to use recipient variables syntax - see
73
  https://documentation.mailgun.com/user_manual.html#batch-sending for more info.
74
 
75
+ Should accept a list of `To` addresses.
76
 
77
  Should *only* return `true` or `false`.
78
 
97
  Mutates messages to use recipient variables syntax - see
98
  https://documentation.mailgun.com/user_manual.html#batch-sending for more info.
99
 
100
+ Should accept a list of `To` addresses.
101
 
102
  Should *only* return `true` or `false`.
103
 
129
 
130
  == Changelog ==
131
 
132
+ = 1.7.2 (2020-10-07): =
133
+ - Test plugin with PHP 7.4.
134
+ - Test plugin up to WordPress 5.5.1.
135
+
136
+ = 1.7.1 (2019-02-07): =
137
+ - Reinstall settings page for multisites.
138
+
139
+ = 1.7 (2019-01-21): =
140
+ - Remove settings page for multisites.
141
+ - Simplify admin notifications.
142
+ - Test plugin with PHP 7.2.
143
+ - Test plugin up to WordPress 5.0.3.
144
+
145
+ = 1.6.1 (2018-10-08): =
146
+ - Restore Settings page form for all install types.
147
+
148
+ = 1.6 (2018-9-21): =
149
+ - Refactor admin notifications
150
+ - Enable Settings page for all WordPress install types
151
+ - Enable Test Configuration for all WordPress install types
152
+ - Test plugin up to WordPress 4.9.8.
153
+
154
+ = 1.5.14 (2018-09-11): =
155
+ - Force SSL-secured SMTP connections to use port 465 (SMTPS) to connect, 587 for plain and TLS
156
+ - Support region endpoint switching for SMTP
157
+
158
+ = 1.5.13.1 (2018-08-15): =
159
+ - Fix line breaks in Test Configuration email
160
+
161
+ = 1.5.13 (2018-08-14): =
162
+ - Default to US region if no region is configured in settings
163
+ - Add admin notification about region configuration
164
+ - Log an error message when an email is sent with no explicit region configured
165
+
166
+ = 1.5.12.3 (2018-08-09): =
167
+ - Fix Region select menu default when wp-config.php variable is set
168
+ - Fix front end email input validation
169
+
170
+ = 1.5.12.2 (2018-08-09): =
171
+ - Fix plugin not saving after update
172
+
173
+ = 1.5.12.1 (2018-08-06): =
174
+ - Fix for backwards compatibility
175
+
176
+ = 1.5.12 (2018-08-02): =
177
+ - Add EU endpoint for Mailgun HTTP API
178
+ - Fix broken logo image on Lists page
179
+ - Test plugin up to Wordpress 4.9.7
180
+
181
+ = 1.5.11 (2018-05-30): =
182
+ - Fix an issue with authentication failing for newer API keys
183
+ - Test plugin up to Wordpress 4.9.6
184
+
185
  = 1.5.10 (2017-11-22): =
186
  - Fix back to settings link on lists page (https://github.com/mailgun/wordpress-plugin/pull/65)
187
  - Fix a bug causing `text/html` emails to send as both `text/plain` *and* `text/html` parts