PluginProbe
Elementor Website Builder – more than just a page builder / 3.35.9
Elementor Website Builder – more than just a page builder v3.35.9
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 / has-template.php

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

162 lines 4.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\Modules\AtomicWidgets\Elements\TemplateRenderer\Template_Renderer;
6 use Elementor\Utils;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 /**
13 * @mixin Has_Atomic_Base
14 */
15 trait Has_Template {
16
17 public function get_initial_config() {
18 $config = parent::get_initial_config();
19
20 $config['twig_main_template'] = $this->get_main_template();
21 $config['twig_templates'] = $this->get_templates_contents();
22 return $config;
23 }
24
25 protected function render() {
26 try {
27 $renderer = Template_Renderer::instance();
28
29 foreach ( $this->get_templates() as $name => $path ) {
30 if ( $renderer->is_registered( $name ) ) {
31 continue;
32 }
33
34 $renderer->register( $name, $path );
35 }
36
37 $context = [
38 'id' => $this->get_id(),
39 'type' => $this->get_name(),
40 'settings' => $this->get_atomic_settings(),
41 'base_styles' => $this->get_base_styles_dictionary(),
42 'interactions' => $this->get_interactions_ids(),
43 ];
44
45 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
46 echo $renderer->render( $this->get_main_template(), $context );
47 } catch ( \Exception $e ) {
48 if ( Utils::is_elementor_debug() ) {
49 throw $e;
50 }
51 }
52 }
53
54 public function get_interactions_ids() {
55 $animation_ids = [];
56
57 $list_of_interactions = ( is_array( $this->interactions ) && isset( $this->interactions['items'] ) )
58 ? $this->interactions['items']
59 : [];
60
61 foreach ( $list_of_interactions as $interaction ) {
62
63 if ( isset( $interaction['$$type'] ) && 'interaction-item' === $interaction['$$type'] ) {
64 $animation_id = $this->extract_animation_id_from_prop_type( $interaction );
65 if ( $animation_id ) {
66 $animation_ids[] = $animation_id;
67 }
68 } elseif ( isset( $interaction['animation']['animation_id'] ) ) {
69 $animation_ids[] = $interaction['animation']['animation_id'];
70 }
71 }
72
73 return $animation_ids;
74 }
75
76 private function extract_animation_id_from_prop_type( $item ) {
77 if ( ! isset( $item['value'] ) || ! is_array( $item['value'] ) ) {
78 return null;
79 }
80
81 $item_value = $item['value'];
82
83 $trigger = $this->extract_prop_value_simple( $item_value, 'trigger' );
84 $animation = $this->extract_prop_value_simple( $item_value, 'animation' );
85
86 if ( ! is_array( $animation ) ) {
87 return null;
88 }
89
90 $effect = $this->extract_prop_value_simple( $animation, 'effect' );
91 $type = $this->extract_prop_value_simple( $animation, 'type' );
92 $direction = $this->extract_prop_value_simple( $animation, 'direction' );
93 $timing_config = $this->extract_prop_value_simple( $animation, 'timing_config' );
94 $config = $this->extract_prop_value_simple( $animation, 'config' );
95
96 $duration = 300;
97 $delay = 0;
98 $replay = 0;
99 $relative_to = 'viewport';
100 $offset_top = 15;
101 $offset_bottom = 85;
102
103 if ( is_array( $timing_config ) ) {
104 $duration = $this->extract_prop_value_simple( $timing_config, 'duration', 300 );
105 $delay = $this->extract_prop_value_simple( $timing_config, 'delay', 0 );
106 }
107
108 if ( is_array( $config ) ) {
109 $relative_to = $this->extract_prop_value_simple( $config, 'relative_to', 'viewport' );
110 $offset_top = $this->extract_prop_value_simple( $config, 'offset_top', 15 );
111 $offset_bottom = $this->extract_prop_value_simple( $config, 'offset_bottom', 85 );
112 $replay = $this->extract_prop_value_simple( $config, 'replay', 0 );
113 if ( empty( $replay ) && 0 !== $replay && '0' !== $replay ) {
114 $replay = 0;
115 }
116 } else {
117 $replay = 0;
118 }
119 return implode( '-', [ $trigger, $effect, $type, $direction, $duration, $delay, $replay, $relative_to, $offset_top, $offset_bottom ] );
120 }
121
122 private function extract_prop_value_simple( $data, $key, $default = '' ) {
123 if ( ! is_array( $data ) || ! isset( $data[ $key ] ) ) {
124 return $default;
125 }
126
127 $value = $data[ $key ];
128
129 if ( is_array( $value ) && isset( $value['$$type'] ) && isset( $value['value'] ) ) {
130 return $value['value'];
131 }
132
133 return null !== $value ? $value : $default;
134 }
135
136 protected function get_templates_contents() {
137 return array_map(
138 fn ( $path ) => Utils::file_get_contents( $path ),
139 $this->get_templates()
140 );
141 }
142
143 protected function get_main_template() {
144 $templates = $this->get_templates();
145
146 if ( count( $templates ) > 1 ) {
147 Utils::safe_throw( 'When having more than one template, you should override this method to return the main template.' );
148
149 return null;
150 }
151
152 foreach ( $templates as $key => $path ) {
153 // Returns first key in the array.
154 return $key;
155 }
156
157 return null;
158 }
159
160 abstract protected function get_templates(): array;
161 }
162