Merge pull request #22417 from woocommerce/fix/21175
Sort countries using new ascii comparison function
This commit is contained in:
commit
380201be33
|
@ -50,7 +50,7 @@ class WC_Countries {
|
|||
if ( empty( $this->countries ) ) {
|
||||
$this->countries = apply_filters( 'woocommerce_countries', include WC()->plugin_path() . '/i18n/countries.php' );
|
||||
if ( apply_filters( 'woocommerce_sort_countries', true ) ) {
|
||||
asort( $this->countries );
|
||||
uasort( $this->countries, 'wc_ascii_uasort_comparison' );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1558,6 +1558,21 @@ function wc_uasort_comparison( $a, $b ) {
|
|||
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.
|
||||
*
|
||||
|
|
|
@ -871,4 +871,26 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case {
|
|||
uasort( $fields, 'wc_checkout_fields_uasort_comparison' );
|
||||
$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 ) );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue