| @@ 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. |
|
| @@ 107-136 (lines=30) @@ | ||
| 104 | * @param string country code |
|
| 105 | * @return boolean |
|
| 106 | */ |
|
| 107 | public function is_valid_postcode( $postcode, $country ) { |
|
| 108 | $codes = $this->get_valid_postcodes(); |
|
| 109 | $postcode = $this->clean( $postcode ); |
|
| 110 | $formatted_postcode = wc_format_postcode( $postcode, $country ); |
|
| 111 | ||
| 112 | if ( in_array( $postcode, $codes ) || in_array( $formatted_postcode, $codes ) ) { |
|
| 113 | return true; |
|
| 114 | } |
|
| 115 | ||
| 116 | // Pattern matching |
|
| 117 | foreach ( $codes as $c ) { |
|
| 118 | $pattern = '/^' . str_replace( '_', '[0-9a-zA-Z]', preg_quote( $c ) ) . '$/i'; |
|
| 119 | if ( preg_match( $pattern, $postcode ) ) { |
|
| 120 | return true; |
|
| 121 | } |
|
| 122 | } |
|
| 123 | ||
| 124 | // Wildcard search |
|
| 125 | $wildcard_postcode = $formatted_postcode . '*'; |
|
| 126 | $postcode_length = strlen( $formatted_postcode ); |
|
| 127 | ||
| 128 | for ( $i = 0; $i < $postcode_length; $i++ ) { |
|
| 129 | if ( in_array( $wildcard_postcode, $codes ) ) { |
|
| 130 | return true; |
|
| 131 | } |
|
| 132 | $wildcard_postcode = substr( $wildcard_postcode, 0, -2 ) . '*'; |
|
| 133 | } |
|
| 134 | ||
| 135 | return false; |
|
| 136 | } |
|
| 137 | ||
| 138 | /** |
|
| 139 | * See if the method is available. |
|