PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.3.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.3.9
4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 All 138 releases
learnpress / inc / class-lp-file-system.php

class-lp-file-system.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.3.9, at inc/class-lp-file-system.php

394 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class LP_WP_Filesystem
4 *
5 * @version 1.0.1
6 * @editor tungnx
7 * @modify 4.1.3.1
8 */
9 if ( ! class_exists( 'LP_WP_Filesystem' ) ) {
10 class LP_WP_Filesystem {
11 /**
12 * @var WP_Filesystem_Direct
13 */
14 public $lp_filesystem;
15 /**
16 * @var LP_WP_Filesystem
17 */
18 protected static $instance;
19
20 protected function __construct() {
21 global $wp_filesystem;
22
23 if ( empty( $wp_filesystem ) ) {
24 require_once ABSPATH . '/wp-admin/includes/file.php';
25 WP_Filesystem();
26 }
27
28 $this->lp_filesystem = $wp_filesystem;
29 }
30
31 /**
32 * Instance
33 *
34 * @return LP_WP_Filesystem
35 */
36 public static function instance(): LP_WP_Filesystem {
37 if ( is_null( self::$instance ) ) {
38 self::$instance = new self();
39 }
40
41 return self::$instance;
42 }
43
44 /*public static function wp_file_system() {
45 global $wp_filesystem;
46
47 if ( empty( $wp_filesystem ) ) {
48 require_once ABSPATH . '/wp-admin/includes/file.php';
49 WP_Filesystem();
50 }
51 }*/
52
53 public static function chmod_dir(): int {
54 if ( defined( 'FS_CHMOD_DIR' ) ) {
55 $chmod_dir = FS_CHMOD_DIR;
56 } else {
57 $chmod_dir = ( fileperms( ABSPATH ) & 0777 | 0755 );
58 }
59
60 return $chmod_dir;
61 }
62
63 public static function chmod_file(): int {
64 if ( defined( 'FS_CHMOD_FILE' ) ) {
65 $chmod_file = FS_CHMOD_FILE;
66 } else {
67 $chmod_file = ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 );
68 }
69
70 return $chmod_file;
71 }
72
73 /**
74 * Set CHMOD
75 *
76 * @param $path
77 * @param null $perms
78 *
79 * @return bool
80 */
81 public function chmod( $path, $perms = null ): bool {
82 if ( is_null( $perms ) ) {
83 $perms = $this->is_file( $path ) ? self::chmod_file() : self::chmod_dir();
84 }
85
86 $output = @chmod( $path, $perms ); // phpcs:ignore
87
88 if ( ! $output ) {
89 $output = $this->lp_filesystem->chmod( $path, $perms, false );
90 }
91
92 return $output;
93 }
94
95 /**
96 * Check is file
97 *
98 * @param $path
99 *
100 * @return bool
101 */
102 public function is_file( $path ): bool {
103 $output = is_file( $path );
104
105 if ( ! $output ) {
106 $output = $this->lp_filesystem->is_file( $path );
107 }
108
109 return $output;
110 }
111
112 /**
113 * Check is dir
114 *
115 * @param $path
116 *
117 * @return bool
118 */
119 public function is_dir( $path ): bool {
120 $output = is_dir( $path );
121
122 if ( ! $output ) {
123 $output = $this->lp_filesystem->is_dir( $path );
124 }
125
126 return $output;
127 }
128
129 /**
130 * Check can read file
131 *
132 * @param $path
133 *
134 * @return bool
135 */
136 public function is_readable( $path ): bool {
137 $output = is_readable( $path );
138
139 if ( ! $output ) {
140 $output = $this->lp_filesystem->is_readable( $path );
141 }
142
143 return $output;
144 }
145
146 /**
147 * Check file can write
148 *
149 * @param $path
150 *
151 * @return bool
152 */
153 public function is_writable( $path ): bool {
154 $output = is_writable( $path );
155
156 if ( ! $output ) {
157 $output = $this->lp_filesystem->is_writable( $path );
158 }
159
160 return $output;
161 }
162
163 /**
164 * Delete file
165 *
166 * @param $path
167 *
168 * @return bool
169 */
170 public function unlink( $path ): bool {
171 $output = @unlink( $path );
172
173 if ( ! $output ) {
174 $output = $this->lp_filesystem->delete( $path, false, false );
175 }
176
177 return $output;
178 }
179
180 /**
181 * Get content of file
182 *
183 * @param $path
184 *
185 * @return false|string
186 */
187 public function file_get_contents( $path ) {
188 $output = @file_get_contents( $path );
189
190 if ( ! $output ) {
191 $output = $this->lp_filesystem->get_contents( $path );
192 }
193
194 return $output;
195 }
196
197 /**
198 * Get LearnPress icon SVG content from local plugin files.
199 *
200 * @param string $icon
201 *
202 * @return string
203 */
204 public static function get_icon_svg( string $icon ): string {
205 static $icons = [];
206
207 $icon = ltrim( $icon, '/' );
208 if ( '' === $icon ) {
209 return '';
210 }
211
212 if ( ! isset( $icons[ $icon ] ) ) {
213 $icons[ $icon ] = '';
214 $svg_path = LP_PLUGIN_PATH . 'assets/images/icons/' . $icon;
215 $lp_filesystem = self::instance();
216
217 if ( $lp_filesystem->file_exists( $svg_path ) ) {
218 $svg_content = $lp_filesystem->file_get_contents( $svg_path );
219 $icons[ $icon ] = is_string( $svg_content ) ? $svg_content : '';
220 }
221 }
222
223 return $icons[ $icon ];
224 }
225
226 /**
227 * Put content to file
228 *
229 * @param $path
230 * @param $content
231 *
232 * @return false|int
233 */
234 public function put_contents( $path, $content ) {
235 $output = @file_put_contents( $path, $content ); // phpcs:ignore
236 $this->chmod( $path );
237
238 if ( ! $output ) {
239 $output = $this->lp_filesystem->put_contents( $path, $content, self::chmod_file() );
240 }
241
242 return $output;
243 }
244
245 /**
246 * Check file exists
247 *
248 * @param $path
249 *
250 * @return bool
251 * @editor tungnx
252 * @modify 4.1.3.1
253 */
254 public function file_exists( $path ): bool {
255 $output = file_exists( $path );
256
257 if ( ! $output ) {
258 $output = $this->lp_filesystem->exists( $path );
259 }
260
261 return $output;
262 }
263
264 /**
265 * Put content
266 *
267 * @param $file_name
268 * @param $content
269 * @param string $folder
270 *
271 * @return false|int|void
272 */
273 public function put_content_upload( $file_name, $content, $folder = 'learnpress' ) {
274 $wp_upload_dir = wp_upload_dir( null, false );
275 $upload_dir = $wp_upload_dir['basedir'] . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR;
276 $file_path = $upload_dir . $file_name;
277
278 $output = @file_put_contents( $file_path, $content ); // phpcs:ignore
279
280 if ( ! $output ) {
281 if ( ! $this->is_writable( $wp_upload_dir['basedir'] ) ) {
282 return;
283 }
284
285 if ( ! $this->is_dir( $upload_dir ) ) {
286 wp_mkdir_p( $upload_dir );
287 }
288
289 $output = $this->lp_filesystem->put_contents( $file_path, $content, self::chmod_file() );
290 }
291
292 return $output;
293 }
294
295 /**
296 * Copy file
297 *
298 * @param $source_path
299 * @param $des_path
300 * @param bool $overwrite
301 * @param false $perms
302 *
303 * @return bool
304 */
305 public function copy( $source_path, $des_path, $overwrite = true, $perms = false ): bool {
306 if ( ! $this->file_exists( $source_path ) ) {
307 return false;
308 }
309
310 if ( ! $overwrite && $this->file_exists( $des_path ) ) {
311 return false;
312 }
313
314 $output = @copy( $source_path, $des_path ); // phpcs:ignore
315
316 if ( $perms && $output ) {
317 $this->chmod( $des_path, $perms );
318 }
319
320 if ( ! $output ) {
321 $output = $this->lp_filesystem->copy( $source_path, $des_path, $overwrite, $perms );
322 }
323
324 return $output;
325 }
326
327 /**
328 * Move file
329 *
330 * @param $source_path
331 * @param $des_path
332 * @param bool $overwrite
333 *
334 * @return bool
335 */
336 public function move( $source_path, $des_path, bool $overwrite = true ): bool {
337 if ( ! $this->file_exists( $source_path ) ) {
338 return false;
339 }
340
341 if ( ! $overwrite && $this->file_exists( $des_path ) ) {
342 return false;
343 } elseif ( @rename( $source_path, $des_path ) ) {
344 return true;
345 } else {
346 if ( $this->copy( $source_path, $des_path, $overwrite ) && $this->file_exists( $des_path ) ) {
347 $this->unlink( $source_path );
348
349 $output = true;
350 } else {
351 $output = false;
352 }
353 }
354
355 if ( ! $output ) {
356 $output = $this->lp_filesystem->move( $source_path, $des_path, $overwrite );
357 }
358
359 return $output;
360 }
361
362 /**
363 * Code custom for user Unigrant is using.
364 */
365 public function download_url( $url, $timeout = 300, $signature_verification = false ) {
366 return download_url( $url, $timeout, $signature_verification );
367 }
368
369 public function lp_handle_upload( &$file, $overrides = false, $time = null ) {
370 return wp_handle_upload( $file, $overrides, $time );
371 }
372
373 /**
374 * Get size of file from url
375 *
376 * @param $url
377 *
378 * @return string
379 * @since 4.2.3
380 * @version 1.0.1
381 * @depreacted 4.2.7.8.5
382 */
383 public function get_file_size_from_url( $url ) {
384 $tmp_file = $this->download_url( $url );
385 $size = '';
386 if ( $tmp_file && ! is_wp_error( $tmp_file ) ) {
387 $size = ( filesize( $tmp_file ) / 1024 < 1024 ) ? round( filesize( $tmp_file ) / 1024, 2 ) . 'KB' : round( filesize( $tmp_file ) / 1024 / 1024, 2 ) . 'MB';
388 }
389
390 return $size;
391 }
392 }
393 }
394