Skip to content

Commit

Permalink
= 1.9.6 (2023-09-10): = (#168)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
oleksandr-mykhailenko authored Sep 18, 2023
1 parent b798984 commit 45037ca
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 60 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
Changelog
=========
1.9.6 (2023-09-10)
- 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.9.5 (2023-06-20)
- Fix bug with sending emails

Expand Down
63 changes: 39 additions & 24 deletions includes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function init()
* @return void
*
*/
public function admin_menu()
public function admin_menu(): void
{
if (current_user_can('manage_options')) {
$this->hook_suffix = add_options_page(__('Mailgun', 'mailgun'), __('Mailgun', 'mailgun'),
Expand All @@ -121,7 +121,7 @@ public function admin_menu()
* @return void
*
*/
public function admin_js()
public function admin_js(): void
{
wp_enqueue_script('jquery');
}
Expand All @@ -130,10 +130,13 @@ public function admin_js()
* Output JS to footer for enhanced admin page functionality.
*
*/
public function admin_footer_js()
public function admin_footer_js(): void
{
?>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css" >
<script src="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>
<script type="text/javascript">

/* <![CDATA[ */
var mailgunApiOrNot = function () {
if (jQuery('#mailgun-api').val() == 1) {
Expand Down Expand Up @@ -173,13 +176,16 @@ public function admin_footer_js()
jQuery('#mailgun-test').val('<?php _e('Test Configuration', 'mailgun'); ?>')
})
.success(function (data) {
alert(
'Mailgun ' + data.method + ' Test ' + data.message
+ '; status "' + data.error + '"'
)
if (typeof data.message !== 'undefined' && data.message === 'Failure') {
toastr.error('Mailgun ' + data.method + ' Test ' + data.message
+ '; status "' + data.error + '"');
} else {
toastr.success('Mailgun ' + data.method + ' Test ' + data.message
+ '; status "' + data.error + '"');
}
})
.error(function () {
alert('Mailgun Test <?php _e('Failure', 'mailgun'); ?>')
toastr.error('Mailgun Test <?php _e('Failure', 'mailgun'); ?>')
})
})
jQuery('#mailgun-form').change(function () {
Expand All @@ -197,7 +203,7 @@ public function admin_footer_js()
* @return void
*
*/
public function options_page()
public function options_page(): void
{
if (!@include 'options-page.php') {
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>',
Expand All @@ -211,7 +217,7 @@ public function options_page()
* @return void
*
*/
public function lists_page()
public function lists_page(): void
{
if (!@include 'lists-page.php') {
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>',
Expand All @@ -227,7 +233,7 @@ public function lists_page()
* @return void
*
*/
public function admin_init()
public function admin_init(): void
{
$this->register_settings();

Expand All @@ -240,7 +246,7 @@ public function admin_init()
* @return void
*
*/
public function register_settings()
public function register_settings(): void
{
register_setting('mailgun', 'mailgun', array(&$this, 'validation'));
}
Expand All @@ -253,7 +259,7 @@ public function register_settings()
* @return array
*
*/
public function validation(array $options)
public function validation(array $options): array
{
$apiKey = trim($options['apiKey']);
$username = trim($options['username']);
Expand Down Expand Up @@ -303,12 +309,15 @@ public function validation(array $options)
* @return void
*
*/
public function admin_notices()
public function admin_notices(): void
{
$screen = get_current_screen();
if (!current_user_can('manage_options') || $screen->id == $this->hook_suffix):
if (!isset($screen)) {
return;
endif;
}
if (!current_user_can('manage_options') || $screen->id === $this->hook_suffix) {
return;
}

$smtpPasswordUndefined = (!$this->get_option('password') && (!defined('MAILGUN_PASSWORD') || !MAILGUN_PASSWORD));
$smtpActiveNotConfigured = ($this->get_option('useAPI') === '0' && $smtpPasswordUndefined);
Expand Down Expand Up @@ -362,7 +371,7 @@ public function admin_notices()
* @return array
*
*/
public function filter_plugin_actions($links)
public function filter_plugin_actions($links): array
{
$settings_link = '<a href="' . menu_page_url('mailgun', false) . '">' . __('Settings', 'mailgun') . '</a>';
array_unshift($links, $settings_link);
Expand Down Expand Up @@ -427,13 +436,19 @@ public function ajax_send_test()
), JSON_THROW_ON_ERROR)
);
}
$result = wp_mail(
$admin_email,
__('Mailgun WordPress Plugin Test', 'mailgun'),
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.",
'mailgun'), $region, $method),
['Content-Type: text/plain']
);

try {
$result = wp_mail(
$admin_email,
__('Mailgun WordPress Plugin Test', 'mailgun'),
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.",
'mailgun'), $region, $method),
['Content-Type: text/plain']
);
} catch (Throwable $throwable) {
//Log purpose
}




Expand Down
41 changes: 41 additions & 0 deletions includes/mg-filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,44 @@ function mg_smtp_get_region($getRegion)
return false;
}
}

/**
* Override WP VIP GO filter
* It sets default email address to `[email protected]`
*/
if ((defined('WPCOM_IS_VIP_ENV') && WPCOM_IS_VIP_ENV) || (defined('VIP_GO_ENV') && VIP_GO_ENV)) {

global $mg_from_mail;

/**
* @param string $from_mail
* @return mixed
*/
function mg_wp_mail_from_standard(string $from_mail)
{
global $mg_from_mail;

$mg_from_mail = $from_mail;

return $from_mail;
}

add_filter('wp_mail_from', 'mg_wp_mail_from_standard', 0);

/**
* @param string $from_mail
* @return mixed
*/
function mg_wp_mail_from_new(string $from_mail)
{
global $mg_from_mail;

if (!empty($mg_from_mail) && is_email($mg_from_mail)) {
return $mg_from_mail;
}

return $from_mail;
}

add_filter('wp_mail_from', 'mg_wp_mail_from_new', 2);
}
24 changes: 18 additions & 6 deletions includes/widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ public function __construct()
);
}

// Creating widget front-end
// This is where the action happens
/**
* @param $args
* @param $instance
* @return void
* @throws JsonException
*/
public function widget($args, $instance)
{
$mailgun = Mailgun::getInstance();
Expand All @@ -61,6 +65,11 @@ public function widget($args, $instance)
}

// Widget Backend

/**
* @param $instance
* @return string|void
*/
public function form($instance)
{
if (isset($instance['list_address'])) {
Expand Down Expand Up @@ -102,11 +111,14 @@ public function form($instance)
}

// Updating widget replacing old instances with new

/**
* @param $new_instance
* @param $old_instance
* @return array
*/
public function update($new_instance, $old_instance)
{
$instance = array();
$instance = $new_instance;

return $instance;
return $new_instance;
}
}
56 changes: 33 additions & 23 deletions includes/wp-mail-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,11 @@
/**
* mg_api_last_error is a compound getter/setter for the last error that was
* encountered during a Mailgun API call.
*
* @param string $error OPTIONAL
*
* @return string Last error that occurred.
*
* @param string|null $error OPTIONAL
* @return string|null Last error that occurred.
* @since 1.5.0
*/
function mg_api_last_error($error = null)
function mg_api_last_error(string $error = null): ?string
{
static $last_error;

Expand All @@ -61,7 +58,11 @@ function mg_api_last_error($error = null)
* @since 1.5.7
*/
add_filter('mg_mutate_to_rcpt_vars', 'mg_mutate_to_rcpt_vars_cb');
function mg_mutate_to_rcpt_vars_cb($to_addrs)
/**
* @param $to_addrs
* @return array
*/
function mg_mutate_to_rcpt_vars_cb($to_addrs): array
{
if (is_string($to_addrs)) {
$to_addrs = explode(',', $to_addrs);
Expand All @@ -79,7 +80,7 @@ function mg_mutate_to_rcpt_vars_cb($to_addrs)

return [
'to' => '%recipient%',
'rcpt_vars' => json_encode($rcpt_vars),
'rcpt_vars' => json_encode($rcpt_vars, JSON_THROW_ON_ERROR),
];
}
}
Expand Down Expand Up @@ -473,7 +474,12 @@ function wp_mail($to, $subject, $message, $headers = '', $attachments = [])
}
}

