PluginProbe
WooCommerce / 11.1.0-rc.2
WooCommerce v11.1.0-rc.2
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / includes / class-wc-validation.php
class-wc-validation.php
230 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * General user data validation methods
4 *
5 * @package WooCommerce\Classes
6 * @version 2.4.0
7 */
8
9 defined( 'ABSPATH' ) || exit;
10
11 /**
12 * Validation class.
13 */
14 class WC_Validation {
15
16 /**
17 * Validates an email using WordPress native is_email function.
18 *
19 * @param string $email Email address to validate.
20 * @return bool
21 */
22 public static function is_email( $email ) {
23 return is_email( $email );
24 }
25
26 /**
27 * Checks whether a string has the basic shape of a phone number, i.e. it contains
28 * only digits and characters commonly used in phone numbers (whitespace and the
29 * "# _ - + / ( ) ." characters).
30 *
31 * Unlike `is_phone`, this method doesn't apply the `woocommerce_validate_phone` filter,
32 * so its result always reflects the default validation rules regardless of any
33 * merchant-defined validation policy. It's intended for contexts that need a
34 * country-agnostic sanity check, such as phone number formatting.
35 *
36 * @since 11.0.0
37 *
38 * @param string $phone Phone number to check.
39 * @return bool
40 */
41 public static function is_phone_format( $phone ): bool {
42 return '' === trim( preg_replace( '/[\s\#0-9_\-\+\/\(\)\.]/', '', (string) $phone ) );
43 }
44
45 /**
46 * Validates a phone number using a regular expression.
47 *
48 * @param string $phone Phone number to validate.
49 * @param string|null $country The country code the phone is being validated for, or null if unknown.
50 * @return bool
51 */
52 public static function is_phone( $phone, $country = null ) {
53 $valid = self::is_phone_format( $phone );
54
55 /**
56 * Filters whether a phone number is considered valid.
57 *
58 * @since 11.0.0
59 *
60 * @param bool $valid Whether the phone number passed the default validation.
61 * @param string $phone The phone number being validated.
62 * @param string|null $country The country code the phone is being validated for, or null if unknown.
63 */
64 return (bool) apply_filters( 'woocommerce_validate_phone', $valid, $phone, $country );
65 }
66
67 /**
68 * Checks for a valid postcode.
69 *
70 * @param string $postcode Postcode to validate.
71 * @param string $country Country to validate the postcode for.
72 * @return bool
73 */
74 public static function is_postcode( $postcode, $country ) {
75 if ( strlen( trim( preg_replace( '/[\s\-A-Za-z0-9]/', '', $postcode ) ) ) > 0 ) {
76 return false;
77 }
78
79 switch ( $country ) {
80 case 'AT':
81 case 'BE':
82 case 'CH':
83 case 'HU':
84 case 'NO':
85 $valid = (bool) preg_match( '/^([0-9]{4})$/', $postcode );
86 break;
87 case 'BA':
88 $valid = (bool) preg_match( '/^([7-8]{1})([0-9]{4})$/', $postcode );
89 break;
90 case 'BR':
91 $valid = (bool) preg_match( '/^([0-9]{5})([-])?([0-9]{3})$/', $postcode );
92 break;
93 case 'DE':
94 $valid = (bool) preg_match( '/^([0]{1}[1-9]{1}|[1-9]{1}[0-9]{1})[0-9]{3}$/', $postcode );
95 break;
96 case 'DK':
97 $valid = (bool) preg_match( '/^(DK-)?([1-24-9]\d{3}|3[0-8]\d{2})$/', $postcode );
98 break;
99 case 'ES':
100 case 'FI':
101 case 'EE':
102 case 'FR':
103 case 'IT':
104 $valid = (bool) preg_match( '/^([0-9]{5})$/i', $postcode );
105 break;
106 case 'GB':
107 $valid = self::is_gb_postcode( $postcode );
108 break;
109 case 'IE':
110 $valid = (bool) preg_match( '/([AC-FHKNPRTV-Y]\d{2}|D6W)[0-9AC-FHKNPRTV-Y]{4}/', wc_normalize_postcode( $postcode ) );
111 break;
112 case 'IN':
113 $valid = (bool) preg_match( '/^[1-9]{1}[0-9]{2}\s{0,1}[0-9]{3}$/', $postcode );
114 break;
115 case 'JP':
116 $valid = (bool) preg_match( '/^([0-9]{3})([-]?)([0-9]{4})$/', $postcode );
117 break;
118 case 'PT':
119 $valid = (bool) preg_match( '/^([0-9]{4})([-])([0-9]{3})$/', $postcode );
120 break;
121 case 'PR':
122 case 'US':
123 $valid = (bool) preg_match( '/^([0-9]{5})(-[0-9]{4})?$/i', $postcode );
124 break;
125 case 'CA':
126 // CA Postal codes cannot contain D,F,I,O,Q,U and cannot start with W or Z. https://en.wikipedia.org/wiki/Postal_codes_in_Canada#Number_of_possible_postal_codes.
127 $valid = (bool) preg_match( '/^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])([\ ])?(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$/i', $postcode );
128 break;
129 case 'PL':
130 $valid = (bool) preg_match( '/^([0-9]{2})([-])([0-9]{3})$/', $postcode );
131 break;
132 case 'CZ':
133 case 'SE':
134 case 'SK':
135 $valid = (bool) preg_match( "/^($country-)?([0-9]{3})(\s?)([0-9]{2})$/", $postcode );
136 break;
137 case 'NL':
138 $valid = (bool) preg_match( '/^([1-9][0-9]{3})(\s?)(?!SA|SD|SS)[A-Z]{2}$/i', $postcode );
139 break;
140 case 'SI':
141 $valid = (bool) preg_match( '/^([1-9][0-9]{3})$/', $postcode );
142 break;
143 case 'LI':
144 $valid = (bool) preg_match( '/^(94[8-9][0-9])$/', $postcode );
145 break;
146 default:
147 $valid = true;
148 break;
149 }
150
151 return apply_filters( 'woocommerce_validate_postcode', $valid, $postcode, $country );
152 }
153
154 /**
155 * Check if is a GB postcode.
156 *
157 * @param string $to_check A postcode.
158 * @return bool
159 */
160 public static function is_gb_postcode( $to_check ) {
161
162 // Permitted letters depend upon their position in the postcode.
163 // https://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Validation.
164 $alpha1 = '[abcdefghijklmnoprstuwyz]'; // Character 1.
165 $alpha2 = '[abcdefghklmnopqrstuvwxy]'; // Character 2.
166 $alpha3 = '[abcdefghjkpstuw]'; // Character 3 == ABCDEFGHJKPSTUW.
167 $alpha4 = '[abehmnprvwxy]'; // Character 4 == ABEHMNPRVWXY.
168 $alpha5 = '[abdefghjlnpqrstuwxyz]'; // Character 5 != CIKMOV.
169
170 $pcexp = array();
171
172 // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA.
173 $pcexp[0] = '/^(' . $alpha1 . '{1}' . $alpha2 . '{0,1}[0-9]{1,2})([0-9]{1}' . $alpha5 . '{2})$/';
174
175 // Expression for postcodes: ANA NAA.
176 $pcexp[1] = '/^(' . $alpha1 . '{1}[0-9]{1}' . $alpha3 . '{1})([0-9]{1}' . $alpha5 . '{2})$/';
177
178 // Expression for postcodes: AANA NAA.
179 $pcexp[2] = '/^(' . $alpha1 . '{1}' . $alpha2 . '[0-9]{1}' . $alpha4 . ')([0-9]{1}' . $alpha5 . '{2})$/';
180
181 // Exception for the special postcode GIR 0AA.
182 $pcexp[3] = '/^(gir)(0aa)$/';
183
184 // Standard BFPO numbers.
185 $pcexp[4] = '/^(bfpo)([0-9]{1,4})$/';
186
187 // c/o BFPO numbers.
188 $pcexp[5] = '/^(bfpo)(c\/o[0-9]{1,3})$/';
189
190 // Load up the string to check, converting into lowercase and removing spaces.
191 $postcode = strtolower( $to_check );
192 $postcode = str_replace( ' ', '', $postcode );
193
194 // Assume we are not going to find a valid postcode.
195 $valid = false;
196
197 // Check the string against the six types of postcodes.
198 foreach ( $pcexp as $regexp ) {
199 if ( preg_match( $regexp, $postcode, $matches ) ) {
200 // Remember that we have found that the code is valid and break from loop.
201 $valid = true;
202 break;
203 }
204 }
205
206 return $valid;
207 }
208
209 /**
210 * Format the postcode according to the country and length of the postcode.
211 *
212 * @param string $postcode Postcode to format.
213 * @param string $country Country to format the postcode for.
214 * @return string Formatted postcode.
215 */
216 public static function format_postcode( $postcode, $country ) {
217 return wc_format_postcode( $postcode, $country );
218 }
219
220 /**
221 * Format a given phone number.
222 *
223 * @param mixed $tel Phone number to format.
224 * @return string
225 */
226 public static function format_phone( $tel ) {
227 return wc_format_phone_number( $tel );
228 }
229 }
230