PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.3
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.3
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.3, at inc/class-lp-file-system.php

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