Validate.php
70 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Validate |
| 5 | * |
| 6 | * @package kirki |
| 7 | */ |
| 8 | |
| 9 | namespace Kirki\FormValidator; |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; // Exit if accessed directly. |
| 13 | } |
| 14 | |
| 15 | class Validate { |
| 16 | |
| 17 | public static function email( $email = '' ) { |
| 18 | return is_email( $email ); |
| 19 | } |
| 20 | |
| 21 | public static function number( $input ) { |
| 22 | return ! empty( $input ) && is_numeric( $input ); |
| 23 | } |
| 24 | |
| 25 | public static function tel( $input ) { |
| 26 | $tel_regex = '/^[0-9]{10}+$/'; |
| 27 | |
| 28 | return preg_match( $tel_regex, $input ); |
| 29 | } |
| 30 | |
| 31 | public static function date( $input ) { |
| 32 | $date_regex = '/^\d{4}-\d{2}-\d{2}$/'; |
| 33 | |
| 34 | return preg_match( $date_regex, $input ); |
| 35 | } |
| 36 | |
| 37 | public static function datetime( $input ) { |
| 38 | $datetime_regex = '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/'; |
| 39 | |
| 40 | return preg_match( $datetime_regex, $input ); |
| 41 | } |
| 42 | |
| 43 | public static function max_char( $input, $max_length ) { |
| 44 | return strlen( (string) $input ) <= (int) $max_length; |
| 45 | } |
| 46 | |
| 47 | public static function min_char( $input, $min_length ) { |
| 48 | return strlen( (string) $input ) >= (int) $min_length; |
| 49 | } |
| 50 | |
| 51 | public static function accepted_file_type( $file_type, $accepted_file_types ) { |
| 52 | // kirki supported media types for file input |
| 53 | $kirki_supported_media_types = KIRKI_SUPPORTED_MEDIA_TYPES_FOR_FILE_INPUT[ $accepted_file_types ]; |
| 54 | |
| 55 | // check if file type is in kirki supported media types |
| 56 | if ( ! empty( $kirki_supported_media_types ) && in_array( $file_type, $kirki_supported_media_types, true ) ) { |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | public static function max_file_size( $file_size, $max_file_size ) { |
| 64 | // convert file size to MB |
| 65 | $file_size = ! empty( $file_size ) ? (float) number_format( $file_size / 1048576, 2 ) : 2; |
| 66 | |
| 67 | return $file_size <= (int) $max_file_size; |
| 68 | } |
| 69 | } |
| 70 |