Refactor settings check

This commit is contained in:
Alex P 2023-02-27 12:04:43 +02:00
parent f8daa7ff2f
commit 8c1af84204
No known key found for this signature in database
GPG key ID: 54487A734A204D71

View file

@ -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 );
}
}