1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
4
|
|
|
exit; // Exit if accessed directly |
5
|
|
|
} |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Contains Validation functions |
9
|
|
|
* |
10
|
|
|
* @class WC_Validation |
11
|
|
|
* @version 2.4.0 |
12
|
|
|
* @package WooCommerce/Classes |
13
|
|
|
* @category Class |
14
|
|
|
* @author WooThemes |
15
|
|
|
*/ |
16
|
|
|
class WC_Validation { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Validates an email using wordpress native is_email function. |
20
|
|
|
* |
21
|
|
|
* @param string email address |
22
|
|
|
* @return bool |
23
|
|
|
*/ |
24
|
|
|
public static function is_email( $email ) { |
25
|
|
|
return is_email( $email ); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Validates a phone number using a regular expression. |
30
|
|
|
* |
31
|
|
|
* @param string phone number |
32
|
|
|
* @return bool |
33
|
|
|
*/ |
34
|
|
|
public static function is_phone( $phone ) { |
35
|
|
|
if ( 0 < strlen( trim( preg_replace( '/[\s\#0-9_\-\+\(\)]/', '', $phone ) ) ) ) { |
36
|
|
|
return false; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Checks for a valid postcode. |
44
|
|
|
* |
45
|
|
|
* @param string postcode |
46
|
|
|
* @param string country |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
public static function is_postcode( $postcode, $country ) { |
50
|
|
|
if ( strlen( trim( preg_replace( '/[\s\-A-Za-z0-9]/', '', $postcode ) ) ) > 0 ) { |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
switch ( $country ) { |
55
|
|
|
case 'BR' : |
56
|
|
|
$valid = (bool) preg_match( '/^([0-9]{5})([-])?([0-9]{3})$/', $postcode ); |
57
|
|
|
break; |
58
|
|
|
case 'CH' : |
59
|
|
|
$valid = (bool) preg_match( '/^([0-9]{4})$/i', $postcode ); |
60
|
|
|
break; |
61
|
|
|
case 'DE' : |
62
|
|
|
$valid = (bool) preg_match( '/^([0]{1}[1-9]{1}|[1-9]{1}[0-9]{1})[0-9]{3}$/', $postcode ); |
63
|
|
|
break; |
64
|
|
|
case 'GB' : |
65
|
|
|
$valid = self::is_GB_postcode( $postcode ); |
66
|
|
|
break; |
67
|
|
|
case 'PT' : |
68
|
|
|
$valid = (bool) preg_match( '/^([0-9]{4})([-])([0-9]{3})$/', $postcode ); |
69
|
|
|
break; |
70
|
|
|
case 'US' : |
71
|
|
|
$valid = (bool) preg_match( '/^([0-9]{5})(-[0-9]{4})?$/i', $postcode ); |
72
|
|
|
break; |
73
|
|
|
case 'CA' : |
74
|
|
|
// 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 |
75
|
|
|
$valid = (bool) preg_match( '/^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])([\ ])?(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$/i', $postcode ); |
76
|
|
|
break; |
77
|
|
|
|
78
|
|
|
default : |
79
|
|
|
$valid = true; |
80
|
|
|
break; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return apply_filters( 'woocommerce_validate_postcode', $valid, $postcode, $country ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Check if is a GB postcode. |
88
|
|
|
* |
89
|
|
|
* @author John Gardner |
90
|
|
|
* @param string $to_check A postcode |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
public static function is_GB_postcode( $to_check ) { |
94
|
|
|
|
95
|
|
|
// Permitted letters depend upon their position in the postcode. |
96
|
|
|
// http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Validation |
97
|
|
|
$alpha1 = "[abcdefghijklmnoprstuwyz]"; // Character 1 |
98
|
|
|
$alpha2 = "[abcdefghklmnopqrstuvwxy]"; // Character 2 |
99
|
|
|
$alpha3 = "[abcdefghjkpstuw]"; // Character 3 == ABCDEFGHJKPSTUW |
100
|
|
|
$alpha4 = "[abehmnprvwxy]"; // Character 4 == ABEHMNPRVWXY |
101
|
|
|
$alpha5 = "[abdefghjlnpqrstuwxyz]"; // Character 5 != CIKMOV |
102
|
|
|
|
103
|
|
|
$pcexp = array(); |
104
|
|
|
|
105
|
|
|
// Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA |
106
|
|
|
$pcexp[0] = '/^('.$alpha1.'{1}'.$alpha2.'{0,1}[0-9]{1,2})([0-9]{1}'.$alpha5.'{2})$/'; |
107
|
|
|
|
108
|
|
|
// Expression for postcodes: ANA NAA |
109
|
|
|
$pcexp[1] = '/^('.$alpha1.'{1}[0-9]{1}'.$alpha3.'{1})([0-9]{1}'.$alpha5.'{2})$/'; |
110
|
|
|
|
111
|
|
|
// Expression for postcodes: AANA NAA |
112
|
|
|
$pcexp[2] = '/^('.$alpha1.'{1}'.$alpha2.'[0-9]{1}'.$alpha4.')([0-9]{1}'.$alpha5.'{2})$/'; |
113
|
|
|
|
114
|
|
|
// Exception for the special postcode GIR 0AA |
115
|
|
|
$pcexp[3] = '/^(gir)(0aa)$/'; |
116
|
|
|
|
117
|
|
|
// Standard BFPO numbers |
118
|
|
|
$pcexp[4] = '/^(bfpo)([0-9]{1,4})$/'; |
119
|
|
|
|
120
|
|
|
// c/o BFPO numbers |
121
|
|
|
$pcexp[5] = '/^(bfpo)(c\/o[0-9]{1,3})$/'; |
122
|
|
|
|
123
|
|
|
// Load up the string to check, converting into lowercase and removing spaces |
124
|
|
|
$postcode = strtolower( $to_check ); |
125
|
|
|
$postcode = str_replace( ' ', '', $postcode ); |
126
|
|
|
|
127
|
|
|
// Assume we are not going to find a valid postcode |
128
|
|
|
$valid = false; |
129
|
|
|
|
130
|
|
|
// Check the string against the six types of postcodes |
131
|
|
|
foreach ( $pcexp as $regexp ) { |
132
|
|
|
if ( preg_match( $regexp, $postcode, $matches ) ) { |
133
|
|
|
// Remember that we have found that the code is valid and break from loop |
134
|
|
|
$valid = true; |
135
|
|
|
break; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $valid; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Format the postcode according to the country and length of the postcode. |
144
|
|
|
* |
145
|
|
|
* @param string postcode |
146
|
|
|
* @param string country |
147
|
|
|
* @return string formatted postcode |
148
|
|
|
*/ |
149
|
|
|
public static function format_postcode( $postcode, $country ) { |
150
|
|
|
return wc_format_postcode( $postcode, $country ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* format_phone function. |
155
|
|
|
* |
156
|
|
|
* @access public |
157
|
|
|
* @param mixed $tel |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
public static function format_phone( $tel ) { |
161
|
|
|
return wc_format_phone_number( $tel ); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.