← All changes
|
modules/atomic-widgets/props-resolver/props-resolver.php
+84
-43
4.3.0-beta3
→
3.26.2
View file →
| @@ -11,42 +11,81 @@ | ||
| 11 | 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | 12 | exit; // Exit if accessed directly. |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | -abstract class Props_Resolver { | |
| 16 | - protected static array $instances = []; | |
| 15 | +class Props_Resolver { | |
| 16 | + /** | |
| 17 | + * Each transformer can return a value that is also a transformable value, | |
| 18 | + * which means that it can be transformed again by another transformer. | |
| 19 | + * This constant defines the maximum depth of transformations to avoid infinite loops. | |
| 20 | + */ | |
| 21 | + const TRANSFORM_DEPTH_LIMIT = 3; | |
| 17 | 22 | |
| 18 | - protected Transformers_Registry $transformers_registry; | |
| 23 | + const CONTEXT_SETTINGS = 'settings'; | |
| 24 | + const CONTEXT_STYLES = 'styles'; | |
| 19 | 25 | |
| 20 | - protected function __construct( Transformers_Registry $transformers_registry ) { | |
| 21 | - $this->transformers_registry = $transformers_registry; | |
| 26 | + /** | |
| 27 | + * @var array<string, Props_Resolver> | |
| 28 | + */ | |
| 29 | + private static array $instances = []; | |
| 30 | + | |
| 31 | + private Transformers_Registry $transformers; | |
| 32 | + | |
| 33 | + private function __construct( Transformers_Registry $transformers ) { | |
| 34 | + $this->transformers = $transformers; | |
| 22 | 35 | } |
| 23 | 36 | |
| 24 | - protected static function instance( string $context ) { | |
| 25 | - if ( ! isset( static::$instances[ $context ] ) ) { | |
| 26 | - $instance = new static( new Transformers_Registry() ); | |
| 37 | + public static function for_styles(): self { | |
| 38 | + return self::instance( self::CONTEXT_STYLES ); | |
| 39 | + } | |
| 27 | 40 | |
| 28 | - static::$instances[ $context ] = $instance; | |
| 41 | + public static function for_settings(): self { | |
| 42 | + return self::instance( self::CONTEXT_SETTINGS ); | |
| 43 | + } | |
| 29 | 44 | |
| 30 | - do_action( | |
| 31 | - "elementor/atomic-widgets/$context/transformers/register", | |
| 32 | - $instance->get_transformers_registry(), | |
| 33 | - $instance | |
| 34 | - ); | |
| 45 | + private static function instance( string $context ): self { | |
| 46 | + if ( ! isset( self::$instances[ $context ] ) ) { | |
| 47 | + $registry = new Transformers_Registry(); | |
| 48 | + | |
| 49 | + do_action( "elementor/atomic-widgets/$context/transformers/register", $registry ); | |
| 50 | + | |
| 51 | + self::$instances[ $context ] = new self( $registry ); | |
| 35 | 52 | } |
| 36 | 53 | |
| 37 | - return static::$instances[ $context ]; | |
| 54 | + return self::$instances[ $context ]; | |
| 38 | 55 | } |
| 39 | 56 | |
| 40 | 57 | public static function reset(): void { |
| 41 | - static::$instances = []; | |
| 58 | + self::$instances = []; | |
| 42 | 59 | } |
| 43 | 60 | |
| 44 | - public function get_transformers_registry(): Transformers_Registry { | |
| 45 | - return $this->transformers_registry; | |
| 61 | + public function resolve( array $schema, array $props ): array { | |
| 62 | + $resolved = []; | |
| 63 | + | |
| 64 | + foreach ( $schema as $key => $prop_type ) { | |
| 65 | + if ( ! ( $prop_type instanceof Prop_Type ) ) { | |
| 66 | + continue; | |
| 67 | + } | |
| 68 | + | |
| 69 | + $resolved[ $key ] = $props[ $key ] ?? $prop_type->get_default(); | |
| 70 | + } | |
| 71 | + | |
| 72 | + return $this->assign_values( $resolved, $schema ); | |
| 46 | 73 | } |
| 47 | 74 | |
| 48 | - protected function transform( $value, $key, Prop_Type $prop_type ) { | |
| 75 | + private function transform( $value, $key, Prop_Type $prop_type, int $depth = 0 ) { | |
| 76 | + if ( ! $value || ! $this->is_transformable( $value ) ) { | |
| 77 | + return $value; | |
| 78 | + } | |
| 79 | + | |
| 80 | + if ( $depth >= self::TRANSFORM_DEPTH_LIMIT ) { | |
| 81 | + return null; | |
| 82 | + } | |
| 83 | + | |
| 84 | + if ( isset( $value['disabled'] ) && true === $value['disabled'] ) { | |
| 85 | + return null; | |
| 86 | + } | |
| 87 | + | |
| 49 | 88 | if ( $prop_type instanceof Union_Prop_Type ) { |
| 50 | 89 | $prop_type = $prop_type->get_prop_type( $value['$$type'] ); |
| 51 | 90 | |
| 52 | 91 | if ( ! $prop_type ) { |
| @@ -53,12 +92,8 @@ | ||
| 53 | 92 | return null; |
| 54 | 93 | } |
| 55 | 94 | } |
| 56 | 95 | |
| 57 | - if ( $value['$$type'] !== $prop_type::get_key() ) { | |
| 58 | - return null; | |
| 59 | - } | |
| 60 | - | |
| 61 | 96 | if ( $prop_type instanceof Object_Prop_Type ) { |
| 62 | 97 | if ( ! is_array( $value['value'] ) ) { |
| 63 | 98 | return null; |
| 64 | 99 | } |
| @@ -73,22 +108,15 @@ | ||
| 73 | 108 | if ( ! is_array( $value['value'] ) ) { |
| 74 | 109 | return null; |
| 75 | 110 | } |
| 76 | 111 | |
| 77 | - $resolved_items = []; | |
| 78 | - | |
| 79 | - foreach ( $value['value'] as $item ) { | |
| 80 | - $resolved = $this->resolve_item( $item, null, $prop_type->get_item_type() ); | |
| 81 | - | |
| 82 | - if ( null !== $resolved ) { | |
| 83 | - $resolved_items[] = $resolved; | |
| 84 | - } | |
| 85 | - } | |
| 86 | - | |
| 87 | - $value['value'] = $resolved_items; | |
| 112 | + $value['value'] = $this->assign_values( | |
| 113 | + $value['value'], | |
| 114 | + $prop_type->get_item_type() | |
| 115 | + ); | |
| 88 | 116 | } |
| 89 | 117 | |
| 90 | - $transformer = $this->transformers_registry->get( $value['$$type'] ); | |
| 118 | + $transformer = $this->transformers->get( $value['$$type'] ); | |
| 91 | 119 | |
| 92 | 120 | if ( ! ( $transformer instanceof Transformer_Base ) ) { |
| 93 | 121 | return null; |
| 94 | 122 | } |
| @@ -93,20 +121,17 @@ | ||
| 93 | 121 | return null; |
| 94 | 122 | } |
| 95 | 123 | |
| 96 | 124 | try { |
| 97 | - $context = Props_Resolver_Context::make() | |
| 98 | - ->set_key( $key ) | |
| 99 | - ->set_disabled( (bool) ( $value['disabled'] ?? false ) ) | |
| 100 | - ->set_prop_type( $prop_type ); | |
| 125 | + $transformed_value = $transformer->transform( $value['value'], $key ); | |
| 101 | 126 | |
| 102 | - return $transformer->transform( $value['value'], $context ); | |
| 127 | + return $this->transform( $transformed_value, $key, $prop_type, $depth + 1 ); | |
| 103 | 128 | } catch ( Exception $e ) { |
| 104 | 129 | return null; |
| 105 | 130 | } |
| 106 | 131 | } |
| 107 | 132 | |
| 108 | - protected function is_transformable( $value ): bool { | |
| 133 | + private function is_transformable( $value ): bool { | |
| 109 | 134 | return ( |
| 110 | 135 | ! empty( $value['$$type'] ) && |
| 111 | 136 | array_key_exists( 'value', $value ) |
| 112 | 137 | ); |
| @@ -111,8 +136,24 @@ | ||
| 111 | 136 | array_key_exists( 'value', $value ) |
| 112 | 137 | ); |
| 113 | 138 | } |
| 114 | 139 | |
| 115 | - abstract public function resolve( array $schema, array $props ): array; | |
| 140 | + private function assign_values( $values, $schema ) { | |
| 141 | + $assigned = []; | |
| 116 | 142 | |
| 117 | - abstract protected function resolve_item( $value, $key, Prop_Type $prop_type ); | |
| 143 | + foreach ( $values as $key => $value ) { | |
| 144 | + $prop_type = $schema instanceof Prop_Type ? $schema : $schema[ $key ]; | |
| 145 | + | |
| 146 | + $transformed = $this->transform( $value, $key, $prop_type ); | |
| 147 | + | |
| 148 | + if ( Multi_Props::is( $transformed ) ) { | |
| 149 | + $assigned = array_merge( $assigned, Multi_Props::get_value( $transformed ) ); | |
| 150 | + | |
| 151 | + continue; | |
| 152 | + } | |
| 153 | + | |
| 154 | + $assigned[ $key ] = $transformed; | |
| 155 | + } | |
| 156 | + | |
| 157 | + return $assigned; | |
| 158 | + } | |
| 118 | 159 | } |