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