AnsPress_Validation()

Description #

AnsPress form validation class.

Changelog #

VersionDescription
2.0.1Introduced.

Source #

File: includes/class/validation.php

13
14
15
16
17
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
class AnsPress_Validation
{
    public $args = array();
 
    private $errors = array();
 
    private $fields = array();
 
    /**
     * Initialize the class
     * @param array $args
     */
    public function __construct($args = array()) {
 
        if ( empty( $args ) ) {
            return;
        }
 
        $this->args = $args;
 
        $this->name_to_key();
        $this->fields_to_include();
        $this->actions();
    }
 
    /**
     * Add name value as array key.
     * @since 3.0.0
     */
    private function name_to_key() {
        foreach ( (array) $this->args as $k => $f ) {
            if ( isset( $f['name'] ) ) {
                $name = $f['name'];
                unset( $f['name'] );
                unset( $this->args[ $k ] );
                $this->args[ $name ] = $f;
            }
        }
    }
 
    /**
     * Check fields to process
     * @return void
     * @since 2.0.1
     */
    private function fields_to_include() {
        foreach ( (array) $this->args as $field => $actions ) {
            $value = isset( $_REQUEST[ $field ] ) ? $_REQUEST[ $field ] : '';
            $this->fields[ $field ] = $value;
        }
    }
 
    /**
     * Check if field is empty or not set
     * @param  string $field
     * @return void
     * @since 2.0.1
     */
    public function required($field) {
        if ( ! isset( $this->fields[$field] ) || '' == $this->fields[ $field ] ) {
            $this->errors[ $field ] = __( 'This field is required', 'anspress-question-answer' );
        }
    }
 
    /**
     * Sanitize text fields
     * @param   string $field Field name.
     * @return  void
     * @since   2.0.1
     */
    private function sanitize_text_field( $field ) {
        if ( isset( $this->fields[ $field ] ) ) {
            $this->fields[ $field ] = sanitize_text_field( $this->fields[ $field ] );
        }
    }
 
    /**
     * Check length of a string, if less then specified then return error
     * @param  string $field
     * @param  string $param
     * @return void
     * @since  2.0
     */
    private function length_check( $field, $param ) {
        // Dont check if Administrator.
        if ( current_user_can( 'manage_options' ) ) {
            return;
        }
 
        if ( $param != 0 && ( ! isset( $this->fields[$field] ) || mb_strlen( strip_tags( $this->fields[$field] ) ) <= $param ) ) {
            $this->errors[$field] = sprintf( __( 'Field value should be at least %d characters', 'anspress-question-answer' ), $param );
        }
    }
 
    /**
     * Count comma separated strings
     * @param  string $field
     * @param  string $param
     * @return void
     * @since  2.0
     */
    private function comma_separted_count($field, $param) {
        if ( isset( $this->fields[$field] ) ) {
            $tags = $this->fields[$field];
 
            if ( ! is_array( $tags ) ) {
                $tags = explode( ',', $tags );
            }
 
            if ( count( $tags ) < $param ) {
                $this->errors[$field] = sprintf( __( 'It must be minimum %d characters', 'anspress-question-answer' ), $param );
            }
        } elseif ( $param > 0 ) {
            $this->errors[$field] = sprintf( __( 'It must be minimum %d characters', 'anspress-question-answer' ), $param );
        }
    }
 
    /**
     * @param string $field
     */
    private function is_email($field) {
 
        $email = is_email( $this->fields[$field] );
 
        if ( ! $email ) {
            $this->errors[$field] = __( 'Not a valid email address', 'anspress-question-answer' );
        } else {
            $this->fields[$field] = $email;
        }
    }
 
    /**
     * Sanitize as a boolean value
     * @param  string $field
     * @return void
     * @since 2.0.1
     */
    private function only_boolean($field) {
 
        $this->fields[$field] = (bool) $this->fields[$field];
 
    }
 
    /**
     * Sanitize as a integer value
     * @param  string $field
     * @return void
     * @since 2.0.1
     */
    private function only_int($field) {
 
        $this->fields[$field] = (int) $this->fields[$field];
 
    }
 
    /**
     * Sanitize field using wp_kses
     * @param  string $field
     * @return void
     * @since 2.0.1
     */
    private function wp_kses($field) {
        $this->fields[$field] = wp_kses( $this->fields[$field], ap_form_allowed_tags() );
    }
 
    /**
     * Sanitize field using wp_kses
     * @param  string $field
     * @return void
     * @since 2.0.1
     */
    private function sanitize_description($field) {
        $this->fields[$field] = ap_sanitize_description_field( $this->fields[$field] );
    }
 
    /**
     * Remove wordpress read more tag
     * @param  string $field
     * @return void
     * @since 2.0.1
     */
    private function remove_more($field) {
 
        $this->fields[$field] = str_replace( '<!--more-->', '', $this->fields[$field] );
    }
 
    /**
     * Stripe shortcode tags
     * @param  string $field
     * @return void
     * @since 2.0.1
     */
    private function strip_shortcodes($field) {
 
        $this->fields[$field] = strip_shortcodes( $this->fields[$field] );
    }
 
