FormValidator.php
258 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * FormValidator class |
| 5 | * |
| 6 | * This class is responsible for validating form data |
| 7 | * |
| 8 | * @package kirki |
| 9 | */ |
| 10 | |
| 11 | namespace Kirki\FormValidator; |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; // Exit if accessed directly. |
| 15 | } |
| 16 | |
| 17 | class FormValidator { |
| 18 | |
| 19 | private $rulesets = array( 'type', 'maxLength', 'minLength', 'max', 'min' ); |
| 20 | private $fields_validation_results = array(); |
| 21 | |
| 22 | private $has_error = false; |
| 23 | private $error_message = ''; |
| 24 | |
| 25 | private function __construct() { |
| 26 | } |
| 27 | |
| 28 | public static function validate( $data, $rules ) { |
| 29 | $validator = new FormValidator(); |
| 30 | |
| 31 | $validation_result = $validator->validate_data( $data, $rules ); |
| 32 | |
| 33 | return $validation_result; |
| 34 | } |
| 35 | |
| 36 | private function validate_data( $data, $fields ) { |
| 37 | foreach ( $fields as $field_name => $rules ) { |
| 38 | // check if the rules['type'] is file then continue |
| 39 | if ( isset( $rules['type'] ) && $rules['type'] === 'file' ) { |
| 40 | continue; |
| 41 | } |
| 42 | |
| 43 | $input = ''; |
| 44 | // in form data, the field name can be in camel case or snake case |
| 45 | if ( isset( $data[ $field_name ] ) || isset( $data[ str_replace( ' ', '_', $field_name ) ] ) ) { |
| 46 | $input = isset( $data[ $field_name ] ) ? $data[ $field_name ] : $data[ str_replace( ' ', '_', $field_name ) ]; |
| 47 | } |
| 48 | |
| 49 | if ( empty( $input ) ) { |
| 50 | $this->check_required( $field_name, $rules ); |
| 51 | } else { |
| 52 | $this->validate_field( $field_name, $input, $rules ); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | $this->validate_files( $fields ); |
| 57 | |
| 58 | // total errors count |
| 59 | $total_errors = array_sum( array_map( 'count', $this->fields_validation_results ) ) - 1; |
| 60 | |
| 61 | // set error message |
| 62 | $message = $this->has_error ? "$this->error_message." . |
| 63 | ( $total_errors > 0 ? sprintf( /* translators: %d: number of errors */ __( ' (and %d more errors)', 'kirki' ), $total_errors ) : '' ) : __( 'No errors found.', 'kirki' ); |
| 64 | |
| 65 | return array( |
| 66 | 'has_error' => $this->has_error, |
| 67 | 'message' => $message, |
| 68 | 'errors' => $this->fields_validation_results, |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | private function validate_files( $fields ) { |
| 73 | if ( count( $_FILES ) > 0 ) { |
| 74 | foreach ( $_FILES as $field_name => $file ) { |
| 75 | if ( isset( $fields[ $field_name ] ) && isset( $file['error'] ) ) { |
| 76 | $rules = $fields[ $field_name ]; |
| 77 | |
| 78 | switch ( $file['error'] ) { |
| 79 | case UPLOAD_ERR_OK: |
| 80 | // check if the file type is supported |
| 81 | $validated_file_type = Validate::accepted_file_type( $file['type'], $rules['accept'] ); |
| 82 | |
| 83 | if ( false === $validated_file_type ) { |
| 84 | $this->has_error = true; |
| 85 | $this->set_error_message( $field_name, 'accept', $rules['accept'] ); |
| 86 | } |
| 87 | |
| 88 | // check if the file size is supported |
| 89 | $validated_file_size = Validate::max_file_size( $file['size'], $rules['max-file-size'] ); |
| 90 | |
| 91 | if ( false === $validated_file_size ) { |
| 92 | $this->has_error = true; |
| 93 | $this->set_error_message( $field_name, 'max-file-size', $rules['max-file-size'] ); |
| 94 | } |
| 95 | break; |
| 96 | |
| 97 | case UPLOAD_ERR_NO_FILE: |
| 98 | $this->check_required( $field_name, $rules ); |
| 99 | break; |
| 100 | |
| 101 | case UPLOAD_ERR_INI_SIZE: |
| 102 | $this->has_error = true; |
| 103 | $this->set_error_message( $field_name, 'UPLOAD_ERR_INI_SIZE', '' ); |
| 104 | break; |
| 105 | |
| 106 | case UPLOAD_ERR_FORM_SIZE: |
| 107 | $this->has_error = true; |
| 108 | $this->set_error_message( $field_name, 'UPLOAD_ERR_FORM_SIZE', '' ); |
| 109 | break; |
| 110 | |
| 111 | case UPLOAD_ERR_PARTIAL: |
| 112 | $this->has_error = true; |
| 113 | $this->set_error_message( $field_name, 'UPLOAD_ERR_PARTIAL', '' ); |
| 114 | break; |
| 115 | |
| 116 | case UPLOAD_ERR_NO_TMP_DIR: |
| 117 | $this->has_error = true; |
| 118 | $this->set_error_message( $field_name, 'UPLOAD_ERR_NO_TMP_DIR', '' ); |
| 119 | break; |
| 120 | |
| 121 | case UPLOAD_ERR_CANT_WRITE: |
| 122 | $this->has_error = true; |
| 123 | $this->set_error_message( $field_name, 'UPLOAD_ERR_CANT_WRITE', '' ); |
| 124 | break; |
| 125 | |
| 126 | case UPLOAD_ERR_EXTENSION: |
| 127 | $this->has_error = true; |
| 128 | $this->set_error_message( $field_name, 'UPLOAD_ERR_EXTENSION', '' ); |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | private function check_required( $field_name, $rules ) { |
| 137 | if ( isset( $rules['required'] ) && true === $rules['required'] ) { |
| 138 | $this->has_error = true; |
| 139 | |
| 140 | // check if there is a custom error message |
| 141 | $message = ''; |
| 142 | if ( isset( $rules['data-error-msg'] ) && ! empty( $rules['data-error-msg'] ) ) { |
| 143 | $message = $rules['data-error-msg']; |
| 144 | } else { |
| 145 | /* translators: %s: field name */ |
| 146 | $message = sprintf( __( '%s field is required', 'kirki' ), $field_name ); |
| 147 | } |
| 148 | |
| 149 | if ( |
| 150 | isset( $this->fields_validation_results[ $field_name ] ) && |
| 151 | is_array( $this->fields_validation_results[ $field_name ] ) |
| 152 | ) { |
| 153 | array_push( $this->fields_validation_results[ $field_name ], $message ); |
| 154 | } else { |
| 155 | $this->fields_validation_results[ $field_name ] = array( $message ); |
| 156 | $this->error_message = empty( $this->error_message ) ? $message : $this->error_message; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | private function validate_field( $field_name, $input, $rules ) { |
| 162 | foreach ( $rules as $rule_key => $rule_value ) { |
| 163 | if ( ! in_array( $rule_key, $this->rulesets, true ) ) { |
| 164 | continue; |
| 165 | } |
| 166 | |
| 167 | $validated = $this->validate_rule( $input, $rule_key, $rule_value ); |
| 168 | |
| 169 | if ( false === $validated ) { |
| 170 | $this->has_error = true; |
| 171 | $this->set_error_message( $field_name, $rule_key, $rule_value ); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | private function validate_rule( $input, $rule_key, $rule_value ) { |
| 177 | switch ( $rule_key ) { |
| 178 | case 'type': |
| 179 | return $this->validate_type( $input, $rule_value ); |
| 180 | case 'maxLength': |
| 181 | return Validate::max_char( $input, $rule_value ); |
| 182 | case 'minLength': |
| 183 | return Validate::min_char( $input, $rule_value ); |
| 184 | } |
| 185 | |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | private function set_error_message( $field_name, $rule_key, $rule_value ) { |
| 190 | $message = ''; |
| 191 | |
| 192 | switch ( $rule_key ) { |
| 193 | case 'type': |
| 194 | /* translators: %1$s: field name, %2$s: expected type */ |
| 195 | $message = sprintf( __( 'Invalid type! %1$s should be a valid %2$s', 'kirki' ), $field_name, $rule_value ); |
| 196 | break; |
| 197 | case 'maxLength': |
| 198 | /* translators: %1$s: field name, %2$s: maximum length */ |
| 199 | $message = sprintf( __( '%1$s should be less than %2$s charecters', 'kirki' ), $field_name, $rule_value ); |
| 200 | break; |
| 201 | case 'minLength': |
| 202 | /* translators: %1$s: field name, %2$s: minimum length */ |
| 203 | $message = sprintf( __( '%1$s should be more than %2$s charecters', 'kirki' ), $field_name, $rule_value ); |
| 204 | break; |
| 205 | case 'accept': |
| 206 | $message = __( 'Unsupported file type!', 'kirki' ); |
| 207 | break; |
| 208 | case 'max-file-size': |
| 209 | /* translators: %s: maximum file size in MB */ |
| 210 | $message = sprintf( __( 'File size should be less than %s MB', 'kirki' ), $rule_value ); |
| 211 | break; |
| 212 | case 'UPLOAD_ERR_CANT_WRITE': |
| 213 | $message = __( 'Failed to write file to disk', 'kirki' ); |
| 214 | break; |
| 215 | case 'UPLOAD_ERR_INI_SIZE': |
| 216 | $message = __( 'The uploaded file exceeds the upload_max_filesize directive in php.ini', 'kirki' ); |
| 217 | break; |
| 218 | case 'UPLOAD_ERR_FORM_SIZE': |
| 219 | $message = __( 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form', 'kirki' ); |
| 220 | break; |
| 221 | case 'UPLOAD_ERR_PARTIAL': |
| 222 | $message = __( 'The uploaded file was only partially uploaded', 'kirki' ); |
| 223 | break; |
| 224 | case 'UPLOAD_ERR_NO_TMP_DIR': |
| 225 | $message = __( 'Missing a temporary folder', 'kirki' ); |
| 226 | break; |
| 227 | case 'UPLOAD_ERR_EXTENSION': |
| 228 | $message = __( 'File upload stopped by extension', 'kirki' ); |
| 229 | break; |
| 230 | } |
| 231 | |
| 232 | if ( |
| 233 | isset( $this->fields_validation_results[ $field_name ] ) && |
| 234 | is_array( $this->fields_validation_results[ $field_name ] ) |
| 235 | ) { |
| 236 | array_push( $this->fields_validation_results[ $field_name ], $message ); |
| 237 | } else { |
| 238 | $this->error_message = empty( $this->error_message ) ? $message : $this->error_message; |
| 239 | $this->fields_validation_results[ $field_name ] = array( $message ); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | private function validate_type( $input, $rule_value ) { |
| 244 | switch ( $rule_value ) { |
| 245 | case 'email': |
| 246 | return Validate::email( $input ); |
| 247 | case 'number': |
| 248 | return Validate::number( $input ); |
| 249 | case 'tel': |
| 250 | return Validate::tel( $input ); |
| 251 | case 'date': |
| 252 | return Validate::date( $input ); |
| 253 | case 'datetime-local': |
| 254 | return Validate::datetime( $input ); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 |