| 1 |
<?php |
| 2 |
|
| 3 |
class LSD_Folder extends LSD_Base |
| 4 |
{ |
| 5 |
protected static function filesystem() |
| 6 |
{ |
| 7 |
global $wp_filesystem; |
| 8 |
|
| 9 |
if (!function_exists('WP_Filesystem')) require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 10 |
|
| 11 |
if (!is_object($wp_filesystem) || !is_a($wp_filesystem, 'WP_Filesystem_Base')) WP_Filesystem(); |
| 12 |
|
| 13 |
if (!is_object($wp_filesystem) || !is_a($wp_filesystem, 'WP_Filesystem_Base')) return null; |
| 14 |
|
| 15 |
return $wp_filesystem; |
| 16 |
} |
| 17 |
|
| 18 |
protected static function directory_permissions(): int |
| 19 |
{ |
| 20 |
return defined('FS_CHMOD_DIR') ? FS_CHMOD_DIR : 0755; |
| 21 |
} |
| 22 |
|
| 23 |
public static function files($path, $filter = '.') |
| 24 |
{ |
| 25 |
// Path doesn't exists |
| 26 |
if (!self::exists($path)) return false; |
| 27 |
|
| 28 |
$files = []; |
| 29 |
if ($handle = opendir($path)) |
| 30 |
{ |
| 31 |
while (false !== ($entry = readdir($handle))) |
| 32 |
{ |
| 33 |
if ($entry == '.' or $entry == '..' or is_dir($entry)) continue; |
| 34 |
if (!preg_match("/$filter/", $entry)) continue; |
| 35 |
|
| 36 |
$files[] = $entry; |
| 37 |
} |
| 38 |
|
| 39 |
closedir($handle); |
| 40 |
} |
| 41 |
|
| 42 |
return $files; |
| 43 |
} |
| 44 |
|
| 45 |
public static function exists($path): bool |
| 46 |
{ |
| 47 |
$filesystem = self::filesystem(); |
| 48 |
if ($filesystem) return $filesystem->is_dir($path); |
| 49 |
|
| 50 |
return is_dir($path); |
| 51 |
} |
| 52 |
|
| 53 |
public static function create($path): bool |
| 54 |
{ |
| 55 |
// Directory Exists Already |
| 56 |
if (LSD_Folder::exists($path)) return true; |
| 57 |
|
| 58 |
// Check Parent Directory |
| 59 |
$parent = substr($path, 0, strrpos($path, '/', -2) + 1); |
| 60 |
$return = LSD_Folder::create($parent); |
| 61 |
|
| 62 |
// Create Directory |
| 63 |
if (!$return) return false; |
| 64 |
|
| 65 |
$filesystem = self::filesystem(); |
| 66 |
if (!$filesystem) return false; |
| 67 |
|
| 68 |
if (!$filesystem->is_dir($parent) || !$filesystem->is_writable($parent)) return false; |
| 69 |
|
| 70 |
if ($filesystem->is_dir($path)) return true; |
| 71 |
|
| 72 |
return $filesystem->mkdir($path, self::directory_permissions()); |
| 73 |
} |
| 74 |
} |
| 75 |
|