| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Services\Libs; |
| 4 |
|
| 5 |
use FluentBoards\Framework\Support\Arr; |
| 6 |
|
| 7 |
class FileSystem |
| 8 |
{ |
| 9 |
protected $subDir = ''; |
| 10 |
|
| 11 |
public function _setSubDir($subDir) |
| 12 |
{ |
| 13 |
$this->subDir = $subDir; |
| 14 |
return $this; |
| 15 |
} |
| 16 |
/** |
| 17 |
* Read file content from custom upload dir of this application |
| 18 |
* @return string [path] |
| 19 |
*/ |
| 20 |
public function _get($file) |
| 21 |
{ |
| 22 |
$arr = explode('/', $file); |
| 23 |
$fileName = end($arr); |
| 24 |
if ($this->subDir) { |
| 25 |
$fileName = $this->subDir . DIRECTORY_SEPARATOR . $fileName; |
| 26 |
} |
| 27 |
return file_get_contents( |
| 28 |
$this->getDir() . DIRECTORY_SEPARATOR . $fileName |
| 29 |
); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get custom upload dir name of this application |
| 34 |
* @return string [directory path] |
| 35 |
*/ |
| 36 |
public function _getDir() |
| 37 |
{ |
| 38 |
$uploadDir = wp_upload_dir(); |
| 39 |
|
| 40 |
$fbsUploadDir = apply_filters('fluent_boards/upload_folder_name', FLUENT_BOARDS_UPLOAD_DIR); |
| 41 |
|
| 42 |
if ($this->subDir) { |
| 43 |
return $uploadDir['basedir'] . DIRECTORY_SEPARATOR . $fbsUploadDir. DIRECTORY_SEPARATOR. $this->subDir; |
| 44 |
} |
| 45 |
|
| 46 |
return $uploadDir['basedir'] . DIRECTORY_SEPARATOR . $fbsUploadDir; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get absolute path of file using custom upload dir name of this application |
| 51 |
* @return string [file path] |
| 52 |
*/ |
| 53 |
public function _getAbsolutePathOfFile($file) |
| 54 |
{ |
| 55 |
return $this->_getDir() . DIRECTORY_SEPARATOR . $file; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Upload files into custom upload dir of this application |
| 60 |
* @return array |
| 61 |
*/ |
| 62 |
public function _uploadFromRequest() |
| 63 |
{ |
| 64 |
return $this->_put(FluentBoards('request')->files()); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Upload files into custom upload dir of this application |
| 69 |
* @param array $files |
| 70 |
* @return array |
| 71 |
*/ |
| 72 |
public function _put($files) |
| 73 |
{ |
| 74 |
if (!function_exists('wp_handle_upload')) { |
| 75 |
require_once(ABSPATH . 'wp-admin/includes/file.php'); |
| 76 |
} |
| 77 |
|
| 78 |
$this->overrideUploadDir(); |
| 79 |
|
| 80 |
$uploadOverrides = ['test_form' => false]; |
| 81 |
|
| 82 |
foreach ((array)$files as $file) { |
| 83 |
$filesArray = $file->toArray(); |
| 84 |
$extraData = Arr::only($filesArray, ['name', 'size']); |
| 85 |
$uploadsData = \wp_handle_upload($filesArray, $uploadOverrides); |
| 86 |
$uploadedFiles[] = array_merge($extraData, $uploadsData); |
| 87 |
} |
| 88 |
|
| 89 |
return $uploadedFiles; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Delete a file from custom upload directory of this application |
| 94 |
* @param array $files |
| 95 |
* @return void |
| 96 |
*/ |
| 97 |
public function _delete($files) |
| 98 |
{ |
| 99 |
$files = (array)$files; |
| 100 |
|
| 101 |
foreach ($files as $file) { |
| 102 |
$arr = explode('/', $file); |
| 103 |
$fileName = end($arr); |
| 104 |
@unlink($this->getDir() . '/' . $fileName); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Register filters for custom upload dir |
| 110 |
*/ |
| 111 |
public function _overrideUploadDir() |
| 112 |
{ |
| 113 |
add_filter('wp_handle_upload_prefilter', function ($file) { |
| 114 |
add_filter('upload_dir', [$this, '_setCustomUploadDir']); |
| 115 |
|
| 116 |
add_filter('wp_handle_upload', function ($fileinfo) { |
| 117 |
remove_filter('upload_dir', [$this, '_setCustomUploadDir']); |
| 118 |
$fileinfo['file'] = basename($fileinfo['file']); |
| 119 |
return $fileinfo; |
| 120 |
}); |
| 121 |
|
| 122 |
return $this->_renameFileName($file); |
| 123 |
}); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Set plugin's custom upload dir |
| 128 |
* @param array $param |
| 129 |
* @return array $param |
| 130 |
*/ |
| 131 |
public function _setCustomUploadDir($param) |
| 132 |
{ |
| 133 |
$fbsUploadDir = apply_filters('fluent_boards/upload_folder_name', FLUENT_BOARDS_UPLOAD_DIR); |
| 134 |
|
| 135 |
if ($this->subDir) { |
| 136 |
$fbsUploadDir .= DIRECTORY_SEPARATOR . $this->subDir; |
| 137 |
if (!is_dir($param['basedir'] . $fbsUploadDir)) { |
| 138 |
@mkdir($param['basedir'] . $fbsUploadDir, 0755); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
$param['url'] = $param['baseurl'] . DIRECTORY_SEPARATOR . $fbsUploadDir; |
| 143 |
|
| 144 |
$param['path'] = $param['basedir'] . DIRECTORY_SEPARATOR . $fbsUploadDir; |
| 145 |
|
| 146 |
return $param; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Rename the uploaded file name before saving |
| 151 |
* @param array $file |
| 152 |
* @return array $file |
| 153 |
*/ |
| 154 |
public function _renameFileName($file) |
| 155 |
{ |
| 156 |
$currentTimeStamp = (new \DateTimeImmutable())->getTimestamp(); |
| 157 |
$prefix = $currentTimeStamp . '-'; |
| 158 |
$prefix = apply_filters('fluent_boards/uploaded_file_name_prefix', $prefix); |
| 159 |
$file['name'] = $prefix . $file['name']; |
| 160 |
|
| 161 |
return $file; |
| 162 |
} |
| 163 |
|
| 164 |
public static function __callStatic($method, $params) |
| 165 |
{ |
| 166 |
$instance = new static; |
| 167 |
|
| 168 |
return call_user_func_array([$instance, $method], $params); |
| 169 |
} |
| 170 |
|
| 171 |
public function __call($method, $params) |
| 172 |
{ |
| 173 |
$hiddenMethod = "_" . $method; |
| 174 |
|
| 175 |
$method = method_exists($this, $hiddenMethod) ? $hiddenMethod : $method; |
| 176 |
|
| 177 |
return call_user_func_array([$this, $method], $params); |
| 178 |
} |
| 179 |
} |
| 180 |
|