PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 2.0.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v2.0.0
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / includes / utils / validation.php

validation.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 2.0.0, at includes/utils/validation.php

188 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Validation utilities
4 */
5
6 namespace StoreEngine\Utils;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 class Validation {
13
14 /**
15 * Validates an email using WordPress native is_email function.
16 *
17 * @param string $email Email address to validate.
18 *
19 * @return bool
20 */
21 public static function is_email( string $email ): bool {
22 return is_email( $email );
23 }
24
25 /**
26 * Validates a phone number using a regular expression.
27 *
28 * @param ?string $phone Phone number to validate.
29 *
30 * @return bool
31 */
32 public static function is_phone( ?string $phone ): bool {
33 if ( 0 < strlen( trim( preg_replace( '/[\s\#0-9_\-\+\/\(\)\.]/', '', $phone ?? '' ) ) ) ) {
34 return false;
35 }
36
37 return true;
38 }
39
40 /**
41 * Checks for a valid postcode.
42 *
43 * @param ?string $postcode Postcode to validate.
44 * @param string $country Country to validate the postcode for.
45 *
46 * @return bool
47 */
48 public static function is_postcode( ?string $postcode, string $country ): bool {
49 $postcode = $postcode ?? '';
50
51 if ( strlen( trim( preg_replace( '/[\s\-A-Za-z0-9]/', '', $postcode ) ) ) > 0 ) {
52 return false;
53 }
54
55 switch ( $country ) {
56 case 'AT':
57 case 'BE':
58 case 'CH':
59 case 'HU':
60 case 'NO':
61 $valid = (bool) preg_match( '/^([0-9]{4})$/', $postcode );
62 break;
63 case 'BA':
64 $valid = (bool) preg_match( '/^([7-8]{1})([0-9]{4})$/', $postcode );
65 break;
66 case 'BR':
67 $valid = (bool) preg_match( '/^([0-9]{5})([-])?([0-9]{3})$/', $postcode );
68 break;
69 case 'DE':
70 $valid = (bool) preg_match( '/^([0]{1}[1-9]{1}|[1-9]{1}[0-9]{1})[0-9]{3}$/', $postcode );
71 break;
72 case 'DK':
73 $valid = (bool) preg_match( '/^(DK-)?([1-24-9]\d{3}|3[0-8]\d{2})$/', $postcode );
74 break;
75 case 'ES':
76 case 'FI':
77 case 'EE':
78 case 'FR':
79 case 'IT':
80 $valid = (bool) preg_match( '/^([0-9]{5})$/i', $postcode );
81 break;
82 case 'GB':
83 $valid = self::is_gb_postcode( $postcode );
84 break;
85 case 'IE':
86 $valid = (bool) preg_match( '/([AC-FHKNPRTV-Y]\d{2}|D6W)[0-9AC-FHKNPRTV-Y]{4}/', Formatting::normalize_postcode( $postcode ) );
87 break;
88 case 'IN':
89 $valid = (bool) preg_match( '/^[1-9]{1}[0-9]{2}\s{0,1}[0-9]{3}$/', $postcode );
90 break;
91 case 'JP':
92 $valid = (bool) preg_match( '/^([0-9]{3})([-]?)([0-9]{4})$/', $postcode );
93 break;
94 case 'PT':
95 $valid = (bool) preg_match( '/^([0-9]{4})([-])([0-9]{3})$/', $postcode );
96 break;
97 case 'PR':
98 case 'US':
99 $valid = (bool) preg_match( '/^([0-9]{5})(-[0-9]{4})?$/i', $postcode );
100 break;
101 case 'CA':
102 // 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.
103 $valid = (bool) preg_match( '/^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])([\ ])?(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$/i', $postcode );
104 break;
105 case 'PL':
106 $valid = (bool) preg_match( '/^([0-9]{2})([-])([0-9]{3})$/', $postcode );
107 break;
108 case 'CZ':
109 case 'SE':
110 case 'SK':
111 $valid = (bool) preg_match( "/^($country-)?([0-9]{3})(\s?)([0-9]{2})$/", $postcode );
112 break;
113 case 'NL':
114 $valid = (bool) preg_match( '/^([1-9][0-9]{3})(\s?)(?!SA|SD|SS)[A-Z]{2}$/i', $postcode );
115 break;
116 case 'SI':
117 $valid = (bool) preg_match( '/^([1-9][0-9]{3})$/', $postcode );
118 break;
119 case 'LI':
120 $valid = (bool) preg_match( '/^(94[8-9][0-9])$/', $postcode );
121 break;
122 default:
123 $valid = true;
124 break;
125 }
126
127 return apply_filters( 'storeengine/validate_postcode', $valid, $postcode, $country );
128 }
129
130 /**
131 * Check if is a GB postcode.
132 *
133 * @param string $to_check A postcode.
134 *
135 * @return bool
136 */
137 public static function is_gb_postcode( string $to_check ): bool {
138
139 // Permitted letters depend upon their position in the postcode.
140 // https://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Validation.
141 $alpha1 = '[abcdefghijklmnoprstuwyz]'; // Character 1.
142 $alpha2 = '[abcdefghklmnopqrstuvwxy]'; // Character 2.
143 $alpha3 = '[abcdefghjkpstuw]'; // Character 3 == ABCDEFGHJKPSTUW.
144 $alpha4 = '[abehmnprvwxy]'; // Character 4 == ABEHMNPRVWXY.
145 $alpha5 = '[abdefghjlnpqrstuwxyz]'; // Character 5 != CIKMOV.
146
147 $pcexp = array();
148
149 // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA.
150 $pcexp[0] = '/^(' . $alpha1 . '{1}' . $alpha2 . '{0,1}[0-9]{1,2})([0-9]{1}' . $alpha5 . '{2})$/';
151
152 // Expression for postcodes: ANA NAA.
153 $pcexp[1] = '/^(' . $alpha1 . '{1}[0-9]{1}' . $alpha3 . '{1})([0-9]{1}' . $alpha5 . '{2})$/';
154
155 // Expression for postcodes: AANA NAA.
156 $pcexp[2] = '/^(' . $alpha1 . '{1}' . $alpha2 . '[0-9]{1}' . $alpha4 . ')([0-9]{1}' . $alpha5 . '{2})$/';
157
158 // Exception for the special postcode GIR 0AA.
159 $pcexp[3] = '/^(gir)(0aa)$/';
160
161 // Standard BFPO numbers.
162 $pcexp[4] = '/^(bfpo)([0-9]{1,4})$/';
163
164 // c/o BFPO numbers.
165 $pcexp[5] = '/^(bfpo)(c\/o[0-9]{1,3})$/';
166
167 // Load up the string to check, converting into lowercase and removing spaces.
168 $postcode = strtolower( $to_check );
169 $postcode = str_replace( ' ', '', $postcode );
170
171 // Assume we are not going to find a valid postcode.
172 $valid = false;
173
174 // Check the string against the six types of postcodes.
175 foreach ( $pcexp as $regexp ) {
176 if ( preg_match( $regexp, $postcode, $matches ) ) {
177 // Remember that we have found that the code is valid and break from loop.
178 $valid = true;
179 break;
180 }
181 }
182
183 return $valid;
184 }
185 }
186
187 // End of file validation.php
188