Skip to content

Commit

Permalink
Adding admin notice for regions, default to US region if none set (#80)
Browse files Browse the repository at this point in the history
* Adding admin notice for regions, default to US region if none set

* Update changelogs
  • Loading branch information
pirogoeth authored Aug 14, 2018
1 parent 25412a9 commit ce8b780
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 31 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

1.5.13 (2018-08-14)
- Default to US region if no region is configured in settings
- Add admin notification about region configuration
- Log an error message when an email is sent with no explicit region configured

1.5.12.3 (2018-08-09)
- Fix Region select menu default when wp-config.php variable is set
- Fix front end email input validation
Expand Down
31 changes: 29 additions & 2 deletions includes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,14 @@ public function admin_notices()
|| (!$this->get_option('password') && $this->get_option('useAPI') === '0')
) {
?>
<div id='mailgun-warning' class='notice notice-warning fade'><p><strong><?php _e('Mailgun is almost ready. ', 'mailgun'); ?></strong><?php printf(__('You must <a href="%1$s">configure Mailgun</a> for it to work.', 'mailgun'), menu_page_url('mailgun', false)); ?></p></div>
<div id='mailgun-warning' class='notice notice-warning fade'>
<p>
<strong>
<?php _e('Mailgun is almost ready. ', 'mailgun'); ?>
</strong>
<?php printf(__('You must <a href="%1$s">configure Mailgun</a> for it to work.', 'mailgun'), menu_page_url('mailgun', false)); ?>
</p>
</div>
<?php

}
Expand All @@ -338,10 +345,30 @@ public function admin_notices()
|| !$this->get_option('from-address'))
) {
?>
<div id='mailgun-warning' class='notice notice-warning fade'><p><strong><?php _e('Mailgun is almost ready. ', 'mailgun'); ?></strong><?php printf(__('"Override From" option requires that "From Name" and "From Address" be set to work properly! <a href="%1$s">Configure Mailgun now</a>.', 'mailgun'), menu_page_url('mailgun', false)); ?></p></div>
<div id='mailgun-warning' class='notice notice-warning fade'>
<p>
<strong>
<?php _e('Mailgun is almost ready. ', 'mailgun'); ?>
</strong>
<?php printf(__('"Override From" option requires that "From Name" and "From Address" be set to work properly! <a href="%1$s">Configure Mailgun now</a>.', 'mailgun'), menu_page_url('mailgun', false)); ?>
</p>
</div>
<?php

}

if (!$this->get_option('region') && $this->get_option('useAPI') === '1') {
?>
<div id='mailgun-warning' class='notice notice-warning fade'>
<p>
<strong>
<?php _e('Mailgun is almost ready. ', 'mailgun'); ?>
</strong>
<?php printf(__('Mailgun now supports multiple regions! By default, we will use the US region, but we now have an EU region generally available. You can change regions <a href="%1$s">here</a>.', 'mailgun'), menu_page_url('mailgun', false)); ?>
</p>
</div>
<?php
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion includes/options-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<option value="us"<?php selected('us', $this->get_option('region')); ?>><?php _e('U.S./North America', 'mailgun') ?></option>
<option value="eu"<?php selected('eu', $this->get_option('region')); ?>><?php _e('Europe', 'mailgun') ?></option>
</select>
<p class="description"><?php _e('Choose a region - U.S./North America or Europe - from which to send email, and to store your customer data.', 'mailgun') ?></p>
<p class="description"><?php _e('Choose a region - U.S./North America or Europe - from which to send email, and to store your customer data. Please note that your sending domain must be set up in whichever region you choose.', 'mailgun') ?></p>
<?php endif; ?>
</td>
</tr>
Expand Down
16 changes: 11 additions & 5 deletions includes/wp-mail-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,19 @@ function wp_mail($to, $subject, $message, $headers = '', $attachments = array())
extract(apply_filters('wp_mail', compact('to', 'subject', 'message', 'headers', 'attachments')));

$mailgun = get_option('mailgun');
$getRegion = (defined('MAILGUN_REGION') && MAILGUN_REGION) ? MAILGUN_REGION : $mailgun['region'];
$region = (defined('MAILGUN_REGION') && MAILGUN_REGION) ? MAILGUN_REGION : $mailgun['region'];
$apiKey = (defined('MAILGUN_APIKEY') && MAILGUN_APIKEY) ? MAILGUN_APIKEY : $mailgun['apiKey'];
$domain = (defined('MAILGUN_DOMAIN') && MAILGUN_DOMAIN) ? MAILGUN_DOMAIN : $mailgun['domain'];

if (empty($apiKey) || empty($domain) || (bool) !$getRegion) {
return false;
}
if (empty($apiKey) || empty($domain)) {
return false;
}

// If a region is not set via defines or through the options page, default to US region.
if (!((bool) $region)) {
error_log('[Mailgun] No region configuration was found! Defaulting to US region.');
$region = 'us';
}

if (!is_array($attachments)) {
$attachments = explode("\n", str_replace("\r\n", "\n", $attachments));
Expand Down Expand Up @@ -358,7 +364,7 @@ function wp_mail($to, $subject, $message, $headers = '', $attachments = array())
),
);

