Skip to content

Commit 45037ca

Browse files
= 1.9.6 (2023-09-10): = (#168)
- Added typehints to methods - Added code for making plugin WP VIP GO compatible for the `From mail` - Added Toastr for sending nice message instead of using alerts
1 parent b798984 commit 45037ca

8 files changed

Lines changed: 153 additions & 60 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
Changelog
22
=========
3+
1.9.6 (2023-09-10)
4+
- Added typehints to methods
5+
- Added code for making plugin WP VIP GO compatible for the `From mail`
6+
- Added Toastr for sending nice message instead of using alerts
7+
38
1.9.5 (2023-06-20)
49
- Fix bug with sending emails
510

includes/admin.php

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function init()
102102
* @return void
103103
*
104104
*/
105-
public function admin_menu()
105+
public function admin_menu(): void
106106
{
107107
if (current_user_can('manage_options')) {
108108
$this->hook_suffix = add_options_page(__('Mailgun', 'mailgun'), __('Mailgun', 'mailgun'),
@@ -121,7 +121,7 @@ public function admin_menu()
121121
* @return void
122122
*
123123
*/
124-
public function admin_js()
124+
public function admin_js(): void
125125
{
126126
wp_enqueue_script('jquery');
127127
}
@@ -130,10 +130,13 @@ public function admin_js()
130130
* Output JS to footer for enhanced admin page functionality.
131131
*
132132
*/
133-
public function admin_footer_js()
133+
public function admin_footer_js(): void
134134
{
135135
?>
136+
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css" >
137+
<script src="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>
136138
<script type="text/javascript">
139+
137140
/* <![CDATA[ */
138141
var mailgunApiOrNot = function () {
139142
if (jQuery('#mailgun-api').val() == 1) {
@@ -173,13 +176,16 @@ public function admin_footer_js()
173176
jQuery('#mailgun-test').val('<?php _e('Test Configuration', 'mailgun'); ?>')
174177
})
175178
.success(function (data) {
176-
alert(
177-
'Mailgun ' + data.method + ' Test ' + data.message
178-
+ '; status "' + data.error + '"'
179-
)
179+
if (typeof data.message !== 'undefined' && data.message === 'Failure') {
180+
toastr.error('Mailgun ' + data.method + ' Test ' + data.message
181+
+ '; status "' + data.error + '"');
182+
} else {
183+
toastr.success('Mailgun ' + data.method + ' Test ' + data.message
184+
+ '; status "' + data.error + '"');
185+
}
180186
})
181187
.error(function () {
182-
alert('Mailgun Test <?php _e('Failure', 'mailgun'); ?>')
188+
toastr.error('Mailgun Test <?php _e('Failure', 'mailgun'); ?>')
183189
})
184190
})
185191
jQuery('#mailgun-form').change(function () {
@@ -197,7 +203,7 @@ public function admin_footer_js()
197203
* @return void
198204
*
199205
*/
200-
public function options_page()
206+
public function options_page(): void
201207
{
202208
if (!@include 'options-page.php') {
203209
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>',
@@ -211,7 +217,7 @@ public function options_page()
211217
* @return void
212218
*
213219
*/
214-
public function lists_page()
220+
public function lists_page(): void
215221
{
216222
if (!@include 'lists-page.php') {
217223
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>',
@@ -227,7 +233,7 @@ public function lists_page()
227233
* @return void
228234
*
229235
*/
230-
public function admin_init()
236+
public function admin_init(): void
231237
{
232238
$this->register_settings();
233239

@@ -240,7 +246,7 @@ public function admin_init()
240246
* @return void
241247
*
242248
*/
243-
public function register_settings()
249+
public function register_settings(): void
244250
{
245251
register_setting('mailgun', 'mailgun', array(&$this, 'validation'));
246252
}
@@ -253,7 +259,7 @@ public function register_settings()
253259
* @return array
254260
*
255261
*/
256-
public function validation(array $options)
262+
public function validation(array $options): array
257263
{
258264
$apiKey = trim($options['apiKey']);
259265
$username = trim($options['username']);
@@ -303,12 +309,15 @@ public function validation(array $options)
303309
* @return void
304310
*
305311
*/
306-
public function admin_notices()
312+
public function admin_notices(): void
307313
{
308314
$screen = get_current_screen();
309-
if (!current_user_can('manage_options') || $screen->id == $this->hook_suffix):
315+
if (!isset($screen)) {
310316
return;
311-
endif;
317+
}
318+
if (!current_user_can('manage_options') || $screen->id === $this->hook_suffix) {
319+
return;
320+
}
312321

313322
$smtpPasswordUndefined = (!$this->get_option('password') && (!defined('MAILGUN_PASSWORD') || !MAILGUN_PASSWORD));
314323
$smtpActiveNotConfigured = ($this->get_option('useAPI') === '0' && $smtpPasswordUndefined);
@@ -362,7 +371,7 @@ public function admin_notices()
362371
* @return array
363372
*
364373
*/
365-
public function filter_plugin_actions($links)
374+
public function filter_plugin_actions($links): array
366375
{
367376
$settings_link = '<a href="' . menu_page_url('mailgun', false) . '">' . __('Settings', 'mailgun') . '</a>';
368377
array_unshift($links, $settings_link);
@@ -427,13 +436,19 @@ public function ajax_send_test()
427436
), JSON_THROW_ON_ERROR)
428437
);
429438
}
430-
$result = wp_mail(
431-
$admin_email,
432-
__('Mailgun WordPress Plugin Test', 'mailgun'),
433-
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.",
434-
'mailgun'), $region, $method),
435-
['Content-Type: text/plain']
436-
);
439+
440+
try {
441+
$result = wp_mail(
442+
$admin_email,
443+
__('Mailgun WordPress Plugin Test', 'mailgun'),
444+
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.",
445+
'mailgun'), $region, $method),
446+
['Content-Type: text/plain']
447+
);
448+
} catch (Throwable $throwable) {
449+
//Log purpose
450+
}
451+
437452

438453

439454

includes/mg-filter.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,44 @@ function mg_smtp_get_region($getRegion)
331331
return false;
332332
}
333333
}
334+
335+
/**
336+
* Override WP VIP GO filter
337+
* It sets default email address to `donotreply@wordpress.com`
338+
*/
339+
if ((defined('WPCOM_IS_VIP_ENV') && WPCOM_IS_VIP_ENV) || (defined('VIP_GO_ENV') && VIP_GO_ENV)) {
340+
341+
global $mg_from_mail;
342+
343+
/**
344+
* @param string $from_mail
345+
* @return mixed
346+
*/
347+
function mg_wp_mail_from_standard(string $from_mail)
348+
{
349+
global $mg_from_mail;
350+
351+
$mg_from_mail = $from_mail;
352+
353+
return $from_mail;
354+
}
355+
356+
add_filter('wp_mail_from', 'mg_wp_mail_from_standard', 0);
357+
358+
/**
359+
* @param string $from_mail
360+
* @return mixed
361+
*/
362+
function mg_wp_mail_from_new(string $from_mail)
363+
{
364+
global $mg_from_mail;
365+
366+
if (!empty($mg_from_mail) && is_email($mg_from_mail)) {
367+
return $mg_from_mail;
368+
}
369+
370+
return $from_mail;
371+
}
372+
373+
add_filter('wp_mail_from', 'mg_wp_mail_from_new', 2);
374+
}

includes/widget.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ public function __construct()
3333
);
3434
}
3535

36-
// Creating widget front-end
37-
// This is where the action happens
36+
/**
37+
* @param $args
38+
* @param $instance
39+
* @return void
40+
* @throws JsonException
41+
*/
3842
public function widget($args, $instance)
3943
{
4044
$mailgun = Mailgun::getInstance();
@@ -61,6 +65,11 @@ public function widget($args, $instance)
6165
}
6266

6367
// Widget Backend
68+
69+
/**
70+
* @param $instance
71+
* @return string|void
72+
*/
6473
public function form($instance)
6574
{
6675
if (isset($instance['list_address'])) {
@@ -102,11 +111,14 @@ public function form($instance)
102111
}
103112

104113
// Updating widget replacing old instances with new
114+
115+
/**
116+
* @param $new_instance
117+
* @param $old_instance
118+
* @return array
119+
*/
105120
public function update($new_instance, $old_instance)
106121
{
107-
$instance = array();
108-
$instance = $new_instance;
109-
110-
return $instance;
122+
return $new_instance;
111123
}
112124
}

includes/wp-mail-api.php

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,11 @@
2727
/**
2828
* mg_api_last_error is a compound getter/setter for the last error that was
2929
* encountered during a Mailgun API call.
30-
*
31-
* @param string $error OPTIONAL
32-
*
33-
* @return string Last error that occurred.
34-
*
30+
* @param string|null $error OPTIONAL
31+
* @return string|null Last error that occurred.
3532
* @since 1.5.0
3633
*/
37-
function mg_api_last_error($error = null)
34+
function mg_api_last_error(string $error = null): ?string
3835
{
3936
static $last_error;
4037

@@ -61,7 +58,11 @@ function mg_api_last_error($error = null)
6158
* @since 1.5.7
6259
*/
6360
add_filter('mg_mutate_to_rcpt_vars', 'mg_mutate_to_rcpt_vars_cb');
64-
function mg_mutate_to_rcpt_vars_cb($to_addrs)
61+
/**
62+
* @param $to_addrs
63+
* @return array
64+
*/
65+
function mg_mutate_to_rcpt_vars_cb($to_addrs): array
6566
{
6667
if (is_string($to_addrs)) {
6768
$to_addrs = explode(',', $to_addrs);
@@ -79,7 +80,7 @@ function mg_mutate_to_rcpt_vars_cb($to_addrs)
7980

8081
return [
8182
'to' => '%recipient%',
82-
'rcpt_vars' => json_encode($rcpt_vars),
83+
'rcpt_vars' => json_encode($rcpt_vars, JSON_THROW_ON_ERROR),
8384
];
8485
}
8586
}
@@ -473,7 +474,12 @@ function wp_mail($to, $subject, $message, $headers = '', $attachments = [])
473474
}
474475
}
475476

