PluginProbe
Elementor Website Builder – more than just a page builder / 3.28.0-dev3
Elementor Website Builder – more than just a page builder v3.28.0-dev3
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 / elements / has-atomic-base.php

has-atomic-base.php in Elementor Website Builder – more than just a page builder 3.28.0-dev3, at modules/atomic-widgets/elements/has-atomic-base.php

169 lines 4.2 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\Elements;
4
5 use Elementor\Element_Base;
6 use Elementor\Modules\AtomicWidgets\Base\Atomic_Control_Base;
7 use Elementor\Modules\AtomicWidgets\Controls\Section;
8 use Elementor\Modules\AtomicWidgets\PropsResolver\Props_Resolver;
9 use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
10 use Elementor\Modules\AtomicWidgets\Styles\Style_Schema;
11 use Elementor\Modules\AtomicWidgets\Parsers\Props_Parser;
12 use Elementor\Modules\AtomicWidgets\Parsers\Style_Parser;
13 use Elementor\Utils;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // Exit if accessed directly.
17 }
18
19 /**
20 * @mixin Element_Base
21 */
22 trait Has_Atomic_Base {
23 use Has_Base_Styles;
24
25 public function has_widget_inner_wrapper(): bool {
26 return false;
27 }
28
29 abstract public static function get_element_type(): string;
30
31 final public function get_name() {
32 return static::get_element_type();
33 }
34
35 private function get_valid_controls( array $schema, array $controls ): array {
36 $valid_controls = [];
37
38 foreach ( $controls as $control ) {
39 if ( $control instanceof Section ) {
40 $cloned_section = clone $control;
41
42 $cloned_section->set_items(
43 $this->get_valid_controls( $schema, $control->get_items() )
44 );
45
46 $valid_controls[] = $cloned_section;
47 continue;
48 }
49
50 if ( ! ( $control instanceof Atomic_Control_Base ) ) {
51 Utils::safe_throw( 'Control must be an instance of `Atomic_Control_Base`.' );
52 continue;
53 }
54
55 $prop_name = $control->get_bind();
56
57 if ( ! $prop_name ) {
58 Utils::safe_throw( 'Control is missing a bound prop from the schema.' );
59 continue;
60 }
61
62 if ( ! array_key_exists( $prop_name, $schema ) ) {
63 Utils::safe_throw( "Prop `{$prop_name}` is not defined in the schema of `{$this->get_name()}`." );
64 continue;
65 }
66
67 $valid_controls[] = $control;
68 }
69
70 return $valid_controls;
71 }
72
73 private static function validate_schema( array $schema ) {
74 $widget_name = static::class;
75
76 foreach ( $schema as $key => $prop ) {
77 if ( ! ( $prop instanceof Prop_Type ) ) {
78 Utils::safe_throw( "Prop `$key` must be an instance of `Prop_Type` in `{$widget_name}`." );
79 }
80 }
81 }
82
83 private function parse_atomic_styles( array $styles ): array {
84 $style_parser = Style_Parser::make( Style_Schema::get() );
85
86 foreach ( $styles as $style_id => $style ) {
87 [ $is_valid, $sanitized_style, $errors ] = $style_parser->parse( $style );
88
89 if ( ! $is_valid ) {
90 throw new \Exception( esc_html( 'Styles validation failed. Invalid keys: ' . join( ', ', $errors ) ) );
91 }
92
93 $styles[ $style_id ] = $sanitized_style;
94 }
95
96 return $styles;
97 }
98
99 private function parse_atomic_settings( array $settings ): array {
100 $schema = static::get_props_schema();
101 $props_parser = Props_Parser::make( $schema );
102
103 [ $is_valid, $parsed, $errors ] = $props_parser->parse( $settings );
104
105 if ( ! $is_valid ) {
106 throw new \Exception( esc_html( 'Settings validation failed. Invalid keys: ' . join( ', ', $errors ) ) );
107 }
108
109 return $parsed;
110 }
111
112 public function get_atomic_controls() {
113 $controls = $this->define_atomic_controls();
114 $schema = static::get_props_schema();
115
116 // Validate the schema only in the Editor.
117 static::validate_schema( $schema );
118
119 return $this->get_valid_controls( $schema, $controls );
120 }
121
122 final public function get_controls( $control_id = null ) {
123 if ( ! empty( $control_id ) ) {
124 return null;
125 }
126
127 return [];
128 }
129
130 final public function get_data_for_save() {
131 $data = parent::get_data_for_save();
132
133 $data['version'] = $this->version;
134 $data['settings'] = $this->parse_atomic_settings( $data['settings'] );
135 $data['styles'] = $this->parse_atomic_styles( $data['styles'] );
136
137 return $data;
138 }
139
140 final public function get_raw_data( $with_html_content = false ) {
141 $raw_data = parent::get_raw_data( $with_html_content );
142
143 $raw_data['styles'] = $this->styles;
144
145 return $raw_data;
146 }
147
148 final public function get_stack( $with_common_controls = true ) {
149 return [
150 'controls' => [],
151 'tabs' => [],
152 ];
153 }
154
155 public function get_atomic_settings(): array {
156 $schema = static::get_props_schema();
157 $props = $this->get_settings();
158
159 return Props_Resolver::for_settings()->resolve( $schema, $props );
160 }
161
162 public static function get_props_schema(): array {
163 return apply_filters(
164 'elementor/atomic-widgets/props-schema',
165 static::define_props_schema()
166 );
167 }
168 }
169