| @@ -2,16 +2,16 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Give\Framework\FieldsAPI; |
| 4 | 4 | |
| 5 | 5 | use Give\Framework\ValidationRules\Rules\AllowedTypes; |
| 6 | +use Give\Framework\ValidationRules\Rules\File as FileRule; | |
| 7 | +use Give\Vendors\StellarWP\Validation\Rules\Max; | |
| 6 | 8 | |
| 7 | -use function get_allowed_mime_types; | |
| 8 | -use function wp_max_upload_size; | |
| 9 | - | |
| 10 | 9 | /** |
| 11 | 10 | * A file upload field. |
| 12 | 11 | * |
| 13 | - * @since 2.12.0 | |
| 12 | + * @since 2.32.0 Updated to use the new Validation File Rule; added description | |
| 13 | + * @since 2.12.0 | |
| 14 | 14 | * @since 2.23.1 Moved default rule values inline since inherited constructor is final. |
| 15 | 15 | */ |
| 16 | 16 | class File extends Field |
| 17 | 17 | { |
| @@ -18,17 +18,21 @@ | ||
| 18 | 18 | use Concerns\AllowMultiple; |
| 19 | 19 | use Concerns\HasEmailTag; |
| 20 | 20 | use Concerns\HasHelpText; |
| 21 | 21 | use Concerns\HasLabel; |
| 22 | - use Concerns\AllowMultiple; | |
| 22 | + use Concerns\HasDescription; | |
| 23 | 23 | |
| 24 | 24 | const TYPE = 'file'; |
| 25 | 25 | |
| 26 | + protected $allowedMimeTypes = []; | |
| 27 | + | |
| 26 | 28 | /** |
| 27 | 29 | * Set the maximum file size. |
| 28 | 30 | * |
| 29 | - * @param int $maxSize | |
| 31 | + * @deprecated use maxUploadSize() instead | |
| 30 | 32 | * |
| 33 | + * @param int $maxSize | |
| 34 | + * | |
| 31 | 35 | * @return $this |
| 32 | 36 | */ |
| 33 | 37 | public function maxSize($maxSize) |
| 34 | 38 | { |
| @@ -44,12 +48,14 @@ | ||
| 44 | 48 | } |
| 45 | 49 | |
| 46 | 50 | /** |
| 47 | 51 | * Access the maximum file size. |
| 52 | + * | |
| 53 | + * @deprecated use getMaxUploadSize() instead | |
| 48 | 54 | */ |
| 49 | 55 | public function getMaxSize(): int |
| 50 | 56 | { |
| 51 | - if (!$this->hasRule('max')) { | |
| 57 | + if ( ! $this->hasRule('max')) { | |
| 52 | 58 | return wp_max_upload_size(); |
| 53 | 59 | } |
| 54 | 60 | |
| 55 | 61 | return $this->getRule('max')->getSize(); |
| @@ -55,15 +61,90 @@ | ||
| 55 | 61 | return $this->getRule('max')->getSize(); |
| 56 | 62 | } |
| 57 | 63 | |
| 58 | 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 | + /** | |
| 59 | 140 | * Set the allowed file types. |
| 60 | 141 | * |
| 61 | - * @param string[] $allowedTypes | |
| 142 | + * @deprecated use allowedMimeTypes() instead | |
| 62 | 143 | * |
| 63 | - * @return $this | |
| 144 | + * @param string[] $allowedTypes | |
| 64 | 145 | */ |
| 65 | - public function allowedTypes(array $allowedTypes) | |
| 146 | + public function allowedTypes(array $allowedTypes): File | |
| 66 | 147 | { |
| 67 | 148 | if ($this->hasRule('allowedTypes')) { |
| 68 | 149 | /** @var AllowedTypes $rule */ |
| 69 | 150 | $rule = $this->getRule('allowedTypes'); |
| @@ -77,15 +158,20 @@ | ||
| 77 | 158 | |
| 78 | 159 | /** |
| 79 | 160 | * Access the allowed file types. |
| 80 | 161 | * |
| 162 | + * @deprecated use getAllowedMimeTypes() instead | |
| 163 | + * | |
| 81 | 164 | * @return string[] |
| 82 | 165 | */ |
| 83 | - public function getAllowedTypes() | |
| 166 | + public function getAllowedTypes(): array | |
| 84 | 167 | { |
| 85 | - if (!$this->hasRule('allowedTypes')) { | |
| 168 | + if ( ! $this->hasRule('allowedTypes')) { | |
| 86 | 169 | return get_allowed_mime_types(); |
| 87 | 170 | } |
| 88 | 171 | |
| 89 | - return $this->getRule('allowedTypes')->getAllowedTypes(); | |
| 172 | + /** @var AllowedTypes $rule */ | |
| 173 | + $rule = $this->getRule('allowedTypes'); | |
| 174 | + | |
| 175 | + return $rule->getAllowedTypes(); | |
| 90 | 176 | } |
| 91 | 177 | } |