Mailgun for WordPress - Version 1.7.5

Version Description

Download this release

Release Info

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

Code changes from version 1.7.4 to 1.7.5

Files changed (2) hide show
  1. mailgun.php +480 -480
  2. readme.txt +2 -5
mailgun.php CHANGED
@@ -1,486 +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.7.3
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;
 
 
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.4
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: 5.5.1
8
- Stable tag: 1.7.3
9
  License: GPLv2 or later
10
 
11
 
@@ -129,9 +129,6 @@ MAILGUN_FROM_ADDRESS Type: string
129
 
130
  == Changelog ==
131
 
132
- = 1.7.3 (2020-10-08): =
133
- - New assets
134
-
135
  = 1.7.2 (2020-10-07): =
136
  - Test plugin with PHP 7.4.
137
  - Test plugin up to WordPress 5.5.1.
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.3
8
+ Stable tag: 1.7.4
9
  License: GPLv2 or later
10
 
11
 
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.