PluginProbe
Elementor Website Builder – more than just a page builder / 3.32.2
Elementor Website Builder – more than just a page builder v3.32.2
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 / parsers / props-parser.php

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

91 lines 2.0 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\Parsers;
4
5 use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
6 use Elementor\Core\Utils\Api\Parse_Result;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 class Props_Parser {
13
14 private array $schema;
15
16 public function __construct( array $schema ) {
17 $this->schema = $schema;
18 }
19
20 public static function make( array $schema ): self {
21 return new static( $schema );
22 }
23
24 /**
25 * @param array $props
26 * The key of each item represents the prop name (should match the schema),
27 * and the value is the prop value to validate
28 */
29 public function validate( array $props ): Parse_Result {
30 $result = Parse_Result::make();
31
32 $validated = [];
33
34 foreach ( $this->schema as $key => $prop_type ) {
35 if ( ! ( $prop_type instanceof Prop_Type ) ) {
36 continue;
37 }
38
39 $value = $props[ $key ] ?? null;
40
41 $is_valid = $prop_type->validate( $value ?? $prop_type->get_default() );
42
43 if ( ! $is_valid ) {
44 $result->errors()->add( $key, 'invalid_value' );
45
46 continue;
47 }
48
49 if ( ! is_null( $value ) ) {
50 $validated[ $key ] = $value;
51 }
52 }
53
54 return $result->wrap( $validated );
55 }
56
57 /**
58 * @param array $props
59 * The key of each item represents the prop name (should match the schema),
60 * and the value is the prop value to sanitize
61 */
62 public function sanitize( array $props ): Parse_Result {
63 $sanitized = [];
64
65 foreach ( $this->schema as $key => $prop_type ) {
66 if ( ! isset( $props[ $key ] ) ) {
67 continue;
68 }
69
70 $sanitized[ $key ] = $prop_type->sanitize( $props[ $key ] );
71 }
72
73 return Parse_Result::make()->wrap( $sanitized );
74 }
75
76 /**
77 * @param array $props
78 * The key of each item represents the prop name (should match the schema),
79 * and the value is the prop value to parse
80 */
81 public function parse( array $props ): Parse_Result {
82 $validate_result = $this->validate( $props );
83
84 $sanitize_result = $this->sanitize( $validate_result->unwrap() );
85
86 $sanitize_result->errors()->merge( $validate_result->errors() );
87
88 return $sanitize_result;
89 }
90 }
91