PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.2
Elementor Website Builder – more than just a page builder v4.2.2
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 4.2.2, at modules/atomic-widgets/elements/base/atomic-element-base.php

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