    /**
     * Encode contents inside pre and code tag
     * @param  string $field
     * @return void
     * @since 2.0.1
     */
    private function encode_pre_code($field) {
 
        $this->fields[$field] = preg_replace_callback( '/<pre.*?>(.*?)<\/pre>/imsu', array( $this, 'pre_content' ), $this->fields[$field] );
        $this->fields[$field] = preg_replace_callback( '/<code.*?>(.*?)<\/code>/imsu', array( $this, 'code_content' ), $this->fields[$field] );
    }
 
    private function pre_content($matches) {
 
        return '<pre>'.esc_html( $matches[1] ).'</pre>';
    }
 
    private function code_content($matches) {
 
        return '<code>'.esc_html( $matches[1] ).'</code>';
    }
 
    /**
     * Strip all tags
     * @param  string $field
     * @return void
     * @since  2.0
     */
    private function strip_tags($field) {
 
        $this->fields[$field] = strip_tags( $this->fields[$field] );
    }
 
    /**
     * Santitize tags field
     * @param  string $field
     * @return void
     * @since  2.0
     */
    private function sanitize_tags($field) {
 
        $this->fields[$field] = $this->fields[$field];
 
        $tags = $this->fields[$field];
 
        if ( ! is_array( $tags ) ) {
            $tags = explode( ',', $tags ); }
 
        $sanitized_tags = '';
 
        if ( is_array( $tags ) ) {
            $count = count( $tags );
            $i = 1;
            foreach ( $tags  as $tag ) {
                $sanitized_tags .= sanitize_text_field( $tag );
 
                if ( $count != $i ) {
                    $sanitized_tags .= ','; }
 
                $i++;
            }
        }
 
        $this->fields[$field] = $sanitized_tags;
    }
 
    /**
     * Sanitize field based on actions passed
     * @param  string $field
     * @param  array  $actions
     * @return void
     * @since 2.0.1
     */
    private function sanitize($field, $actions) {
 
        foreach ( $actions as $type ) {
            switch ( $type ) {
                case 'sanitize_text_field':
                    $this->sanitize_text_field( $field );
                    break;
 
                case 'only_boolean':
                    $this->only_boolean( $field );
                    break;
 
                case 'only_int':
                    $this->only_int( $field );
                    break;
 
                case 'wp_kses':
                    $this->wp_kses( $field );
                    break;
 
                case 'remove_more':
                    $this->remove_more( $field );
                    break;
 
                case 'strip_shortcodes':
                    $this->strip_shortcodes( $field );
                    break;
 
                case 'encode_pre_code':
                    $this->encode_pre_code( $field );
                    break;
 
                case 'strip_tags':
                    $this->strip_tags( $field );
                    break;
 
                case 'sanitize_tags':
                    $this->sanitize_tags( $field );
                    break;
 
                case 'is_email':
                    $this->is_email( $field );
                    break;
 
                case 'sanitize_description':
                    $this->sanitize_description( $field );
                    break;
 
                default:
                    $this->fields[$field] = apply_filters( 'ap_validation_sanitize_field', $field, $actions );
                    break;
            }
        }
    }
 
    /**
     * Validate a field based on actions passed
     * @param  string $field
     * @param  array  $actions
     * @return void
     * @since 2.0.1
     */
    private function validate($field, $actions) {
 
        foreach ( $actions as $type => $param ) {
            if ( isset( $this->errors[$field] ) ) {
                return; }
 
            switch ( $type ) {
                case 'required':
                    $this->required( $field );
                    break;
 
                case 'length_check':
                    $this->length_check( $field, $param );
                    break;
 
                case 'comma_separted_count':
                    $this->comma_separted_count( $field, $param );
                    break;
 
                case 'is_email':
                    $this->is_email( $field );
                    break;
 
                default:
                    $this->errors[$field] = apply_filters( 'ap_validation_validate_field', $field, $actions );
                    break;
            }
        }
    }
 
    /**
     * Append error to a field
     * @param  string $field  field name.
     * @param  string $errors  Error message.
     */
    private function append_errors($field, $errors) {
        $this->errors[$field] = $errors;
    }
 
    /**
     * Field is being checked and sanitized
     * @return void
     * @since 2.0.1
     */
    private function actions() {
 
        foreach ( (array) $this->args as $field => $actions ) {
            if ( isset( $actions['sanitize'] ) ) {
                $this->sanitize( $field, $actions['sanitize'] );
            }
 
            if ( isset( $actions['validate'] ) ) {
                $this->validate( $field, $actions['validate'] );
            }
 
            if ( isset( $actions['error'] ) ) {
                $this->append_errors( $field, $actions['error'] );
            }
        }
 
    }
 
    /**
     * Check if fields have any error
     * @return boolean
     * @since 2.0.1
     */
    public function have_error() {
        if ( count( $this->errors ) > 0 ) {
            return true; }
 
        return false;
    }
 
    /**
     * Get all errors
     * @return array | boolean
     */
    public function get_errors() {
        if ( count( $this->errors ) > 0 ) {
            return $this->errors;
        }
 
        return false;
    }
 
    /**
     * Return all sanitized fields
     * @return array
     * @since 2.0.1
     */
    public function get_sanitized_fields() {
 
        return $this->fields;
    }
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment