PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 4.0.0
WP 2FA – Two-factor authentication for WordPress v4.0.0
4.0.0 1.7.1 2.0.0 2.0.1 2.1.0 2.2.0 2.2.1 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.8.0 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0 3.0.1 3.1.0 3.1.1 3.1.1.2 trunk 1.2.0 1.3.0 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1 1.6.2 1.7.0
wp-2fa / includes / classes / Utils / class-validator.php
wp-2fa / includes / classes / Utils Last commit date
class-abstract-migration.php 1 day ago class-date-time-utils.php 1 day ago class-debugging.php 1 day ago class-generate-modal.php 1 day ago class-migration.php 1 day ago class-request-utils.php 1 day ago class-settings-utils.php 1 day ago class-upgrade-guard.php 1 day ago class-user-utils.php 1 day ago class-validator.php 1 day ago class-white-label.php 1 day ago index.php 1 day ago
class-validator.php
198 lines
1 <?php
2 /**ValidatorAbstract migration class.
3 *
4 * @package wp2fa
5 * @subpackage utils
6 * @copyright 2026 Melapress
7 * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
8 * @link https://wordpress.org/plugins/wp-2fa/
9 *
10 * @since 2.8.0
11 */
12
13 declare(strict_types=1);
14
15 namespace WP2FA\Utils;
16
17 defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
18
19 /**
20 * Validator class
21 */
22 if ( ! class_exists( '\WP2FA\Utils\Validator' ) ) {
23
24 /**
25 * Provides validation functionalities for the plugin
26 *
27 * @since 2.8.0
28 */
29 class Validator {
30
31 /**
32 * All the errors collected by the validator
33 *
34 * @var array
35 *
36 * @since 2.1.0
37 */
38 private static $errors = array();
39
40 /**
41 * Validates variables against given rules
42 *
43 * @param mixed $variable - Variable to validate.
44 * @param string $type - Type to be used for validation.
45 * @param bool $default - If set to true, will return default value based on given validation type.
46 * @param mixed $default_value - If the default value to return.
47 *
48 * @return mixed
49 *
50 * @since 2.8.0
51 */
52 public static function validate( $variable, string $type, bool $default = false, $default_value = null ) {
53 $valid = true;
54 $default_val = null;
55
56 if ( empty( $type ) ) {
57 $valid = false;
58 } else {
59 $type = strval( $type );
60 switch ( $type ) {
61 case 'slug':
62 case 'string':
63 $default_val = '';
64 if ( ! isset( $variable ) ) {
65 $valid = false;
66 self::$errors[] = 'Variable is not set';
67 } else {
68 $variable = \sanitize_text_field( \wp_unslash( $variable ) );
69 }
70 break;
71 case 'email':
72 $variable = \sanitize_email( $variable );
73 $valid = self::validate_email( $variable );
74 $default_val = '';
75 break;
76 case 'int':
77 case 'integer':
78 $valid = self::validate_integer( $variable );
79 $default_val = 0;
80 break;
81 case 'bool':
82 case 'boolean':
83 $valid = self::validate_boolean( $variable );
84 $default_val = false;
85 break;
86 default:
87 $valid = false;
88 self::$errors[] = 'No rules are set - nothing to test against';
89 break;
90 }
91 }
92
93 if ( ! $valid && $default ) {
94 return $default_value ?? $default_val;
95 } elseif ( ! $valid ) {
96 return false;
97 }
98
99 return $variable;
100 }
101
102 /**
103 * Validates email
104 *
105 * @param string $variable - Value which needs to be validated.
106 *
107 * @return bool
108 *
109 * @since 2.8.0
110 */
111 public static function validate_email( string $variable ): bool {
112 $valid = true;
113
114 if ( false === ( $valid = self::filter_validate( $variable, 'email' ) ) ) { // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.Found, Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure
115 self::$errors[] = 'Variable is not valid e-mail' . "\n";
116 $valid = false;
117 }
118
119 return $valid;
120 }
121
122 /**
123 * Validates integer
124 *
125 * @param string $variable - Value which needs to be validated.
126 *
127 * @return bool
128 *
129 * @since 2.8.0
130 */
131 public static function validate_integer( string $variable ): bool {
132 $valid = true;
133
134 if ( false === ( $valid = self::filter_validate( $variable, 'int' ) ) ) { // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.Found, Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure
135 self::$errors[] = 'Variable is not valid integer' . "\n";
136 $valid = false;
137 }
138
139 return $valid;
140 }
141
142 /**
143 * Validates boolean
144 *
145 * @param string $variable - Value which needs to be validated.
146 *
147 * @return bool
148 *
149 * @since 2.8.0
150 */
151 public static function validate_boolean( string|bool $variable ): bool {
152 $valid = true;
153
154 $variable = (string) $variable;
155
156 if ( false === ( $valid = self::filter_validate( $variable, 'bool' ) ) ) { // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.Found, Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure
157 self::$errors[] = 'Variable is not valid boolean' . "\n";
158 $valid = false;
159 }
160
161 return $valid;
162 }
163
164 /**
165 * Uses standard PHP filter validation
166 *
167 * @param mixed $variable - The value which needs to be validated.
168 * @param string $type - The type of the variable - using that info method knows which validation to execute.
169 *
170 * @return bool
171 *
172 * @since 2.8.0
173 */
174 private static function filter_validate( $variable, string $type ): bool {
175 $result = false;
176
177 switch ( $type ) {
178 case 'email':
179 $result = (bool) filter_var( $variable, \FILTER_VALIDATE_EMAIL );
180 break;
181 case 'boolean':
182 case 'bool':
183 $result = (bool) filter_var( $variable, \FILTER_VALIDATE_BOOLEAN );
184 break;
185 case 'integer':
186 case 'int':
187 $result = (bool) filter_var( $variable, \FILTER_VALIDATE_INT );
188 break;
189 default:
190 // code...
191 break;
192 }
193
194 return $result;
195 }
196 }
197 }
198