Merge pull request #12926 from woocommerce/fix-12910

Use mb_ functions in wc_get_wildcard_postcodes
This commit is contained in:
Claudio Sanches 2017-01-23 15:48:59 -02:00 committed by GitHub
commit 09123c9fc8
3 changed files with 18 additions and 3 deletions

View File

@ -1261,7 +1261,7 @@ function wc_help_tip( $tip, $allow_html = false ) {
*/
function wc_get_wildcard_postcodes( $postcode, $country = '' ) {
$formatted_postcode = wc_format_postcode( $postcode, $country );
$length = strlen( $formatted_postcode );
$length = function_exists( 'mb_strlen' ) ? mb_strlen( $formatted_postcode ) : strlen( $formatted_postcode );
$postcodes = array(
$postcode,
$formatted_postcode,
@ -1269,7 +1269,7 @@ function wc_get_wildcard_postcodes( $postcode, $country = '' ) {
);
for ( $i = 0; $i < $length; $i ++ ) {
$postcodes[] = substr( $formatted_postcode, 0, ( $i + 1 ) * -1 ) . '*';
$postcodes[] = ( function_exists( 'mb_substr' ) ? mb_substr( $formatted_postcode, 0, ( $i + 1 ) * -1 ) : substr( $formatted_postcode, 0, ( $i + 1 ) * -1 ) ) . '*';
}
return $postcodes;

View File

@ -758,7 +758,8 @@ function wc_format_postcode( $postcode, $country ) {
* @return string Sanitized postcode.
*/
function wc_normalize_postcode( $postcode ) {
return preg_replace( '/[\s\-]/', '', trim( strtoupper( $postcode ) ) );
$postcode = function_exists( 'mb_strtoupper' ) ? mb_strtoupper( $postcode ) : strtoupper( $postcode );
return preg_replace( '/[\s\-]/', '', trim( $postcode ) );
}
/**

View File

@ -395,4 +395,18 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case {
return array_slice( $alternatives, $skip++ );
}
/**
* Test wc_get_wildcard_postcodes
*/
public function test_wc_get_wildcard_postcodes() {
$postcode = 'cb23 6as';
$country = 'GB';
$wildcards = array( 'cb23 6as', 'CB23 6AS', 'CB23 6AS*', 'CB23 6A*', 'CB23 6*', 'CB23 *', 'CB23*', 'CB2*', 'CB*', 'C*', '*' );
$this->assertEquals( $wildcards, wc_get_wildcard_postcodes( $postcode, $country ) );
$postcode = 'GIJóN';
$country = '';
$wildcards = array( 'GIJóN', 'GIJÓN', 'GIJÓN*', 'GIJÓ*', 'GIJ*', 'GI*', 'G*', '*' );
$this->assertEquals( $wildcards, wc_get_wildcard_postcodes( $postcode, $country ) );
}
}