PluginProbe
Elementor Website Builder – more than just a page builder / 3.25.0-dev3
Elementor Website Builder – more than just a page builder v3.25.0-dev3
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 / files / base.php

base.php in Elementor Website Builder – more than just a page builder 3.25.0-dev3, at core/files/base.php

319 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core\Files;
3
4 use Elementor\Plugin;
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit; // Exit if accessed directly
8 }
9
10 abstract class Base {
11
12 const UPLOADS_DIR = 'elementor/';
13
14 const DEFAULT_FILES_DIR = 'css/';
15
16 const META_KEY = '';
17
18 private static $wp_uploads_dir = [];
19
20 private $files_dir;
21
22 private $file_name;
23
24 /**
25 * File path.
26 *
27 * Holds the file path.
28 *
29 * @access private
30 *
31 * @var string
32 */
33 private $path;
34
35 /**
36 * Content.
37 *
38 * Holds the file content.
39 *
40 * @access private
41 *
42 * @var string
43 */
44 private $content;
45
46 /**
47 * @since 2.1.0
48 * @access public
49 * @static
50 */
51 public static function get_base_uploads_dir() {
52 $wp_upload_dir = self::get_wp_uploads_dir();
53
54 return $wp_upload_dir['basedir'] . '/' . self::UPLOADS_DIR;
55 }
56
57 /**
58 * @since 2.1.0
59 * @access public
60 * @static
61 */
62 public static function get_base_uploads_url() {
63 $wp_upload_dir = self::get_wp_uploads_dir();
64
65 return $wp_upload_dir['baseurl'] . '/' . self::UPLOADS_DIR;
66 }
67
68 /**
69 * Use a create function for PhpDoc (@return static).
70 *
71 * @return static
72 */
73 public static function create() {
74 return Plugin::$instance->files_manager->get( get_called_class(), func_get_args() );
75 }
76
77 /**
78 * @since 2.1.0
79 * @access public
80 */
81 public function __construct( $file_name ) {
82 /**
83 * Elementor File Name
84 *
85 * Filters the File name
86 *
87 * @since 2.3.0
88 *
89 * @param string $file_name
90 * @param object $this The file instance, which inherits Elementor\Core\Files
91 */
92 $file_name = apply_filters( 'elementor/files/file_name', $file_name, $this );
93
94 $this->set_file_name( $file_name );
95
96 $this->set_files_dir( static::DEFAULT_FILES_DIR );
97
98 $this->set_path();
99 }
100
101 /**
102 * @since 2.1.0
103 * @access public
104 */
105 public function set_files_dir( $files_dir ) {
106 $this->files_dir = $files_dir;
107 }
108
109 /**
110 * @since 2.1.0
111 * @access public
112 */
113 public function set_file_name( $file_name ) {
114 $this->file_name = $file_name;
115 }
116
117 /**
118 * @since 2.1.0
119 * @access public
120 */
121 public function get_file_name() {
122 return $this->file_name;
123 }
124
125 /**
126 * @since 2.1.0
127 * @access public
128 */
129 public function get_url() {
130 $url = set_url_scheme( self::get_base_uploads_url() . $this->files_dir . $this->file_name );
131
132 return add_query_arg( [ 'ver' => $this->get_meta( 'time' ) ], $url );
133 }
134
135 /**
136 * Get Path
137 *
138 * Returns the local path of the generated file.
139 *
140 * @since 3.5.0
141 * @access public
142 *
143 * @return string
144 */
145 public function get_path() {
146 return set_url_scheme( self::get_base_uploads_dir() . $this->files_dir . $this->file_name );
147 }
148
149 /**
150 * @since 2.1.0
151 * @access public
152 */
153 public function get_content() {
154 if ( ! $this->content ) {
155 $this->content = $this->parse_content();
156 }
157
158 return $this->content;
159 }
160
161 /**
162 * @since 2.1.0
163 * @access public
164 */
165 public function update() {
166 $this->update_file();
167
168 $meta = $this->get_meta();
169
170 $meta['time'] = time();
171
172 $this->update_meta( $meta );
173 }
174
175 /**
176 * @since 2.1.0
177 * @access public
178 */
179 public function update_file() {
180 $this->content = $this->parse_content();
181
182 if ( $this->content ) {
183 $this->write();
184 } else {
185 $this->delete();
186 }
187 }
188
189 /**
190 * @since 2.1.0
191 * @access public
192 */
193 public function write() {
194 return file_put_contents( $this->path, $this->content );
195 }
196
197 /**
198 * @since 2.1.0
199 * @access public
200 */
201 public function delete() {
202 if ( file_exists( $this->path ) ) {
203 unlink( $this->path );
204 }
205
206 $this->delete_meta();
207 }
208
209 /**
210 * Get meta data.
211 *
212 * Retrieve the CSS file meta data. Returns an array of all the data, or if
213 * custom property is given it will return the property value, or `null` if
214 * the property does not exist.
215 *
216 * @since 2.1.0
217 * @access public
218 *
219 * @param string $property Optional. Custom meta data property. Default is
220 * null.
221 *
222 * @return array|null An array of all the data, or if custom property is
223 * given it will return the property value, or `null` if
224 * the property does not exist.
225 */
226 public function get_meta( $property = null ) {
227 $meta = array_merge( $this->get_default_meta(), (array) $this->load_meta() );
228
229 if ( $property ) {
230 return isset( $meta[ $property ] ) ? $meta[ $property ] : null;
231 }
232
233 return $meta;
234 }
235
236 /**
237 * @since 2.1.0
238 * @access protected
239 * @abstract
240 */
241 abstract protected function parse_content();
242
243 /**
244 * Load meta.
245 *
246 * Retrieve the file meta data.
247 *
248 * @since 2.1.0
249 * @access protected
250 */
251 protected function load_meta() {
252 return get_option( static::META_KEY );
253 }
254
255 /**
256 * Update meta.
257 *
258 * Update the file meta data.
259 *
260 * @since 2.1.0
261 * @access protected
262 *
263 * @param array $meta New meta data.
264 */
265 protected function update_meta( $meta ) {
266 update_option( static::META_KEY, $meta );
267 }
268
269 /**
270 * Delete meta.
271 *
272 * Delete the file meta data.
273 *
274 * @since 2.1.0
275 * @access protected
276 */
277 protected function delete_meta() {
278 delete_option( static::META_KEY );
279 }
280
281 /**
282 * @since 2.1.0
283 * @access protected
284 */
285 protected function get_default_meta() {
286 return [
287 'time' => 0,
288 ];
289 }
290
291 /**
292 * @since 2.1.0
293 * @access private
294 * @static
295 */
296 private static function get_wp_uploads_dir() {
297 global $blog_id;
298 if ( empty( self::$wp_uploads_dir[ $blog_id ] ) ) {
299 self::$wp_uploads_dir[ $blog_id ] = wp_upload_dir( null, false );
300 }
301
302 return self::$wp_uploads_dir[ $blog_id ];
303 }
304
305 /**
306 * @since 2.1.0
307 * @access private
308 */
309 private function set_path() {
310 $dir_path = self::get_base_uploads_dir() . $this->files_dir;
311
312 if ( ! is_dir( $dir_path ) ) {
313 wp_mkdir_p( $dir_path );
314 }
315
316 $this->path = $dir_path . $this->file_name;
317 }
318 }
319