From 8c1af842042e163919fbd93cf5c3b3703d6cd6f3 Mon Sep 17 00:00:00 2001 From: Alex P Date: Mon, 27 Feb 2023 12:04:43 +0200 Subject: [PATCH] Refactor settings check --- .../src/Helper/SettingsStatus.php | 58 +++++++------------ 1 file changed, 22 insertions(+), 36 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Helper/SettingsStatus.php b/modules/ppcp-wc-gateway/src/Helper/SettingsStatus.php index 1e13a884b..7655ef8ad 100644 --- a/modules/ppcp-wc-gateway/src/Helper/SettingsStatus.php +++ b/modules/ppcp-wc-gateway/src/Helper/SettingsStatus.php @@ -52,26 +52,13 @@ class SettingsStatus { * @return bool true if is enabled, otherwise false. */ public function is_pay_later_messaging_enabled_for_location( string $location ): bool { - $location = $this->normalize_location( $location ); - - if ( ! $this->is_pay_later_messaging_enabled() ) { - return false; - } - - $selected_locations = $this->settings->has( 'pay_later_messaging_locations' ) ? $this->settings->get( 'pay_later_messaging_locations' ) : array(); - - if ( empty( $selected_locations ) ) { - return false; - } - - return in_array( $location, $selected_locations, true ); + return $this->is_pay_later_messaging_enabled() && $this->is_enabled_for_location( 'pay_later_messaging_locations', $location ); } /** * Check whether Pay Later button is enabled either for checkout, cart or product page. * * @return bool true if is enabled, otherwise false. - * @throws NotFoundException When a setting was not found. */ public function is_pay_later_button_enabled(): bool { $pay_later_button_enabled = $this->settings->has( 'pay_later_button_enabled' ) && $this->settings->get( 'pay_later_button_enabled' ); @@ -87,19 +74,7 @@ class SettingsStatus { * @return bool true if is enabled, otherwise false. */ public function is_pay_later_button_enabled_for_location( string $location ): bool { - $location = $this->normalize_location( $location ); - - if ( ! $this->is_pay_later_button_enabled() ) { - return false; - } - - $selected_locations = $this->settings->has( 'pay_later_button_locations' ) ? $this->settings->get( 'pay_later_button_locations' ) : array(); - - if ( empty( $selected_locations ) ) { - return false; - } - - return in_array( $location, $selected_locations, true ); + return $this->is_pay_later_button_enabled() && $this->is_enabled_for_location( 'pay_later_button_locations', $location ); } /** @@ -109,15 +84,7 @@ class SettingsStatus { * @return bool true if is enabled, otherwise false. */ public function is_smart_button_enabled_for_location( string $location ): bool { - $location = $this->normalize_location( $location ); - - $selected_locations = $this->settings->has( 'smart_button_locations' ) ? $this->settings->get( 'smart_button_locations' ) : array(); - - if ( empty( $selected_locations ) ) { - return false; - } - - return in_array( $location, $selected_locations, true ); + return $this->is_enabled_for_location( 'smart_button_locations', $location ); } /** @@ -132,4 +99,23 @@ class SettingsStatus { } return $location; } + + /** + * Checks whether the locations field in the settings includes the given location. + * + * @param string $setting_name The name of the enabled locations field in the settings. + * @param string $location The location. + * @return bool + */ + protected function is_enabled_for_location( string $setting_name, string $location ): bool { + $location = $this->normalize_location( $location ); + + $selected_locations = $this->settings->has( $setting_name ) ? $this->settings->get( $setting_name ) : array(); + + if ( empty( $selected_locations ) ) { + return false; + } + + return in_array( $location, $selected_locations, true ); + } }