transformers
1 year ago
multi-props.php
1 year ago
props-resolver.php
1 year ago
transformer-base.php
1 year ago
transformers-registry.php
1 year ago
props-resolver.php
160 lines
| 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 | 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; |
| 22 | |
| 23 | const CONTEXT_SETTINGS = 'settings'; |
| 24 | const CONTEXT_STYLES = 'styles'; |
| 25 | |
| 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; |
| 35 | } |
| 36 | |
| 37 | public static function for_styles(): self { |
| 38 | return self::instance( self::CONTEXT_STYLES ); |
| 39 | } |
| 40 | |
| 41 | public static function for_settings(): self { |
| 42 | return self::instance( self::CONTEXT_SETTINGS ); |
| 43 | } |
| 44 | |
| 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 ); |
| 52 | } |
| 53 | |
| 54 | return self::$instances[ $context ]; |
| 55 | } |
| 56 | |
| 57 | public static function reset(): void { |
| 58 | self::$instances = []; |
| 59 | } |
| 60 | |
| 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 ); |
| 73 | } |
| 74 | |
| 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 | |
| 88 | if ( $prop_type instanceof Union_Prop_Type ) { |
| 89 | $prop_type = $prop_type->get_prop_type( $value['$$type'] ); |
| 90 | |
| 91 | if ( ! $prop_type ) { |
| 92 | return null; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if ( $prop_type instanceof Object_Prop_Type ) { |
| 97 | if ( ! is_array( $value['value'] ) ) { |
| 98 | return null; |
| 99 | } |
| 100 | |
| 101 | $value['value'] = $this->resolve( |
| 102 | $prop_type->get_shape(), |
| 103 | $value['value'] |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | if ( $prop_type instanceof Array_Prop_Type ) { |
| 108 | if ( ! is_array( $value['value'] ) ) { |
| 109 | return null; |
| 110 | } |
| 111 | |
| 112 | $value['value'] = $this->assign_values( |
| 113 | $value['value'], |
| 114 | $prop_type->get_item_type() |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | $transformer = $this->transformers->get( $value['$$type'] ); |
| 119 | |
| 120 | if ( ! ( $transformer instanceof Transformer_Base ) ) { |
| 121 | return null; |
| 122 | } |
| 123 | |
| 124 | try { |
| 125 | $transformed_value = $transformer->transform( $value['value'], $key ); |
| 126 | |
| 127 | return $this->transform( $transformed_value, $key, $prop_type, $depth + 1 ); |
| 128 | } catch ( Exception $e ) { |
| 129 | return null; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | private function is_transformable( $value ): bool { |
| 134 | return ( |
| 135 | ! empty( $value['$$type'] ) && |
| 136 | array_key_exists( 'value', $value ) |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | private function assign_values( $values, $schema ) { |
| 141 | $assigned = []; |
| 142 | |
| 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 | } |
| 159 | } |
| 160 |