PluginProbe
Elementor Website Builder – more than just a page builder / 3.34.3
Elementor Website Builder – more than just a page builder v3.34.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 / atomic-element-base.php

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

269 lines 6.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\PropDependencies\Manager as Dependency_Manager;
7 use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type;
8 use Elementor\Modules\AtomicWidgets\Render_Context;
9 use Elementor\Plugin;
10 use Elementor\Utils;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit; // Exit if accessed directly.
14 }
15
16 abstract class Atomic_Element_Base extends Element_Base {
17 use Has_Atomic_Base;
18
19 protected $version = '0.0';
20 protected $styles = [];
21 protected $interactions = [];
22 protected $editor_settings = [];
23
24
25 public function __construct( $data = [], $args = null ) {
26 parent::__construct( $data, $args );
27
28 $this->version = $data['version'] ?? '0.0';
29 $this->styles = $data['styles'] ?? [];
30 $this->interactions = $this->parse_atomic_interactions( $data['interactions'] ?? [] );
31 $this->editor_settings = $data['editor_settings'] ?? [];
32 }
33
34 private function parse_atomic_interactions( $interactions ) {
35 if ( empty( $interactions ) ) {
36 return [];
37 }
38
39 if ( is_string( $interactions ) ) {
40 $decoded = json_decode( $interactions, true );
41 if ( json_last_error() === JSON_ERROR_NONE && is_array( $decoded ) ) {
42 $interactions = $decoded;
43 }
44 }
45
46 if ( ! is_array( $interactions ) ) {
47 return [];
48 }
49
50 if ( isset( $interactions['items'] ) && is_array( $interactions['items'] ) ) {
51 return $this->convert_prop_type_interactions_to_legacy_for_runtime( $interactions );
52 }
53
54 return $interactions;
55 }
56
57 private function convert_prop_type_interactions_to_legacy_for_runtime( $interactions ) {
58 $legacy_items = [];
59
60 foreach ( $interactions['items'] as $item ) {
61 if ( isset( $item['$$type'] ) && 'interaction-item' === $item['$$type'] ) {
62 $legacy_item = $this->extract_legacy_interaction_from_prop_type( $item );
63 if ( $legacy_item ) {
64 $legacy_items[] = $legacy_item;
65 }
66 } else {
67 $legacy_items[] = $item;
68 }
69 }
70
71 return [
72 'version' => $interactions['version'] ?? 1,
73 'items' => $legacy_items,
74 ];
75 }
76
77 abstract protected function define_atomic_controls(): array;
78
79 protected function define_atomic_style_states(): array {
80 return [];
81 }
82
83 public function get_global_scripts() {
84 return [];
85 }
86
87 final public function get_initial_config() {
88 $config = parent::get_initial_config();
89 $props_schema = static::get_props_schema();
90
91 $config['atomic_controls'] = $this->get_atomic_controls();
92 $config['atomic_props_schema'] = $props_schema;
93 $config['atomic_style_states'] = $this->define_atomic_style_states();
94 $config['dependencies_per_target_mapping'] = Dependency_Manager::get_source_to_dependents( $props_schema );
95 $config['base_styles'] = $this->get_base_styles();
96 $config['version'] = $this->version;
97 $config['show_in_panel'] = $this->should_show_in_panel();
98 $config['categories'] = [ 'v4-elements' ];
99 $config['hide_on_search'] = false;
100 $config['controls'] = [];
101 $config['keywords'] = $this->get_keywords();
102 $config['default_children'] = $this->define_default_children();
103 $config['initial_attributes'] = $this->define_initial_attributes();
104 $config['include_in_widgets_config'] = true;
105 $config['default_html_tag'] = $this->define_default_html_tag();
106
107 return $config;
108 }
109
110 protected function should_show_in_panel() {
111 return true;
112 }
113
114 protected function define_default_children() {
115 return [];
116 }
117
118 protected function define_default_html_tag() {
119 return 'div';
120 }
121
122 protected function define_initial_attributes() {
123 return [];
124 }
125
126 protected function add_render_attributes() {
127 parent::add_render_attributes();
128
129 $interaction_ids = $this->get_interactions_ids();
130
131 if ( ! empty( $interaction_ids ) ) {
132 $this->add_render_attribute( '_wrapper', 'data-interaction-id', $this->get_id() );
133 $this->add_render_attribute( '_wrapper', 'data-interactions', json_encode( $interaction_ids ) );
134 }
135 }
136
137 protected function define_render_context(): array {
138 return [];
139 }
140
141 /**
142 * Get Element keywords.
143 *
144 * Retrieve the element keywords.
145 *
146 * @since 3.29
147 * @access public
148 *
149 * @return array Element keywords.
150 */
151 public function get_keywords() {
152 return [];
153 }
154
155 /**
156 * @return array<string, Prop_Type>
157 */
158 abstract protected static function define_props_schema(): array;
159
160 /**
161 * Get the HTML tag for rendering.
162 *
163 * @return string
164 */
165 protected function get_html_tag(): string {
166 $settings = $this->get_atomic_settings();
167 $default_html_tag = $this->define_default_html_tag();
168
169 return ! empty( $settings['link']['href'] ) ? 'a' : ( $settings['tag'] ?? $default_html_tag );
170 }
171
172 /**
173 * Print safe HTML tag for the element based on the element settings.
174 *
175 * @return void
176 */
177 protected function print_html_tag() {
178 $html_tag = $this->get_html_tag();
179 Utils::print_validated_html_tag( $html_tag );
180 }
181
182 /**
183 * Print custom attributes if they exist.
184 *
185 * @return void
186 */
187 protected function print_custom_attributes() {
188 $settings = $this->get_atomic_settings();
189 $attributes = $settings['attributes'] ?? '';
190 if ( ! empty( $attributes ) && is_string( $attributes ) ) {
191 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
192 echo ' ' . $attributes;
193 }
194 }
195
196 /**
197 * Get default child type for container elements.
198 *
199 * @param array $element_data
200 * @return mixed
201 */
202 protected function _get_default_child_type( array $element_data ) {
203 $el_types = array_keys( Plugin::$instance->elements_manager->get_element_types() );
204
205 if ( in_array( $element_data['elType'], $el_types, true ) ) {
206 return Plugin::$instance->elements_manager->get_element_types( $element_data['elType'] );
207 }
208
209 if ( ! isset( $element_data['widgetType'] ) ) {
210 return null;
211 }
212
213 return Plugin::$instance->widgets_manager->get_widget_types( $element_data['widgetType'] );
214 }
215
216 public function print_content() {
217 $element_context = $this->define_render_context();
218
219 $has_context = ! empty( $element_context );
220
221 if ( ! $has_context ) {
222 return parent::print_content();
223 }
224
225 Render_Context::push( static::class, $element_context );
226
227 parent::print_content();
228
229 Render_Context::pop( static::class );
230 }
231
232 /**
233 * Default before render for container elements.
234 *
235 * @return void
236 */
237 public function before_render() {
238 ?>
239 <<?php $this->print_html_tag(); ?> <?php $this->print_render_attribute_string( '_wrapper' );
240 $this->print_custom_attributes(); ?>>
241 <?php
242 }
243
244 /**
245 * Default after render for container elements.
246 *
247 * @return void
248 */
249 public function after_render() {
250 ?>
251 </<?php $this->print_html_tag(); ?>>
252 <?php
253 }
254
255 /**
256 * Default content template - can be overridden by elements that need custom templates.
257 *
258 * @return void
259 */
260 protected function content_template() {
261 ?>
262 <?php
263 }
264
265 public static function generate() {
266 return Element_Builder::make( static::get_type() );
267 }
268 }
269