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

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

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