LLMS_Form_Validator

LLMS_Form_Handler class.


Source Source

File: includes/forms/class-llms-form-validator.php

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
class LLMS_Form_Validator {
 
    /**
     * Filters a list of fields down to only the required fields.
     *
     * @since 5.0.0
     *
     * @param array[] $fields Array of LifterLMS Form Field settings arrays.
     * @return array[]
     */
    public function get_required_fields( $fields ) {
        return array_values(
            array_filter(
                $fields,
                function( $field ) {
                    return ! empty( $field['required'] );
                }
            )
        );
    }
 
    /**
     * Sanitize a single field according to its type
     *
     * @since 5.0.0
     *
     * @param mixed $posted_value User-submitted (dirty) value.
     * @param array $field        LifterLMS field settings.
     * @return mixed
     */
    public function sanitize_field( $posted_value, $field ) {
 
        $map = array(
            'email'    => 'sanitize_email',
            'number'   => array( $this, 'sanitize_field_number' ),
            'tel'      => array( $this, 'sanitize_field_tel' ),
            'textarea' => 'sanitize_textarea_field',
            'url'      => 'esc_url_raw',
        );
 
        $func = isset( $map[ $field['type'] ] ) ? $map[ $field['type'] ] : 'sanitize_text_field';
 
        // Turn the submitted value into array, so to unify sanitization of scalar and array posted values.
        $to_sanitize = is_array( $posted_value ) ? $posted_value : array( $posted_value );
        $sanitized   = array();
 
        foreach ( $to_sanitize as $value ) {
            $sanitized[] = trim( call_user_func( $func, $value ) );
        }
 
        return is_array( $posted_value ) ? $sanitized : $sanitized[0];
 
    }
 
    /**
     * Sanitize a number field
     *
     * @since 5.0.0
     *
     * @param string $posted_value User-submitted (dirty) value.
     * @return string
     */
    protected function sanitize_field_number( $posted_value ) {
        return preg_replace( '/[^0-9.,]/', '', $posted_value );
    }
 
    /**
     * Sanitize a telephone field
     *
     * @since 5.0.0
     *
     * @param string $posted_value User-submitted (dirty) value.
     * @return string
     */
    protected function sanitize_field_tel( $posted_value ) {
        return preg_replace( '/[^\s\#0-9\-\+\(\)\.]/', '', $posted_value );
 
    }
 
    /**
     * Sanitize all user-submitted data according to field settings
     *
     * @since 5.0.0
     *
     * @param array   $posted_data User-submitted form data.
     * @param array[] $fields      LifterLMS form fields settings.
     * @return array
     */
    public function sanitize_fields( $posted_data, $fields ) {
 
        foreach ( $fields as $field ) {
 
            if ( empty( $field['name'] ) || ! isset( $posted_data[ $field['name'] ] ) ) {
                continue;
            }
 
            $posted_data[ $field['name'] ] = $this->sanitize_field( $posted_data[ $field['name'] ], $field );
 
        }
 
        return $posted_data;
 
    }
 
    /**
     * Validate a posted value
     *
     * @since 5.0.0
     *
     * @param mixed $posted_value Posted data.
     * @param array $field        LifterLMS Form Field settings array.
     * @return WP_Error|true
     */
    public function validate_field( $posted_value, $field ) {
 
        // Validate field by type.
        $type_map = array(
            'email'  => array( $this, 'validate_field_email' ),
            'number' => array( $this, 'validate_field_number' ),
            'tel'    => array( $this, 'validate_field_tel' ),
            'url'    => array( $this, 'validate_field_url' ),
        );
 
        // Turn the submitted value into array, so to unify validation of scalar and array posted values.
        $to_validate = is_array( $posted_value ) ? $posted_value : array( $posted_value );
 
        foreach ( $to_validate as $value ) {
 
            $valid = isset( $type_map[ $field['type'] ] ) ? call_user_func( $type_map[ $field['type'] ], $value, $field ) : true;
            if ( is_wp_error( $valid ) ) { // Return as soon as a field is not valid.
                return $valid;
            }
 
            // HTML Attribute Validations.
            if ( ! empty( $field['attributes']['minlength'] ) ) {
                $valid = $this->validate_field_attribute_minlength( $value, $field['attributes']['minlength'], $field );
                if ( is_wp_error( $valid ) ) {
                    return $valid;
                }
            }
        }
 
        // Perform special validations for special field types (scalar by their nature).
        $extra_map = array(
            'llms_voucher'     => array( $this, 'validate_field_voucher' ),
            'password_current' => array( $this, 'validate_field_current_password' ),
            'user_email'       => array( $this, 'validate_field_user_email' ),
            'user_login'       => array( $this, 'validate_field_user_login' ),
        );
        $valid     = isset( $extra_map[ $field['id'] ] ) ? call_user_func( $extra_map[ $field['id'] ], $posted_value ) : true;
        if ( is_wp_error( $valid ) ) {
            return $valid;
        }
 
        return true;
 
    }
 
