Merge pull request #19705 from woocommerce/fix/19619

Handling of PHP's automatic array key conversion to int
This commit is contained in:
Mike Jolley 2018-04-18 15:40:31 +01:00 committed by GitHub
commit d381330a91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 75 additions and 9 deletions

View File

@ -613,7 +613,7 @@ if ( ! class_exists( 'WC_Admin_Settings', false ) ) :
<?php
if ( ! empty( $countries ) ) {
foreach ( $countries as $key => $val ) {
echo '<option value="' . esc_attr( $key ) . '" ' . selected( in_array( $key, $selections, true ), true, false ) . '>' . esc_html( $val ) . '</option>';
echo '<option value="' . esc_attr( $key ) . '"' . wc_selected( $key, $selections ) . '>' . esc_html( $val ) . '</option>';
}
}
?>

View File

@ -232,7 +232,7 @@ class WC_Meta_Box_Coupon_Data {
if ( $categories ) {
foreach ( $categories as $cat ) {
echo '<option value="' . esc_attr( $cat->term_id ) . '"' . selected( in_array( $cat->term_id, $category_ids ), true, false ) . '>' . esc_html( $cat->name ) . '</option>';
echo '<option value="' . esc_attr( $cat->term_id ) . '"' . wc_selected( $cat->term_id, $category_ids ) . '>' . esc_html( $cat->name ) . '</option>';
}
}
?>
@ -249,7 +249,7 @@ class WC_Meta_Box_Coupon_Data {
if ( $categories ) {
foreach ( $categories as $cat ) {
echo '<option value="' . esc_attr( $cat->term_id ) . '"' . selected( in_array( $cat->term_id, $category_ids ), true, false ) . '>' . esc_html( $cat->name ) . '</option>';
echo '<option value="' . esc_attr( $cat->term_id ) . '"' . wc_selected( $cat->term_id, $category_ids ) . '>' . esc_html( $cat->name ) . '</option>';
}
}
?>

View File

@ -48,7 +48,7 @@ if ( ! defined( 'ABSPATH' ) ) {
foreach ( $all_terms as $term ) {
$options = $attribute->get_options();
$options = ! empty( $options ) ? $options : array();
echo '<option value="' . esc_attr( $term->term_id ) . '" ' . selected( in_array( $term->term_id, $options, true ), true, false ) . '>' . esc_attr( apply_filters( 'woocommerce_product_attribute_term_name', $term->name, $term ) ) . '</option>';
echo '<option value="' . esc_attr( $term->term_id ) . '"' . wc_selected( $term->term_id, $options ) . '>' . esc_attr( apply_filters( 'woocommerce_product_attribute_term_name', $term->name, $term ) ) . '</option>';
}
}
?>

View File

@ -210,7 +210,7 @@ class WC_Report_Coupon_Usage extends WC_Admin_Report {
<option value=""><?php esc_html_e( 'All coupons', 'woocommerce' ); ?></option>
<?php
foreach ( $used_coupons as $coupon ) {
echo '<option value="' . esc_attr( $coupon ) . '" ' . selected( in_array( $coupon, $this->coupon_codes ), true, false ) . '>' . esc_html( $coupon ) . '</option>';
echo '<option value="' . esc_attr( $coupon ) . '"' . wc_selected( $coupon, $this->coupon_codes ) . '>' . esc_html( $coupon ) . '</option>';
}
?>
</select>

View File

@ -38,16 +38,16 @@ if ( ! defined( 'ABSPATH' ) ) {
<select multiple="multiple" data-attribute="zone_locations" id="zone_locations" name="zone_locations" data-placeholder="<?php esc_html_e( 'Select regions within this zone', 'woocommerce' ); ?>" class="wc-shipping-zone-region-select chosen_select">
<?php
foreach ( $continents as $continent_code => $continent ) {
echo '<option value="continent:' . esc_attr( $continent_code ) . '" ' . selected( in_array( "continent:$continent_code", $locations ), true, false ) . ' alt="">' . esc_html( $continent['name'] ) . '</option>';
echo '<option value="continent:' . esc_attr( $continent_code ) . '"' . wc_selected( "continent:$continent_code", $locations ) . ' alt="">' . esc_html( $continent['name'] ) . '</option>';
$countries = array_intersect( array_keys( $allowed_countries ), $continent['countries'] );
foreach ( $countries as $country_code ) {
echo '<option value="country:' . esc_attr( $country_code ) . '" ' . selected( in_array( "country:$country_code", $locations ), true, false ) . ' alt="' . esc_attr( $continent['name'] ) . '">' . esc_html( '&nbsp;&nbsp; ' . $allowed_countries[ $country_code ] ) . '</option>';
echo '<option value="country:' . esc_attr( $country_code ) . '"' . wc_selected( "country:$country_code", $locations ) . ' alt="' . esc_attr( $continent['name'] ) . '">' . esc_html( '&nbsp;&nbsp; ' . $allowed_countries[ $country_code ] ) . '</option>';
if ( $states = WC()->countries->get_states( $country_code ) ) {
foreach ( $states as $state_code => $state_name ) {
echo '<option value="state:' . esc_attr( $country_code . ':' . $state_code ) . '" ' . selected( in_array( "state:$country_code:$state_code", $locations ), true, false ) . ' alt="' . esc_attr( $continent['name'] . ' ' . $allowed_countries[ $country_code ] ) . '">' . esc_html( '&nbsp;&nbsp;&nbsp;&nbsp; ' . $state_name ) . '</option>';
echo '<option value="state:' . esc_attr( $country_code . ':' . $state_code ) . '"' . wc_selected( "state:$country_code:$state_code", $locations ) . ' alt="' . esc_attr( $continent['name'] . ' ' . $allowed_countries[ $country_code ] ) . '">' . esc_html( '&nbsp;&nbsp;&nbsp;&nbsp; ' . $state_name ) . '</option>';
}
}
}

View File

@ -227,7 +227,7 @@ function woocommerce_wp_select( $field ) {
<select <?php echo wc_implode_html_attributes( $field_attributes ); // WPCS: XSS ok. ?>>
<?php
foreach ( $field['options'] as $key => $value ) {
echo '<option value="' . esc_attr( $key ) . '" ' . selected( $field['value'] === $key || ( is_array( $field['value'] ) && in_array( $key, $field['value'], true ) ), true, false ) . '>' . esc_html( $value ) . '</option>';
echo '<option value="' . esc_attr( $key ) . '"' . wc_selected( $key, $field['value'] ) . '>' . esc_html( $value ) . '</option>';
}
?>
</select>

View File

@ -2088,3 +2088,20 @@ function wc_round_discount( $value, $precision ) {
return round( $value, $precision );
}
}
/**
* Return the html selected attribute if stringified $value is found in array of stringified $options
* or if stringified $value is the same as scalar stringified $options.
*
* @param string|int $value Value to find within options.
* @param string|int|array $options Options to go through when looking for value.
* @return string
*/
function wc_selected( $value, $options ) {
if ( is_array( $options ) ) {
$options = array_map( 'strval', $options );
return selected( in_array( (string) $value, $options, true ), true, false );
}
return selected( $value, $options, false );
}

View File

@ -644,4 +644,53 @@ class WC_Tests_Core_Functions extends WC_Unit_Test_Case {
)
);
}
/**
* Test: wc_selected
*/
public function test_wc_selected() {
$test_cases = array(
// both value and options int.
array( 0, 0, true ),
array( 0, 1, false ),
array( 1, 0, false ),
// value string, options int.
array( '0', 0, true ),
array( '0', 1, false ),
array( '1', 0, false ),
// value int, options string.
array( 0, '0', true ),
array( 0, '1', false ),
array( 1, '0', false ),
// both value and options str.
array( '0', '0', true ),
array( '0', '1', false ),
array( '1', '0', false ),
// both value and options int.
array( 0, array( 0, 1, 2 ), true ),
array( 0, array( 1, 1, 1 ), false ),
// value string, options int.
array( '0', array( 0, 1, 2 ), true ),
array( '0', array( 1, 1, 1 ), false ),
// value int, options string.
array( 0, array( '0', '1', '2' ), true ),
array( 0, array( '1', '1', '1' ), false ),
// both value and options str.
array( '0', array( '0', '1', '2' ), true ),
array( '0', array( '1', '1', '1' ), false ),
);
foreach ( $test_cases as $test_case ) {
list( $value, $options, $result ) = $test_case;
$actual_result = $result ? " selected='selected'" : '';
$this->assertEquals( wc_selected( $value, $options ), $actual_result );
}
}
}