mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 14:57:26 +08:00
Remove the unnecessary files
This commit is contained in:
parent
fd8346b487
commit
72554570d3
3 changed files with 0 additions and 369 deletions
|
@ -1,63 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* A map of old to new settings.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\Settings\Compat
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\Settings\Compat;
|
||||
|
||||
use WooCommerce\PayPalCommerce\Settings\Data\AbstractDataModel;
|
||||
|
||||
/**
|
||||
* A map of old to new settings.
|
||||
*
|
||||
* @psalm-type newSettingsKey = string
|
||||
* @psalm-type oldSettingsKey = string
|
||||
*/
|
||||
class SettingsMap {
|
||||
/**
|
||||
* The new settings model.
|
||||
*
|
||||
* @var AbstractDataModel
|
||||
*/
|
||||
private AbstractDataModel $model;
|
||||
|
||||
/**
|
||||
* The map of the old setting key to the new setting keys.
|
||||
*
|
||||
* @var array<oldSettingsKey, newSettingsKey>
|
||||
*/
|
||||
private array $map;
|
||||
|
||||
/**
|
||||
* The constructor.
|
||||
*
|
||||
* @param AbstractDataModel $model The new settings model.
|
||||
* @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;
|
||||
$this->map = $map;
|
||||
}
|
||||
|
||||
/**
|
||||
* The model.
|
||||
*
|
||||
* @return AbstractDataModel
|
||||
*/
|
||||
public function get_model(): AbstractDataModel {
|
||||
return $this->model;
|
||||
}
|
||||
|
||||
/**
|
||||
* The map of the old setting key to the new setting keys.
|
||||
*
|
||||
* @return array<oldSettingsKey, newSettingsKey>
|
||||
*/
|
||||
public function get_map(): array {
|
||||
return $this->map;
|
||||
}
|
||||
}
|
|
@ -1,175 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* A helper for mapping the new/old settings.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\Settings\Compat
|
||||
*/
|
||||
|
||||
declare( strict_types = 1 );
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\Settings\Compat;
|
||||
|
||||
use RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\Settings\Data\StylingSettings;
|
||||
|
||||
/**
|
||||
* A helper class to manage the transition between legacy and new settings.
|
||||
*
|
||||
* This utility provides mapping from old setting keys to new ones and retrieves
|
||||
* their corresponding values from the appropriate models. The class uses lazy
|
||||
* loading and caching to optimize performance during runtime.
|
||||
*/
|
||||
class SettingsMapHelper {
|
||||
|
||||
/**
|
||||
* A list of settings maps containing mapping definitions.
|
||||
*
|
||||
* @var SettingsMap[]
|
||||
*/
|
||||
protected array $settings_map;
|
||||
|
||||
/**
|
||||
* Indexed map for faster lookups, initialized lazily.
|
||||
*
|
||||
* @var array|null Associative array where old keys map to metadata.
|
||||
*/
|
||||
protected ?array $key_to_model = null;
|
||||
|
||||
/**
|
||||
* Cache for results of `to_array()` calls on models.
|
||||
*
|
||||
* @var array Associative array where keys are model IDs.
|
||||
*/
|
||||
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 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, StylingSettingsMapHelper $styling_settings_map_helper ) {
|
||||
$this->validate_settings_map( $settings_map );
|
||||
$this->settings_map = $settings_map;
|
||||
$this->styling_settings_map_helper = $styling_settings_map_helper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the settings map for duplicate keys.
|
||||
*
|
||||
* @param SettingsMap[] $settings_map The settings map to validate.
|
||||
* @throws RuntimeException When an old key has multiple mappings.
|
||||
*/
|
||||
protected function validate_settings_map( array $settings_map ) : void {
|
||||
$seen_keys = array();
|
||||
|
||||
foreach ( $settings_map as $settings_map_instance ) {
|
||||
foreach ( $settings_map_instance->get_map() as $old_key => $new_key ) {
|
||||
if ( isset( $seen_keys[ $old_key ] ) ) {
|
||||
throw new RuntimeException( "Duplicate mapping for legacy key '$old_key'." );
|
||||
}
|
||||
$seen_keys[ $old_key ] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the value of a mapped key from the new settings.
|
||||
*
|
||||
* @param string $old_key The key from the legacy settings.
|
||||
*
|
||||
* @return mixed|null The value of the mapped setting, or null if not found.
|
||||
*/
|
||||
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 = $mapping['model'] ?? false;
|
||||
|
||||
if ( ! $model ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
switch ( true ) {
|
||||
case $model instanceof StylingSettings:
|
||||
return $this->styling_settings_map_helper->mapped_value( $old_key );
|
||||
default:
|
||||
return $this->get_cached_model_value( spl_object_id( $model ), $mapping['new_key'], $model );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if a given legacy key exists in the new settings.
|
||||
*
|
||||
* @param string $old_key The key from the legacy settings.
|
||||
*
|
||||
* @return bool True if the key exists in the new settings, false otherwise.
|
||||
*/
|
||||
public function has_mapped_key( string $old_key ) : bool {
|
||||
$this->ensure_map_initialized();
|
||||
|
||||
return isset( $this->key_to_model[ $old_key ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 $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 ) {
|
||||
if ( ! isset( $this->model_cache[ $model_id ] ) ) {
|
||||
$this->model_cache[ $model_id ] = $model->to_array();
|
||||
}
|
||||
|
||||
return $this->model_cache[ $model_id ][ $new_key ] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures the map of old-to-new settings is initialized.
|
||||
*
|
||||
* This method initializes the `key_to_model` array lazily to improve performance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function ensure_map_initialized() : void {
|
||||
if ( $this->key_to_model === null ) {
|
||||
$this->initialize_key_map();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the indexed map of old-to-new settings keys.
|
||||
*
|
||||
* This method processes the provided settings maps and indexes the legacy
|
||||
* keys to their corresponding metadata for efficient lookup.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function initialize_key_map() : void {
|
||||
$this->key_to_model = array();
|
||||
|
||||
foreach ( $this->settings_map as $settings_map_instance ) {
|
||||
foreach ( $settings_map_instance->get_map() as $old_key => $new_key ) {
|
||||
$this->key_to_model[ $old_key ] = array(
|
||||
'new_key' => $new_key,
|
||||
'model' => $settings_map_instance->get_model(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,131 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* A helper for mapping the old/new styling settings.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\Settings\Compat
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\Settings\Compat;
|
||||
|
||||
use WooCommerce\PayPalCommerce\Settings\Data\StylingSettings;
|
||||
|
||||
/**
|
||||
* A map of old to new styling settings.
|
||||
*
|
||||
* @psalm-import-type newSettingsKey from SettingsMap
|
||||
* @psalm-import-type oldSettingsKey from SettingsMap
|
||||
*/
|
||||
class StylingSettingsMapHelper {
|
||||
|
||||
/**
|
||||
* The constructor.
|
||||
*
|
||||
* @param StylingSettings $model The styling settings model.
|
||||
*/
|
||||
public function __construct( StylingSettings $model ) {
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps old setting keys to new setting style names.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* @psalm-return array<oldSettingsKey, newSettingsKey>
|
||||
*/
|
||||
public function map(): array {
|
||||
|
||||
$mapped_settings = array();
|
||||
|
||||
foreach ( $this->locations_map() as $old_location_name => $new_location_name ) {
|
||||
foreach ( $this->styles() as $style ) {
|
||||
$old_styling_key = $this->get_old_styling_setting_key( $old_location_name, $style );
|
||||
$mapped_settings[ $old_styling_key ] = $style;
|
||||
}
|
||||
}
|
||||
|
||||
return $mapped_settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the value of a mapped key from the new settings.
|
||||
*
|
||||
* @param string $old_key The key from the legacy settings.
|
||||
*
|
||||
* @return mixed|null The value of the mapped setting, or null if not found.
|
||||
*/
|
||||
public function mapped_value( string $old_key ) {
|
||||
foreach ( $this->locations_map() as $old_location_name => $new_location_name ) {
|
||||
foreach ( $this->styles() as $style ) {
|
||||
if ( $old_key !== $this->get_old_styling_setting_key( $old_location_name, $style ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$method = "get_{$new_location_name}";
|
||||
|
||||
if ( ! method_exists( $this->model, $method ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$location_settings = $this->model->$method();
|
||||
|
||||
return $location_settings->$style ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a mapping of old button location names to new settings location names.
|
||||
*
|
||||
* @return string[] The mapping of old location names to new location names.
|
||||
*/
|
||||
protected function locations_map(): array {
|
||||
return array(
|
||||
'product' => 'product',
|
||||
'cart' => 'cart',
|
||||
'checkout' => 'classic_checkout',
|
||||
'mini-cart' => 'mini_cart',
|
||||
'checkout-block-express' => 'express_checkout',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the available style names.
|
||||
*
|
||||
* @return string[] The list of available style names.
|
||||
*/
|
||||
protected function styles(): array {
|
||||
return array(
|
||||
'enabled',
|
||||
'methods',
|
||||
'shape',
|
||||
'label',
|
||||
'color',
|
||||
'layout',
|
||||
'tagline',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the old styling setting key name based on provided location and style names.
|
||||
*
|
||||
* @param string $location The location name.
|
||||
* @param string $style The style name.
|
||||
* @return string The old styling setting key name.
|
||||
*/
|
||||
protected function get_old_styling_setting_key( string $location, string $style ): string {
|
||||
$location_setting_name_part = $location === 'checkout' ? '' : "_{$location}";
|
||||
return "button{$location_setting_name_part}_{$style}";
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue