This commit is contained in:
Alex P 2023-12-27 16:12:04 +02:00
parent a87ca32a12
commit b5b8502997
No known key found for this signature in database
GPG key ID: 54487A734A204D71
2 changed files with 50 additions and 2 deletions

View file

@ -89,6 +89,11 @@ class SaveConfig {
}
}
/**
* Saves the config into the old settings.
*
* @param array $config The configurator config.
*/
private function save_config( array $config ): void {
$this->settings->set( 'pay_later_enable_styling_per_messaging_location', true );
@ -109,6 +114,12 @@ class SaveConfig {
$this->settings->persist();
}
/**
* Saves the config for a location into the old settings.
*
* @param array $config The configurator config for a location.
* @param string $location The location name in the old settings.
*/
private function save_config_for_location( array $config, string $location ): void {
$this->set_value_if_present( $config, 'layout', "pay_later_{$location}_message_layout" );
@ -121,12 +132,24 @@ class SaveConfig {
$this->set_value_if_present( $config, 'text-size', "pay_later_{$location}_message_text_size" );
}
/**
* Sets the value in the settings if it is available in the config.
*
* @param array $config The configurator config.
* @param string $key The key in the config.
* @param string $settings_key The key in the settings.
*/
private function set_value_if_present( array $config, string $key, string $settings_key ): void {
if ( isset( $config[ $key ] ) ) {
$this->settings->set( $settings_key, $config[ $key ] );
}
}
/**
* Converts the configurator placement into location in the old settings.
*
* @param string $placement The configurator placement.
*/
private function configurator_placement_to_location( string $placement ): string {
switch ( $placement ) {
case 'cart':

View file

@ -15,6 +15,11 @@ use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
* Class ConfigFactory.
*/
class ConfigFactory {
/**
* Returns the configurator config for the given old settings.
*
* @param Settings $settings The settings.
*/
public function from_settings( Settings $settings ): array {
return array(
$this->location_to_configurator_placement( 'cart' ) => $this->for_location( $settings, 'cart' ),
@ -25,11 +30,17 @@ class ConfigFactory {
);
}
/**
* Returns the configurator config for a location.
*
* @param Settings $settings The settings.
* @param string $location The location name in the old settings.
*/
private function for_location( Settings $settings, string $location ): array {
$selected_locations = $settings->has( 'pay_later_messaging_locations' ) ? $settings->get( 'pay_later_messaging_locations' ) : array();
$placement = $this->location_to_configurator_placement( $location );
if ( in_array( $placement, array( 'category', 'homepage' ) ) ) {
if ( in_array( $placement, array( 'category', 'homepage' ), true ) ) {
$config = array(
'layout' => 'flex',
'color' => $this->get_or_default( $settings, "pay_later_{$location}_message_flex_color", 'black', array( 'black', 'blue', 'white', 'white-no-border' ) ),
@ -48,13 +59,18 @@ class ConfigFactory {
return array_merge(
array(
'status' => in_array( $location, $selected_locations ) ? 'enabled' : 'disabled',
'status' => in_array( $location, $selected_locations, true ) ? 'enabled' : 'disabled',
'placement' => $placement,
),
$config
);
}
/**
* Converts the location name from the old settings into the configurator placement.
*
* @param string $location The location name in the old settings.
*/
private function location_to_configurator_placement( string $location ): string {
switch ( $location ) {
case 'cart':
@ -70,6 +86,15 @@ class ConfigFactory {
}
}
/**
* Returns the settings value or default, if does not exist or not allowed value.
*
* @param Settings $settings The settings.
* @param string $key The key.
* @param mixed $default The default value.
* @param array|null $allowed_values The list of allowed values, or null if all values are allowed.
* @return mixed
*/
private function get_or_default( Settings $settings, string $key, $default, ?array $allowed_values = null ) {
if ( $settings->has( $key ) ) {
$value = $settings->get( $key );