PluginProbe
Elementor Website Builder – more than just a page builder / 3.35.8
Elementor Website Builder – more than just a page builder v3.35.8
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 4.0.7 All 451 releases
elementor / modules / atomic-widgets / props-resolver / props-resolver.php

props-resolver.php in Elementor Website Builder – more than just a page builder 3.35.8, at modules/atomic-widgets/props-resolver/props-resolver.php

112 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\AtomicWidgets\PropsResolver;
4
5 use Elementor\Modules\AtomicWidgets\PropTypes\Base\Array_Prop_Type;
6 use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type;
7 use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
8 use Elementor\Modules\AtomicWidgets\PropTypes\Union_Prop_Type;
9 use Exception;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit; // Exit if accessed directly.
13 }
14
15 abstract class Props_Resolver {
16 protected static array $instances = [];
17
18 protected Transformers_Registry $transformers_registry;
19
20 protected function __construct( Transformers_Registry $transformers_registry ) {
21 $this->transformers_registry = $transformers_registry;
22 }
23
24 protected static function instance( string $context ) {
25 if ( ! isset( static::$instances[ $context ] ) ) {
26 $instance = new static( new Transformers_Registry() );
27
28 static::$instances[ $context ] = $instance;
29
30 do_action(
31 "elementor/atomic-widgets/$context/transformers/register",
32 $instance->get_transformers_registry(),
33 $instance
34 );
35 }
36
37 return static::$instances[ $context ];
38 }
39
40 public static function reset(): void {
41 static::$instances = [];
42 }
43
44 public function get_transformers_registry(): Transformers_Registry {
45 return $this->transformers_registry;
46 }
47
48 protected function transform( $value, $key, Prop_Type $prop_type ) {
49 if ( $prop_type instanceof Union_Prop_Type ) {
50 $prop_type = $prop_type->get_prop_type( $value['$$type'] );
51
52 if ( ! $prop_type ) {
53 return null;
54 }
55 }
56
57 if ( $value['$$type'] !== $prop_type::get_key() ) {
58 return null;
59 }
60
61 if ( $prop_type instanceof Object_Prop_Type ) {
62 if ( ! is_array( $value['value'] ) ) {
63 return null;
64 }
65
66 $value['value'] = $this->resolve(
67 $prop_type->get_shape(),
68 $value['value']
69 );
70 }
71
72 if ( $prop_type instanceof Array_Prop_Type ) {
73 if ( ! is_array( $value['value'] ) ) {
74 return null;
75 }
76
77 $value['value'] = array_map(
78 fn( $item ) => $this->resolve_item( $item, null, $prop_type->get_item_type() ),
79 $value['value']
80 );
81 }
82
83 $transformer = $this->transformers_registry->get( $value['$$type'] );
84
85 if ( ! ( $transformer instanceof Transformer_Base ) ) {
86 return null;
87 }
88
89 try {
90 $context = Props_Resolver_Context::make()
91 ->set_key( $key )
92 ->set_disabled( (bool) ( $value['disabled'] ?? false ) )
93 ->set_prop_type( $prop_type );
94
95 return $transformer->transform( $value['value'], $context );
96 } catch ( Exception $e ) {
97 return null;
98 }
99 }
100
101 protected function is_transformable( $value ): bool {
102 return (
103 ! empty( $value['$$type'] ) &&
104 array_key_exists( 'value', $value )
105 );
106 }
107
108 abstract public function resolve( array $schema, array $props ): array;
109
110 abstract protected function resolve_item( $value, $key, Prop_Type $prop_type );
111 }
112