Mailgun for WordPress - Version 1.5.4

Version Description

(2016-12-23): = * Changed some missed bracketed array usages to array() syntax * Fix wp_mail_from / wp_mail_from_name not working on old PHP / WP versions * Add a wrapper for using mime_content_type / finfo_file

Download this release

Release Info

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

Code changes from version 1.5.3 to 1.5.4

Files changed (6) hide show
  1. CHANGELOG.md +5 -0
  2. includes/admin.php +9 -9
  3. includes/widget.php +1 -1
  4. includes/wp-mail.php +55 -28
  5. mailgun.php +13 -11
  6. readme.txt +6 -1
CHANGELOG.md CHANGED
@@ -1,6 +1,11 @@
1
  Changelog
2
  =========
3
 
 
 
 
 
 
4
  1.5.3 (2016-12-22):
5
  * Changed all bracketed array usages to `array()` syntax for older PHP support
6
  * Redesigned `Content-Type` processing code to not make such large assumptions
1
  Changelog
2
  =========
3
 
4
+ 1.5.4 (2016-12-23):
5
+ * Changed some missed bracketed array usages to `array()` syntax
6
+ * Fix `wp_mail_from` / `wp_mail_from_name` not working on old PHP / WP versions
7
+ * Add a wrapper for using `mime_content_type` / `finfo_file`
8
+
9
  1.5.3 (2016-12-22):
10
  * Changed all bracketed array usages to `array()` syntax for older PHP support
11
  * Redesigned `Content-Type` processing code to not make such large assumptions
includes/admin.php CHANGED
@@ -64,7 +64,7 @@ class MailgunAdmin extends Mailgun
64
  $sitename = substr($sitename, 4);
65
  }
66
 
67
- $defaults = [
68
  'useAPI' => '1',
69
  'apiKey' => '',
70
  'domain' => '',
@@ -75,7 +75,7 @@ class MailgunAdmin extends Mailgun
75
  'track-opens' => '',
76
  'campaign-id' => '',
77
  'tag' => $sitename,
78
- ];
79
  if (!$this->options) {
80
  $this->options = $defaults;
81
  add_option('mailgun', $this->options);
@@ -331,10 +331,10 @@ class MailgunAdmin extends Mailgun
331
  if (!current_user_can('manage_options') || !wp_verify_nonce($_GET['_wpnonce'])) {
332
  die(
333
  json_encode(
334
- [
335
  'message' => __('Unauthorized', 'mailgun'),
336
  'method' => null,
337
- ]
338
  )
339
  );
340
  }
@@ -352,27 +352,27 @@ class MailgunAdmin extends Mailgun
352
  $admin_email,
353
  __('Mailgun WordPress Plugin Test', 'mailgun'),
354
  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),
355
- ['Content-Type: text/plain']
356
  );
357
 
358
  if ($result) {
359
  die(
360
  json_encode(
361
- [
362
  'message' => __('Success', 'mailgun'),
363
  'method' => $method,
364
  'error' => __('Success', 'mailgun'),
365
- ]
366
  )
367
  );
368
  } else {
369
  die(
370
  json_encode(
371
- [
372
  'message' => __('Failure', 'mailgun'),
373
  'method' => $method,
374
  'error' => mg_api_last_error(),
375
- ]
376
  )
377
  );
378
  }
64
  $sitename = substr($sitename, 4);
65
  }
66
 
67
+ $defaults = array(
68
  'useAPI' => '1',
69
  'apiKey' => '',
70
  'domain' => '',
75
  'track-opens' => '',
76
  'campaign-id' => '',
77
  'tag' => $sitename,
78
+ );
79
  if (!$this->options) {
80
  $this->options = $defaults;
81
  add_option('mailgun', $this->options);
331
  if (!current_user_can('manage_options') || !wp_verify_nonce($_GET['_wpnonce'])) {
332
  die(
333
  json_encode(
334
+ array(
335
  'message' => __('Unauthorized', 'mailgun'),
336
  'method' => null,
337
+ )
338
  )
339
  );
340
  }
352
  $admin_email,
353
  __('Mailgun WordPress Plugin Test', 'mailgun'),
354
  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),
355
+ array('Content-Type: text/plain')
356
  );
357
 
358
  if ($result) {
359
  die(
360
  json_encode(
361
+ array(
362
  'message' => __('Success', 'mailgun'),
363
  'method' => $method,
364
  'error' => __('Success', 'mailgun'),
365
+ )
366
  )
367
  );
368
  } else {
369
  die(
370
  json_encode(
371
+ array(
372
  'message' => __('Failure', 'mailgun'),
373
  'method' => $method,
374
  'error' => mg_api_last_error(),
375
+ )
376
  )
377
  );
378
  }
includes/widget.php CHANGED
@@ -103,7 +103,7 @@ class list_widget extends WP_Widget
103
  // Updating widget replacing old instances with new
