Actions
2 years ago
Concerns
2 years ago
Contracts
3 years ago
Exceptions
2 years ago
Facades
4 years ago
LegacyNodes
3 years ago
Properties
2 years ago
ValueObjects
2 years ago
Amount.php
2 years ago
Authentication.php
2 years ago
BillingAddress.php
2 years ago
Checkbox.php
2 years ago
Consent.php
2 years ago
Date.php
2 years ago
DonationAmount.php
2 years ago
DonationForm.php
2 years ago
DonationSummary.php
2 years ago
Element.php
3 years ago
Email.php
2 years ago
Factory.php
4 years ago
Field.php
2 years ago
File.php
2 years ago
Form.php
3 years ago
Group.php
3 years ago
Hidden.php
3 years ago
Html.php
3 years ago
MultiSelect.php
2 years ago
Name.php
2 years ago
Option.php
3 years ago
Paragraph.php
2 years ago
Password.php
2 years ago
PaymentGateways.php
2 years ago
Phone.php
2 years ago
Radio.php
2 years ago
Section.php
2 years ago
Select.php
2 years ago
Text.php
2 years ago
Textarea.php
2 years ago
Types.php
2 years ago
Url.php
2 years ago
File.php
178 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\FieldsAPI; |
| 4 | |
| 5 | use Give\Framework\ValidationRules\Rules\AllowedTypes; |
| 6 | use Give\Framework\ValidationRules\Rules\File as FileRule; |
| 7 | use Give\Vendors\StellarWP\Validation\Rules\Max; |
| 8 | |
| 9 | /** |
| 10 | * A file upload field. |
| 11 | * |
| 12 | * @since 2.32.0 Updated to use the new Validation File Rule; added description |
| 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\HasDescription; |
| 23 | |
| 24 | const TYPE = 'file'; |
| 25 | |
| 26 | protected $allowedMimeTypes = []; |
| 27 | |
| 28 | /** |
| 29 | * Set the maximum file size. |
| 30 | * |
| 31 | * @deprecated use maxUploadSize() instead |
| 32 | * |
| 33 | * @param int $maxSize |
| 34 | * |
| 35 | * @return $this |
| 36 | */ |
| 37 | public function maxSize($maxSize) |
| 38 | { |
| 39 | if ($this->hasRule('max')) { |
| 40 | /** @var Max $rule */ |
| 41 | $rule = $this->getRule('max'); |
| 42 | $rule->size($maxSize); |
| 43 | } |
| 44 | |
| 45 | $this->rules("max:$maxSize"); |
| 46 | |
| 47 | return $this; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Access the maximum file size. |
| 52 | * |
| 53 | * @deprecated use getMaxUploadSize() instead |
| 54 | */ |
| 55 | public function getMaxSize(): int |
| 56 | { |
| 57 | if ( ! $this->hasRule('max')) { |
| 58 | return wp_max_upload_size(); |
| 59 | } |
| 60 | |
| 61 | return $this->getRule('max')->getSize(); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Set the maximum file upload size. |
| 66 | * |
| 67 | * @since 2.32.0 |
| 68 | */ |
| 69 | public function maxUploadSize(int $maxUploadSize): File |
| 70 | { |
| 71 | if ($this->hasRule(FileRule::id())) { |
| 72 | /** @var FileRule $rule */ |
| 73 | $rule = $this->getRule(FileRule::id()); |
| 74 | $rule->maxSize($maxUploadSize); |
| 75 | } |
| 76 | |
| 77 | $this->rules((new FileRule())->maxSize($maxUploadSize)); |
| 78 | |
| 79 | return $this; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Access the maximum file upload size. |
| 84 | * |
| 85 | * @since 2.32.0 |
| 86 | */ |
| 87 | public function getMaxUploadSize(): int |
| 88 | { |
| 89 | if (!$this->hasRule(FileRule::id())) { |
| 90 | return wp_max_upload_size(); |
| 91 | } |
| 92 | |
| 93 | /** @var FileRule $rule */ |
| 94 | $rule = $this->getRule(FileRule::id()); |
| 95 | |
| 96 | return $rule->getMaxSize(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Set the allowed mime types. |
| 101 | * |
| 102 | * @since 2.32.0 |
| 103 | * |
| 104 | * @param string[] $allowedMimeTypes |
| 105 | */ |
| 106 | public function allowedMimeTypes(array $allowedMimeTypes): File |
| 107 | { |
| 108 | if ($this->hasRule(FileRule::id())) { |
| 109 | /** @var FileRule $rule */ |
| 110 | $rule = $this->getRule(FileRule::id()); |
| 111 | |
| 112 | $rule->allowedMimeTypes($allowedMimeTypes); |
| 113 | } else { |
| 114 | $this->rules((new FileRule())->allowedMimeTypes($allowedMimeTypes)); |
| 115 | } |
| 116 | |
| 117 | $this->allowedMimeTypes = $allowedMimeTypes; |
| 118 | |
| 119 | return $this; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Access the allowed mime types. |
| 124 | * |
| 125 | * @return string[] |
| 126 | */ |
| 127 | public function getAllowedMimeTypes(): array |
| 128 | { |
| 129 | if (!$this->hasRule(FileRule::id())) { |
| 130 | return get_allowed_mime_types(); |
| 131 | } |
| 132 | |
| 133 | /** @var FileRule $rule */ |
| 134 | $rule = $this->getRule(FileRule::id()); |
| 135 | |
| 136 | return $rule->getAllowedMimeTypes(); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Set the allowed file types. |
| 141 | * |
| 142 | * @deprecated use allowedMimeTypes() instead |
| 143 | * |
| 144 | * @param string[] $allowedTypes |
| 145 | */ |
| 146 | public function allowedTypes(array $allowedTypes): File |
| 147 | { |
| 148 | if ($this->hasRule('allowedTypes')) { |
| 149 | /** @var AllowedTypes $rule */ |
| 150 | $rule = $this->getRule('allowedTypes'); |
| 151 | $rule->setAllowedtypes($allowedTypes); |
| 152 | } |
| 153 | |
| 154 | $this->rules('allowedTypes:' . implode(',', $allowedTypes)); |
| 155 | |
| 156 | return $this; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Access the allowed file types. |
| 161 | * |
| 162 | * @deprecated use getAllowedMimeTypes() instead |
| 163 | * |
| 164 | * @return string[] |
| 165 | */ |
| 166 | public function getAllowedTypes(): array |
| 167 | { |
| 168 | if ( ! $this->hasRule('allowedTypes')) { |
| 169 | return get_allowed_mime_types(); |
| 170 | } |
| 171 | |
| 172 | /** @var AllowedTypes $rule */ |
| 173 | $rule = $this->getRule('allowedTypes'); |
| 174 | |
| 175 | return $rule->getAllowedTypes(); |
| 176 | } |
| 177 | } |
| 178 |