| @@ -1,163 +1,78 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Give\Framework\FieldsAPI; |
| 4 | 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; | |
| 5 | +use function get_allowed_mime_types; | |
| 6 | +use function wp_max_upload_size; | |
| 8 | 7 | |
| 9 | 8 | /** |
| 10 | 9 | * A file upload field. |
| 11 | 10 | * |
| 12 | - * @since 4.3.0 Added maxUploadSize property. | |
| 13 | - * @since 2.32.0 Updated to use the new Validation File Rule; added description | |
| 14 | 11 | * @since 2.12.0 |
| 15 | - * @since 2.23.1 Moved default rule values inline since inherited constructor is final. | |
| 16 | 12 | */ |
| 17 | 13 | class File extends Field |
| 18 | 14 | { |
| 15 | + | |
| 19 | 16 | use Concerns\AllowMultiple; |
| 20 | 17 | use Concerns\HasEmailTag; |
| 21 | 18 | use Concerns\HasHelpText; |
| 22 | 19 | use Concerns\HasLabel; |
| 23 | - use Concerns\HasDescription; | |
| 20 | + use Concerns\ShowInReceipt; | |
| 21 | + use Concerns\StoreAsMeta; | |
| 22 | + use Concerns\AllowMultiple; | |
| 24 | 23 | |
| 25 | 24 | const TYPE = 'file'; |
| 26 | 25 | |
| 27 | - protected $allowedMimeTypes = []; | |
| 28 | - protected $maxUploadSize = null; | |
| 29 | - | |
| 30 | 26 | /** |
| 31 | - * Set the maximum file size. | |
| 27 | + * @since 2.16.0 File size unit is bytes, so no need to convert WordPress max file upload size to kilo bytes. | |
| 32 | 28 | * |
| 33 | - * @deprecated use maxUploadSize() instead | |
| 29 | + * @param string $name | |
| 34 | 30 | * |
| 35 | - * @param int $maxSize | |
| 36 | - * | |
| 37 | - * @return $this | |
| 38 | 31 | */ |
| 39 | - public function maxSize($maxSize) | |
| 32 | + public function __construct($name) | |
| 40 | 33 | { |
| 41 | - if ($this->hasRule('max')) { | |
| 42 | - /** @var Max $rule */ | |
| 43 | - $rule = $this->getRule('max'); | |
| 44 | - $rule->size($maxSize); | |
| 45 | - } | |
| 34 | + parent::__construct($name); | |
| 46 | 35 | |
| 47 | - $this->rules("max:$maxSize"); | |
| 48 | - | |
| 49 | - return $this; | |
| 36 | + $this->validationRules->rule('maxSize', wp_max_upload_size()); // in bytes | |
| 37 | + $this->validationRules->rule('allowedTypes', get_allowed_mime_types()); | |
| 50 | 38 | } |
| 51 | 39 | |
| 52 | 40 | /** |
| 53 | - * Access the maximum file size. | |
| 41 | + * Set the maximum file size. | |
| 54 | 42 | * |
| 55 | - * @deprecated use getMaxUploadSize() instead | |
| 56 | - */ | |
| 57 | - public function getMaxSize(): int | |
| 58 | - { | |
| 59 | - if ( ! $this->hasRule('max')) { | |
| 60 | - return wp_max_upload_size(); | |
| 61 | - } | |
| 62 | - | |
| 63 | - return $this->getRule('max')->getSize(); | |
| 64 | - } | |
| 65 | - | |
| 66 | - /** | |
| 67 | - * Set the maximum file upload size. | |
| 43 | + * @param int $maxSize | |
| 68 | 44 | * |
| 69 | - * @since 4.3.0 Set the field's maxUploadSize property. | |
| 70 | - * @since 2.32.0 | |
| 45 | + * @return $this | |
| 71 | 46 | */ |
| 72 | - public function maxUploadSize(int $maxUploadSize): File | |
| 47 | + public function maxSize($maxSize) | |
| 73 | 48 | { |
| 74 | - if ($this->hasRule(FileRule::id())) { | |
| 75 | - /** @var FileRule $rule */ | |
| 76 | - $rule = $this->getRule(FileRule::id()); | |
| 77 | - $rule->maxSize($maxUploadSize); | |
| 78 | - } | |
| 49 | + $this->validationRules->rule('maxSize', $maxSize); | |
| 79 | 50 | |
| 80 | - $this->rules((new FileRule())->maxSize($maxUploadSize)); | |
| 81 | - $this->maxUploadSize = $maxUploadSize; | |
| 82 | - | |
| 83 | 51 | return $this; |
| 84 | 52 | } |
| 85 | 53 | |
| 86 | 54 | /** |
| 87 | - * Access the maximum file upload size. | |
| 55 | + * Access the maximum file size. | |
| 88 | 56 | * |
| 89 | - * @since 2.32.0 | |
| 57 | + * @return int | |
| 90 | 58 | */ |
| 91 | - public function getMaxUploadSize(): int | |
| 59 | + public function getMaxSize() | |
| 92 | 60 | { |
| 93 | - if (!$this->hasRule(FileRule::id())) { | |
| 94 | - return wp_max_upload_size(); | |
| 95 | - } | |
| 96 | - | |
| 97 | - /** @var FileRule $rule */ | |
| 98 | - $rule = $this->getRule(FileRule::id()); | |
| 99 | - | |
| 100 | - return $rule->getMaxSize(); | |
| 61 | + return $this->validationRules->getRule('maxSize'); | |
| 101 | 62 | } |
| 102 | 63 | |
| 103 | 64 | /** |
| 104 | - * Set the allowed mime types. | |
| 105 | - * | |
| 106 | - * @since 2.32.0 | |
| 107 | - * | |
| 108 | - * @param string[] $allowedMimeTypes | |
| 109 | - */ | |
| 110 | - public function allowedMimeTypes(array $allowedMimeTypes): File | |
| 111 | - { | |
| 112 | - if ($this->hasRule(FileRule::id())) { | |
| 113 | - /** @var FileRule $rule */ | |
| 114 | - $rule = $this->getRule(FileRule::id()); | |
| 115 | - | |
| 116 | - $rule->allowedMimeTypes($allowedMimeTypes); | |
| 117 | - } else { | |
| 118 | - $this->rules((new FileRule())->allowedMimeTypes($allowedMimeTypes)); | |
| 119 | - } | |
| 120 | - | |
| 121 | - $this->allowedMimeTypes = $allowedMimeTypes; | |
| 122 | - | |
| 123 | - return $this; | |
| 124 | - } | |
| 125 | - | |
| 126 | - /** | |
| 127 | - * Access the allowed mime types. | |
| 128 | - * | |
| 129 | - * @return string[] | |
| 130 | - */ | |
| 131 | - public function getAllowedMimeTypes(): array | |
| 132 | - { | |
| 133 | - if (!$this->hasRule(FileRule::id())) { | |
| 134 | - return get_allowed_mime_types(); | |
| 135 | - } | |
| 136 | - | |
| 137 | - /** @var FileRule $rule */ | |
| 138 | - $rule = $this->getRule(FileRule::id()); | |
| 139 | - | |
| 140 | - return $rule->getAllowedMimeTypes(); | |
| 141 | - } | |
| 142 | - | |
| 143 | - /** | |
| 144 | 65 | * Set the allowed file types. |
| 145 | 66 | * |
| 146 | - * @deprecated use allowedMimeTypes() instead | |
| 67 | + * @param string[] $allowedTypes | |
| 147 | 68 | * |
| 148 | - * @param string[] $allowedTypes | |
| 69 | + * @return $this | |
| 149 | 70 | */ |
| 150 | - public function allowedTypes(array $allowedTypes): File | |
| 71 | + public function allowedTypes($allowedTypes) | |
| 151 | 72 | { |
| 152 | - if ($this->hasRule('allowedTypes')) { | |
| 153 | - /** @var AllowedTypes $rule */ | |
| 154 | - $rule = $this->getRule('allowedTypes'); | |
| 155 | - $rule->setAllowedtypes($allowedTypes); | |
| 156 | - } | |
| 73 | + $this->validationRules->rule('allowedTypes', $allowedTypes); | |
| 157 | 74 | |
| 158 | - $this->rules('allowedTypes:' . implode(',', $allowedTypes)); | |
| 159 | - | |
| 160 | 75 | return $this; |
| 161 | 76 | } |
| 162 | 77 | |
| 163 | 78 | /** |
| @@ -162,20 +77,11 @@ | ||
| 162 | 77 | |
| 163 | 78 | /** |
| 164 | 79 | * Access the allowed file types. |
| 165 | 80 | * |
| 166 | - * @deprecated use getAllowedMimeTypes() instead | |
| 167 | - * | |
| 168 | 81 | * @return string[] |
| 169 | 82 | */ |
| 170 | - public function getAllowedTypes(): array | |
| 83 | + public function getAllowedTypes() | |
| 171 | 84 | { |
| 172 | - if ( ! $this->hasRule('allowedTypes')) { | |
| 173 | - return get_allowed_mime_types(); | |
| 174 | - } | |
| 175 | - | |
| 176 | - /** @var AllowedTypes $rule */ | |
| 177 | - $rule = $this->getRule('allowedTypes'); | |
| 178 | - | |
| 179 | - return $rule->getAllowedTypes(); | |
| 85 | + return $this->validationRules->getRule('allowedTypes'); | |
| 180 | 86 | } |
| 181 | 87 | } |