diff --git a/includes/admin/class-wc-admin-settings.php b/includes/admin/class-wc-admin-settings.php index e0282d6bdaa..469761c33aa 100644 --- a/includes/admin/class-wc-admin-settings.php +++ b/includes/admin/class-wc-admin-settings.php @@ -613,7 +613,7 @@ if ( ! class_exists( 'WC_Admin_Settings', false ) ) : $val ) { - echo ''; + echo ''; } } ?> diff --git a/includes/admin/meta-boxes/class-wc-meta-box-coupon-data.php b/includes/admin/meta-boxes/class-wc-meta-box-coupon-data.php index 00184e23e8e..f1e93ea8be2 100644 --- a/includes/admin/meta-boxes/class-wc-meta-box-coupon-data.php +++ b/includes/admin/meta-boxes/class-wc-meta-box-coupon-data.php @@ -232,7 +232,7 @@ class WC_Meta_Box_Coupon_Data { if ( $categories ) { foreach ( $categories as $cat ) { - echo ''; + echo ''; } } ?> @@ -249,7 +249,7 @@ class WC_Meta_Box_Coupon_Data { if ( $categories ) { foreach ( $categories as $cat ) { - echo ''; + echo ''; } } ?> diff --git a/includes/admin/meta-boxes/views/html-product-attribute.php b/includes/admin/meta-boxes/views/html-product-attribute.php index 600cacbb6aa..b3b846f8385 100644 --- a/includes/admin/meta-boxes/views/html-product-attribute.php +++ b/includes/admin/meta-boxes/views/html-product-attribute.php @@ -48,7 +48,7 @@ if ( ! defined( 'ABSPATH' ) ) { foreach ( $all_terms as $term ) { $options = $attribute->get_options(); $options = ! empty( $options ) ? $options : array(); - echo ''; + echo ''; } } ?> diff --git a/includes/admin/reports/class-wc-report-coupon-usage.php b/includes/admin/reports/class-wc-report-coupon-usage.php index d17379da025..59141e02b05 100644 --- a/includes/admin/reports/class-wc-report-coupon-usage.php +++ b/includes/admin/reports/class-wc-report-coupon-usage.php @@ -210,7 +210,7 @@ class WC_Report_Coupon_Usage extends WC_Admin_Report { coupon_codes ), true, false ) . '>' . esc_html( $coupon ) . ''; + echo ''; } ?> diff --git a/includes/admin/settings/views/html-admin-page-shipping-zone-methods.php b/includes/admin/settings/views/html-admin-page-shipping-zone-methods.php index 0bf914ea9ad..cacc4bd6495 100644 --- a/includes/admin/settings/views/html-admin-page-shipping-zone-methods.php +++ b/includes/admin/settings/views/html-admin-page-shipping-zone-methods.php @@ -38,16 +38,16 @@ if ( ! defined( 'ABSPATH' ) ) { > $value ) { - echo ''; + echo ''; } ?> diff --git a/includes/wc-core-functions.php b/includes/wc-core-functions.php index 469a2a11350..6deb9d4860c 100644 --- a/includes/wc-core-functions.php +++ b/includes/wc-core-functions.php @@ -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 ); +} diff --git a/tests/unit-tests/util/class-wc-tests-core-functions.php b/tests/unit-tests/util/class-wc-tests-core-functions.php index 3edd437242c..94e84516696 100644 --- a/tests/unit-tests/util/class-wc-tests-core-functions.php +++ b/tests/unit-tests/util/class-wc-tests-core-functions.php @@ -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 ); + } + } }