helpers
1 week ago
tables
1 week ago
aIProviderInterface.php
1 week ago
assets.php
1 week ago
baseObject.php
1 week ago
builderBlock.php
1 week ago
controller.php
1 week ago
date.php
1 week ago
db.php
1 week ago
dispatcher.php
1 week ago
errors.php
1 week ago
field.php
1 week ago
fieldAdapter.php
1 week ago
frame.php
1 week ago
helper.php
1 week ago
html.php
1 week ago
installer.php
1 week ago
installerDbUpdater.php
1 week ago
integration.php
1 week ago
modInstaller.php
1 week ago
model.php
1 week ago
module.php
1 week ago
req.php
1 week ago
response.php
1 week ago
table.php
1 week ago
uri.php
1 week ago
user.php
1 week ago
utils.php
1 week ago
validator.php
1 week ago
view.php
1 week ago
validator.php
152 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | class WaicValidator { |
| 6 | public static $errors = array(); |
| 7 | public static function _( $field, $table = false ) { |
| 8 | return self::validate($field, $table); |
| 9 | } |
| 10 | public static function validate( $field, $table = false ) { |
| 11 | self::$errors = array(); |
| 12 | if (is_object($field) && get_class($field) != 'WaicField') { |
| 13 | $value = $field; |
| 14 | $field = new FieldWaicWaic('noMatter'); |
| 15 | $field->label = $label; |
| 16 | $field->setValue($value); |
| 17 | $field->setValidation($validate); |
| 18 | } |
| 19 | if (!empty($field->validate)) { |
| 20 | foreach ($field->validate as $v) { |
| 21 | if (method_exists('WaicValidator', $v)) { |
| 22 | self::$v($field); |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | if (method_exists('WaicValidator', $field->type)) { |
| 27 | $validate = $field->type; |
| 28 | self::$validate($field); |
| 29 | } |
| 30 | if ($field->maxlen) { |
| 31 | self::validLen($field); |
| 32 | } |
| 33 | if (is_object($table) && get_class($table) != 'WaicTable' && isset($table->lists[$field->name])) { |
| 34 | self::oneFromList($field, $table->lists[$field->name]); |
| 35 | } |
| 36 | return self::$errors; |
| 37 | } |
| 38 | public static function validLen( $field ) { |
| 39 | if ( !(bool) ( strlen($field->value) <= $field->maxlen ) ) { |
| 40 | self::addError(esc_html($field->label . ': ' . __('Invalid length', 'ai-copilot-content-generator') . '. ' . __( 'Max length', 'ai-copilot-content-generator') . ': ' . $field->maxlen), $field->name); |
| 41 | return false; |
| 42 | } |
| 43 | return true; |
| 44 | } |
| 45 | public static function oneFromList( $field, $list ) { |
| 46 | if ( !in_array($list[$field->value]) ) { |
| 47 | self::addError(esc_html($field->label . ': ' . __('Invalid field value', 'ai-copilot-content-generator') . '(' . $field->value . ')' ), $field->name); |
| 48 | return false; |
| 49 | } |
| 50 | return true; |
| 51 | } |
| 52 | public static function getErrors() { |
| 53 | return self::$errors; |
| 54 | } |
| 55 | public static function numeric( $field ) { |
| 56 | if (!is_numeric($field->value) && !empty($field->value)) { |
| 57 | self::addError($field->label . ': ' . esc_html(__('Invalid numeric value', 'ai-copilot-content-generator')), $field->name); |
| 58 | return false; |
| 59 | } |
| 60 | return true; |
| 61 | } |
| 62 | public static function int( $field ) { |
| 63 | return self::numeric($field); |
| 64 | } |
| 65 | public static function float( $field ) { |
| 66 | return self::numeric($field); |
| 67 | } |
| 68 | public static function double( $field ) { |
| 69 | return self::numeric($field); |
| 70 | } |
| 71 | protected static function _notEmpty( $value ) { |
| 72 | if (is_array($value)) { |
| 73 | foreach ($value as $v) { |
| 74 | if (self::_notEmpty($v)) { //If at least 1 element of array are not empty - all array will be not empty |
| 75 | $res = true; |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | } else { |
| 80 | $res = !empty($value); |
| 81 | } |
| 82 | return $res; |
| 83 | } |
| 84 | public static function notEmpty( $field ) { |
| 85 | if (!self::_notEmpty($field->value)) { |
| 86 | self::addError(esc_html(__('Please enter', 'ai-copilot-content-generator') . ' ' . $field->label), $field->name); |
| 87 | return false; |
| 88 | } |
| 89 | return true; |
| 90 | } |
| 91 | public static function selectNotEmpty( $field ) { |
| 92 | if (empty($field->value)) { |
| 93 | self::addError(esc_html(__('Please select', 'ai-copilot-content-generator') . ' ' . $field->label), $field->name); |
| 94 | return false; |
| 95 | } |
| 96 | return true; |
| 97 | } |
| 98 | public static function email( $field ) { |
| 99 | if (!is_email($field->value)) { |
| 100 | self::addError(esc_html(__('Invalid', 'ai-copilot-content-generator') . ' ' . $field->label), $field->name); |
| 101 | return false; |
| 102 | } elseif (email_exists($field->value)) { |
| 103 | self::addError(esc_html($field->label . ' ' . __('is already registered', 'ai-copilot-content-generator')), $field->name); |
| 104 | return false; |
| 105 | } |
| 106 | return true; |
| 107 | } |
| 108 | public static function addError( $error, $key = '' ) { |
| 109 | if ($key) { |
| 110 | self::$errors[$key] = $error; |
| 111 | } else { |
| 112 | self::$errors[] = $error; |
| 113 | } |
| 114 | } |
| 115 | public static function string( $field ) { |
| 116 | if (preg_match('/([0-9].*)/', $field->value)) { |
| 117 | self::addError(esc_html(__('Invalid', 'ai-copilot-content-generator') . ' ' . $field->label), $field->name); |
| 118 | return false; |
| 119 | } |
| 120 | return true; |
| 121 | } |
| 122 | public static function getProductValidationMethods() { |
| 123 | $res = array(); |
| 124 | $all = get_class_methods('WaicValidator'); |
| 125 | foreach ($all as $m) { |
| 126 | if (in_array($m, array('int', 'none', 'string'))) { |
| 127 | $res[$m] = esc_html($m); |
| 128 | } |
| 129 | } |
| 130 | return $res; |
| 131 | } |
| 132 | public static function getUserValidationMethods() { |
| 133 | // here validation for user fields |
| 134 | $res = array(); |
| 135 | $all = get_class_methods('WaicValidator'); |
| 136 | foreach ($all as $m) { |
| 137 | if (in_array($m, array('int', 'none', 'string', 'email', 'validLen'))) { |
| 138 | $res[$m] = esc_html($m); |
| 139 | } |
| 140 | } |
| 141 | return $res; |
| 142 | } |
| 143 | public static function prepareInput( $input ) { |
| 144 | global $wpdb; |
| 145 | if (is_array($input)) { |
| 146 | return array_map(array(validator, 'prepareInput'), $input); |
| 147 | } else { |
| 148 | return $wpdb->_real_escape($input); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 |