476-
function mg_build_payload_from_body($body, $boundary)
477+
/**
478+
* @param $body
479+
* @param $boundary
480+
* @return string
481+
*/
482+
function mg_build_payload_from_body($body, $boundary): string
477483
{
478484
$payload = '';
479485

@@ -500,25 +506,29 @@ function mg_build_payload_from_body($body, $boundary)
500506
return $payload;
501507
}
502508

503-
function mg_build_attachments_payload($attachments, $boundary)
509+
/**
510+
* @param $attachments
511+
* @param $boundary
512+
* @return string|null
513+
*/
514+
function mg_build_attachments_payload($attachments, $boundary): ?string
504515
{
505516
$payload = '';
506517

518+
if (empty($attachments)) {
519+
return null;
520+
}
507521
// If we have attachments, add them to the payload.
508-
if (!empty($attachments)) {
509-
$i = 0;
510-
foreach ($attachments as $attachment) {
511-
if (!empty($attachment)) {
512-
$payload .= '--' . $boundary;
513-
$payload .= "\r\n";
514-
$payload .= 'Content-Disposition: form-data; name="attachment[' . $i . ']"; filename="' . basename($attachment) . '"' . "\r\n\r\n";
515-
$payload .= file_get_contents($attachment);
516-
$payload .= "\r\n";
517-
$i++;
518-
}
522+
$i = 0;
523+
foreach ($attachments as $attachment) {
524+
if (!empty($attachment)) {
525+
$payload .= '--' . $boundary;
526+
$payload .= "\r\n";
527+
$payload .= 'Content-Disposition: form-data; name="attachment[' . $i . ']"; filename="' . basename($attachment) . '"' . "\r\n\r\n";
528+
$payload .= file_get_contents($attachment);
529+
$payload .= "\r\n";
530+
$i++;
519531
}
520-
} else {
521-
return null;
522532
}
523533

524534
return $payload;

mailgun.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
* Plugin Name: Mailgun
44
* Plugin URI: http://wordpress.org/extend/plugins/mailgun/
55
* Description: Mailgun integration for WordPress
6-
* Version: 1.9.5
7-
* Tested up to: 6.1
6+
* Version: 1.9.6
7+
* Tested up to: 6.3.1
88
* Author: Mailgun
99
* Author URI: http://www.mailgun.com/
1010
* License: GPLv2 or later

readme.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ Mailgun for WordPress
44
Contributors: mailgun, sivel, lookahead.io, m35dev
55
Tags: mailgun, smtp, http, api, mail, email
66
Requires at least: 3.3
7-
Tested up to: 6.1.1
8-
Stable tag: 1.9.5
9-
Requires PHP: 5.6
7+
Tested up to: 6.3.1
8+
Stable tag: 1.9.6
9+
Requires PHP: 7.4
1010
License: GPLv2 or later
1111

1212
Easily send email from your WordPress site through Mailgun using the HTTP API or SMTP.
@@ -133,6 +133,11 @@ MAILGUN_TRACK_OPENS Type: string Choices: 'yes' or 'no'
133133

134134
== Changelog ==
135135

136+
= 1.9.6 (2023-09-10): =
137+
- Added typehints to methods
138+
- Added code for making plugin WP VIP GO compatible for the `From mail`
139+
- Added Toastr for sending nice message instead of using alerts
140+
136141
= 1.9.5 (2023-06-20): =
137142
- Fix bug with sending emails
138143

0 commit comments

Comments
 (0)