Skip to content

Commit

Permalink
Merge pull request #3075 from woocommerce/PCP-4160-connect-style-tab
Browse files Browse the repository at this point in the history
Connect styling tab with the frontend (4160)
  • Loading branch information
Dinamiko authored Feb 7, 2025
2 parents b1adebd + b00ac8f commit fa06cf9
Show file tree
Hide file tree
Showing 6 changed files with 338 additions and 28 deletions.
30 changes: 28 additions & 2 deletions modules/ppcp-compat/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

namespace WooCommerce\PayPalCommerce\Compat;

use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\Compat\Assets\CompatAssets;
use WooCommerce\PayPalCommerce\Compat\Settings\SettingsMap;
use WooCommerce\PayPalCommerce\Compat\Settings\SettingsMapHelper;
use WooCommerce\PayPalCommerce\Compat\Settings\StylingSettingsMapHelper;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;

return array(

Expand Down Expand Up @@ -127,6 +130,9 @@
return array();
}

$styling_settings_map_helper = $container->get( 'compat.settings.styling_map_helper' );
assert( $styling_settings_map_helper instanceof StylingSettingsMapHelper );

return array(
new SettingsMap(
$container->get( 'settings.data.general' ),
Expand All @@ -152,9 +158,29 @@
'sandbox_merchant_email' => 'merchant_email',
)
),
new SettingsMap(
$container->get( 'settings.data.styling' ),
/**
* The `StylingSettings` class stores settings as `LocationStylingDTO` objects.
* This method creates a mapping from old setting keys to the corresponding style names.
*
* Example:
* 'button_product_layout' => 'layout'
*
* This mapping will allow to retrieve the correct style value
* from a `LocationStylingDTO` object by dynamically accessing its properties.
*/
$styling_settings_map_helper->map()
),
);
},
'compat.settings.settings_map_helper' => static function( ContainerInterface $container ) : SettingsMapHelper {
return new SettingsMapHelper( $container->get( 'compat.setting.new-to-old-map' ) );
return new SettingsMapHelper(
$container->get( 'compat.setting.new-to-old-map' ),
$container->get( 'compat.settings.styling_map_helper' )
);
},
'compat.settings.styling_map_helper' => static function() : StylingSettingsMapHelper {
return new StylingSettingsMapHelper();
},
);
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?php
/**
* A map of new to old settings.
* A map of old to new settings.
*
* @package WooCommerce\PayPalCommerce\Compat
* @package WooCommerce\PayPalCommerce\Compat\Settings
*/

declare(strict_types=1);

namespace WooCommerce\PayPalCommerce\Compat;
namespace WooCommerce\PayPalCommerce\Compat\Settings;

use WooCommerce\PayPalCommerce\Settings\Data\AbstractDataModel;

/**
* A map of new to old settings.
* A map of old to new settings.
*
* @psalm-type newSettingsKey = string
* @psalm-type oldSettingsKey = string
Expand All @@ -26,17 +26,17 @@ class SettingsMap {
private AbstractDataModel $model;

/**
* The map of the new setting key to the old setting keys.
* The map of the old setting key to the new setting keys.
*
* @var array<newSettingsKey, oldSettingsKey>
* @var array<oldSettingsKey, newSettingsKey>
*/
private array $map;

/**
* The constructor.
*
* @param AbstractDataModel $model The new settings model.
* @param array<newSettingsKey, oldSettingsKey> $map The map of the new setting key to the old setting keys.
* @param array<oldSettingsKey, newSettingsKey> $map The map of the old setting key to the new setting keys.
*/
public function __construct( AbstractDataModel $model, array $map ) {
$this->model = $model;
Expand All @@ -53,9 +53,9 @@ public function get_model(): AbstractDataModel {
}

/**
* The map of the new setting key to the old setting keys.
* The map of the old setting key to the new setting keys.
*
* @return array<newSettingsKey, oldSettingsKey>
* @return array<oldSettingsKey, newSettingsKey>
*/
public function get_map(): array {
return $this->map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
/**
* A helper for mapping the new/old settings.
*
* @package WooCommerce\PayPalCommerce\Compat
* @package WooCommerce\PayPalCommerce\Compat\Settings
*/

declare( strict_types = 1 );

namespace WooCommerce\PayPalCommerce\Compat;
namespace WooCommerce\PayPalCommerce\Compat\Settings;

use RuntimeException;
use WooCommerce\PayPalCommerce\Settings\Data\StylingSettings;

/**
* A helper class to manage the transition between legacy and new settings.
Expand Down Expand Up @@ -41,15 +42,24 @@ class SettingsMapHelper {
*/
protected array $model_cache = array();

/**
* A helper for mapping the old/new styling settings.
*
* @var StylingSettingsMapHelper
*/
protected StylingSettingsMapHelper $styling_settings_map_helper;

/**
* Constructor.
*
* @param SettingsMap[] $settings_map A list of settings maps containing key definitions.
* @param SettingsMap[] $settings_map A list of settings maps containing key definitions.
* @param StylingSettingsMapHelper $styling_settings_map_helper A helper for mapping the old/new styling settings.
* @throws RuntimeException When an old key has multiple mappings.
*/
public function __construct( array $settings_map ) {
public function __construct( array $settings_map, StylingSettingsMapHelper $styling_settings_map_helper ) {
$this->validate_settings_map( $settings_map );
$this->settings_map = $settings_map;
$this->settings_map = $settings_map;
$this->styling_settings_map_helper = $styling_settings_map_helper;
}

/**
Expand Down Expand Up @@ -80,15 +90,18 @@ protected function validate_settings_map( array $settings_map ) : void {
*/
public function mapped_value( string $old_key ) {
$this->ensure_map_initialized();

if ( ! isset( $this->key_to_model[ $old_key ] ) ) {
return null;
}

$mapping = $this->key_to_model[ $old_key ];
$model_id = spl_object_id( $mapping['model'] );
$mapping = $this->key_to_model[ $old_key ];
$model = $mapping['model'] ?? false;

return $this->get_cached_model_value( $model_id, $mapping['new_key'], $mapping['model'] );
if ( ! $model ) {
return null;
}

return $this->get_cached_model_value( spl_object_id( $model ), $old_key, $mapping['new_key'], $mapping['model'] );
}

/**
Expand All @@ -108,17 +121,23 @@ public function has_mapped_key( string $old_key ) : bool {
* Retrieves a cached model value or caches it if not already cached.
*
* @param int $model_id The unique identifier for the model object.
* @param string $old_key The key in the old settings structure.
* @param string $new_key The key in the new settings structure.
* @param object $model The model object.
*
* @return mixed|null The value of the key in the model, or null if not found.
*/
protected function get_cached_model_value( int $model_id, string $new_key, object $model ) {
protected function get_cached_model_value( int $model_id, string $old_key, string $new_key, object $model ) {
if ( ! isset( $this->model_cache[ $model_id ] ) ) {
$this->model_cache[ $model_id ] = $model->to_array();
}

return $this->model_cache[ $model_id ][ $new_key ] ?? null;
switch ( true ) {
case $model instanceof StylingSettings:
return $this->styling_settings_map_helper->mapped_value( $old_key, $this->model_cache[ $model_id ] );
default:
return $this->model_cache[ $model_id ][ $new_key ] ?? null;
}
}

/**
Expand Down
Loading

0 comments on commit fa06cf9

Please sign in to comment.