MaxFileSizeRule.php
31 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kirki\App\Rules\Form; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | |
| 7 | use Kirki\Framework\Validation\Rules\BaseRule; |
| 8 | |
| 9 | /** |
| 10 | * Validates an uploaded file's size against a form field's max size, in MB. |
| 11 | * |
| 12 | * Expects the value to be a raw `$_FILES` entry, or null when no file was |
| 13 | * submitted (handled by the base rule's default null-skip). |
| 14 | */ |
| 15 | class MaxFileSizeRule extends BaseRule |
| 16 | { |
| 17 | public function validate_rule() |
| 18 | { |
| 19 | if (empty($this->value) || !$this->value->is_valid()) { |
| 20 | return false; |
| 21 | } |
| 22 | |
| 23 | return ($this->value->get_size() / 1048576) <= (float) $this->rule_value; |
| 24 | } |
| 25 | |
| 26 | public function get_error_message() |
| 27 | { |
| 28 | return sprintf(__('File size exceeds the maximum allowed for this field (%s MB).', 'kirki'), $this->rule_value); |
| 29 | } |
| 30 | } |
| 31 |