function mg_build_payload_from_body($body, $boundary)
/**
* @param $body
* @param $boundary
* @return string
*/
function mg_build_payload_from_body($body, $boundary): string
{
$payload = '';

Expand All @@ -500,25 +506,29 @@ function mg_build_payload_from_body($body, $boundary)
return $payload;
}

function mg_build_attachments_payload($attachments, $boundary)
/**
* @param $attachments
* @param $boundary
* @return string|null
*/
function mg_build_attachments_payload($attachments, $boundary): ?string
{
$payload = '';

if (empty($attachments)) {
return null;
}
// If we have attachments, add them to the payload.
if (!empty($attachments)) {
$i = 0;
foreach ($attachments as $attachment) {
if (!empty($attachment)) {
$payload .= '--' . $boundary;
$payload .= "\r\n";
$payload .= 'Content-Disposition: form-data; name="attachment[' . $i . ']"; filename="' . basename($attachment) . '"' . "\r\n\r\n";
$payload .= file_get_contents($attachment);
$payload .= "\r\n";
$i++;
}
$i = 0;
foreach ($attachments as $attachment) {
if (!empty($attachment)) {
$payload .= '--' . $boundary;
$payload .= "\r\n";
$payload .= 'Content-Disposition: form-data; name="attachment[' . $i . ']"; filename="' . basename($attachment) . '"' . "\r\n\r\n";
$payload .= file_get_contents($attachment);
$payload .= "\r\n";
$i++;
}
} else {
return null;
}

return $payload;
Expand Down
4 changes: 2 additions & 2 deletions mailgun.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* Plugin Name: Mailgun
* Plugin URI: http://wordpress.org/extend/plugins/mailgun/
* Description: Mailgun integration for WordPress
* Version: 1.9.5
* Tested up to: 6.1
* Version: 1.9.6
* Tested up to: 6.3.1
* Author: Mailgun
* Author URI: http://www.mailgun.com/
* License: GPLv2 or later
Expand Down
11 changes: 8 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ Mailgun for WordPress
Contributors: mailgun, sivel, lookahead.io, m35dev
Tags: mailgun, smtp, http, api, mail, email
Requires at least: 3.3
Tested up to: 6.1.1
Stable tag: 1.9.5
Requires PHP: 5.6
Tested up to: 6.3.1
Stable tag: 1.9.6
Requires PHP: 7.4
License: GPLv2 or later

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

== Changelog ==

= 1.9.6 (2023-09-10): =
- 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.9.5 (2023-06-20): =
- Fix bug with sending emails

Expand Down
Loading

0 comments on commit 45037ca

Please sign in to comment.