Concerns
4 years ago
Conditions
4 years ago
Contracts
4 years ago
Exceptions
4 years ago
Facades
4 years ago
FormFieldMediator
4 years ago
Checkbox.php
4 years ago
Date.php
4 years ago
Element.php
4 years ago
Email.php
4 years ago
Factory.php
4 years ago
Field.php
4 years ago
File.php
4 years ago
Form.php
4 years ago
Group.php
4 years ago
Hidden.php
4 years ago
Html.php
4 years ago
Option.php
4 years ago
Phone.php
4 years ago
Radio.php
4 years ago
Select.php
4 years ago
Text.php
4 years ago
Textarea.php
4 years ago
Types.php
4 years ago
Url.php
4 years ago
File.php
88 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\FieldsAPI; |
| 4 | |
| 5 | use function get_allowed_mime_types; |
| 6 | use function wp_max_upload_size; |
| 7 | |
| 8 | /** |
| 9 | * A file upload field. |
| 10 | * |
| 11 | * @since 2.12.0 |
| 12 | */ |
| 13 | class File extends Field |
| 14 | { |
| 15 | |
| 16 | use Concerns\AllowMultiple; |
| 17 | use Concerns\HasEmailTag; |
| 18 | use Concerns\HasHelpText; |
| 19 | use Concerns\HasLabel; |
| 20 | use Concerns\ShowInReceipt; |
| 21 | use Concerns\StoreAsMeta; |
| 22 | use Concerns\AllowMultiple; |
| 23 | |
| 24 | const TYPE = 'file'; |
| 25 | |
| 26 | /** |
| 27 | * @since 2.16.0 File size unit is bytes, so no need to convert WordPress max file upload size to kilo bytes. |
| 28 | * |
| 29 | * @param string $name |
| 30 | * |
| 31 | */ |
| 32 | public function __construct($name) |
| 33 | { |
| 34 | parent::__construct($name); |
| 35 | |
| 36 | $this->validationRules->rule('maxSize', wp_max_upload_size()); // in bytes |
| 37 | $this->validationRules->rule('allowedTypes', get_allowed_mime_types()); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Set the maximum file size. |
| 42 | * |
| 43 | * @param int $maxSize |
| 44 | * |
| 45 | * @return $this |
| 46 | */ |
| 47 | public function maxSize($maxSize) |
| 48 | { |
| 49 | $this->validationRules->rule('maxSize', $maxSize); |
| 50 | |
| 51 | return $this; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Access the maximum file size. |
| 56 | * |
| 57 | * @return int |
| 58 | */ |
| 59 | public function getMaxSize() |
| 60 | { |
| 61 | return $this->validationRules->getRule('maxSize'); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Set the allowed file types. |
| 66 | * |
| 67 | * @param string[] $allowedTypes |
| 68 | * |
| 69 | * @return $this |
| 70 | */ |
| 71 | public function allowedTypes($allowedTypes) |
| 72 | { |
| 73 | $this->validationRules->rule('allowedTypes', $allowedTypes); |
| 74 | |
| 75 | return $this; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Access the allowed file types. |
| 80 | * |
| 81 | * @return string[] |
| 82 | */ |
| 83 | public function getAllowedTypes() |
| 84 | { |
| 85 | return $this->validationRules->getRule('allowedTypes'); |
| 86 | } |
| 87 | } |
| 88 |