Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/elementor/core/page-assets/data-managers/base.php

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