    /**
     * Validates the html input minlength attribute
     *
     * Used by the User Password field.
     *
     * @since 5.0.0
     *
     * @param string $posted_value User-submitted value.
     * @param int    $minlength    The minimum string length as parsed from the field block.
     * @param array  $field        LifterLMS Form Field settings array.
     * @return WP_Error|boolean Returns `true` for a valid value, otherwise an error.
     */
    protected function validate_field_attribute_minlength( $posted_value, $minlength, $field ) {
 
        if ( strlen( $posted_value ) < $minlength ) {
            return new WP_Error(
                'llms-form-field-invalid',
                sprintf(
                    __( 'The %1$s must be at least %2$d characters in length.', 'lifterlms' ),
                    isset( $field['label'] ) ? $field['label'] : $field['name'],
                    $minlength
                )
            );
        }
 
        return true;
 
    }
 
    /**
     * Validate an email field
     *
     * @since 5.0.0
     *
     * @param string $posted_value User-submitted (dirty) value.
     * @return WP_Error|boolean Returns `true` for a valid submission, otherwise an error.
     */
    protected function validate_field_email( $posted_value ) {
 
        if ( ! is_email( $posted_value ) ) {
            // Translators: %s user submitted value.
            return new WP_Error( 'llms-form-field-invalid', sprintf( __( 'The email address "%s" is not valid.', 'lifterlms' ), $posted_value ) );
        }
 
        return true;
 
    }
 
    /**
     * Validate a number field
     *
     * Ensures the posted valued is numeric and, where applicable, ensures that the number falls
     * within minimum and maximum value requirements.
     *
     * @since 5.0.0
     *
     * @param string $posted_value User-submitted (dirty) value.
     * @param array  $field        The LLMS_Form_Field settings array.
     * @return WP_Error|boolean Returns `true` for a valid submission, otherwise an error.
     */
    protected function validate_field_number( $posted_value, $field ) {
 
        $temp_value = str_replace( ',', '', $posted_value );
        if ( ! is_numeric( $temp_value ) ) {
            // Translators: %1$s field label or name; %2$s = user submitted value.
            return new WP_Error( 'llms-form-field-invalid', sprintf( __( 'The %1$s "%2$s" is not valid number.', 'lifterlms' ), isset( $field['label'] ) ? $field['label'] : $field['name'], $posted_value ) );
        } elseif ( isset( $field['attributes'] ) ) {
            if ( ( ! empty( $field['attributes']['min'] ) || ( isset( $field['attributes']['min'] ) && '0' === $field['attributes']['min'] ) ) && $temp_value < $field['attributes']['min'] ) {
                // Translators: %1$s = field label or name; %2$s = user submitted value; %3$d = minimum allowed number.
                return new WP_Error( 'llms-form-field-invalid', sprintf( __( 'The %1$s "%2$s" must be greater than or equal to %3$d.', 'lifterlms' ), isset( $field['label'] ) ? $field['label'] : $field['name'], $posted_value, $field['attributes']['min'] ) );
            } elseif ( ( ! empty( $field['attributes']['max'] ) || ( isset( $field['attributes']['max'] ) && '0' === $field['attributes']['max'] ) ) && $temp_value > $field['attributes']['max'] ) {
                // Translators: %1$s = field label or name; %2$s = user submitted value; %3$d = maximum allowed number.
                return new WP_Error( 'llms-form-field-invalid', sprintf( __( 'The %1$s "%2$s" must be less than or equal to %3$d.', 'lifterlms' ), isset( $field['label'] ) ? $field['label'] : $field['name'], $posted_value, $field['attributes']['max'] ) );
            }
        }
 
        return true;
 
    }
 
