Merge pull request #27416 from woocommerce/fix/27415

Fixed country list sorting
This commit is contained in:
Claudio Sanches 2020-09-17 10:35:43 -03:00 committed by GitHub
commit ed1a59b39b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 86 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 ) ) {
uasort( $this->countries, 'wc_ascii_uasort_comparison' ); wc_asort_by_locale( $this->countries );
} }
} }

View File

@ -1760,9 +1760,48 @@ function wc_ascii_uasort_comparison( $a, $b ) {
$b = @iconv( 'UTF-8', 'ASCII//TRANSLIT//IGNORE', $b ); $b = @iconv( 'UTF-8', 'ASCII//TRANSLIT//IGNORE', $b );
} }
// phpcs:enable WordPress.PHP.NoSilencedErrors.Discouraged // phpcs:enable WordPress.PHP.NoSilencedErrors.Discouraged
return strcmp( $a, $b ); return strcmp( $a, $b );
} }
/**
* Sort array according to current locale rules and maintaining index association.
* By default tries to use Collator from PHP Internationalization Functions if available.
* If PHP Collator class doesn't exists it fallback to removing accepts from a array
* and by sorting with `uasort( $data, 'strcmp' )` giving support for ASCII values.
*
* @since 4.6.0
* @param array $data List of values to sort.
* @param string $locale Locale.
* @return array
*/
function wc_asort_by_locale( &$data, $locale = '' ) {
// Use Collator if PHP Internationalization Functions (php-intl) is available.
if ( class_exists( 'Collator' ) ) {
$locale = $locale ? $locale : get_locale();
$collator = new Collator( $locale );
$collator->asort( $data, Collator::SORT_STRING );
return $data;
}
$raw_data = $data;
array_walk(
$data,
function ( &$value ) {
$value = remove_accents( html_entity_decode( $value ) );
}
);
uasort( $data, 'strcmp' );
foreach ( $data as $key => $val ) {
$data[ $key ] = $raw_data[ $key ];
}
return $data;
}
/** /**
* Get rounding mode for internal tax calculations. * Get rounding mode for internal tax calculations.
* *

View File

@ -0,0 +1,46 @@
<?php
/**
* Core functions tests
*
* @package WooCommerce\Tests\Functions.
*/
/**
* Class WC_Core_Functions_Test
*/
class WC_Core_Functions_Test extends \WC_Unit_Test_Case {
/**
* Test wc_ascii_uasort_comparison() function.
*/
public function test_wc_ascii_uasort_comparison() {
$unsorted_values = array(
'ET' => 'Éthiopie',
'ES' => 'Espagne',
'AF' => 'Afghanistan',
'AX' => 'Åland Islands',
);
$sorted_values = $unsorted_values;
uasort( $sorted_values, 'wc_ascii_uasort_comparison' );
$this->assertSame( array( 'Afghanistan', 'Åland Islands', 'Espagne', 'Éthiopie' ), array_values( $sorted_values ) );
}
/**
* Test wc_asort_by_locale() function.
*/
public function test_wc_asort_by_locale() {
$unsorted_values = array(
'ET' => 'Éthiopie',
'ES' => 'Espagne',
'AF' => 'Afghanistan',
'AX' => 'Åland Islands',
);
$sorted_values = $unsorted_values;
wc_asort_by_locale( $sorted_values );
$this->assertSame( array( 'Afghanistan', 'Åland Islands', 'Espagne', 'Éthiopie' ), array_values( $sorted_values ) );
}
}