PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 2.9.1
WP 2FA – Two-factor authentication for WordPress v2.9.1
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 11 months ago class-date-time-utils.php 11 months ago class-debugging.php 11 months ago class-generate-modal.php 11 months ago class-migration.php 11 months ago class-request-utils.php 11 months ago class-settings-utils.php 11 months ago class-user-utils.php 11 months ago class-validator.php 11 months ago class-white-label.php 11 months ago index.php 11 months ago
class-validator.php
196 lines
1 <?php
2 /**ValidatorAbstract migration class.
3 *
4 * @package wp2fa
5 * @subpackage utils
6 * @copyright 2025 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 $variable ): bool {
152 $valid = true;
153
154 if ( false === ( $valid = self::filter_validate( $variable, 'bool' ) ) ) { // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.Found, Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure
155 self::$errors[] = 'Variable is not valid boolean' . "\n";
156 $valid = false;
157 }
158
159 return $valid;
160 }
161
162 /**
163 * Uses standard PHP filter validation
164 *
165 * @param mixed $variable - The value which needs to be validated.
166 * @param string $type - The type of the variable - using that info method knows which validation to execute.
167 *
168 * @return bool
169 *
170 * @since 2.8.0
171 */
172 private static function filter_validate( $variable, string $type ): bool {
173 $result = false;
174
175 switch ( $type ) {
176 case 'email':
177 $result = (bool) filter_var( $variable, \FILTER_VALIDATE_EMAIL );
178 break;
179 case 'boolean':
180 case 'bool':
181 $result = (bool) filter_var( $variable, \FILTER_VALIDATE_BOOLEAN );
182 break;
183 case 'integer':
184 case 'int':
185 $result = (bool) filter_var( $variable, \FILTER_VALIDATE_INT );
186 break;
187 default:
188 // code...
189 break;
190 }
191
192 return $result;
193 }
194 }
195 }
196