PluginProbe
Elementor Website Builder – more than just a page builder / 3.10.0-dev1
Elementor Website Builder – more than just a page builder v3.10.0-dev1
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 3.10.0-dev1, at core/base/elements-iteration-actions/assets.php

181 lines 5.0 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 private $page_assets;
17
18 // Default value must be empty.
19 private $saved_page_assets;
20
21 public function element_action( Element_Base $element_data ) {
22 $settings = $element_data->get_active_settings();
23 $controls = $element_data->get_controls();
24
25 $element_assets = $this->get_assets( $settings, $controls );
26
27 if ( $element_assets ) {
28 $this->update_page_assets( $element_assets );
29 }
30 }
31
32 public function is_action_needed() {
33 // No need to evaluate in preview mode, will be made in the saving process.
34 if ( ! $this->is_active_page_assets_mode() ) {
35 return false;
36 }
37
38 $page_assets = $this->get_saved_page_assets();
39
40 // When $page_assets is array it means that the assets registration has already been made at least once.
41 if ( is_array( $page_assets ) ) {
42 return false;
43 }
44
45 return true;
46 }
47
48 public function after_elements_iteration() {
49 // 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.
50 if ( ! is_array( $this->page_assets ) ) {
51 $this->page_assets = [];
52 }
53
54 $this->get_document_assets();
55
56 // Saving the page assets data.
57 $this->document->update_meta( self::ASSETS_META_KEY, $this->page_assets );
58
59 if ( 'render' === $this->mode && $this->page_assets ) {
60 Plugin::$instance->assets_loader->enable_assets( $this->page_assets );
61 }
62 }
63
64 private function get_saved_page_assets( $force_meta_fetch = false ) {
65 if ( ! is_array( $this->saved_page_assets ) || $force_meta_fetch ) {
66 $this->saved_page_assets = $this->document->get_meta( self::ASSETS_META_KEY );
67 }
68
69 return $this->saved_page_assets;
70 }
71
72 private function update_page_assets( $new_assets ) {
73 if ( ! is_array( $this->page_assets ) ) {
74 $this->page_assets = [];
75 }
76
77 foreach ( $new_assets as $assets_type => $assets_type_data ) {
78 if ( ! isset( $this->page_assets[ $assets_type ] ) ) {
79 $this->page_assets[ $assets_type ] = [];
80 }
81
82 foreach ( $new_assets[ $assets_type ] as $asset_name ) {
83 if ( ! isset( $this->page_assets[ $assets_type ][ $asset_name ] ) ) {
84 $this->page_assets[ $assets_type ][] = $asset_name;
85 }
86 }
87 }
88 }
89
90 private function get_assets( $settings, $controls ) {
91 $assets = [];
92
93 foreach ( $settings as $setting_key => $setting ) {
94 if ( ! isset( $controls[ $setting_key ] ) ) {
95 continue;
96 }
97
98 $control = $controls[ $setting_key ];
99
100 // Enabling assets loading from the registered control fields.
101 if ( ! empty( $control['assets'] ) ) {
102 foreach ( $control['assets'] as $assets_type => $dependencies ) {
103 foreach ( $dependencies as $dependency ) {
104 if ( ! empty( $dependency['conditions'] ) ) {
105 $is_condition_fulfilled = Conditions::check( $dependency['conditions'], $settings );
106
107 if ( ! $is_condition_fulfilled ) {
108 continue;
109 }
110 }
111
112 if ( ! isset( $assets[ $assets_type ] ) ) {
113 $assets[ $assets_type ] = [];
114 }
115
116 $assets[ $assets_type ][] = $dependency['name'];
117 }
118 }
119 }
120
121 // Enabling assets loading from the control object.
122 $control_obj = Plugin::$instance->controls_manager->get_control( $control['type'] );
123
124 $control_conditional_assets = $control_obj::get_assets( $setting );
125
126 if ( $control_conditional_assets ) {
127 foreach ( $control_conditional_assets as $assets_type => $dependencies ) {
128 foreach ( $dependencies as $dependency ) {
129 if ( ! isset( $assets[ $assets_type ] ) ) {
130 $assets[ $assets_type ] = [];
131 }
132
133 $assets[ $assets_type ][] = $dependency;
134 }
135 }
136 }
137 }
138
139 return $assets;
140 }
141
142 private function is_active_page_assets_mode() {
143 $is_optimized_mode = Plugin::$instance->experiments->is_feature_active( 'e_optimized_assets_loading' );
144
145 return ! Plugin::$instance->preview->is_preview_mode() && $is_optimized_mode;
146 }
147
148 private function get_document_assets() {
149 $document_id = $this->document->get_post()->ID;
150
151 // Getting the document instance in order to get the most updated settings.
152 $updated_document = Plugin::$instance->documents->get( $document_id, false );
153
154 $document_settings = $updated_document->get_settings();
155
156 $document_controls = $this->document->get_controls();
157
158 $document_assets = $this->get_assets( $document_settings, $document_controls );
159
160 if ( $document_assets ) {
161 $this->update_page_assets( $document_assets );
162 }
163 }
164
165 public function __construct( $document ) {
166 parent::__construct( $document );
167
168 // No need to enable assets in preview mode, all assets will be loaded by default by the assets loader.
169 if ( ! $this->is_active_page_assets_mode() ) {
170 return;
171 }
172
173 $page_assets = $this->get_saved_page_assets();
174
175 // If $page_assets is not empty then enabling the assets for loading.
176 if ( $page_assets ) {
177 Plugin::$instance->assets_loader->enable_assets( $page_assets );
178 }
179 }
180 }
181