    /**
     * Validate a logged-in users current password
     *
     * @since 5.0.0
     *
     * @param string $posted_value User-submitted (dirty) value.
     * @return WP_Error|boolean Returns `true` for a valid submission, otherwise an error.
     */
    protected function validate_field_current_password( $posted_value ) {
 
        if ( ! is_user_logged_in() ) {
            return new WP_Error( 'llms-form-field-invalid-no-user', __( 'You must be logged in to update your password.', 'lifterlms' ), $posted_value );
        }
 
        $user = wp_get_current_user();
        if ( ! wp_check_password( $posted_value, $user->user_pass ) ) {
            return new WP_Error( 'llms-form-field-invalid', __( 'The submitted password was not correct.', 'lifterlms' ), $posted_value );
        }
 
        return true;
    }
 
    /**
     * Validate a telephone field
     *
     * @since 5.0.0
     *
     * @param string $posted_value User-submitted (dirty) value.
     * @return WP_Error|boolean Returns `true` for a valid submission, otherwise an error.
     */
    protected function validate_field_tel( $posted_value ) {
 
        if ( 0 < strlen( trim( preg_replace( '/[\s\#0-9\-\+\(\)\.]/', '', $posted_value ) ) ) ) {
            // Translators: %s = user submitted value.
            return new WP_Error( 'llms-form-field-invalid', sprintf( __( 'The phone number "%s" is not valid.', 'lifterlms' ), $posted_value ) );
        }
 
        return true;
 
    }
 
    /**
     * Validate a url field
     *
     * @since 5.0.0
     *
     * @param string $posted_value User-submitted (dirty) value.
     * @return WP_Error|boolean Returns `true` for a valid submission, otherwise an error.
     */
    protected function validate_field_url( $posted_value ) {
 
        if ( ! filter_var( $posted_value, FILTER_VALIDATE_URL ) ) {
            // Translators: %s = user submitted value.
            return new WP_Error( 'llms-form-field-invalid', sprintf( __( 'The URL "%s" is not valid.', 'lifterlms' ), $posted_value ) );
        }
 
        return true;
 
    }
 
    /**
     * Validate a user-email field
     *
     * User emails must be unique.
     *
     * @since 5.0.0
     *
     * @param string $posted_value User-submitted (dirty) value.
     * @return WP_Error|boolean Returns `true` for a valid submission, otherwise an error.
     */
    protected function validate_field_user_email( $posted_value ) {
        if ( email_exists( $posted_value ) ) {
            return new WP_Error( 'llms-form-field-not-unique', sprintf( __( 'An account with the email address "%s" already exists.', 'lifterlms' ), $posted_value ) );
        }
 
        return true;
    }
 
    /**
     * Validate a user-login field
     *
     * Ensures that a username isn't found in the LifterLMS username blocklist, that it meets the default
     * WP core username criteria and that the username doesn't already exist.
     *
     * @since 5.0.0
     *
     * @param string $posted_value User-submitted (dirty) value.
     * @return WP_Error|boolean Returns `true` for a valid submission, otherwise an error.
     */
    protected function validate_field_user_login( $posted_value ) {
        if ( in_array( $posted_value, llms_get_usernames_blocklist(), true ) || ! validate_username( $posted_value ) ) {
            return new WP_Error( 'llms-form-field-invalid', sprintf( __( 'The username "%s" is invalid, please try a different username.', 'lifterlms' ), $posted_value ), $posted_value );
        } elseif ( username_exists( $posted_value ) ) {
            return new WP_Error( 'llms-form-field-not-unique', sprintf( __( 'An account with the username "%s" already exists.', 'lifterlms' ), $posted_value ), $posted_value );
        }
 
        return true;
    }
 
    /**
     * Validate a voucher field ensuring it's a valid and usable voucher code
     *
     * @since 5.0.0
     *
     * @param string $posted_value User-submitted (dirty) value.
     * @return WP_Error|boolean Returns `true` for a valid submission, otherwise an error.
     */
    protected function validate_field_voucher( $posted_value ) {
 
        $voucher = new LLMS_Voucher();
        $check   = $voucher->check_voucher( $posted_value );
        if ( is_wp_error( $check ) ) {
            return new WP_Error( 'llms-form-field-invalid', $check->get_error_message(), array( $posted_value, $check ) );
        }
 
        return true;
 
    }
 