$endpoint = mg_detect_region($getRegion);
$endpoint = mg_detect_region($region);
$endpoint = ($endpoint) ? $endpoint : 'https://api.mailgun.net/v3/';
$url = $endpoint."{$domain}/messages";

Expand Down
42 changes: 21 additions & 21 deletions mailgun.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin Name: Mailgun
* Plugin URI: http://wordpress.org/extend/plugins/mailgun/
* Description: Mailgun integration for WordPress
* Version: 1.5.12.3
* Version: 1.5.13
* Author: Mailgun
* Author URI: http://www.mailgun.com/
* License: GPLv2 or later
Expand Down Expand Up @@ -78,8 +78,8 @@ public function __construct()
* Get specific option from the options table.
*
* @param string $option Name of option to be used as array key for retrieving the specific value
* @param array $options Array to iterate over for specific values
* @param bool $default False if no options are set
* @param array $options Array to iterate over for specific values
* @param bool $default False if no options are set
*
* @return mixed
*
Expand All @@ -103,7 +103,7 @@ public function get_option($option, $options = null, $default = false)
*
* @param object $phpmailer The PHPMailer object to modify by reference
*
* @return void
* @return void
*
* @since 0.1
*/
Expand Down Expand Up @@ -133,10 +133,10 @@ public function phpmailer_init(&$phpmailer)
* Deactivate this plugin and die.
* Deactivate the plugin when files critical to it's operation cannot be loaded
*
* @param $file Files critical to plugin functionality
*
* @return void
*
* @param $file Files critical to plugin functionality
*
* @return void
*
* @since 0.1
*/
public function deactivate_and_die($file)
Expand All @@ -154,8 +154,8 @@ public function deactivate_and_die($file)
* Make a Mailgun api call.
*
* @param string $uri The endpoint for the Mailgun API
* @param array $params Array of parameters passed to the API
* @param string $method The form request type
* @param array $params Array of parameters passed to the API
* @param string $method The form request type
*
* @return array
*
Expand All @@ -164,8 +164,8 @@ public function deactivate_and_die($file)
public function api_call($uri, $params = array(), $method = 'POST')
{
$options = get_option('mailgun');
$getRegion = (defined('MAILGUN_REGION') && MAILGUN_REGION) ? MAILGUN_REGION : $options['region'];
$apiKey = (defined('MAILGUN_APIKEY') && MAILGUN_APIKEY) ? MAILGUN_APIKEY : $options['apiKey'];
$getRegion = (defined('MAILGUN_REGION') && MAILGUN_REGION) ? MAILGUN_REGION : $options['region'];
$apiKey = (defined('MAILGUN_APIKEY') && MAILGUN_APIKEY) ? MAILGUN_APIKEY : $options['apiKey'];
$domain = (defined('MAILGUN_DOMAIN') && MAILGUN_DOMAIN) ? MAILGUN_DOMAIN : $options['domain'];

$region = mg_detect_region($getRegion);
Expand Down Expand Up @@ -394,15 +394,15 @@ public function list_form($list_address, $args = array(), $instance = array())

}

/**
* Initialize List Form.
*
* @param array $atts Form attributes
*
* @return string
*
* @since 0.1
*/
/**
* Initialize List Form.
*
* @param array $atts Form attributes
*
* @return string
*
* @since 0.1
*/
public function build_list_form($atts)
{
if (isset($atts['id']) && $atts['id'] != '') {
Expand Down
9 changes: 7 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Mailgun for WordPress
Contributors: Mailgun, sivel, lookahead.io, m35dev
Tags: mailgun, smtp, http, api, mail, email
Requires at least: 3.3
Tested up to: 4.9.7
Stable tag: 1.5.12.3
Tested up to: 4.9.8
Stable tag: 1.5.13
License: GPLv2 or later


Expand Down Expand Up @@ -128,6 +128,11 @@ MAILGUN_FROM_ADDRESS Type: string

== Changelog ==

= 1.5.13 (2018-08-14): =
- Default to US region if no region is configured in settings
- Add admin notification about region configuration
- Log an error message when an email is sent with no explicit region configured

= 1.5.12.3 (2018-08-09): =
- Fix Region select menu default when wp-config.php variable is set
- Fix front end email input validation
Expand Down

0 comments on commit ce8b780

Please sign in to comment.