Merge pull request #22417 from woocommerce/fix/21175

Sort countries using new ascii comparison function
This commit is contained in:
Mike Jolley 2019-01-22 14:47:41 +00:00 committed by GitHub
commit 380201be33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 1 deletions

View File

@ -50,7 +50,7 @@ class WC_Countries {
if ( empty( $this->countries ) ) { if ( empty( $this->countries ) ) {
$this->countries = apply_filters( 'woocommerce_countries', include WC()->plugin_path() . '/i18n/countries.php' ); $this->countries = apply_filters( 'woocommerce_countries', include WC()->plugin_path() . '/i18n/countries.php' );
if ( apply_filters( 'woocommerce_sort_countries', true ) ) { if ( apply_filters( 'woocommerce_sort_countries', true ) ) {
asort( $this->countries ); uasort( $this->countries, 'wc_ascii_uasort_comparison' );
} }
} }

View File

@ -1558,6 +1558,21 @@ function wc_uasort_comparison( $a, $b ) {
return ( $a < $b ) ? -1 : 1; return ( $a < $b ) ? -1 : 1;
} }
/**
* Sort values based on ascii, usefull for special chars in strings.
*
* @param string $a First value.
* @param string $b Second value.
* @return int
*/
function wc_ascii_uasort_comparison( $a, $b ) {
if ( function_exists( 'iconv' ) ) {
$a = iconv( 'UTF-8', 'ASCII//TRANSLIT', $a );
$b = iconv( 'UTF-8', 'ASCII//TRANSLIT', $b );
}
return strcmp( $a, $b );
}
/** /**
* Get rounding mode for internal tax calculations. * Get rounding mode for internal tax calculations.
* *

View File

@ -871,4 +871,26 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case {
uasort( $fields, 'wc_checkout_fields_uasort_comparison' ); uasort( $fields, 'wc_checkout_fields_uasort_comparison' );
$this->assertSame( 0, array_search( 'billing_email', array_keys( $fields ) ) ); $this->assertSame( 0, array_search( 'billing_email', array_keys( $fields ) ) );
} }
/**
* Test wc_ascii_uasort_comparison function
*
* @return void
*/
public function test_wc_ascii_uasort_comparison() {
$unsorted_values = array(
'Benin',
'Bélgica',
);
// First test a normal asort which does not work right for accented characters.
$sorted_values = $unsorted_values;
asort( $sorted_values );
$this->assertSame( array( 'Benin', 'Bélgica' ), array_values( $sorted_values ) );
$sorted_values = $unsorted_values;
// Now test the new wc_ascii_uasort_comparison function which sorts the strings correctly.
uasort( $sorted_values, 'wc_ascii_uasort_comparison' );
$this->assertSame( array( 'Bélgica', 'Benin' ), array_values( $sorted_values ) );
}
} }