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

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