PluginProbe
Bookit — Booking & Appointment Calendar / 2.6.0.5
Bookit — Booking & Appointment Calendar v2.6.0.5
2.6.0.5 2.6.0.4 2.6.0.3 2.6.0.2 2.6.0.1 2.6.0 trunk 1.2 1.2.2 1.2.3 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 All 62 releases
bookit / includes / helpers / CleanHelper.php

CleanHelper.php in Bookit — Booking & Appointment Calendar 2.6.0.5, at includes/helpers/CleanHelper.php

113 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Bookit\Helpers;
4
5 /**
6 * Bookit Clean Helper
7 */
8
9
10 class CleanHelper {
11
12 /**
13 * @param array $data
14 * @param array $rules
15 *
16 * @return array|null[]
17 * Clean plugin data by rules for each field
18 */
19 public static function cleanData( array $data, array $rules ) {
20 $data = array_map(
21 function ( $value ) {
22 return ( 'null' == $value ) ? null : $value;
23 },
24 $data
25 );
26
27 foreach ( $data as $key => $value ) {
28 $value = sanitize_text_field( $value );
29
30 if ( ! array_key_exists( $key, $rules ) ) {
31 $data[ $key ] = $value;
32 continue;
33 }
34
35 // convert to correct type
36 if ( array_key_exists( 'type', $rules[ $key ] ) ) {
37 $setTypeFunction = $rules[ $key ]['type'];
38 $value = $setTypeFunction( $value );
39 }
40
41 // apply function to clean
42 if ( array_key_exists( 'function', $rules[ $key ] ) ) {
43
44 if ( ! $rules[ $key ]['function']['custom'] ) {
45 $value = $rules[ $key ]['function']['name']( $value );
46 }
47
48 if ( $rules[ $key ]['function']['custom']
49 && method_exists( self::class, $rules[ $key ]['function']['name'] ) ) {
50 $value = self::{$rules[ $key ]['function']['name']}( $value );
51 }
52 }
53 $data[ $key ] = $value;
54 }
55
56 return $data;
57 }
58
59 /**
60 * Custom phone number sanitization.
61 *
62 * If the phone is empty and was not empty before, return false to trigger
63 * validation error.
64 *
65 * @param string $phone The phone number to sanitize.
66 *
67 * @return string|false The sanitized phone number or false to trigger
68 * validation error.
69 */
70 protected static function custom_sanitize_phone( string $phone ) {
71 if ( ! $phone ) {
72 return $phone;
73 }
74
75 $has_phone = ! empty( $phone );
76
77 $phone = filter_var( $phone, FILTER_SANITIZE_NUMBER_INT );
78 $phone = str_replace( '-', '', $phone );
79
80 // Return false if the phone is empty and was not empty before to trigger validation error.
81 if (
82 $has_phone
83 && empty( $phone )
84 ) {
85 return false;
86 }
87
88 return $phone;
89 }
90
91 protected static function custom_sanitize_json( string $json ) {
92 return json_decode( stripslashes( $json ), true );
93 }
94
95 protected static function custom_sanitize_price( string $price ) {
96 return number_format( $price, 2, '.', '' );
97 }
98
99 /**
100 * @param string $ids
101 * check is string contain comma separated ids
102 */
103 protected static function custom_comma_separated_ids( string $ids_string ) {
104 $ids = array_filter(
105 explode( ',', $ids_string ),
106 function( $value ) {
107 return ! filter_var( $value, FILTER_VALIDATE_INT ) === false;
108 }
109 );
110 return implode( ',', $ids );
111 }
112 }
113