| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\PropsResolver; |
| 4 |
|
| 5 |
use Elementor\Modules\AtomicWidgets\DynamicTags\Dynamic_Prop_Type; |
| 6 |
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Array_Prop_Type; |
| 7 |
use Elementor\Modules\AtomicWidgets\PropTypes\Base\Object_Prop_Type; |
| 8 |
use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type; |
| 9 |
use Elementor\Modules\AtomicWidgets\PropTypes\Html_Prop_Type; |
| 10 |
use Elementor\Modules\AtomicWidgets\PropTypes\Union_Prop_Type; |
| 11 |
use Elementor\Plugin; |
| 12 |
use Exception; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; // Exit if accessed directly. |
| 16 |
} |
| 17 |
|
| 18 |
class Render_Props_Resolver extends Props_Resolver { |
| 19 |
/** |
| 20 |
* Each transformer can return a value that is also a transformable value, |
| 21 |
* which means that it can be transformed again by another transformer. |
| 22 |
* This constant defines the maximum depth of transformations to avoid infinite loops. |
| 23 |
*/ |
| 24 |
const TRANSFORM_DEPTH_LIMIT = 3; |
| 25 |
|
| 26 |
const CONTEXT_SETTINGS = 'settings'; |
| 27 |
const CONTEXT_STYLES = 'styles'; |
| 28 |
const CONTEXT_PLAIN = 'plain'; |
| 29 |
|
| 30 |
public static function for_styles(): self { |
| 31 |
return static::instance( self::CONTEXT_STYLES ); |
| 32 |
} |
| 33 |
|
| 34 |
public static function for_settings(): self { |
| 35 |
return static::instance( self::CONTEXT_SETTINGS ); |
| 36 |
} |
| 37 |
|
| 38 |
public static function for_plain(): self { |
| 39 |
return static::instance( self::CONTEXT_PLAIN ); |
| 40 |
} |
| 41 |
|
| 42 |
public function resolve_value( $value, Prop_Type $prop_type ) { |
| 43 |
$value = $this->get_validated_value( $prop_type, $value ); |
| 44 |
|
| 45 |
return $this->resolve_item( $value, null, $prop_type ); |
| 46 |
} |
| 47 |
|
| 48 |
public function resolve( array $schema, array $props ): array { |
| 49 |
$resolved = []; |
| 50 |
|
| 51 |
foreach ( $schema as $key => $prop_type ) { |
| 52 |
if ( ! ( $prop_type instanceof Prop_Type ) ) { |
| 53 |
continue; |
| 54 |
} |
| 55 |
|
| 56 |
$prop_value = $props[ $key ] ?? null; |
| 57 |
$actual_value = $this->get_validated_value( $prop_type, $prop_value ); |
| 58 |
|
| 59 |
$transformed = $this->resolve_item( |
| 60 |
$actual_value, |
| 61 |
$key, |
| 62 |
$prop_type |
| 63 |
); |
| 64 |
|
| 65 |
if ( Multi_Props::is( $transformed ) ) { |
| 66 |
$resolved = array_merge( $resolved, Multi_Props::get_value( $transformed ) ); |
| 67 |
|
| 68 |
continue; |
| 69 |
} |
| 70 |
|
| 71 |
$resolved[ $key ] = $transformed; |
| 72 |
} |
| 73 |
|
| 74 |
return $resolved; |
| 75 |
} |
| 76 |
|
| 77 |
protected function resolve_item( $value, $key, Prop_Type $prop_type, int $depth = 0 ) { |
| 78 |
if ( null === $value ) { |
| 79 |
return null; |
| 80 |
} |
| 81 |
|
| 82 |
if ( ! $this->is_transformable( $value ) ) { |
| 83 |
return $this->sanitize_html_prop_value( $value, $prop_type ); |
| 84 |
} |
| 85 |
|
| 86 |
if ( $depth >= self::TRANSFORM_DEPTH_LIMIT ) { |
| 87 |
return null; |
| 88 |
} |
| 89 |
|
| 90 |
if ( isset( $value['disabled'] ) && true === $value['disabled'] ) { |
| 91 |
return null; |
| 92 |
} |
| 93 |
|
| 94 |
$transformed = $this->transform( $value, $key, $prop_type ); |
| 95 |
|
| 96 |
return $this->resolve_item( $transformed, $key, $prop_type, $depth + 1 ); |
| 97 |
} |
| 98 |
|
| 99 |
private function get_validated_value( Prop_Type $prop_type, $prop_value ) { |
| 100 |
$default = $prop_type->get_default() ?? null; |
| 101 |
|
| 102 |
if ( null === $prop_value ) { |
| 103 |
return $default; |
| 104 |
} |
| 105 |
|
| 106 |
if ( ! Dynamic_Prop_Type::is_dynamic_prop_value( $prop_value ) ) { |
| 107 |
return $prop_value; |
| 108 |
} |
| 109 |
|
| 110 |
$tag_name = $prop_value['value']['name'] ?? null; |
| 111 |
$tag = Plugin::$instance->dynamic_tags->get_tag_info( $tag_name ); |
| 112 |
|
| 113 |
return ! $tag ? $default : $prop_value; |
| 114 |
} |
| 115 |
|
| 116 |
private function sanitize_html_prop_value( $value, Prop_Type $prop_type ) { |
| 117 |
if ( ! is_string( $value ) || ! $this->requires_html_sanitization( $prop_type ) ) { |
| 118 |
return $value; |
| 119 |
} |
| 120 |
|
| 121 |
return Html_Prop_Type::sanitize_allowed_html( $value ); |
| 122 |
} |
| 123 |
|
| 124 |
private function requires_html_sanitization( Prop_Type $prop_type ): bool { |
| 125 |
if ( $prop_type instanceof Html_Prop_Type ) { |
| 126 |
return true; |
| 127 |
} |
| 128 |
|
| 129 |
if ( $prop_type instanceof Union_Prop_Type ) { |
| 130 |
foreach ( $prop_type->get_prop_types() as $variant ) { |
| 131 |
if ( $this->requires_html_sanitization( $variant ) ) { |
| 132 |
return true; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
return false; |
| 138 |
} |
| 139 |
} |
| 140 |
|