PluginProbe
Elementor Website Builder – more than just a page builder / 3.30.3
Elementor Website Builder – more than just a page builder v3.30.3
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.30.3, at modules/atomic-widgets/elements/has-atomic-base.php

186 lines 4.7 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\Render_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 $result = $style_parser->parse( $style );
88
89 if ( ! $result->is_valid() ) {
90 throw new \Exception( esc_html( "Styles validation failed for style `$style_id`. " . $result->errors()->to_string() ) );
91 }
92
93 $styles[ $style_id ] = $result->unwrap();
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 $result = $props_parser->parse( $settings );
104
105 if ( ! $result->is_valid() ) {
106 throw new \Exception( esc_html( 'Settings validation failed. ' . $result->errors()->to_string() ) );
107 }
108
109 return $result->unwrap();
110 }
111
112 public function get_atomic_controls() {
113 $controls = apply_filters(
114 'elementor/atomic-widgets/controls',
115 $this->define_atomic_controls(),
116 $this
117 );
118
119 $schema = static::get_props_schema();
120
121 // Validate the schema only in the Editor.
122 static::validate_schema( $schema );
123
124 return $this->get_valid_controls( $schema, $controls );
125 }
126
127 final public function get_controls( $control_id = null ) {
128 if ( ! empty( $control_id ) ) {
129 return null;
130 }
131
132 return [];
133 }
134
135 final public function get_data_for_save() {
136 $data = parent::get_data_for_save();
137
138 $data['version'] = $this->version;
139 $data['settings'] = $this->parse_atomic_settings( $data['settings'] );
140 $data['styles'] = $this->parse_atomic_styles( $data['styles'] );
141 $data['editor_settings'] = $this->parse_editor_settings( $data['editor_settings'] );
142
143 return $data;
144 }
145
146 final public function get_raw_data( $with_html_content = false ) {
147 $raw_data = parent::get_raw_data( $with_html_content );
148
149 $raw_data['styles'] = $this->styles;
150 $raw_data['editor_settings'] = $this->editor_settings;
151
152 return $raw_data;
153 }
154
155 final public function get_stack( $with_common_controls = true ) {
156 return [
157 'controls' => [],
158 'tabs' => [],
159 ];
160 }
161
162 public function get_atomic_settings(): array {
163 $schema = static::get_props_schema();
164 $props = $this->get_settings();
165
166 return Render_Props_Resolver::for_settings()->resolve( $schema, $props );
167 }
168
169 private function parse_editor_settings( array $data ): array {
170 $editor_data = [];
171
172 if ( isset( $data['title'] ) && is_string( $data['title'] ) ) {
173 $editor_data['title'] = sanitize_text_field( $data['title'] );
174 }
175
176 return $editor_data;
177 }
178
179 public static function get_props_schema(): array {
180 return apply_filters(
181 'elementor/atomic-widgets/props-schema',
182 static::define_props_schema()
183 );
184 }
185 }
186