PluginProbe
Elementor Website Builder – more than just a page builder / 4.0.3
Elementor Website Builder – more than just a page builder v4.0.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 / core / base / elements-iteration-actions / assets.php

assets.php in Elementor Website Builder – more than just a page builder 4.0.3, at core/base/elements-iteration-actions/assets.php

205 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core\Base\Elements_Iteration_Actions;
3
4 use Elementor\Conditions;
5 use Elementor\Element_Base;
6 use Elementor\Plugin;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 class Assets extends Base {
13 const ASSETS_META_KEY = '_elementor_page_assets';
14 /**
15 * Default value must be empty.
16 *
17 * @var array
18 */
19 private $page_assets;
20
21 /**
22 * Default value must be empty.
23 *
24 * @var array
25 */
26 private $saved_page_assets;
27
28 public function element_action( Element_Base $element_data ) {
29 $settings = $element_data->get_active_settings();
30 $controls = $element_data->get_controls();
31
32 $element_assets = $this->get_assets( $settings, $controls );
33
34 $element_assets_depend = [
35 'styles' => $element_data->get_style_depends(),
36 'scripts' => array_merge( $element_data->get_script_depends(), $element_data->get_global_scripts() ),
37 ];
38
39 if ( $element_assets_depend ) {
40 foreach ( $element_assets_depend as $assets_type => $assets ) {
41 if ( empty( $assets ) ) {
42 continue;
43 }
44
45 if ( ! isset( $element_assets[ $assets_type ] ) ) {
46 $element_assets[ $assets_type ] = [];
47 }
48
49 foreach ( $assets as $asset_name ) {
50 if ( ! in_array( $asset_name, $element_assets[ $assets_type ], true ) ) {
51 $element_assets[ $assets_type ][] = $asset_name;
52 }
53 }
54 }
55 }
56
57 if ( $element_assets ) {
58 $this->update_page_assets( $element_assets );
59 }
60 }
61
62 public function is_action_needed() {
63 // No need to evaluate in preview mode, will be made in the saving process.
64 if ( Plugin::$instance->preview->is_preview_mode() ) {
65 return false;
66 }
67
68 $page_assets = $this->get_saved_page_assets();
69
70 // When $page_assets is array it means that the assets registration has already been made at least once.
71 if ( is_array( $page_assets ) ) {
72 return false;
73 }
74
75 return true;
76 }
77
78 public function after_elements_iteration() {
79 // In case that the page assets value is empty, it should still be saved as an empty array as an indication that at lease one iteration has occurred.
80 if ( ! is_array( $this->page_assets ) ) {
81 $this->page_assets = [];
82 }
83
84 $this->get_document_assets();
85
86 // Saving the page assets data.
87 $this->document->update_meta( self::ASSETS_META_KEY, $this->page_assets );
88
89 if ( 'render' === $this->mode && $this->page_assets ) {
90 Plugin::$instance->assets_loader->enable_assets( $this->page_assets );
91 }
92 }
93
94 private function get_saved_page_assets( $force_meta_fetch = false ) {
95 if ( ! is_array( $this->saved_page_assets ) || $force_meta_fetch ) {
96 $this->saved_page_assets = $this->document->get_meta( self::ASSETS_META_KEY );
97 }
98
99 return $this->saved_page_assets;
100 }
101
102 private function update_page_assets( $new_assets ) {
103 if ( ! is_array( $this->page_assets ) ) {
104 $this->page_assets = [];
105 }
106
107 foreach ( $new_assets as $assets_type => $assets_type_data ) {
108 if ( ! isset( $this->page_assets[ $assets_type ] ) ) {
109 $this->page_assets[ $assets_type ] = [];
110 }
111
112 foreach ( $assets_type_data as $asset_name ) {
113 if ( ! in_array( $asset_name, $this->page_assets[ $assets_type ], true ) ) {
114 $this->page_assets[ $assets_type ][] = $asset_name;
115 }
116 }
117 }
118 }
119
120 private function get_assets( $settings, $controls ) {
121 $assets = [];
122
123 foreach ( $settings as $setting_key => $setting ) {
124 if ( ! isset( $controls[ $setting_key ] ) ) {
125 continue;
126 }
127
128 $control = $controls[ $setting_key ];
129
130 // Enabling assets loading from the registered control fields.
131 if ( ! empty( $control['assets'] ) ) {
132 foreach ( $control['assets'] as $assets_type => $dependencies ) {
133 foreach ( $dependencies as $dependency ) {
134 if ( ! empty( $dependency['conditions'] ) ) {
135 $is_condition_fulfilled = Conditions::check( $dependency['conditions'], $settings );
136
137 if ( ! $is_condition_fulfilled ) {
138 continue;
139 }
140 }
141
142 if ( ! isset( $assets[ $assets_type ] ) ) {
143 $assets[ $assets_type ] = [];
144 }
145
146 $assets[ $assets_type ][] = $dependency['name'];
147 }
148 }
149 }
150
151 // Enabling assets loading from the control object.
152 $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
153
154 $control_conditional_assets = $control_obj::get_assets( $setting );
155
156 if ( $control_conditional_assets ) {
157 foreach ( $control_conditional_assets as $assets_type => $dependencies ) {
158 foreach ( $dependencies as $dependency ) {
159 if ( ! isset( $assets[ $assets_type ] ) ) {
160 $assets[ $assets_type ] = [];
161 }
162
163 $assets[ $assets_type ][] = $dependency;
164 }
165 }
166 }
167 }
168
169 return $assets;
170 }
171
172 private function get_document_assets() {
173 $document_id = $this->document->get_post()->ID;
174
175 // Getting the document instance in order to get the most updated settings.
176 $updated_document = Plugin::$instance->documents->get( $document_id, false );
177
178 $document_settings = $updated_document->get_settings();
179
180 $document_controls = $this->document->get_controls();
181
182 $document_assets = $this->get_assets( $document_settings, $document_controls );
183
184 if ( $document_assets ) {
185 $this->update_page_assets( $document_assets );
186 }
187 }
188
189 public function __construct( $document ) {
190 parent::__construct( $document );
191
192 // No need to enable assets in preview mode, all assets will be loaded by default by the assets loader.
193 if ( Plugin::$instance->preview->is_preview_mode() ) {
194 return;
195 }
196
197 $page_assets = $this->get_saved_page_assets();
198
199 // If $page_assets is not empty then enabling the assets for loading.
200 if ( $page_assets ) {
201 Plugin::$instance->assets_loader->enable_assets( $page_assets );
202 }
203 }
204 }
205