Skip to content

Commit 77dcd63

Browse files
Admin notice improvements. (#174)
Small code cleanup
1 parent 41b8d45 commit 77dcd63

8 files changed

Lines changed: 62 additions & 62 deletions

File tree

includes/admin.php

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,16 @@ public function __construct()
6161
add_action('wp_ajax_mailgun-test', [&$this, 'ajax_send_test']);
6262
}
6363

64-
/**
65-
* Adds the default options during plugin activation.
66-
*
67-
* @return void
68-
*
69-
*/
70-
public function activation() {
71-
if (!$this->options) {
72-
$this->options = $this->defaults;
73-
add_option('mailgun', $this->options);
74-
}
64+
/**
65+
* Adds the default options during plugin activation.
66+
* @return void
67+
*/
68+
public function activation(): void
69+
{
70+
if (!$this->options) {
71+
$this->options = $this->defaults;
72+
add_option('mailgun', $this->options);
73+
}
7574
}
7675

7776

@@ -81,7 +80,7 @@ public function activation() {
8180
* @return void
8281
*
8382
*/
84-
public function init()
83+
public function init(): void
8584
{
8685
$sitename = sanitize_text_field(strtolower($_SERVER['SERVER_NAME']));
8786
if (substr($sitename, 0, 4) === 'www.') {
@@ -338,26 +337,25 @@ public function admin_notices(): void
338337
$apiKeyUndefined = (!$this->get_option('apiKey') && (!defined('MAILGUN_APIKEY') || !MAILGUN_APIKEY));
339338
$apiActiveNotConfigured = ($this->get_option('useAPI') === '1' && ($apiRegionUndefined || $apiKeyUndefined));
340339

341-
if ($apiActiveNotConfigured || $smtpActiveNotConfigured):
342-
?>
340+
if ($_SESSION['settings_turned_of'] === false && ($apiActiveNotConfigured || $smtpActiveNotConfigured) ) { ?>
343341
<div id='mailgun-warning' class='notice notice-warning is-dismissible'>
344342
<p>
345343
<?php
346344
printf(
347-
__('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>',
345+
__('Use HTTP API is turned off or you do not have SMTP credentials. You can configure your Mailgun settings in your wp-config.php file or <a href="%1$s">here</a>',
348346
'mailgun'),
349347
menu_page_url('mailgun', false)
350348
);
351349
?>
352350
</p>
353351
</div>
354-
<?php
355-
endif;
352+
<?php $_SESSION['settings_turned_of'] = true; ?>
353+
<?php } ?>
356354

355+
<?php
357356
if ($this->get_option('override-from') === '1' &&
358357
(!$this->get_option('from-name') || !$this->get_option('from-address'))
359-
):
360-
?>
358+
) { ?>
361359
<div id='mailgun-warning' class='notice notice-warning is-dismissible'>
362360
<p>
363361
<strong>
@@ -372,8 +370,7 @@ public function admin_notices(): void
372370
?>
373371
</p>
374372
</div>
375-
<?php
376-
endif;
373+
<?php }
377374
}
378375

379376
/**
@@ -384,7 +381,7 @@ public function admin_notices(): void
384381
* @return array
385382
*
386383
*/
387-
public function filter_plugin_actions($links): array
384+
public function filter_plugin_actions(array $links): array
388385
{
389386
$settings_link = '<a href="' . menu_page_url('mailgun', false) . '">' . __('Settings', 'mailgun') . '</a>';
390387
array_unshift($links, $settings_link);
@@ -462,9 +459,6 @@ public function ajax_send_test()
462459
//Log purpose
463460
}
464461

465-
466-
467-
468462
if ($useAPI) {
469463
if (!function_exists('mg_api_last_error')) {
470464
if (!include __DIR__ . '/wp-mail-api.php') {

includes/lists-page.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
$api_key = (defined('MAILGUN_APIKEY') && MAILGUN_APIKEY) ? MAILGUN_APIKEY : $this->get_option('apiKey');
2727
$mailgun_domain = (defined('MAILGUN_DOMAIN') && MAILGUN_DOMAIN) ? MAILGUN_DOMAIN : $this->get_option('domain');
2828

29-
if ($api_key != '') {
30-
if ($mailgun_domain == '') {
29+
if ($api_key !== '') {
30+
if ($mailgun_domain === '') {
3131
$missing_error = '<strong style="color:red;">Missing or invalid Mailgun Domain</strong>. ';
3232
}
3333
} else {

includes/mg-filter.php

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,19 @@ function mg_detect_from_name($from_name_header = null)
8080
$from_name = $from_name_header;
8181
} elseif (defined('MAILGUN_FROM_NAME') && MAILGUN_FROM_NAME) {
8282
$from_name = MAILGUN_FROM_NAME;
83-
} else {
84-
if (empty($mg_from_name)) {
85-
if (function_exists('get_current_site')) {
86-
$from_name = get_current_site()->site_name;
87-
} else {
88-
$from_name = 'WordPress';
89-
}
83+
} else if (empty($mg_from_name)) {
84+
if (function_exists('get_current_site')) {
85+
$from_name = get_current_site()->site_name;
9086
} else {
91-
$from_name = $mg_from_name;
87+
$from_name = 'WordPress';
9288
}
89+
} else {
90+
$from_name = $mg_from_name;
9391
}
9492

9593
$filter_from_name = null;
9694

97-
if ((!isset($mg_override_from) || $mg_override_from == '0') && has_filter('wp_mail_from_name')) {
95+
if ((!isset($mg_override_from) || $mg_override_from === '0') && has_filter('wp_mail_from_name')) {
9896
$filter_from_name = apply_filters(
9997
'wp_mail_from_name',
10098
$from_name
@@ -148,25 +146,23 @@ function mg_detect_from_address($from_addr_header = null): string
148146
$from_addr = $from_addr_header;
149147
} elseif (defined('MAILGUN_FROM_ADDRESS') && MAILGUN_FROM_ADDRESS) {
150148
$from_addr = MAILGUN_FROM_ADDRESS;
151-
} else {
152-
if (empty($mg_from_addr)) {
153-
if (function_exists('get_current_site')) {
154-
$sitedomain = get_current_site()->domain;
155-
} else {
156-
$sitedomain = strtolower(sanitize_text_field($_SERVER['SERVER_NAME']));
157-
if (substr($sitedomain, 0, 4) === 'www.') {
158-
$sitedomain = substr($sitedomain, 4);
159-
}
160-
}
161-
162-
$from_addr = 'wordpress@' . $sitedomain;
149+
} else if (empty($mg_from_addr)) {
150+
if (function_exists('get_current_site')) {
151+
$sitedomain = get_current_site()->domain;
163152
} else {
164-
$from_addr = $mg_from_addr;
153+
$sitedomain = strtolower(sanitize_text_field($_SERVER['SERVER_NAME']));
154+
if (substr($sitedomain, 0, 4) === 'www.') {
155+
$sitedomain = substr($sitedomain, 4);
156+
}
165157
}
158+
159+
$from_addr = 'wordpress@' . $sitedomain;
160+
} else {
161+
$from_addr = $mg_from_addr;
166162
}
167163

168164
$filter_from_addr = null;
169-
if ((!isset($mg_override_from) || $mg_override_from == '0') && has_filter('wp_mail_from')) {
165+
if ((!isset($mg_override_from) || $mg_override_from === '0') && has_filter('wp_mail_from')) {
170166
$filter_from_addr = apply_filters(
171167
'wp_mail_from',
172168
$from_addr
@@ -214,7 +210,7 @@ function mg_parse_headers($headers = []): array
214210
$tmp = $headers;
215211
}
216212

217-
$new_headers = array();
213+
$new_headers = [];
218214
if (!empty($tmp)) {
219215
$name = null;
220216
$value = null;
@@ -242,14 +238,14 @@ function mg_parse_headers($headers = []): array
242238
$value = trim($value);
243239

244240
if (!isset($new_headers[$name])) {
245-
$new_headers[$name] = array();
241+
$new_headers[$name] = [];
246242
}
247243

248-
$new_headers[$name][] = array(
244+
$new_headers[$name][] = [
249245
'value' => $value,
250246
'boundary' => $boundary,
251247
'parts' => $parts,
252-
);
248+
];
253249
}
254250
}
255251

@@ -268,7 +264,7 @@ function mg_parse_headers($headers = []): array
268264
*/
269265
function mg_dump_headers($headers = null): string
270266
{
271-
if (is_null($headers) || !is_array($headers)) {
267+
if (!is_array($headers)) {
272268
return '';
273269
}
274270

includes/options-page.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@
5656
<div class="wrap">
5757
<div id="icon-options-general" class="icon32"><br/></div>
5858
<span class="alignright">
59-
<a target="_blank" href="http://www.mailgun.com/">
60-
<img src="<?php echo esc_attr($icon) ?>" alt="Mailgun" style="width:50px;"/>
61-
</a>
62-
</span>
59+
<a target="_blank" href="http://www.mailgun.com/">
60+
<img src="<?php echo esc_attr($icon) ?>" alt="Mailgun" style="width:50px;"/>
61+
</a>
62+
</span>
6363
<h2><?php _e('Mailgun', 'mailgun'); ?></h2>
6464

6565
<p>

includes/wp-mail-api.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ function mg_api_last_error(string $error = null): ?string
5858
* @since 1.5.7
5959
*/
6060
add_filter('mg_mutate_to_rcpt_vars', 'mg_mutate_to_rcpt_vars_cb');
61+
6162
/**
6263
* @param $to_addrs
6364
* @return array
65+
* @throws JsonException
6466
*/
6567
function mg_mutate_to_rcpt_vars_cb($to_addrs): array
6668
{

mailgun.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: Mailgun
44
* Plugin URI: http://wordpress.org/extend/plugins/mailgun/
55
* Description: Mailgun integration for WordPress
6-
* Version: 1.9.7
6+
* Version: 1.9.8
77
* Requires PHP: 7.4
88
* Requires at least: 4.4
99
* Author: Mailgun

readme.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Mailgun for WordPress
44
Contributors: mailgun, sivel, lookahead.io, m35dev, alanfuller
55
Tags: mailgun, smtp, http, api, mail, email
66
Tested up to: 6.4
7-
Stable tag: 1.9.7
7+
Stable tag: 1.9.8
88
License: GPLv2 or later
99

1010
Easily send email from your WordPress site through Mailgun using the HTTP API or SMTP.
@@ -130,6 +130,10 @@ MAILGUN_TRACK_OPENS Type: string Choices: 'yes' or 'no'
130130

131131

132132
== Changelog ==
133+
= 1.9.8 (2024-02-25): =
134+
- Improve admin notices about not fully configured plugin
135+
- Small code cleaning
136+
133137
= 1.9.7 (2024-01-03): =
134138
- Ensure defaults are always set to remove warnings in PHP 8.1+
135139

readme.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Mailgun for WordPress
44
Contributors: mailgun, sivel, lookahead.io, m35dev, alanfuller
55
Tags: mailgun, smtp, http, api, mail, email
66
Tested up to: 6.4
7-
Stable tag: 1.9.7
7+
Stable tag: 1.9.8
88
License: GPLv2 or later
99

1010
Easily send email from your WordPress site through Mailgun using the HTTP API or SMTP.
@@ -128,6 +128,10 @@ MAILGUN_TRACK_OPENS Type: string Choices: 'yes' or 'no'
128128

129129

130130
== Changelog ==
131+
= 1.9.8 (2024-02-25): =
132+
- Improve admin notices about not fully configured plugin
133+
- Small code cleaning
134+
131135
= 1.9.7 (2024-01-03): =
132136
- Ensure defaults are always set to remove warnings in PHP 8.1+
133137

0 commit comments

Comments
 (0)