104
  public function update($new_instance, $old_instance)
105
  {
106
- $instance = [];
107
  $instance = $new_instance;
108
 
109
  return $instance;
103
  // Updating widget replacing old instances with new
104
  public function update($new_instance, $old_instance)
105
  {
106
+ $instance = array();
107
  $instance = $new_instance;
108
 
109
  return $instance;
includes/wp-mail.php CHANGED
@@ -27,7 +27,7 @@
27
  *
28
  * @return string Last error that occurred.
29
  *
30
- * @since 1.5
31
  */
32
  function mg_api_last_error($error = null)
33
  {
@@ -43,6 +43,31 @@ function mg_api_last_error($error = null)
43
  }
44
  }
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  /**
47
  * wp_mail function to be loaded in to override the core wp_mail function
48
  * from wp-includes/pluggable.php.
@@ -60,7 +85,7 @@ function mg_api_last_error($error = null)
60
  *
61
  * @since 0.1
62
  */
63
- function wp_mail($to, $subject, $message, $headers = '', $attachments = [])
64
  {
65
  // Compact the input, apply the filters, and extract them back out
66
  extract(apply_filters('wp_mail', compact('to', 'subject', 'message', 'headers', 'attachments')));
@@ -79,7 +104,7 @@ function wp_mail($to, $subject, $message, $headers = '', $attachments = [])
79
 
80
  // Headers
81
  if (empty($headers)) {
82
- $headers = [];
83
  } else {
84
  if (!is_array($headers)) {
85
  // Explode the headers out, so this function can take both
@@ -88,9 +113,9 @@ function wp_mail($to, $subject, $message, $headers = '', $attachments = [])
88
  } else {
89
  $tempheaders = $headers;
90
  }
91
- $headers = [];
92
- $cc = [];
93
- $bcc = [];
94
 
95
  // If it's actually got contents
96
  if (!empty($tempheaders)) {
@@ -155,21 +180,6 @@ function wp_mail($to, $subject, $message, $headers = '', $attachments = [])
155
  }
156
  }
157
 
158
- // Attempt to apply external filters to these values before using our own.
159
- if (has_filter('wp_mail_from')) {
160
- $from_email = apply_filters(
161
- 'wp_mail_from',
162
- isset($from_email) ? $from_email : null
163
- );
164
- }
165
-
166
- if (has_filter('wp_mail_from_name')) {
167
- $from_name = apply_filters(
168
- 'wp_mail_from_name',
169
- isset($from_name) ? $from_name : null
170
- );
171
- }
172
-
173
  // From email and name
174
  // If we don't have a name from the input headers
175
  if (!isset($from_name) && !empty($mailgun['from-name'])) {
@@ -197,12 +207,27 @@ function wp_mail($to, $subject, $message, $headers = '', $attachments = [])
197
  $from_email = 'wordpress@'.$sitename;
198
  }
199
 
200
- $body = [
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
  'from' => "{$from_name} <{$from_email}>",
202
  'to' => $to,
203
  'subject' => $subject,
204
  'text' => $message,
205
- ];
206
 
207
  $body['o:tag'] = '';
208
  $body['o:tracking-clicks'] = !empty($mailgun['track-clicks']) ? $mailgun['track-clicks'] : 'no';
@@ -246,7 +271,7 @@ function wp_mail($to, $subject, $message, $headers = '', $attachments = [])
246
  fclose($tmp);
247
 
248
  // Get mime type with mime_content_type
249
- $content_type = mime_content_type($tmppath);
250
 
251
  // Remove the tmpfile
252
  unlink($tmppath);
@@ -332,11 +357,13 @@ function wp_mail($to, $subject, $message, $headers = '', $attachments = [])
332
 
333
  $payload .= '--'.$boundary.'--';
334
 
335
- $data = [
336
  'body' => $payload,
337
- 'headers' => ['Authorization' => 'Basic '.base64_encode("api:{$apiKey}"),
338
- 'Content-Type' => 'multipart/form-data; boundary='.$boundary, ],
339
- ];
 
 
340
 
341
  $url = "https://api.mailgun.net/v3/{$domain}/messages";
342
 
27
  *
28
  * @return string Last error that occurred.
29
  *
30
+ * @since 1.5.0
31
  */
32
  function mg_api_last_error($error = null)
33
  {
43
  }
44
  }
45
 
46
+ /**
47
+ * Tries several methods to get the MIME Content-Type of a file.
48
+ *
49
+ * @param string $filepath
50
+ * @param string $default_type If all methods fail, fallback to $default_type
51
+ *
52
+ * @return string Content-Type
53
+ *
54
+ * @since 1.5.4
55
+ */
56
+ function get_mime_content_type($filepath, $default_type = 'text/plain')
57
+ {
58
+ if (function_exists('mime_content_type')) {
59
+ return mime_content_type($filepath);
60
+ } elseif (function_exists('finfo_file')) {
61
+ $fi = finfo_open(FILEINFO_MIME_TYPE);
62
+ $ret = finfo_file($fi, $filepath);
63
+ finfo_close($fi);
64
+
65
+ return $ret;
66
+ } else {
67
+ return $default_type;
68
+ }
69
+ }
70
+
71
  /**
72
  * wp_mail function to be loaded in to override the core wp_mail function
73
  * from wp-includes/pluggable.php.
85
  *
86
  * @since 0.1
87
  */
88
+ function wp_mail($to, $subject, $message, $headers = '', $attachments = array())
89
  {
90
  // Compact the input, apply the filters, and extract them back out
91
  extract(apply_filters('wp_mail', compact('to', 'subject', 'message', 'headers', 'attachments')));
104
 
105
  // Headers
106
  if (empty($headers)) {
107
+ $headers = array();
108
  } else {
109
  if (!is_array($headers)) {
110
  // Explode the headers out, so this function can take both
113
  } else {
114
  $tempheaders = $headers;
115
  }
116
+ $headers = array();
117
+ $cc = array();
118
+ $bcc = array();
119
 
120
  // If it's actually got contents
121
  if (!empty($tempheaders)) {
180
  }
181
  }
182
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  // From email and name
184
  // If we don't have a name from the input headers
185
  if (!isset($from_name) && !empty($mailgun['from-name'])) {
207
  $from_email = 'wordpress@'.$sitename;
208
  }
209
 
210
+ // Attempt to apply external filters to these values before using our own.
211
+ if (has_filter('wp_mail_from')) {
212
+ $from_email = apply_filters(
213
+ 'wp_mail_from',
214
+ $from_email
215
+ );
216
+ }
217
+
218
+ if (has_filter('wp_mail_from_name')) {
219
+ $from_name = apply_filters(
220
+ 'wp_mail_from_name',
221
+ $from_name
222
+ );
223
+ }
224
+
225
+ $body = array(
226
  'from' => "{$from_name} <{$from_email}>",
227
  'to' => $to,
228
  'subject' => $subject,
229
  'text' => $message,
230
+ );
231
 
232
  $body['o:tag'] = '';
233
  $body['o:tracking-clicks'] = !empty($mailgun['track-clicks']) ? $mailgun['track-clicks'] : 'no';
271
  fclose($tmp);
272
 
273
  // Get mime type with mime_content_type
274
+ $content_type = get_mime_content_type($tmppath, 'text/plain');
275
 
276
  // Remove the tmpfile
277
  unlink($tmppath);
357
 
358
  $payload .= '--'.$boundary.'--';
359
 
360
+ $data = array(
361
  'body' => $payload,
362
+ 'headers' => array(
363
+ 'Authorization' => 'Basic '.base64_encode("api:{$apiKey}"),
364
+ 'Content-Type' => 'multipart/form-data; boundary='.$boundary,
365
+ ),
366
+ );
367
 
368
  $url = "https://api.mailgun.net/v3/{$domain}/messages";
369
 
mailgun.php CHANGED
@@ -23,7 +23,7 @@
23
  * Plugin Name: Mailgun
24
  * Plugin URI: http://wordpress.org/extend/plugins/mailgun/
25
  * Description: Mailgun integration for WordPress
26
- * Version: 1.5.3
27
  * Author: Mailgun
28
  * Author URI: http://www.mailgun.com/
29
  * License: GPLv2 or later
@@ -136,7 +136,7 @@ class Mailgun
136
  *
137
  * @since 0.1
138
  */
139
- public function api_call($uri, $params = [], $method = 'POST')
140
  {
141
  $options = get_option('mailgun');
142
  $apiKey = (defined('MAILGUN_APIKEY') && MAILGUN_APIKEY) ? MAILGUN_APIKEY : $options['apiKey'];
@@ -144,7 +144,9 @@ class Mailgun
144
 
145
  $time = time();
146
  $url = $this->api_endpoint.$uri;
147
- $headers = ['Authorization' => 'Basic '.base64_encode("api:{$apiKey}")];
 
 
148
 
149
  switch ($method) {
150
  case 'GET':
@@ -163,12 +165,12 @@ class Mailgun
163
  }
164
 
165
  // make the request
166
- $args = [
167
  'method' => $method,
168
  'body' => $params,
169
  'headers' => $headers,
170
  'sslverify' => true,
171
- ];
172
 
173
  // make the remote request
174
  $result = wp_remote_request($url, $args);
@@ -188,9 +190,9 @@ class Mailgun
188
  */
189
  public function get_lists()
190
  {
191
- $results = [];
192
 
193
- $lists_json = $this->api_call('lists', [], 'GET');
194
  $lists_arr = json_decode($lists_json, true);
195
  if (isset($lists_arr['items']) && !empty($lists_arr['items'])) {
196
  $results = $lists_arr['items'];
@@ -208,7 +210,7 @@ class Mailgun
208
  */
209
  public function add_list()
210
  {
211
- $response = [];
212
 
213
  $name = isset($_POST['name']) ? $_POST['name'] : null;
214
  $email = isset($_POST['email']) ? $_POST['email'] : null;
@@ -219,10 +221,10 @@ class Mailgun
219
  foreach ($list_addresses as $address => $val) {
220
  $response[] = $this->api_call(
221
  "lists/{$address}/members",
222
- [
223
  'address' => $email,
224
  'name' => $name,
225
- ]
226
  );
227
  }
228
 
@@ -243,7 +245,7 @@ class Mailgun
243
  *
244
  * @since 0.1
245
  */
246
- public function list_form($list_address, $args = [], $instance = [])
247
  {
248
  $widget_class_id = "mailgun-list-widget-{$args['widget_id']}";
249
  $form_class_id = "list-form-{$args['widget_id']}";
23
  * Plugin Name: Mailgun
24
  * Plugin URI: http://wordpress.org/extend/plugins/mailgun/
25
  * Description: Mailgun integration for WordPress
26
+ * Version: 1.5.4
27
  * Author: Mailgun
28
  * Author URI: http://www.mailgun.com/
29
  * License: GPLv2 or later
136
  *
137
  * @since 0.1
138
  */
139
+ public function api_call($uri, $params = array(), $method = 'POST')
140
  {
141
  $options = get_option('mailgun');
142
  $apiKey = (defined('MAILGUN_APIKEY') && MAILGUN_APIKEY) ? MAILGUN_APIKEY : $options['apiKey'];
144
 
145
  $time = time();
146
  $url = $this->api_endpoint.$uri;
147
+ $headers = array(
148
+ 'Authorization' => 'Basic '.base64_encode("api:{$apiKey}")
149
+ );
150
 
151
  switch ($method) {
152
  case 'GET':
165
  }
166
 
167
  // make the request
168
+ $args = array(
169
  'method' => $method,
170
  'body' => $params,
171
  'headers' => $headers,
172
  'sslverify' => true,
173
+ );
174
 
175
  // make the remote request
176
  $result = wp_remote_request($url, $args);
190
  */
191
  public function get_lists()
192
  {
193
+ $results = array();
194
 
195
+ $lists_json = $this->api_call('lists', array(), 'GET');
196
  $lists_arr = json_decode($lists_json, true);
197
  if (isset($lists_arr['items']) && !empty($lists_arr['items'])) {
198
  $results = $lists_arr['items'];
210
  */
211
  public function add_list()
212
  {
213
+ $response = array();
214
 
215
  $name = isset($_POST['name']) ? $_POST['name'] : null;
216
  $email = isset($_POST['email']) ? $_POST['email'] : null;
221
  foreach ($list_addresses as $address => $val) {
222
  $response[] = $this->api_call(
223
  "lists/{$address}/members",
224
+ array(
225
  'address' => $email,
226
  'name' => $name,
227
+ )
228
  );
229
  }
230
 
245
  *
246
  * @since 0.1
247
  */
248
+ public function list_form($list_address, $args = array(), $instance = array())
249
  {
250
  $widget_class_id = "mailgun-list-widget-{$args['widget_id']}";
251
  $form_class_id = "list-form-{$args['widget_id']}";
readme.txt CHANGED
@@ -5,7 +5,7 @@ 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.7
8
- Stable tag: 1.5.3
9
  License: GPLv2 or later
10
 
11
 
@@ -68,6 +68,11 @@ MAILGUN_SECURE Type: boolean
68
 
69
  == Changelog ==
70
 
 
 
 
 
 
71
  = 1.5.3 (2016-12-22): =
72
  * Changed all bracketed array usages to `array()` syntax for older PHP support
73
  * Redesigned `Content-Type` processing code to not make such large assumptions
5
  Tags: mailgun, smtp, http, api, mail, email
6
  Requires at least: 3.3
7
  Tested up to: 4.7
8
+ Stable tag: 1.5.4
9
  License: GPLv2 or later
10
 
11
 
68
 
69
  == Changelog ==
70
 
71
+ = 1.5.4 (2016-12-23): =
72
+ * Changed some missed bracketed array usages to `array()` syntax
73
+ * Fix `wp_mail_from` / `wp_mail_from_name` not working on old PHP / WP versions
74
+ * Add a wrapper for using `mime_content_type` / `finfo_file`
75
+
76
  = 1.5.3 (2016-12-22): =
77
  * Changed all bracketed array usages to `array()` syntax for older PHP support
78
  * Redesigned `Content-Type` processing code to not make such large assumptions