| 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 Render_Props_Resolver extends 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 |
public static function for_styles(): self { |
| 27 |
return static::instance( self::CONTEXT_STYLES ); |
| 28 |
} |
| 29 |
|
| 30 |
public static function for_settings(): self { |
| 31 |
return static::instance( self::CONTEXT_SETTINGS ); |
| 32 |
} |
| 33 |
|
| 34 |
public function resolve( array $schema, array $props ): array { |
| 35 |
$resolved = []; |
| 36 |
|
| 37 |
foreach ( $schema as $key => $prop_type ) { |
| 38 |
if ( ! ( $prop_type instanceof Prop_Type ) ) { |
| 39 |
continue; |
| 40 |
} |
| 41 |
|
| 42 |
$transformed = $this->resolve_item( |
| 43 |
$props[ $key ] ?? $prop_type->get_default(), |
| 44 |
$key, |
| 45 |
$prop_type |
| 46 |
); |
| 47 |
|
| 48 |
if ( Multi_Props::is( $transformed ) ) { |
| 49 |
$resolved = array_merge( $resolved, Multi_Props::get_value( $transformed ) ); |
| 50 |
|
| 51 |
continue; |
| 52 |
} |
| 53 |
|
| 54 |
$resolved[ $key ] = $transformed; |
| 55 |
} |
| 56 |
|
| 57 |
return $resolved; |
| 58 |
} |
| 59 |
|
| 60 |
protected function resolve_item( $value, $key, Prop_Type $prop_type, int $depth = 0 ) { |
| 61 |
if ( null === $value ) { |
| 62 |
return null; |
| 63 |
} |
| 64 |
|
| 65 |
if ( ! $this->is_transformable( $value ) ) { |
| 66 |
return $value; |
| 67 |
} |
| 68 |
|
| 69 |
if ( $depth >= self::TRANSFORM_DEPTH_LIMIT ) { |
| 70 |
return null; |
| 71 |
} |
| 72 |
|
| 73 |
if ( isset( $value['disabled'] ) && true === $value['disabled'] ) { |
| 74 |
return null; |
| 75 |
} |
| 76 |
|
| 77 |
$transformed = $this->transform( $value, $key, $prop_type ); |
| 78 |
|
| 79 |
return $this->resolve_item( $transformed, $key, $prop_type, $depth + 1 ); |
| 80 |
} |
| 81 |
} |
| 82 |
|