PluginProbe
Elementor Website Builder – more than just a page builder / 3.26.4
Elementor Website Builder – more than just a page builder v3.26.4
4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
← All changes | modules/atomic-widgets/props-resolver/props-resolver.php +83 -35 4.1.0-beta13.26.4 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,15 +108,15 @@
73 108 if ( ! is_array( $value['value'] ) ) {
74 109 return null;
75 110 }
76 111
77 - $value['value'] = array_map(
78 - fn( $item ) => $this->resolve_item( $item, null, $prop_type->get_item_type() ),
79 - $value['value']
112 + $value['value'] = $this->assign_values(
113 + $value['value'],
114 + $prop_type->get_item_type()
80 115 );
81 116 }
82 117
83 - $transformer = $this->transformers_registry->get( $value['$$type'] );
118 + $transformer = $this->transformers->get( $value['$$type'] );
84 119
85 120 if ( ! ( $transformer instanceof Transformer_Base ) ) {
86 121 return null;
87 122 }
@@ -86,20 +121,17 @@
86 121 return null;
87 122 }
88 123
89 124 try {
90 - $context = Props_Resolver_Context::make()
91 - ->set_key( $key )
92 - ->set_disabled( (bool) ( $value['disabled'] ?? false ) )
93 - ->set_prop_type( $prop_type );
125 + $transformed_value = $transformer->transform( $value['value'], $key );
94 126
95 - return $transformer->transform( $value['value'], $context );
127 + return $this->transform( $transformed_value, $key, $prop_type, $depth + 1 );
96 128 } catch ( Exception $e ) {
97 129 return null;
98 130 }
99 131 }
100 132
101 - protected function is_transformable( $value ): bool {
133 + private function is_transformable( $value ): bool {
102 134 return (
103 135 ! empty( $value['$$type'] ) &&
104 136 array_key_exists( 'value', $value )
105 137 );
@@ -104,8 +136,24 @@
104 136 array_key_exists( 'value', $value )
105 137 );
106 138 }
107 139
108 - abstract public function resolve( array $schema, array $props ): array;
140 + private function assign_values( $values, $schema ) {
141 + $assigned = [];
109 142
110 - 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 + }
111 159 }