From 97c12c2be3eab588b95dcad11aaa26be1e39eb52 Mon Sep 17 00:00:00 2001 From: Oleksandr Mykhailenko Date: Thu, 18 Aug 2022 20:42:22 +0300 Subject: [PATCH] Plugin refactoring. Version updated --- includes/lists-page.php | 14 +-- includes/options-page.php | 6 +- mailgun.php | 219 +++++++++++++++++++++----------------- readme.md | 6 +- readme.txt | 6 +- 5 files changed, 139 insertions(+), 112 deletions(-) diff --git a/includes/lists-page.php b/includes/lists-page.php index f9d7169..7062aa4 100644 --- a/includes/lists-page.php +++ b/includes/lists-page.php @@ -26,17 +26,17 @@ $api_key = (defined('MAILGUN_APIKEY') && MAILGUN_APIKEY) ? MAILGUN_APIKEY : $this->get_option('apiKey'); $mailgun_domain = (defined('MAILGUN_DOMAIN') && MAILGUN_DOMAIN) ? MAILGUN_DOMAIN : $this->get_option('domain'); - -if ($api_key != ''): - if ($mailgun_domain == ''): +if ($api_key != '') { + if ($mailgun_domain == '') { $missing_error = 'Missing or invalid Mailgun Domain. '; - endif; -else: + } +} else { $missing_error = 'Missing or invalid API Key. '; -endif; +} // import available lists $lists_arr = $mailgun->get_lists(); +$icon = $mailgun->getAssetsPath() . 'icon-128x128.png'; ?> @@ -46,7 +46,7 @@ - Mailgun + Mailgun diff --git a/includes/options-page.php b/includes/options-page.php index 371b144..79bbdb5 100755 --- a/includes/options-page.php +++ b/includes/options-page.php @@ -19,6 +19,8 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +global $mailgun; + $mailgun_domain_const = ((defined('MAILGUN_DOMAIN') && MAILGUN_DOMAIN) ? MAILGUN_DOMAIN : null); $mailgun_domain = $mailgun_domain_const ? $mailgun_domain_const : $this->get_option('domain'); @@ -48,14 +50,14 @@ $mailgun_use_api_const = (defined('MAILGUN_USEAPI') ? MAILGUN_USEAPI : null); $mailgun_use_api = !is_null($mailgun_use_api_const) ? ((string)(1 * $mailgun_use_api_const)) : $this->get_option('useAPI'); - +$icon = $mailgun->getAssetsPath() . 'icon-128x128.png'; ?>

- Mailgun + Mailgun

