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