From 84ae5146cd32e9e7f8d3a09d6f10e255e1ae8986 Mon Sep 17 00:00:00 2001 From: Guido Scialfa Date: Thu, 5 Dec 2019 12:42:16 +0100 Subject: [PATCH 1/2] Fix mollie components asking multiple times for merchant profile ID via API --- inc/utils.php | 28 +++++++++++++++-- src/Mollie/WC/Helper/Settings.php | 50 +++++++++++++++++++++++++++---- src/Mollie/WC/Plugin.php | 6 ++++ 3 files changed, 75 insertions(+), 9 deletions(-) diff --git a/inc/utils.php b/inc/utils.php index 9a04286bc..0509e93df 100644 --- a/inc/utils.php +++ b/inc/utils.php @@ -49,7 +49,7 @@ function merchantProfile() { static $profile = null; - if (null === $profile) { + if ($profile === null) { $isTestMode = isTestModeEnabled(); $apiHelper = Mollie_WC_Plugin::getApiHelper(); @@ -67,9 +67,31 @@ function merchantProfile() */ function merchantProfileId() { - $merchantProfile = merchantProfile(); + static $merchantProfileId = null; + $merchantProfileIdOptionKey = Mollie_WC_Plugin::PLUGIN_ID . '_merchant_profile_id'; - return isset($merchantProfile->id) ? $merchantProfile->id : 0; + if ($merchantProfileId === null) { + $merchantProfileId = get_option($merchantProfileIdOptionKey, ''); + + /* + * Try to retrieve the merchant profile ID from an Api Request if not stored already, + * then store it into the database + */ + if (!$merchantProfileId) { + try { + $merchantProfile = merchantProfile(); + $merchantProfileId = isset($merchantProfile->id) ? $merchantProfile->id : ''; + } catch (ApiException $exception) { + $merchantProfileId = ''; + } + + if ($merchantProfileId) { + update_option($merchantProfileIdOptionKey, $merchantProfileId, false); + } + } + } + + return $merchantProfileId; } /** diff --git a/src/Mollie/WC/Helper/Settings.php b/src/Mollie/WC/Helper/Settings.php index fb14b254d..46bee98c4 100644 --- a/src/Mollie/WC/Helper/Settings.php +++ b/src/Mollie/WC/Helper/Settings.php @@ -1,4 +1,7 @@ getSettingId($setting_id))); + $apiKeyId = $this->getSettingId($setting_id); + $apiKey = get_option($apiKeyId); + + if (!$apiKey && is_admin()) { + $apiKey = filter_input(INPUT_POST, $apiKeyId, FILTER_SANITIZE_STRING); + } + + return trim($apiKey); } /** @@ -133,11 +143,37 @@ public function getGlobalSettingsUrl () /** * @return string */ - public function getLogsUrl () + public function getLogsUrl() { return admin_url('admin.php?page=wc-status&tab=logs'); } + public function updateMerchantIdOnApiKeyChanges($optionValue, $optionName) + { + $optionId = isset($optionName['id']) ? $optionName['id'] : ''; + $allowedOptionsId = [ + $this->getSettingId('live_api_key'), + $this->getSettingId('test_api_key'), + ]; + + if (!in_array($optionId, $allowedOptionsId, true)) { + return $optionValue; + } + + $merchantProfileIdOptionKey = Mollie_WC_Plugin::PLUGIN_ID . '_merchant_profile_id'; + + try { + $merchantProfile = merchantProfile(); + $merchantProfileId = isset($merchantProfile->id) ? $merchantProfile->id : ''; + } catch (ApiException $exception) { + $merchantProfileId = ''; + } + + update_option($merchantProfileIdOptionKey, $merchantProfileId, false); + + return $optionValue; + } + /** * Get plugin status * @@ -146,16 +182,18 @@ public function getLogsUrl () * * @return string */ - protected function getPluginStatus () + protected function getPluginStatus() { $status = Mollie_WC_Plugin::getStatusHelper(); - if (!$status->isCompatible()) - { + if (!$status->isCompatible()) { // Just stop here! return '' . '
' - . '

' . __('Error', 'mollie-payments-for-woocommerce') . ': ' . implode('
', $status->getErrors()) + . '

' . __( + 'Error', + 'mollie-payments-for-woocommerce' + ) . ': ' . implode('
', $status->getErrors()) . '

'; } diff --git a/src/Mollie/WC/Plugin.php b/src/Mollie/WC/Plugin.php index b5692b7a2..77c92a34c 100644 --- a/src/Mollie/WC/Plugin.php +++ b/src/Mollie/WC/Plugin.php @@ -205,6 +205,12 @@ function () { } } ); + add_action( + 'woocommerce_admin_settings_sanitize_option', + [$settings_helper, 'updateMerchantIdOnApiKeyChanges'], + 10, + 2 + ); // Add settings link to plugins page add_filter( 'plugin_action_links_' . $plugin_basename, array ( __CLASS__, 'addPluginActionLinks' ) ); From a54f21ab4cac7179e120e0796872e1bbfc908fe8 Mon Sep 17 00:00:00 2001 From: Guido Scialfa Date: Thu, 5 Dec 2019 12:44:17 +0100 Subject: [PATCH 2/2] Update plugin version to 5.4.1 --- changelog.txt | 6 +++++- mollie-payments-for-woocommerce.php | 2 +- src/Mollie/WC/Plugin.php | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/changelog.txt b/changelog.txt index 644e569b1..5875ec4ba 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,6 +1,10 @@ == Changelog == -= 5.4.0 - 02-12-2019 = += 5.4.1 - 05-12-2019 = + +* Fix - Mollie Components request multiple times the merchant profile ID via API + += 5.4.0 - 04-12-2019 = * Fix - Apple Pay Gateway is removed from available gateways during WooCommerce Api calls * Fix - Giftcard Gateway does not show the right payment icon in checkout page diff --git a/mollie-payments-for-woocommerce.php b/mollie-payments-for-woocommerce.php index caa973703..7236ff1cd 100644 --- a/mollie-payments-for-woocommerce.php +++ b/mollie-payments-for-woocommerce.php @@ -3,7 +3,7 @@ * Plugin Name: Mollie Payments for WooCommerce * Plugin URI: https://www.mollie.com * Description: Accept payments in WooCommerce with the official Mollie plugin - * Version: 5.4.0 + * Version: 5.4.1 * Author: Mollie * Author URI: https://www.mollie.com * Requires at least: 3.8 diff --git a/src/Mollie/WC/Plugin.php b/src/Mollie/WC/Plugin.php index 77c92a34c..01df4b72d 100644 --- a/src/Mollie/WC/Plugin.php +++ b/src/Mollie/WC/Plugin.php @@ -8,7 +8,7 @@ class Mollie_WC_Plugin { const PLUGIN_ID = 'mollie-payments-for-woocommerce'; const PLUGIN_TITLE = 'Mollie Payments for WooCommerce'; - const PLUGIN_VERSION = '5.4.0'; + const PLUGIN_VERSION = '5.4.1'; const DB_VERSION = '1.0'; const DB_VERSION_PARAM_NAME = 'mollie-db-version';