| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JFB_Modules\Block_Parsers; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Classes\Resources\File; |
| 7 |
use Jet_Form_Builder\Classes\Resources\File_Collection; |
| 8 |
use Jet_Form_Builder\Classes\Resources\File_Tools; |
| 9 |
use Jet_Form_Builder\Classes\Resources\Media_Block_Value; |
| 10 |
use Jet_Form_Builder\Classes\Resources\Uploaded_Collection; |
| 11 |
use Jet_Form_Builder\Classes\Resources\Uploaded_File; |
| 12 |
use Jet_Form_Builder\Classes\Resources\Upload_Exception; |
| 13 |
use Jet_Form_Builder\Classes\Resources\Upload_Permission_Exception; |
| 14 |
use Jet_Form_Builder\Classes\Tools; |
| 15 |
use JFB_Modules\Block_Parsers\Fields\Media_Field_Parser; |
| 16 |
|
| 17 |
// If this file is called directly, abort. |
| 18 |
if ( ! defined( 'WPINC' ) ) { |
| 19 |
die; |
| 20 |
} |
| 21 |
|
| 22 |
class File_Uploader { |
| 23 |
|
| 24 |
protected $settings = array(); |
| 25 |
|
| 26 |
/** @var Uploaded_File|Uploaded_File[] */ |
| 27 |
protected $preset = array(); |
| 28 |
|
| 29 |
/** @var File|File_Collection */ |
| 30 |
protected $file; |
| 31 |
|
| 32 |
protected $default_value; |
| 33 |
|
| 34 |
protected $max_files; |
| 35 |
protected $max_size; |
| 36 |
protected $allowed_user_cap; |
| 37 |
protected $allowed_mimes; |
| 38 |
protected $insert_attachment; |
| 39 |
|
| 40 |
/** @var Field_Data_Parser|Media_Field_Parser */ |
| 41 |
protected $parser; |
| 42 |
|
| 43 |
/** |
| 44 |
* @return Media_Block_Value |
| 45 |
* @throws Upload_Exception |
| 46 |
* @throws Upload_Permission_Exception |
| 47 |
*/ |
| 48 |
public function upload() { |
| 49 |
$this->sanitize(); |
| 50 |
|
| 51 |
return $this->upload_files(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @throws Upload_Permission_Exception|Upload_Exception |
| 56 |
*/ |
| 57 |
protected function sanitize() { |
| 58 |
$this->sanitize_permissions(); |
| 59 |
$this->sanitize_max_files(); |
| 60 |
$this->sanitize_files(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* @return Media_Block_Value |
| 65 |
* @throws Upload_Exception |
| 66 |
*/ |
| 67 |
protected function upload_files() { |
| 68 |
if ( $this->file instanceof File ) { |
| 69 |
return $this->upload_file( $this->file ); |
| 70 |
} |
| 71 |
|
| 72 |
$uploaded = array(); |
| 73 |
|
| 74 |
/** @var File $file */ |
| 75 |
foreach ( $this->file as $file ) { |
| 76 |
try { |
| 77 |
$uploaded[] = $this->upload_file( $file ); |
| 78 |
} catch ( Upload_Exception $exception ) { |
| 79 |
continue; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return new Uploaded_Collection( $uploaded ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* @param File $file |
| 88 |
* |
| 89 |
* @return Uploaded_File |
| 90 |
* @throws Upload_Exception |
| 91 |
*/ |
| 92 |
protected function upload_file( File $file ): Uploaded_File { |
| 93 |
$preset_uploaded = $this->get_uploaded( $file ); |
| 94 |
|
| 95 |
if ( $preset_uploaded instanceof Uploaded_File ) { |
| 96 |
return $preset_uploaded; |
| 97 |
} |
| 98 |
|
| 99 |
$uploaded = new Uploaded_File(); |
| 100 |
$uploaded->upload( $file ); |
| 101 |
|
| 102 |
if ( ! $this->insert_attachment ) { |
| 103 |
return $uploaded; |
| 104 |
} |
| 105 |
|
| 106 |
$uploaded->add_attachment(); |
| 107 |
|
| 108 |
do_action( 'jet-form-builder/inserted-attachment', $uploaded, $this ); |
| 109 |
|
| 110 |
return $uploaded; |
| 111 |
} |
| 112 |
|
| 113 |
protected function get_uploaded( File $file ) { |
| 114 |
if ( empty( $this->preset ) ) { |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
return File_Tools::get_uploaded( $file, $this->preset ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* @throws Upload_Permission_Exception |
| 123 |
*/ |
| 124 |
protected function sanitize_permissions() { |
| 125 |
if ( ! self::is_permitted( $this->settings ) ) { |
| 126 |
throw new Upload_Permission_Exception( |
| 127 |
__( 'You are not allowed to upload files', 'jet-form-builder' ) // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 128 |
); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Whether the current user satisfies the field's "user access" capability. |
| 134 |
* Public so that a submission without a file can be checked the same way. |
| 135 |
* |
| 136 |
* @param array $settings Media field settings. |
| 137 |
* |
| 138 |
* @return bool |
| 139 |
*/ |
| 140 |
public static function is_permitted( array $settings ): bool { |
| 141 |
$cap = ! empty( $settings['allowed_user_cap'] ) ? $settings['allowed_user_cap'] : 'upload_files'; |
| 142 |
|
| 143 |
if ( 'any_user' !== $cap && ! is_user_logged_in() ) { |
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
if ( ! in_array( $cap, array( 'all', 'any_user' ), true ) && ! current_user_can( $cap ) ) { |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
return true; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* @throws Upload_Exception |
| 156 |
*/ |
| 157 |
protected function sanitize_max_files() { |
| 158 |
if ( $this->count_files() > $this->max_files ) { |
| 159 |
throw new Upload_Exception( 'upload_max_files' ); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* @throws Upload_Exception |
| 165 |
*/ |
| 166 |
protected function sanitize_files() { |
| 167 |
if ( $this->file instanceof File ) { |
| 168 |
$this->sanitize_file( $this->file ); |
| 169 |
|
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
if ( ! ( $this->file instanceof File_Collection ) ) { |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
/** @var File $file */ |
| 178 |
foreach ( $this->file as $file ) { |
| 179 |
$this->sanitize_file( $file ); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* @param File $file |
| 185 |
* |
| 186 |
* @throws Upload_Exception |
| 187 |
*/ |
| 188 |
protected function sanitize_file( File $file ) { |
| 189 |
if ( $file->get_size() > $this->max_size ) { |
| 190 |
throw new Upload_Exception( 'upload_max_size' ); |
| 191 |
} |
| 192 |
|
| 193 |
if ( empty( $this->allowed_mimes ) ) { |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
if ( ! in_array( $file->get_type(), $this->allowed_mimes, true ) ) { |
| 198 |
throw new Upload_Exception( 'upload_mime_types' ); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
public function get_max_size(): int { |
| 203 |
$max_size = wp_max_upload_size(); |
| 204 |
|
| 205 |
if ( empty( $this->settings['max_size'] ) ) { |
| 206 |
return $max_size; |
| 207 |
} |
| 208 |
|
| 209 |
$field_max_size = floatval( |
| 210 |
\JFB_Modules\Rich_Content\Module::rich( $this->settings['max_size'] ?? '' ) |
| 211 |
); |
| 212 |
$field_max_size = intval( $field_max_size * MB_IN_BYTES ); |
| 213 |
|
| 214 |
if ( $field_max_size > $max_size ) { |
| 215 |
return $max_size; |
| 216 |
} |
| 217 |
|
| 218 |
return $field_max_size; |
| 219 |
} |
| 220 |
|
| 221 |
public function count_files(): int { |
| 222 |
$counter = 0; |
| 223 |
if ( $this->file instanceof File ) { |
| 224 |
++$counter; |
| 225 |
} elseif ( $this->file instanceof File_Collection ) { |
| 226 |
$counter += count( $this->file ); |
| 227 |
} |
| 228 |
|
| 229 |
return $counter; |
| 230 |
} |
| 231 |
|
| 232 |
public function get_max_files(): int { |
| 233 |
$files = absint( |
| 234 |
\JFB_Modules\Rich_Content\Module::rich( $this->settings['max_files'] ?? '' ) |
| 235 |
); |
| 236 |
|
| 237 |
return empty( $files ) ? 1 : $files; |
| 238 |
} |
| 239 |
|
| 240 |
public function get_mime_types(): array { |
| 241 |
return $this->settings['allowed_mimes'] ?? array(); |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* @return bool |
| 246 |
*/ |
| 247 |
public function is_insert_attachment(): bool { |
| 248 |
// Prevent non logged-in users insert attachment |
| 249 |
if ( ! is_user_logged_in() ) { |
| 250 |
return $this->parser->get_context()->is_allowed_for_guest(); |
| 251 |
} |
| 252 |
|
| 253 |
return ! empty( $this->settings['insert_attachment'] ); |
| 254 |
} |
| 255 |
|
| 256 |
public function get_allowed_user_cap(): string { |
| 257 |
$capability = $this->settings['allowed_user_cap'] ?? 'upload_files'; |
| 258 |
|
| 259 |
return empty( $capability ) ? 'upload_files' : $capability; |
| 260 |
} |
| 261 |
|
| 262 |
public function is_must_be_single(): bool { |
| 263 |
return 1 === $this->max_files; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* @param File|File_Collection $file |
| 268 |
* |
| 269 |
* @return File_Uploader |
| 270 |
*/ |
| 271 |
public function set_file( $file ): File_Uploader { |
| 272 |
$this->file = $file; |
| 273 |
|
| 274 |
return $this; |
| 275 |
} |
| 276 |
|
| 277 |
public function set_preset( $preset ): File_Uploader { |
| 278 |
if ( empty( $preset ) || ! is_array( $preset ) ) { |
| 279 |
return $this; |
| 280 |
} |
| 281 |
|
| 282 |
if ( $this->is_must_be_single() ) { |
| 283 |
$uploaded = new Uploaded_File(); |
| 284 |
$this->preset = $uploaded->set_from_array( $preset[0] ?? $preset ); |
| 285 |
|
| 286 |
return $this; |
| 287 |
} |
| 288 |
|
| 289 |
$files = isset( $preset[0] ) ? $preset : array( $preset ); |
| 290 |
|
| 291 |
foreach ( $files as $index => $file ) { |
| 292 |
if ( ! is_array( $file ) ) { |
| 293 |
unset( $files[ $index ] ); |
| 294 |
continue; |
| 295 |
} |
| 296 |
$files[ $index ] = ( new Uploaded_File() )->set_from_array( $file ); |
| 297 |
} |
| 298 |
|
| 299 |
$this->preset = $files; |
| 300 |
|
| 301 |
return $this; |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* @param array $settings |
| 306 |
* |
| 307 |
* @return File_Uploader |
| 308 |
*/ |
| 309 |
public function set_settings( array $settings ): File_Uploader { |
| 310 |
$this->settings = $settings; |
| 311 |
|
| 312 |
$this->max_files = $this->get_max_files(); |
| 313 |
$this->max_size = $this->get_max_size(); |
| 314 |
$this->allowed_user_cap = $this->get_allowed_user_cap(); |
| 315 |
$this->insert_attachment = $this->is_insert_attachment(); |
| 316 |
$this->allowed_mimes = $this->get_mime_types(); |
| 317 |
|
| 318 |
return $this; |
| 319 |
} |
| 320 |
|
| 321 |
public function set_context( Field_Data_Parser $parser ): File_Uploader { |
| 322 |
$this->parser = $parser; |
| 323 |
|
| 324 |
$this->set_settings( $parser->get_settings() ); |
| 325 |
$this->set_preset( Tools::decode_json( $parser->get_value() ) ); |
| 326 |
$this->set_file( $parser->get_file() ); |
| 327 |
|
| 328 |
return $this; |
| 329 |
} |
| 330 |
|
| 331 |
public function get_settings(): array { |
| 332 |
return $this->settings; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
} |
| 337 |
|