mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 14:57:26 +08:00
✨ Allow callbacks in SettingsMap to resolve values
This commit is contained in:
parent
4efdfe0b17
commit
7b12166b5c
2 changed files with 15 additions and 6 deletions
|
@ -14,7 +14,7 @@ use WooCommerce\PayPalCommerce\Settings\Data\AbstractDataModel;
|
||||||
/**
|
/**
|
||||||
* A map of old to new settings.
|
* A map of old to new settings.
|
||||||
*
|
*
|
||||||
* @psalm-type newSettingsKey = string
|
* @psalm-type newSettingsKey = string|callable
|
||||||
* @psalm-type oldSettingsKey = string
|
* @psalm-type oldSettingsKey = string
|
||||||
*/
|
*/
|
||||||
class SettingsMap {
|
class SettingsMap {
|
||||||
|
|
|
@ -134,18 +134,27 @@ class SettingsMapHelper {
|
||||||
/**
|
/**
|
||||||
* Retrieves a cached model value or caches it if not already cached.
|
* Retrieves a cached model value or caches it if not already cached.
|
||||||
*
|
*
|
||||||
* @param int $model_id The unique identifier for the model object.
|
* @param int $model_id The unique identifier for the model object.
|
||||||
* @param string $old_key The key in the old settings structure.
|
* @param string $old_key The key in the old settings structure.
|
||||||
* @param string $new_key The key in the new settings structure.
|
* @param string|callable $new_key The key in the new settings structure.
|
||||||
* @param object $model The model object.
|
* @param object $model The model object.
|
||||||
*
|
*
|
||||||
* @return mixed|null The value of the key in the model, or null if not found.
|
* @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 $old_key, string $new_key, object $model ) {
|
protected function get_cached_model_value( int $model_id, string $old_key, $new_key, object $model ) {
|
||||||
if ( ! isset( $this->model_cache[ $model_id ] ) ) {
|
if ( ! isset( $this->model_cache[ $model_id ] ) ) {
|
||||||
$this->model_cache[ $model_id ] = $model->to_array();
|
$this->model_cache[ $model_id ] = $model->to_array();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( is_callable( $new_key ) ) {
|
||||||
|
// Resolve the callback once and store it in the request-cache.
|
||||||
|
if ( ! isset( $this->model_cache[ $model_id ][ $old_key ] ) ) {
|
||||||
|
$this->model_cache[ $model_id ][ $old_key ] = $new_key( $this->model_cache[ $model_id ] );
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->model_cache[ $model_id ][ $old_key ];
|
||||||
|
}
|
||||||
|
|
||||||
switch ( true ) {
|
switch ( true ) {
|
||||||
case $model instanceof StylingSettings:
|
case $model instanceof StylingSettings:
|
||||||
return $this->styling_settings_map_helper->mapped_value( $old_key, $this->model_cache[ $model_id ] );
|
return $this->styling_settings_map_helper->mapped_value( $old_key, $this->model_cache[ $model_id ] );
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue