Actions
3 years ago
Concerns
3 years ago
Contracts
3 years ago
Exceptions
3 years ago
Facades
4 years ago
LegacyNodes
3 years ago
Checkbox.php
3 years ago
Date.php
3 years ago
Element.php
3 years ago
Email.php
3 years ago
Factory.php
4 years ago
Field.php
3 years ago
File.php
3 years ago
Form.php
3 years ago
Group.php
3 years ago
Hidden.php
3 years ago
Html.php
3 years ago
Option.php
3 years ago
Phone.php
3 years ago
Radio.php
3 years ago
Section.php
3 years ago
Select.php
3 years ago
Text.php
3 years ago
Textarea.php
3 years ago
Types.php
3 years ago
Url.php
3 years ago
File.php
92 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\FieldsAPI; |
| 4 | |
| 5 | use Give\Framework\ValidationRules\Rules\AllowedTypes; |
| 6 | |
| 7 | use function get_allowed_mime_types; |
| 8 | use function wp_max_upload_size; |
| 9 | |
| 10 | /** |
| 11 | * A file upload field. |
| 12 | * |
| 13 | * @since 2.12.0 |
| 14 | * @since 2.23.1 Moved default rule values inline since inherited constructor is final. |
| 15 | */ |
| 16 | class File extends Field |
| 17 | { |
| 18 | use Concerns\AllowMultiple; |
| 19 | use Concerns\HasEmailTag; |
| 20 | use Concerns\HasHelpText; |
| 21 | use Concerns\HasLabel; |
| 22 | use Concerns\AllowMultiple; |
| 23 | |
| 24 | const TYPE = 'file'; |
| 25 | |
| 26 | /** |
| 27 | * Set the maximum file size. |
| 28 | * |
| 29 | * @param int $maxSize |
| 30 | * |
| 31 | * @return $this |
| 32 | */ |
| 33 | public function maxSize($maxSize) |
| 34 | { |
| 35 | if ($this->hasRule('max')) { |
| 36 | /** @var Max $rule */ |
| 37 | $rule = $this->getRule('max'); |
| 38 | $rule->size($maxSize); |
| 39 | } |
| 40 | |
| 41 | $this->rules("max:$maxSize"); |
| 42 | |
| 43 | return $this; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Access the maximum file size. |
| 48 | */ |
| 49 | public function getMaxSize(): int |
| 50 | { |
| 51 | if (!$this->hasRule('max')) { |
| 52 | return wp_max_upload_size(); |
| 53 | } |
| 54 | |
| 55 | return $this->getRule('max')->getSize(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Set the allowed file types. |
| 60 | * |
| 61 | * @param string[] $allowedTypes |
| 62 | * |
| 63 | * @return $this |
| 64 | */ |
| 65 | public function allowedTypes(array $allowedTypes) |
| 66 | { |
| 67 | if ($this->hasRule('allowedTypes')) { |
| 68 | /** @var AllowedTypes $rule */ |
| 69 | $rule = $this->getRule('allowedTypes'); |
| 70 | $rule->setAllowedtypes($allowedTypes); |
| 71 | } |
| 72 | |
| 73 | $this->rules('allowedTypes:' . implode(',', $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 | if (!$this->hasRule('allowedTypes')) { |
| 86 | return get_allowed_mime_types(); |
| 87 | } |
| 88 | |
| 89 | return $this->getRule('allowedTypes')->getAllowedTypes(); |
| 90 | } |
| 91 | } |
| 92 |