@@ 141-170 (lines=30) @@ | ||
138 | * @param string country code |
|
139 | * @return boolean |
|
140 | */ |
|
141 | public function is_valid_postcode( $postcode, $country ) { |
|
142 | $codes = $this->get_valid_postcodes(); |
|
143 | $postcode = $this->clean( $postcode ); |
|
144 | $formatted_postcode = wc_format_postcode( $postcode, $country ); |
|
145 | ||
146 | if ( in_array( $postcode, $codes ) || in_array( $formatted_postcode, $codes ) ) { |
|
147 | return true; |
|
148 | } |
|
149 | ||
150 | // Pattern matching |
|
151 | foreach ( $codes as $c ) { |
|
152 | $pattern = '/^' . str_replace( '_', '[0-9a-zA-Z]', preg_quote( $c ) ) . '$/i'; |
|
153 | if ( preg_match( $pattern, $postcode ) ) { |
|
154 | return true; |
|
155 | } |
|
156 | } |
|
157 | ||
158 | // Wildcard search |
|
159 | $wildcard_postcode = $formatted_postcode . '*'; |
|
160 | $postcode_length = strlen( $formatted_postcode ); |
|
161 | ||
162 | for ( $i = 0; $i < $postcode_length; $i++ ) { |
|
163 | if ( in_array( $wildcard_postcode, $codes ) ) { |
|
164 | return true; |
|
165 | } |
|
166 | $wildcard_postcode = substr( $wildcard_postcode, 0, -2 ) . '*'; |
|
167 | } |
|
168 | ||
169 | return false; |
|
170 | } |
|
171 | ||
172 | /** |
|
173 | * See if the method is available. |
@@ 106-135 (lines=30) @@ | ||
103 | * @param string country code |
|
104 | * @return boolean |
|
105 | */ |
|
106 | public function is_valid_postcode( $postcode, $country ) { |
|
107 | $codes = $this->get_valid_postcodes(); |
|
108 | $postcode = $this->clean( $postcode ); |
|
109 | $formatted_postcode = wc_format_postcode( $postcode, $country ); |
|
110 | ||
111 | if ( in_array( $postcode, $codes ) || in_array( $formatted_postcode, $codes ) ) { |
|
112 | return true; |
|
113 | } |
|
114 | ||
115 | // Pattern matching |
|
116 | foreach ( $codes as $c ) { |
|
117 | $pattern = '/^' . str_replace( '_', '[0-9a-zA-Z]', preg_quote( $c ) ) . '$/i'; |
|
118 | if ( preg_match( $pattern, $postcode ) ) { |
|
119 | return true; |
|
120 | } |
|
121 | } |
|
122 | ||
123 | // Wildcard search |
|
124 | $wildcard_postcode = $formatted_postcode . '*'; |
|
125 | $postcode_length = strlen( $formatted_postcode ); |
|
126 | ||
127 | for ( $i = 0; $i < $postcode_length; $i++ ) { |
|
128 | if ( in_array( $wildcard_postcode, $codes ) ) { |
|
129 | return true; |
|
130 | } |
|
131 | $wildcard_postcode = substr( $wildcard_postcode, 0, -2 ) . '*'; |
|
132 | } |
|
133 | ||
134 | return false; |
|
135 | } |
|
136 | ||
137 | /** |
|
138 | * See if the method is available. |