PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.1
4.4.8 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 All 139 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.1.7.1, at inc/class-lp-file-system.php

354 lines 6.8 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 protected $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 * @editor tungnx
182 * @modify 4.1.3.1 comment - replace file_get_contents
183 */
184 /*public static function get_contents( $path ) {
185 _deprecated_function( __FUNCTION__, '4.1.3.1', 'file_get_contents' );
186 $output = @file_get_contents( $path );
187
188 if ( ! $output ) {
189 global $wp_filesystem;
190 self::wp_file_system();
191
192 if ( self::file_exists( $path ) ) {
193 $output = $wp_filesystem->get_contents( $path );
194 }
195 }
196
197 return $output;
198 }*/
199
200 /**
201 * Get content of file
202 *
203 * @param $path
204 *
205 * @return false|string
206 */
207 public function file_get_contents( $path ) {
208 $output = @file_get_contents( $path );
209
210 if ( ! $output ) {
211 $output = $this->lp_filesystem->get_contents( $path );
212 }
213
214 return $output;
215 }
216
217 /**
218 * Put content to file
219 *
220 * @param $path
221 * @param $content
222 *
223 * @return false|int
224 */
225 public function put_contents( $path, $content ) {
226 $output = @file_put_contents( $path, $content ); // phpcs:ignore
227 $this->chmod( $path );
228
229 if ( ! $output ) {
230 $output = $this->lp_filesystem->put_contents( $path, $content, self::chmod_file() );
231 }
232
233 return $output;
234 }
235
236 /**
237 * Check file exists
238 *
239 * @param $path
240 *
241 * @return bool
242 * @editor tungnx
243 * @modify 4.1.3.1
244 */
245 public function file_exists( $path ): bool {
246 $output = file_exists( $path );
247
248 if ( ! $output ) {
249 $output = $this->lp_filesystem->exists( $path );
250 }
251
252 return $output;
253 }
254
255 /**
256 * Put content
257 *
258 * @param $file_name
259 * @param $content
260 * @param string $folder
261 *
262 * @return false|int|void
263 */
264 public function put_content_upload( $file_name, $content, $folder = 'learnpress' ) {
265 $wp_upload_dir = wp_upload_dir( null, false );
266 $upload_dir = $wp_upload_dir['basedir'] . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR;
267 $file_path = $upload_dir . $file_name;
268
269 $output = @file_put_contents( $file_path, $content ); // phpcs:ignore
270
271 if ( ! $output ) {
272 if ( ! $this->is_writable( $wp_upload_dir['basedir'] ) ) {
273 return;
274 }
275
276 if ( ! $this->is_dir( $upload_dir ) ) {
277 wp_mkdir_p( $upload_dir );
278 }
279
280 $output = $this->lp_filesystem->put_contents( $file_path, $content, self::chmod_file() );
281 }
282
283 return $output;
284 }
285
286 /**
287 * Copy file
288 *
289 * @param $source_path
290 * @param $des_path
291 * @param bool $overwrite
292 * @param false $perms
293 *
294 * @return bool
295 */
296 public function copy( $source_path, $des_path, $overwrite = true, $perms = false ): bool {
297 if ( ! $this->file_exists( $source_path ) ) {
298 return false;
299 }
300
301 if ( ! $overwrite && $this->file_exists( $des_path ) ) {
302 return false;
303 }
304
305 $output = @copy( $source_path, $des_path ); // phpcs:ignore
306
307 if ( $perms && $output ) {
308 $this->chmod( $des_path, $perms );
309 }
310
311 if ( ! $output ) {
312 $output = $this->lp_filesystem->copy( $source_path, $des_path, $overwrite, $perms );
313 }
314
315 return $output;
316 }
317
318 /**
319 * Move file
320 *
321 * @param $source_path
322 * @param $des_path
323 * @param bool $overwrite
324 *
325 * @return bool
326 */
327 public function move( $source_path, $des_path, bool $overwrite = true ): bool {
328 if ( ! $this->file_exists( $source_path ) ) {
329 return false;
330 }
331
332 if ( ! $overwrite && $this->file_exists( $des_path ) ) {
333 return false;
334 } elseif ( @rename( $source_path, $des_path ) ) {
335 return true;
336 } else {
337 if ( $this->copy( $source_path, $des_path, $overwrite ) && $this->file_exists( $des_path ) ) {
338 $this->unlink( $source_path );
339
340 $output = true;
341 } else {
342 $output = false;
343 }
344 }
345
346 if ( ! $output ) {
347 $output = $this->lp_filesystem->move( $source_path, $des_path, $overwrite );
348 }
349
350 return $output;
351 }
352 }
353 }
354