FileUploadValidator.php
111 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Form\LegacyConsumer\Validators; |
| 4 | |
| 5 | use Give\Form\LegacyConsumer\Traits\HasFilesArray; |
| 6 | use Give\Framework\FieldsAPI\File; |
| 7 | use function _n; |
| 8 | use function esc_html__; |
| 9 | use function give_set_error; |
| 10 | use function size_format; |
| 11 | |
| 12 | /** |
| 13 | * @package Give\Form\LegacyConsumer\Validators |
| 14 | * @since 2.14.0 |
| 15 | */ |
| 16 | class FileUploadValidator { |
| 17 | use HasFilesArray; |
| 18 | |
| 19 | /** |
| 20 | * @var array |
| 21 | */ |
| 22 | private $files; |
| 23 | /** |
| 24 | * @var File |
| 25 | */ |
| 26 | private $field; |
| 27 | /** |
| 28 | * @var int |
| 29 | */ |
| 30 | private $uploadSize; |
| 31 | /** |
| 32 | * @var array |
| 33 | */ |
| 34 | private $uploadedTypes; |
| 35 | |
| 36 | /** |
| 37 | * @since 2.14.0 |
| 38 | */ |
| 39 | public function __construct( File $field ) { |
| 40 | $this->field = $field; |
| 41 | $this->files = $this->getFiles(); |
| 42 | |
| 43 | foreach ( $this->files as $file ) { |
| 44 | $this->uploadSize += $file['size']; |
| 45 | $this->uploadedTypes[] = $file['type']; |
| 46 | } |
| 47 | } |
| 48 | /** |
| 49 | * @since 2.14.0 |
| 50 | */ |
| 51 | public function __invoke() { |
| 52 | if( ! $this->files ) { |
| 53 | $this->validateRequired(); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | $this->validateUploadTypes(); |
| 58 | $this->validateUploadSize(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @since 2.14.0 |
| 63 | */ |
| 64 | private function validateRequired(){ |
| 65 | if( $this->field->isRequired() ) { |
| 66 | give_set_error( |
| 67 | "give-{$this->field->getName()}-required-field-missing", |
| 68 | $this->field->getRequiredError()['error_message'] |
| 69 | ); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @since 2.14.0 |
| 75 | */ |
| 76 | private function validateUploadTypes(){ |
| 77 | $allowedTypes = $this->field->getAllowedTypes(); |
| 78 | |
| 79 | if ( array_diff( $this->uploadedTypes, $allowedTypes ) ) { |
| 80 | give_set_error( 'field-api-file-upload-allowed-type-error', sprintf( |
| 81 | esc_html__( 'Unable to upload file. Allowed file %1$s: %2$s', 'give' ), |
| 82 | _n( 'type', 'types', count( $allowedTypes ), 'give' ), |
| 83 | array_reduce( |
| 84 | array_keys( $allowedTypes ), |
| 85 | function ( $initial, $fileType ){ |
| 86 | $separator = $initial ? ', ' : ''; |
| 87 | $initial .= $separator . str_replace( '|', ', ', $fileType ); |
| 88 | |
| 89 | return $initial; |
| 90 | }, |
| 91 | '' |
| 92 | ) |
| 93 | ) ); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @since 2.14.0 |
| 99 | */ |
| 100 | private function validateUploadSize(){ |
| 101 | $allowedFileSize = $this->field->getMaxSize(); |
| 102 | |
| 103 | if ( $allowedFileSize < $this->uploadSize ) { |
| 104 | give_set_error( 'field-api-file-upload-size-error', sprintf( |
| 105 | esc_html__( 'File size exceed upload limit. Maximum file limit is %s', 'give' ), |
| 106 | size_format( $allowedFileSize * 1024 ) |
| 107 | ) ); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 |