exists($path)) { return $wp_filesystem->get_contents($path); } return false; } public static function getFolders($path, $filter = '.', $recurse = false, $full = false, $exclude = ['.svn', 'CVS', '.DS_Store', '__MACOSX'], $excludefilter = ['^\..*']) { $path = self::cleanPath($path); if (!is_dir($path)) { /* translators: %s: path of the folder that does not exist */ Message::enqueueMessage(sprintf(__('%s is not a folder', 'acychecker'), $path), 'error'); return []; } if (count($excludefilter)) { $excludefilter_string = '/('.implode('|', $excludefilter).')/'; } else { $excludefilter_string = ''; } $arr = self::getItems($path, $filter, $recurse, $full, $exclude, $excludefilter_string, false); asort($arr); return array_values($arr); } public static function getItems($path, $filter, $recurse, $full, $exclude, $excludefilter_string, $findfiles) { $arr = []; if (!($handle = @opendir($path))) { return $arr; } while (($file = readdir($handle)) !== false) { if ($file == '.' || $file == '..' || in_array($file, $exclude) || (!empty($excludefilter_string) && preg_match( $excludefilter_string, $file ))) { continue; } $fullpath = $path.'/'.$file; $isDir = is_dir($fullpath); if (($isDir xor $findfiles) && preg_match("/$filter/", $file)) { if ($full) { $arr[] = $fullpath; } else { $arr[] = $file; } } if ($isDir && $recurse) { if (is_int($recurse)) { $arr = array_merge( $arr, self::getItems( $fullpath, $filter, $recurse - 1, $full, $exclude, $excludefilter_string, $findfiles ) ); } else { $arr = array_merge( $arr, self::getItems( $fullpath, $filter, $recurse, $full, $exclude, $excludefilter_string, $findfiles ) ); } } } closedir($handle); return $arr; } public static function cleanPath($path, $ds = DIRECTORY_SEPARATOR) { $path = trim($path); if (empty($path)) { $path = ACYC_ROOT; } elseif (($ds == '\\') && substr($path, 0, 2) == '\\\\') { $path = "\\".preg_replace('#[/\\\\]+#', $ds, $path); } else { $path = preg_replace('#[/\\\\]+#', $ds, $path); } return $path; } public static function getFiles( $path, $filter = '.', $recurse = false, $full = false, $exclude = ['.svn', 'CVS', '.DS_Store', '__MACOSX'], $excludefilter = [ '^\..*', '.*~', ], $naturalSort = false ) { $path = self::cleanPath($path); if (!is_dir($path)) { /* translators: %s: path of the folder that does not exist */ Message::enqueueMessage(sprintf(__('%s is not a folder', 'acychecker'), $path), 'error'); return false; } if (count($excludefilter)) { $excludefilter_string = '/('.implode('|', $excludefilter).')/'; } else { $excludefilter_string = ''; } $arr = self::getItems($path, $filter, $recurse, $full, $exclude, $excludefilter_string, true); if ($naturalSort) { natsort($arr); } else { asort($arr); } return array_values($arr); } public static function writeFile($file, $buffer, $flags = 0) { if (!file_exists(dirname($file)) && self::createFolder(dirname($file)) == false) { return false; } $file = self::cleanPath($file); return is_int(file_put_contents($file, $buffer, $flags)); } public static function createFolder(string $path = '') { $path = self::cleanPath($path); if (empty($path)) { return false; } if (file_exists($path)) { return true; } return wp_mkdir_p($path); } public static function deleteFile(string $file): bool { if (!file_exists($file)) { return true; } return wp_delete_file($file); } }