Added wc_selected function.
- Added unit test for the wc_selected function.
This commit is contained in:
parent
93a0808887
commit
49529e0912
|
@ -183,22 +183,29 @@ function woocommerce_wp_checkbox( $field ) {
|
|||
|
||||
|
||||
/**
|
||||
* Return true if stringified $value is found in array of stringified $options or if stringified $value
|
||||
* is the same as stringified $options.
|
||||
* 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 bool
|
||||
* @return string
|
||||
*/
|
||||
function wc_is_value_in_options( $value, $options ) {
|
||||
function wc_selected( $value, $options ) {
|
||||
$value = (string) $value;
|
||||
if ( is_array( $options ) ) {
|
||||
$options = array_map( 'strval', $options );
|
||||
return in_array( $value, $options, true );
|
||||
$is_value_in_options = in_array( $value, $options, true );
|
||||
} else {
|
||||
$options = (string) $options;
|
||||
return $value === $options;
|
||||
$is_value_in_options = ( $value === $options );
|
||||
}
|
||||
|
||||
if ( $is_value_in_options ) {
|
||||
return " selected='selected'";
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -247,7 +254,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( wc_is_value_in_options( $key, $field['value'] ), true, false ) . '>' . esc_html( $value ) . '</option>';
|
||||
echo '<option value="' . esc_attr( $key ) . '"' . wc_selected( $key, $field['value'] ) . '>' . esc_html( $value ) . '</option>';
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for the admin/wc-meta-box-functions.php.
|
||||
*
|
||||
* @package WooCommerce\Tests\Admin
|
||||
*/
|
||||
|
||||
/**
|
||||
* Test wc_selected().
|
||||
*
|
||||
* @since 3.4.0
|
||||
*/
|
||||
class WC_Meta_Box_Functions_Test extends WC_Unit_Test_Case {
|
||||
/**
|
||||
* Load the necessary files, as they're not automatically loaded by WooCommerce.
|
||||
*/
|
||||
public static function setUpBeforeClass() {
|
||||
include_once WC_Unit_Tests_Bootstrap::instance()->plugin_dir . '/includes/admin/wc-meta-box-functions.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 );
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue