diff --git a/.github/workflows/wpcs.yml b/.github/workflows/wpcs.yml
new file mode 100644
index 0000000..299ec1b
--- /dev/null
+++ b/.github/workflows/wpcs.yml
@@ -0,0 +1,28 @@
+name: Wordpress Basic Checks
+
+on:
+ push:
+ branches:
+ - '**' # matches every branch
+ - '!master'
+
+jobs:
+ phpcs:
+ name: WPCS
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v3
+ - name: WPCS check
+ uses: 10up/wpcs-action@stable
+ with:
+ enable_warnings: false # Enable checking for warnings (-w)
+ paths: '.' # Paths to check, space separated
+ excludes: '' # Paths to excludes, space separated
+ standard: 'WordPress' # Standard to use. Accepts WordPress|WordPress-Core|WordPress-Docs|WordPress-Extra|WordPress-VIP-Go|WordPressVIPMinimum|10up-Default.
+ standard_repo: '' # Public (git) repository URL of the coding standard
+ repo_branch: 'master' # Branch of Standard repository
+ phpcs_bin_path: 'phpcs' # Custom PHPCS bin path
+ use_local_config: 'true' # Use local config if available
+ extra_args: '' # Extra arguments passing to the command
+ only_changed_files: '' # Run the linter only on the changed files. Accepts true|false
+ only_changed_lines: '' # Run the linter only on the changed lines. Accepts true|false
\ No newline at end of file
diff --git a/includes/admin.php b/includes/admin.php
index 7b8ede6..8bb1f1b 100755
--- a/includes/admin.php
+++ b/includes/admin.php
@@ -1,6 +1,5 @@
init();
@@ -49,25 +50,25 @@ public function __construct()
load_plugin_textdomain('mailgun', false, 'mailgun/languages');
// Activation hook
- register_activation_hook($this->plugin_file, [&$this, 'activation']);
+ register_activation_hook($this->plugin_file, array( &$this, 'activation' ));
// Hook into admin_init and register settings and potentially register an admin_notice
- add_action('admin_init', [&$this, 'admin_init']);
+ add_action('admin_init', array( &$this, 'admin_init' ));
// Activate the options page
- add_action('admin_menu', [&$this, 'admin_menu']);
+ add_action('admin_menu', array( &$this, 'admin_menu' ));
// Register an AJAX action for testing mail sending capabilities
- add_action('wp_ajax_mailgun-test', [&$this, 'ajax_send_test']);
+ add_action('wp_ajax_mailgun-test', array( &$this, 'ajax_send_test' ));
}
/**
* Adds the default options during plugin activation.
+ *
* @return void
*/
- public function activation(): void
- {
- if (!$this->options) {
+ public function activation(): void {
+ if ( ! $this->options) {
$this->options = $this->defaults;
add_option('mailgun', $this->options);
}
@@ -78,52 +79,57 @@ public function activation(): void
* Initialize the default property.
*
* @return void
- *
*/
- public function init(): void
- {
- $sitename = sanitize_text_field(strtolower($_SERVER['SERVER_NAME']));
+ public function init(): void {
+ $sitename = sanitize_text_field(strtolower($_SERVER['SERVER_NAME'] ?? site_url()));
if (substr($sitename, 0, 4) === 'www.') {
$sitename = substr($sitename, 4);
}
- $region = (defined('MAILGUN_REGION') && MAILGUN_REGION) ? MAILGUN_REGION : $this->get_option('region');
+ $region = ( defined('MAILGUN_REGION') && MAILGUN_REGION ) ? MAILGUN_REGION : $this->get_option('region');
$regionDefault = $region ?: 'us';
$this->defaults = array(
- 'region' => $regionDefault,
- 'useAPI' => '1',
- 'apiKey' => '',
- 'domain' => '',
- 'username' => '',
- 'password' => '',
- 'secure' => '1',
- 'sectype' => 'tls',
- 'track-clicks' => '',
- 'track-opens' => '',
- 'campaign-id' => '',
+ 'region' => $regionDefault,
+ 'useAPI' => '1',
+ 'apiKey' => '',
+ 'domain' => '',
+ 'username' => '',
+ 'password' => '',
+ 'secure' => '1',
+ 'sectype' => 'tls',
+ 'track-clicks' => '',
+ 'track-opens' => '',
+ 'campaign-id' => '',
'override-from' => '0',
- 'tag' => $sitename,
+ 'tag' => $sitename,
);
-
}
/**
* Add the options page.
*
* @return void
- *
*/
- public function admin_menu(): void
- {
+ public function admin_menu(): void {
if (current_user_can('manage_options')) {
- $this->hook_suffix = add_options_page(__('Mailgun', 'mailgun'), __('Mailgun', 'mailgun'),
- 'manage_options', 'mailgun', [&$this, 'options_page']);
- add_options_page(__('Mailgun Lists', 'mailgun'), __('Mailgun Lists', 'mailgun'), 'manage_options',
- 'mailgun-lists', [&$this, 'lists_page']);
- add_action("admin_print_scripts-{$this->hook_suffix}", [&$this, 'admin_js']);
- add_filter("plugin_action_links_{$this->plugin_basename}", [&$this, 'filter_plugin_actions']);
- add_action("admin_footer-{$this->hook_suffix}", [&$this, 'admin_footer_js']);
+ $this->hook_suffix = add_options_page(
+ __('Mailgun', 'mailgun'),
+ __('Mailgun', 'mailgun'),
+ 'manage_options',
+ 'mailgun',
+ array( &$this, 'options_page' )
+ );
+ add_options_page(
+ __('Mailgun Lists', 'mailgun'),
+ __('Mailgun Lists', 'mailgun'),
+ 'manage_options',
+ 'mailgun-lists',
+ array( &$this, 'lists_page' )
+ );
+ add_action("admin_print_scripts-{$this->hook_suffix}", array( &$this, 'admin_js' ));
+ add_filter("plugin_action_links_{$this->plugin_basename}", array( &$this, 'filter_plugin_actions' ));
+ add_action("admin_footer-{$this->hook_suffix}", array( &$this, 'admin_footer_js' ));
}
}
@@ -131,19 +137,15 @@ public function admin_menu(): void
* Enqueue javascript required for the admin settings page.
*
* @return void
- *
*/
- public function admin_js(): void
- {
+ public function admin_js(): void {
wp_enqueue_script('jquery');
}
/**
* Output JS to footer for enhanced admin page functionality.
- *
*/
- public function admin_footer_js(): void
- {
+ public function admin_footer_js(): void {
?>
@@ -169,8 +171,14 @@ public function admin_footer_js(): void
jQuery('#mailgun-test').click(function (e) {
e.preventDefault()
if (formModified) {
- var doTest = confirm('')
+ var doTest = confirm('
+
+ ')
if (!doTest) {
return false
}
@@ -213,13 +221,16 @@ public function admin_footer_js(): void
* Output the options page.
*
* @return void
- *
*/
- public function options_page(): void
- {
- if (!@include 'options-page.php') {
- printf(__('
The options page for the Mailgun plugin cannot be displayed. The file %s is missing. Please reinstall the plugin.
',
- 'mailgun'), __DIR__ . '/options-page.php');
+ public function options_page(): void {
+ if ( ! @include 'options-page.php') {
+ printf(
+ __(
+ '
The options page for the Mailgun plugin cannot be displayed. The file %s is missing. Please reinstall the plugin.
',
+ 'mailgun'
+ ),
+ __DIR__ . '/options-page.php'
+ );
}
}
@@ -227,13 +238,16 @@ public function options_page(): void
* Output the lists page.
*
* @return void
- *
*/
- public function lists_page(): void
- {
- if (!@include 'lists-page.php') {
- printf(__('
The lists page for the Mailgun plugin cannot be displayed. The file %s is missing. Please reinstall the plugin.
',
- 'mailgun'), __DIR__ . '/lists-page.php');
+ public function lists_page(): void {
+ if ( ! @include 'lists-page.php') {
+ printf(
+ __(
+ '
The lists page for the Mailgun plugin cannot be displayed. The file %s is missing. Please reinstall the plugin.
',
+ 'mailgun'
+ ),
+ __DIR__ . '/lists-page.php'
+ );
}
}
@@ -243,24 +257,20 @@ public function lists_page(): void
* been configured yet.
*
* @return void
- *
*/
- public function admin_init(): void
- {
+ public function admin_init(): void {
$this->register_settings();
- add_action('admin_notices', [&$this, 'admin_notices']);
+ add_action('admin_notices', array( &$this, 'admin_notices' ));
}
/**
* Whitelist the mailgun options.
*
* @return void
- *
*/
- public function register_settings(): void
- {
- register_setting('mailgun', 'mailgun', [&$this, 'validation']);
+ public function register_settings(): void {
+ register_setting('mailgun', 'mailgun', array( &$this, 'validation' ));
}
/**
@@ -269,13 +279,11 @@ public function register_settings(): void
* @param array $options An array of options posted from the options page
*
* @return array
- *
*/
- public function validation(array $options): array
- {
- $apiKey = trim($options['apiKey']);
+ public function validation( array $options ): array {
+ $apiKey = trim($options['apiKey']);
$username = trim($options['username']);
- if (!empty($apiKey)) {
+ if ( ! empty($apiKey)) {
$pos = strpos($apiKey, 'api:');
if ($pos !== false && $pos == 0) {
$apiKey = substr($apiKey, 4);
@@ -292,13 +300,13 @@ public function validation(array $options): array
}
}
- if (!empty($username)) {
- $username = preg_replace('/@.+$/', '', $username);
+ if ( ! empty($username)) {
+ $username = preg_replace('/@.+$/', '', $username);
$options['username'] = $username;
}
foreach ($options as $key => $value) {
- $options[$key] = trim($value);
+ $options[ $key ] = trim($value);
}
if (empty($options['override-from'])) {
@@ -319,31 +327,32 @@ public function validation(array $options): array
* when plugin settings or constants need to be configured
*
* @return void
- *
*/
- public function admin_notices(): void
- {
+ public function admin_notices(): void {
$screen = get_current_screen();
- if (!isset($screen)) {
+ if ( ! isset($screen)) {
return;
}
- if (!current_user_can('manage_options') || $screen->id === $this->hook_suffix) {
+ 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);
- $apiRegionUndefined = (!$this->get_option('region') && (!defined('MAILGUN_REGION') || !MAILGUN_REGION));
- $apiKeyUndefined = (!$this->get_option('apiKey') && (!defined('MAILGUN_APIKEY') || !MAILGUN_APIKEY));
- $apiActiveNotConfigured = ($this->get_option('useAPI') === '1' && ($apiRegionUndefined || $apiKeyUndefined));
+ $smtpPasswordUndefined = ( ! $this->get_option('password') && ( ! defined('MAILGUN_PASSWORD') || ! MAILGUN_PASSWORD ) );
+ $smtpActiveNotConfigured = ( $this->get_option('useAPI') === '0' && $smtpPasswordUndefined );
+ $apiRegionUndefined = ( ! $this->get_option('region') && ( ! defined('MAILGUN_REGION') || ! MAILGUN_REGION ) );
+ $apiKeyUndefined = ( ! $this->get_option('apiKey') && ( ! defined('MAILGUN_APIKEY') || ! MAILGUN_APIKEY ) );
+ $apiActiveNotConfigured = ( $this->get_option('useAPI') === '1' && ( $apiRegionUndefined || $apiKeyUndefined ) );
- if (isset($_SESSION) && (!isset($_SESSION['settings_turned_of']) || $_SESSION['settings_turned_of'] === false) && ($apiActiveNotConfigured || $smtpActiveNotConfigured)) { ?>
+ if (isset($_SESSION) && ( ! isset($_SESSION['settings_turned_of']) || $_SESSION['settings_turned_of'] === false ) && ( $apiActiveNotConfigured || $smtpActiveNotConfigured )) {
+ ?>
here',
- 'mailgun'),
+ __(
+ '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 here',
+ 'mailgun'
+ ),
menu_page_url('mailgun', false)
);
?>
@@ -354,8 +363,9 @@ public function admin_notices(): void
get_option('override-from') === '1' &&
- (!$this->get_option('from-name') || !$this->get_option('from-address'))
- ) { ?>
+ ( ! $this->get_option('from-name') || ! $this->get_option('from-address') )
+ ) {
+ ?>
@@ -363,14 +373,17 @@ public function admin_notices(): void
Configure Mailgun now.',
- 'mailgun'),
+ __(
+ '"Override From" option requires that "From Name" and "From Address" be set to work properly! Configure Mailgun now.',
+ 'mailgun'
+ ),
menu_page_url('mailgun', false)
);
?>
- ' . __('Settings', 'mailgun') . '';
array_unshift($links, $settings_link);
@@ -392,89 +403,99 @@ public function filter_plugin_actions(array $links): array
/**
* AJAX callback function to test mail sending functionality.
*
- * @return string
- *
+ * @return void
* @throws JsonException
*/
- public function ajax_send_test()
- {
+ public function ajax_send_test(): void {
nocache_headers();
header('Content-Type: application/json');
- if (!current_user_can('manage_options') || !wp_verify_nonce(sanitize_text_field($_GET['_wpnonce']))) {
+ if ( ! current_user_can('manage_options') || ! wp_verify_nonce(sanitize_text_field($_GET['_wpnonce']))) {
die(
- json_encode([
- 'message' => __('Unauthorized', 'mailgun'),
- 'method' => null,
- 'error' => __('Unauthorized', 'mailgun'),
- ], JSON_THROW_ON_ERROR)
+ json_encode(
+ array(
+ 'message' => __('Unauthorized', 'mailgun'),
+ 'method' => null,
+ 'error' => __('Unauthorized', 'mailgun'),
+ ),
+ JSON_THROW_ON_ERROR
+ )
);
}
- $getRegion = (defined('MAILGUN_REGION') && MAILGUN_REGION) ? MAILGUN_REGION : $this->get_option('region');
- $useAPI = (defined('MAILGUN_USEAPI') && MAILGUN_USEAPI) ? MAILGUN_USEAPI : $this->get_option('useAPI');
- $secure = (defined('MAILGUN_SECURE') && MAILGUN_SECURE) ? MAILGUN_SECURE : $this->get_option('secure');
- $sectype = (defined('MAILGUN_SECTYPE') && MAILGUN_SECTYPE) ? MAILGUN_SECTYPE : $this->get_option('sectype');
- $replyTo = (defined('MAILGUN_REPLY_TO_ADDRESS') && MAILGUN_REPLY_TO_ADDRESS) ? MAILGUN_REPLY_TO_ADDRESS : $this->get_option('reply_to');
+ $getRegion = ( defined('MAILGUN_REGION') && MAILGUN_REGION ) ? MAILGUN_REGION : $this->get_option('region');
+ $useAPI = ( defined('MAILGUN_USEAPI') && MAILGUN_USEAPI ) ? MAILGUN_USEAPI : $this->get_option('useAPI');
+ $secure = ( defined('MAILGUN_SECURE') && MAILGUN_SECURE ) ? MAILGUN_SECURE : $this->get_option('secure');
+ $sectype = ( defined('MAILGUN_SECTYPE') && MAILGUN_SECTYPE ) ? MAILGUN_SECTYPE : $this->get_option('sectype');
+ $replyTo = ( defined('MAILGUN_REPLY_TO_ADDRESS') && MAILGUN_REPLY_TO_ADDRESS ) ? MAILGUN_REPLY_TO_ADDRESS : $this->get_option('reply_to');
- if (!$getRegion) {
- mg_api_last_error(__("Region has not been selected", "mailgun"));
+ if ( ! $getRegion) {
+ mg_api_last_error(__('Region has not been selected', 'mailgun'));
} else {
if ($getRegion === 'us') {
- $region = __("U.S./North America", "mailgun");
+ $region = __('U.S./North America', 'mailgun');
}
- if ($getRegion === "eu") {
- $region = __("Europe", "mailgun");
+ if ($getRegion === 'eu') {
+ $region = __('Europe', 'mailgun');
}
}
if ($useAPI) {
$method = __('HTTP API', 'mailgun');
} else {
- $method = ($secure) ? __('Secure SMTP', 'mailgun') : __('Insecure SMTP', 'mailgun');
+ $method = ( $secure ) ? __('Secure SMTP', 'mailgun') : __('Insecure SMTP', 'mailgun');
if ($secure) {
$method .= sprintf(__(' via %s', 'mailgun'), $sectype);
}
}
$admin_email = get_option('admin_email');
- if (!$admin_email) {
+ if ( ! $admin_email) {
die(
- json_encode([
- 'message' => __('Admin Email is empty', 'mailgun'),
- 'method' => $method,
- 'error' => __('Admin Email is empty', 'mailgun'),
- ], JSON_THROW_ON_ERROR)
+ json_encode(
+ array(
+ 'message' => __('Admin Email is empty', 'mailgun'),
+ 'method' => $method,
+ 'error' => __('Admin Email is empty', 'mailgun'),
+ ),
+ JSON_THROW_ON_ERROR
+ )
);
}
try {
- $headers = [
+ $headers = array(
'Content-Type: text/plain',
'Reply-To: ' . $replyTo,
- ];
+ );
$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),
+ 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
+ ),
$headers
);
} catch (Throwable $throwable) {
- //Log purpose
+ // Log purpose
}
if ($useAPI) {
- if (!function_exists('mg_api_last_error')) {
- if (!include __DIR__ . '/wp-mail-api.php') {
+ if ( ! function_exists('mg_api_last_error')) {
+ if ( ! include __DIR__ . '/wp-mail-api.php') {
$this->deactivate_and_die(__DIR__ . '/wp-mail-api.php');
}
}
$error_msg = mg_api_last_error();
} else {
- if (!function_exists('mg_smtp_last_error')) {
- if (!include __DIR__ . '/wp-mail-smtp.php') {
+ if ( ! function_exists('mg_smtp_last_error')) {
+ if ( ! include __DIR__ . '/wp-mail-smtp.php') {
$this->deactivate_and_die(__DIR__ . '/wp-mail-smtp.php');
}
}
@@ -491,22 +512,28 @@ public function ajax_send_test()
if ($result) {
die(
- json_encode([
- 'message' => __('Success', 'mailgun'),
- 'method' => $method,
- 'error' => __('Success', 'mailgun'),
- ], JSON_THROW_ON_ERROR)
+ json_encode(
+ array(
+ 'message' => __('Success', 'mailgun'),
+ 'method' => $method,
+ 'error' => __('Success', 'mailgun'),
+ ),
+ JSON_THROW_ON_ERROR
+ )
);
}
// Error message will always be returned in case of failure, if not - connection wasn't successful
$error_msg = $error_msg ?: "Can't connect to Mailgun";
die(
- json_encode([
- 'message' => __('Failure', 'mailgun'),
- 'method' => $method,
- 'error' => $error_msg,
- ], JSON_THROW_ON_ERROR)
+ json_encode(
+ array(
+ 'message' => __('Failure', 'mailgun'),
+ 'method' => $method,
+ 'error' => $error_msg,
+ ),
+ JSON_THROW_ON_ERROR
+ )
);
}
}
diff --git a/includes/lists-page.php b/includes/lists-page.php
index de3025d..85e2c1d 100644
--- a/includes/lists-page.php
+++ b/includes/lists-page.php
@@ -1,7 +1,6 @@
get_option('apiKey');
-$mailgun_domain = (defined('MAILGUN_DOMAIN') && MAILGUN_DOMAIN) ? MAILGUN_DOMAIN : $this->get_option('domain');
+$missing_error = '';
+$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 === '') {
@@ -36,7 +37,7 @@
// import available lists
$lists_arr = $mailgun->get_lists();
-$icon = $mailgun->getAssetsPath() . 'icon-128x128.png';
+$icon = $mailgun->getAssetsPath() . 'icon-128x128.png';
?>
@@ -46,7 +47,7 @@
-
+
@@ -60,7 +61,7 @@