    /**
     * Validate submitted field values.
     *
     * @since 5.0.0
     * @since 5.1.0 Don't validate form with no user input only if the form is not empty itself (e.g. contains only invisible fields).
     *
     * @param array   $posted_data Array of posted data.
     * @param array[] $fields      Array of LifterLMS Form Fields.
     * @return WP_Error|true
     */
    public function validate_fields( $posted_data, $fields ) {
 
        if ( empty( $posted_data ) && ! empty( $fields ) ) {
            return new WP_Error( 'llms-form-no-input', __( 'Cannot validate a form with no user input.', 'lifterlms' ) );
        }
 
        $err      = new WP_Error();
        $err_data = array();
        foreach ( $fields as $field ) {
 
            if ( empty( $field['name'] ) || empty( $posted_data[ $field['name'] ] ) ) {
                continue;
            }
 
            $valid = $this->validate_field( $posted_data[ $field['name'] ], $field );
            if ( is_wp_error( $valid ) ) {
                $err->add( $valid->get_error_code(), $valid->get_error_message() );
                $err_data[ $field['name'] ] = $field;
            }
        }
 
        if ( $err->errors ) {
            $err->add_data( $err_data );
            return $err;
        }
 
        return true;
 
    }
 
    /**
     * Ensure matching fields match one another.
     *
     * @since 5.0.0
     *
     * @param array   $posted_data Array of posted data.
     * @param array[] $fields      Array of LifterLMS form fields.
     * @return WP_Error|true
     */
    public function validate_matching_fields( $posted_data, $fields ) {
 
        $err      = new WP_Error();
        $err_data = array();
 
        $matches = array();
        foreach ( $fields as $field ) {
 
            // Field doesn't have a match to check or it was already checked by it's match.
            if ( empty( $field['match'] ) || in_array( $field['id'], $matches, true ) ) {
                continue;
            }
 
            $field_name = isset( $field['label'] ) ? $field['label'] : $field['name'];
 
            $name        = $field['name'];
            $match_field = LLMS_Forms::instance()->get_field_by( $fields, 'id', $field['match'] );
            if ( ! $match_field ) {
                continue;
            }
 
            $match = $match_field['name'];
 
            $val   = isset( $posted_data[ $name ] ) ? $posted_data[ $name ] : '';
            $match = isset( $posted_data[ $match ] ) ? $posted_data[ $match ] : '';
 
            if ( $val !== $match ) {
 
                $match_name = isset( $match_field['label'] ) ? $match_field['label'] : $match_field['name'];
                $err->add( 'llms-form-field-not-matched', sprintf( __( '%1$s must match %2$s.', 'lifterlms' ), $field_name, $match_name ) );
                $err_data[] = array( $field, $match_field );
 
            }
 
            // Fields reference each other so we only need to check the pair one time.
            $matches[] = $match_field['id'];
 
        }
 
        if ( $err->errors ) {
            $err->add_data( $err_data, 'llms-form-field-not-matched' );
            return $err;
        }
 
        return true;
 
    }
 
    /**
     * Ensure that all of the forms required fields are present in the submitted data.
     *
     * @since 5.0.0
     *
     * @param array   $posted_data User data (likely from $_POST).
     * @param array[] $fields      Array of LifterLMS form fields.
     * @return WP_Error|true
     */
    public function validate_required_fields( $posted_data, $fields ) {
 
        // Ensure all required fields have been submitted.
        $err      = new WP_Error();
        $err_data = array();
        foreach ( $this->get_required_fields( $fields ) as $field ) {
 
            if ( empty( $posted_data[ $field['name'] ] ) ) {
                // Translators: %s = field label or name.
                $err->add( 'llms-form-missing-required', sprintf( __( '%s is a required field.', 'lifterlms' ), isset( $field['label'] ) ? $field['label'] : $field['name'] ) );
                $err_data[ $field['name'] ] = $field;
            }
        }
 
        if ( $err->errors ) {
            $err->add_data( $err_data, 'llms-form-missing-required' );
            return $err;
        }
 
        return true;
 
    }
 
}

Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
5.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.