PluginProbe
Elementor Website Builder – more than just a page builder / 3.5.1
Elementor Website Builder – more than just a page builder v3.5.1
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 / page-assets / data-managers / base.php

base.php in Elementor Website Builder – more than just a page builder 3.5.1, at core/page-assets/data-managers/base.php

344 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core\Page_Assets\Data_Managers;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit; // Exit if accessed directly.
6 }
7
8 /**
9 * Elementor Assets Data.
10 *
11 * @since 3.3.0
12 */
13 abstract class Base {
14 const ASSETS_DATA_KEY = '_elementor_assets_data';
15
16 /**
17 * @var array
18 */
19 protected $assets_data;
20
21 /**
22 * @var string
23 */
24 protected $content_type;
25
26 /**
27 * @var string
28 */
29 protected $assets_category;
30
31 /**
32 * @var array
33 */
34 private $assets_config;
35
36 /**
37 * @var array
38 */
39 private $files_data;
40
41 /**
42 * Get Asset Content.
43 *
44 * Responsible for extracting the asset data from a certain file.
45 * Will be triggered automatically when the asset data does not exist, or when the asset version was changed.
46 *
47 * @since 3.3.0
48 * @access public
49 *
50 * @return string
51 */
52 abstract protected function get_asset_content();
53
54 /**
55 * Get Asset Key.
56 *
57 * The asset data will be saved in the DB under this key.
58 *
59 * @since 3.3.0
60 * @access protected
61 *
62 * @return string
63 */
64 protected function get_key() {
65 return $this->assets_config['key'];
66 }
67
68 /**
69 * Get Relative Version.
70 *
71 * The asset data will be re-evaluated according the version number.
72 *
73 * @since 3.3.0
74 * @access protected
75 *
76 * @return string
77 */
78 protected function get_version() {
79 return $this->assets_config['version'];
80 }
81
82 /**
83 * Get Asset Path.
84 *
85 * The asset data will be extracted from the file path.
86 *
87 * @since 3.3.0
88 * @access protected
89 *
90 * @return string
91 */
92 protected function get_file_path() {
93 return $this->assets_config['file_path'];
94 }
95
96 /**
97 * Get Config Data.
98 *
99 * Holds a unique data relevant for the specific assets category type.
100 *
101 * @since 3.3.0
102 * @access protected
103 *
104 * @return string|array
105 */
106 protected function get_config_data( $key = '' ) {
107 if ( isset( $this->assets_config['data'] ) ) {
108 if ( $key ) {
109 if ( isset( $this->assets_config['data'][ $key ] ) ) {
110 return $this->assets_config['data'][ $key ];
111 }
112
113 return '';
114 }
115
116 return $this->assets_config['data'];
117 }
118
119 return [];
120 }
121
122 /**
123 * Set Asset Data.
124 *
125 * Responsible for setting the current asset data.
126 *
127 * @since 3.3.0
128 * @access protected
129 *
130 * @return void
131 */
132 protected function set_asset_data( $asset_key ) {
133 if ( ! isset( $this->assets_data[ $asset_key ] ) ) {
134 $this->assets_data[ $asset_key ] = [];
135 }
136
137 $this->assets_data[ $asset_key ]['content'] = $this->get_asset_content();
138 $this->assets_data[ $asset_key ]['version'] = $this->get_version();
139
140 $this->save_asset_data( $asset_key );
141 }
142
143 /**
144 * Save Asset Data.
145 *
146 * Responsible for saving the asset data in the DB.
147 *
148 * @since 3.3.0
149 * @access protected
150 *
151 * @param string $asset_key
152 *
153 * @return void
154 */
155 protected function save_asset_data( $asset_key ) {
156 $assets_data = $this->get_saved_assets_data();
157
158 $content_type = $this->content_type;
159 $assets_category = $this->assets_category;
160
161 $assets_data[ $content_type ][ $assets_category ][ $asset_key ] = $this->assets_data[ $asset_key ];
162
163 update_option( self::ASSETS_DATA_KEY, $assets_data );
164 }
165
166 /**
167 * Is Asset Version Changed.
168 *
169 * Responsible for comparing the saved asset data version to the current relative version.
170 *
171 * @since 3.3.0
172 * @access protected
173 *
174 * @param string $asset_key
175 *
176 * @return boolean
177 */
178 protected function is_asset_version_changed( $version ) {
179 return $this->get_version() !== $version;
180 }
181
182 /**
183 * Get File Data.
184 *
185 * Getting a file content or size.
186 *
187 * @since 3.3.0
188 * @access protected
189 *
190 * @param string $data_type (content|size)
191 * @param string $file_key - In case that the same file data is needed for multiple assets (like a JSON file), the file data key should be the same for all shared assets to make sure that the file is being read only once.
192 *
193 * @return string|number
194 */
195 protected function get_file_data( $data_type, $file_key = '' ) {
196 $asset_key = $file_key ? $file_key : $this->get_key();
197
198 if ( isset( $this->files_data[ $asset_key ][ $data_type ] ) ) {
199 return $this->files_data[ $asset_key ][ $data_type ];
200 }
201
202 if ( ! isset( $this->files_data[ $asset_key ] ) ) {
203 $this->files_data[ $asset_key ] = [];
204 }
205
206 $asset_path = $this->get_file_path();
207
208 if ( 'content' === $data_type ) {
209 $data = file_get_contents( $asset_path );
210
211 if ( ! $data ) {
212 $data = '';
213 }
214 } elseif ( 'size' === $data_type ) {
215 $data = file_exists( $asset_path ) ? filesize( $asset_path ) : 0;
216 }
217
218 $this->files_data[ $asset_key ][ $data_type ] = $data;
219
220 return $data;
221 }
222
223 /**
224 * Get Saved Assets Data.
225 *
226 * Getting the assets data from the DB.
227 *
228 * @since 3.3.0
229 * @access protected
230 *
231 * @return array
232 */
233 protected function get_saved_assets_data() {
234 $assets_data = get_option( self::ASSETS_DATA_KEY, [] );
235
236 $content_type = $this->content_type;
237 $assets_category = $this->assets_category;
238
239 if ( ! isset( $assets_data[ $content_type ] ) ) {
240 $assets_data[ $content_type ] = [];
241 }
242
243 if ( ! isset( $assets_data[ $content_type ][ $assets_category ] ) ) {
244 $assets_data[ $content_type ][ $assets_category ] = [];
245 }
246 return $assets_data;
247 }
248
249 /**
250 * Get Config.
251 *
252 * Getting the assets data config.
253 *
254 * @since 3.5.0
255 * @access protected
256 *
257 * @return array
258 */
259 protected function get_config( $data ) {
260 return [];
261 }
262
263 /**
264 * Init Asset Data.
265 *
266 * Initialize the asset data and handles the asset content updates when needed.
267 *
268 * @since 3.3.0
269 * @access public
270 *
271 * @param array $config {
272 * @type string 'key'
273 * @type string 'version'
274 * @type string 'file_path'
275 * @type array 'data'
276 * }
277 *
278 * @return void
279 */
280 public function init_asset_data( $config ) {
281 $this->assets_config = $config;
282
283 $asset_key = $config['key'];
284
285 $asset_data = isset( $this->assets_data[ $asset_key ] ) ? $this->assets_data[ $asset_key ] : [];
286
287 if ( ! $asset_data || $this->is_asset_version_changed( $asset_data['version'] ) ) {
288 $this->set_asset_data( $asset_key );
289 }
290 }
291
292 /**
293 * Get Asset Data From Config.
294 *
295 * Getting the asset data content from config.
296 *
297 * @since 3.3.0
298 * @access public
299 *
300 * @param array $config {
301 * @type string 'key'
302 * @type string 'version'
303 * @type string 'file_path'
304 * @type array 'data'
305 * }
306 *
307 * @return mixed
308 */
309 public function get_asset_data_from_config( array $config ) {
310 $this->init_asset_data( $config );
311
312 $asset_key = $config['key'];
313
314 return $this->assets_data[ $asset_key ]['content'];
315 }
316
317 /**
318 * Get Asset Data.
319 *
320 * Getting the asset data content.
321 *
322 * @since 3.5.0
323 * @access public
324 *
325 * @param array $data
326 *
327 * @return mixed
328 */
329 public function get_asset_data( array $data ) {
330 $config = $this->get_config( $data );
331
332 return $this->get_asset_data_from_config( $config );
333 }
334
335 public function __construct() {
336 $assets_data = $this->get_saved_assets_data();
337
338 $content_type = $this->content_type;
339 $assets_category = $this->assets_category;
340
341 $this->assets_data = $assets_data[ $content_type ][ $assets_category ];
342 }
343 }
344