diff --git a/mailgun.php b/mailgun.php index fbde7c1..5085dae 100755 --- a/mailgun.php +++ b/mailgun.php @@ -4,7 +4,7 @@ * Plugin Name: Mailgun * Plugin URI: http://wordpress.org/extend/plugins/mailgun/ * Description: Mailgun integration for WordPress - * Version: 1.7.9 + * Version: 1.8.0 * Author: Mailgun * Author URI: http://www.mailgun.com/ * License: GPLv2 or later @@ -53,7 +53,12 @@ class Mailgun /** * @var string */ - private $plugin_basename; + protected $plugin_basename; + + /** + * @var string + */ + protected $assetsDir; /** * Setup shared functionality for Admin and Front End. @@ -65,42 +70,43 @@ public function __construct() $this->options = get_option('mailgun'); $this->plugin_file = __FILE__; $this->plugin_basename = plugin_basename($this->plugin_file); + $this->assetsDir = plugin_dir_url($this->plugin_file) . 'assets/'; // Either override the wp_mail function or configure PHPMailer to use the // Mailgun SMTP servers // When using SMTP, we also need to inject a `wp_mail` filter to make "from" settings // work properly. Fixed issues with 1.5.7+ - if ($this->get_option('useAPI') || (defined('MAILGUN_USEAPI') && MAILGUN_USEAPI)): - if (!function_exists('wp_mail')): - if (!include __DIR__ . '/includes/wp-mail-api.php'): + if ($this->get_option('useAPI') || (defined('MAILGUN_USEAPI') && MAILGUN_USEAPI)) { + if (!function_exists('wp_mail')) { + if (!include __DIR__ . '/includes/wp-mail-api.php') { $this->deactivate_and_die(__DIR__ . '/includes/wp-mail-api.php'); - endif; - endif; - else: + } + } + } else { // Using SMTP, include the SMTP filter - if (!function_exists('mg_smtp_mail_filter')): - if (!include __DIR__ . '/includes/wp-mail-smtp.php'): + if (!function_exists('mg_smtp_mail_filter')) { + if (!include __DIR__ . '/includes/wp-mail-smtp.php') { $this->deactivate_and_die(__DIR__ . '/includes/wp-mail-smtp.php'); - endif; - endif; + } + } add_filter('wp_mail', 'mg_smtp_mail_filter'); - add_action('phpmailer_init', array(&$this, 'phpmailer_init')); + add_action('phpmailer_init', [&$this, 'phpmailer_init']); add_action('wp_mail_failed', 'wp_mail_failed'); - endif; + } } /** * 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 string $option Name of option to be used as array key for retrieving the specific value + * @param array|null $options Array to iterate over for specific values + * @param bool $default False if no options are set * * @return mixed * * @since 0.1 */ - public function get_option($option, $options = null, $default = false) + public function get_option(string $option, ?array $options = null, bool $default = false) { if (is_null($options)) { $options = &$this->options; @@ -139,13 +145,13 @@ public function phpmailer_init(&$phpmailer) $phpmailer->Mailer = 'smtp'; $phpmailer->Host = $smtp_endpoint; - if ('ssl' === $sectype): + if ('ssl' === $sectype) { // For SSL-only connections, use 465 $phpmailer->Port = 465; - else: + } else { // Otherwise, use 587. $phpmailer->Port = 587; - endif; + } $phpmailer->SMTPAuth = true; $phpmailer->Username = $username; @@ -176,9 +182,9 @@ public function deactivate_and_die($file) load_plugin_textdomain('mailgun', false, 'mailgun/languages'); $message = sprintf(__('Mailgun has been automatically deactivated because the file %s is missing. Please reinstall the plugin and reactivate.'), $file); - if (!function_exists('deactivate_plugins')): + if (!function_exists('deactivate_plugins')) { include ABSPATH . 'wp-admin/includes/plugin.php'; - endif; + } deactivate_plugins(__FILE__); wp_die($message); } @@ -190,11 +196,11 @@ public function deactivate_and_die($file) * @param array $params Array of parameters passed to the API * @param string $method The form request type * - * @return array + * @return string * * @since 0.1 */ - public function api_call($uri, $params = array(), $method = 'POST') + public function api_call($uri, $params = [], $method = 'POST'): string { $options = get_option('mailgun'); $getRegion = (defined('MAILGUN_REGION') && MAILGUN_REGION) ? MAILGUN_REGION : $options[ 'region' ]; @@ -206,9 +212,9 @@ public function api_call($uri, $params = array(), $method = 'POST') $time = time(); $url = $this->api_endpoint . $uri; - $headers = array( + $headers = [ 'Authorization' => 'Basic ' . base64_encode("api:{$apiKey}"), - ); + ]; switch ($method) { case 'GET': @@ -227,20 +233,31 @@ public function api_call($uri, $params = array(), $method = 'POST') } // make the request - $args = array( + $args = [ 'method' => $method, 'body' => $params, 'headers' => $headers, 'sslverify' => true, - ); + ]; // make the remote request $result = wp_remote_request($url, $args); - if (!is_wp_error($result)): - return $result[ 'body' ]; - else: + if (!is_wp_error($result)) { + return $result['body']; + } + + if (is_callable($result)) { return $result->get_error_message(); - endif; + } + + if (is_array($result)) { + if (isset($result['response'])) { + return $result['response']['message'] ?? ''; + } + } + + return ''; + } /** @@ -248,17 +265,19 @@ public function api_call($uri, $params = array(), $method = 'POST') * * @return array * + * @throws JsonException * @since 0.1 */ - public function get_lists() + public function get_lists(): array { - $results = array(); + $results = []; - $lists_json = $this->api_call('lists', array(), 'GET'); - $lists_arr = json_decode($lists_json, true); - if (isset($lists_arr[ 'items' ]) && !empty($lists_arr[ 'items' ])): - $results = $lists_arr[ 'items' ]; - endif; + $lists_json = $this->api_call('lists', [], 'GET'); + + $lists_arr = json_decode($lists_json, true, 512, JSON_THROW_ON_ERROR); + if (isset($lists_arr[ 'items' ]) && !empty($lists_arr[ 'items' ])) { + $results = $lists_arr['items']; + } return $results; } @@ -268,49 +287,47 @@ public function get_lists() * * @return string json * + * @throws JsonException * @since 0.1 */ public function add_list() { - $response = array(); - - $name = isset($_POST[ 'name' ]) ? $_POST[ 'name' ] : null; - $email = isset($_POST[ 'email' ]) ? $_POST[ 'email' ] : null; + $name = $_POST['name'] ?? null; + $email = $_POST['email'] ?? null; $list_addresses = $_POST[ 'addresses' ]; - if (!empty($list_addresses)): - foreach ($list_addresses as $address => $val): - $response[] = $this->api_call( + if (!empty($list_addresses)) { + foreach ($list_addresses as $address => $val) { + $this->api_call( "lists/{$address}/members", - array( + [ 'address' => $email, 'name' => $name, - ) + ] ); - endforeach; - - echo json_encode(array('status' => 200, 'message' => 'Thank you!')); - else: - echo json_encode(array( + } + echo json_encode(['status' => 200, 'message' => 'Thank you!'], JSON_THROW_ON_ERROR); + } else { + echo json_encode([ 'status' => 500, 'message' => 'Uh oh. We weren\'t able to add you to the list' . count($list_addresses) ? 's.' : '. Please try again.' - )); - endif; - + ], JSON_THROW_ON_ERROR); + } wp_die(); } /** * Frontend List Form. * - * @param string $list_address Mailgun address list id - * @param array $args widget arguments - * @param array $instance widget instance params + * @param string $list_address Mailgun address list id + * @param array $args widget arguments + * @param array $instance widget instance params * + * @throws JsonException * @since 0.1 */ - public function list_form($list_address, $args = array(), $instance = array()) + public function list_form(string $list_address, array $args = [], array $instance = []): void { $widget_class_id = "mailgun-list-widget-{$args['widget_id']}"; $form_class_id = "list-form-{$args['widget_id']}"; @@ -435,42 +452,38 @@ public function list_form($list_address, $args = array(), $instance = array()) /** * Initialize List Form. * - * @param array $atts Form attributes + * @param array $atts Form attributes * * @return string * + * @throws JsonException * @since 0.1 */ - public function build_list_form($atts) + public function build_list_form(array $atts): string { - if (isset($atts[ 'id' ]) && $atts[ 'id' ] != ''): - $args[ 'widget_id' ] = md5(rand(10000, 99999) . $atts[ 'id' ]); + if (isset($atts['id']) && $atts['id'] != '') { + $args['widget_id'] = md5(rand(10000, 99999) . $atts['id']); - if (isset($atts[ 'collect_name' ])): - $args[ 'collect_name' ] = true; - endif; + if (isset($atts['collect_name'])) { + $args['collect_name'] = true; + } - if (isset($atts[ 'title' ])): - $args[ 'list_title' ] = $atts[ 'title' ]; - endif; + if (isset($atts['title'])) { + $args['list_title'] = $atts['title']; + } - if (isset($atts[ 'description' ])): - $args[ 'list_description' ] = $atts[ 'description' ]; - endif; + if (isset($atts['description'])) { + $args['list_description'] = $atts['description']; + } ob_start(); - $this->list_form($atts[ 'id' ], $args); - $output_string = ob_get_contents(); - ob_end_clean(); - - return $output_string; - else: - ?> - Mailgun list ID needed to render form! -
- Example : [mailgun id="[your list id]"] - list_form($atts['id'], $args); + return ob_get_clean(); + } + + return 'Mailgun list ID needed to render form! +
+ Example : [mailgun id="[your list id]"]'; } /** @@ -478,25 +491,33 @@ public function build_list_form($atts) * * @since 0.1 */ - public function load_list_widget() + public function load_list_widget(): void { register_widget('list_widget'); - add_shortcode('mailgun', array(&$this, 'build_list_form')); + add_shortcode('mailgun', [&$this, 'build_list_form']); + } + + /** + * @return string + */ + public function getAssetsPath(): string + { + return $this->assetsDir; } } $mailgun = new Mailgun(); -if (@include dirname(__FILE__) . '/includes/widget.php'): - add_action('widgets_init', array(&$mailgun, 'load_list_widget')); - add_action('wp_ajax_nopriv_add_list', array(&$mailgun, 'add_list')); - add_action('wp_ajax_add_list', array(&$mailgun, 'add_list')); -endif; +if (@include __DIR__ . '/includes/widget.php') { + add_action('widgets_init', [&$mailgun, 'load_list_widget']); + add_action('wp_ajax_nopriv_add_list', [&$mailgun, 'add_list']); + add_action('wp_ajax_add_list', [&$mailgun, 'add_list']); +} -if (is_admin()): - if (@include dirname(__FILE__) . '/includes/admin.php'): +if (is_admin()) { + if (@include __DIR__ . '/includes/admin.php') { $mailgunAdmin = new MailgunAdmin(); - else: - Mailgun::deactivate_and_die(dirname(__FILE__) . '/includes/admin.php'); - endif; -endif; + } else { + $mailgun->deactivate_and_die(__DIR__ . '/includes/admin.php'); + } +} diff --git a/readme.md b/readme.md index fdf898e..263bfc6 100755 --- a/readme.md +++ b/readme.md @@ -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: 5.7.2 -Stable tag: 1.7.9 +Tested up to: 6.0.1 +Stable tag: 1.8.0 License: GPLv2 or later @@ -131,6 +131,8 @@ MAILGUN_FROM_ADDRESS Type: string == Changelog == += 1.8.0 (2022-08-18): = +- Plugin refactoring. Using new languages constructions. Extended readme. Update version = 1.7.9 (2021-05-24): = - API Key description diff --git a/readme.txt b/readme.txt index 55ab5ad..2306111 100755 --- a/readme.txt +++ b/readme.txt @@ -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: 5.7.2 -Stable tag: 1.7.9 +Tested up to: 6.0.1 +Stable tag: 1.8.0 License: GPLv2 or later @@ -128,6 +128,8 @@ MAILGUN_FROM_ADDRESS Type: string == Changelog == += 1.8.0 (2022-08-18): = +- Plugin refactoring. Using new languages constructions. Extended readme. Update version = 1.7.9 (2021-05-24): = - API Key description