Handling of PHP's automatic array key conversion to int

This commit is contained in:
Peter Fabian 2018-04-09 16:39:14 +02:00
parent 900c8fc33f
commit d2efef5a1c
1 changed files with 3 additions and 1 deletions

View File

@ -227,7 +227,9 @@ 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>';
// In case the value is an array, the $key from options might have been converted to int automatically by PHP,
// thus the comparison needs to check both $key and (string)$key.
echo '<option value="' . esc_attr( $key ) . '" ' . selected( $field['value'] === $key || ( is_array( $field['value'] ) && ( in_array( $key, $field['value'], true ) || in_array( (string) $key, $field['value'], true ) ) ), true, false ) . '>' . esc_html( $value ) . '</option>';
}
?>
</select>