From 0ef060f0ca71448c7670db3f34b4a60e458808da Mon Sep 17 00:00:00 2001 From: Tim Flight <4959691+tflight@users.noreply.github.com> Date: Tue, 13 Jul 2021 11:38:38 -0400 Subject: [PATCH 01/11] Merge settings from database with constants Since both page-settings.php and wp-mail.php (class Postmark_Mail) need these settings a new file is created that will return an array of settings. The array of settings coming from the database is merged with the settings defined as constants, with constants taking priority. --- postmark-settings.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 postmark-settings.php diff --git a/postmark-settings.php b/postmark-settings.php new file mode 100644 index 0000000..8353af6 --- /dev/null +++ b/postmark-settings.php @@ -0,0 +1,15 @@ + defined('POSTMARK_API_KEY') ? POSTMARK_API_KEY : null, + 'stream_name' => defined('POSTMARK_STREAM_NAME') ? POSTMARK_STREAM_NAME : null, + 'sender_address' => defined('POSTMARK_SENDER_ADDRESS') ? POSTMARK_SENDER_ADDRESS : null, +); +$settings = array_merge( $settings, $settings_from_constants ); +return $settings; From ba909bbd9bd5b43cefd8a78816ec48685c7962ff Mon Sep 17 00:00:00 2001 From: Tim Flight <4959691+tflight@users.noreply.github.com> Date: Tue, 13 Jul 2021 11:39:39 -0400 Subject: [PATCH 02/11] Add overrideen settings property so we can show in UI --- postmark.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/postmark.php b/postmark.php index 1a39e77..60dd8d1 100644 --- a/postmark.php +++ b/postmark.php @@ -19,6 +19,13 @@ class Postmark_Mail { */ public $settings; + /** + * Overridden Settings. + * + * @var array + */ + public $overridden_settings; + /** * Last Error. * @@ -45,6 +52,7 @@ public function __construct() { add_filter( 'init', array( $this, 'init' ) ); $this->settings = $this->load_settings(); + $this->overridden_settings = include POSTMARK_DIR . '/postmark-settings.php'; } /** From e0096ba4ea823197ee52dd7a6b0299ba017db79c Mon Sep 17 00:00:00 2001 From: Tim Flight <4959691+tflight@users.noreply.github.com> Date: Tue, 13 Jul 2021 11:40:16 -0400 Subject: [PATCH 03/11] When sending mail, use constants if defined --- wp-mail.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-mail.php b/wp-mail.php index b4fb885..aec3e0a 100644 --- a/wp-mail.php +++ b/wp-mail.php @@ -52,7 +52,7 @@ function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() // Compact the input, apply the filters, and extract them back out. extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ) ); - $settings = json_decode( get_option( 'postmark_settings' ), true ); + $settings = include POSTMARK_DIR . '/postmark-settings.php'; if ( ! is_array( $attachments ) ) { $attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) ); From 9dd25a17bffa253b265866caa21055973267df65 Mon Sep 17 00:00:00 2001 From: Tim Flight <4959691+tflight@users.noreply.github.com> Date: Tue, 13 Jul 2021 11:42:42 -0400 Subject: [PATCH 04/11] Show the user which settings have been overridden MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In the WP Dashboard, show the user if settings have been overridden by constants, but don’t show them the actual constant values. This will somehwat obfuscate the API key from the user. (Think agencies that define constants for a client using the agencies sandboxed api key.) --- page-settings.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/page-settings.php b/page-settings.php index 57e1127..6c3b3bd 100644 --- a/page-settings.php +++ b/page-settings.php @@ -42,6 +42,18 @@
+ overridden_settings['api_key'] ) ) : ?> +POSTMARK_API_KEY
is defined in your wp-config.php and overrides the API Key
set here.POSTMARK_STREAM_NAME
is defined in your wp-config.php and overrides the Message Stream
set here.POSTMARK_SENDER_ADDRESS
is defined in your wp-config.php and overrides the Sender Email
set here.