diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index 9c49b97b56..905b07b598 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -2132,19 +2132,15 @@ function sanitize_user( $username, $strict = false ) { * * @since 3.0.0 * - * @param string $key String key - * @return string Sanitized key + * @param string $key String key. + * @return string Sanitized key. */ function sanitize_key( $key ) { - $raw_key = $key; + $sanitized_key = ''; - if ( ! is_string( $key ) ) { - $key = ''; - } - - if ( '' !== $key ) { - $key = strtolower( $key ); - $key = preg_replace( '/[^a-z0-9_\-]/', '', $key ); + if ( is_scalar( $key ) ) { + $sanitized_key = strtolower( $key ); + $sanitized_key = preg_replace( '/[^a-z0-9_\-]/', '', $sanitized_key ); } /** @@ -2152,10 +2148,10 @@ function sanitize_key( $key ) { * * @since 3.0.0 * - * @param string $key Sanitized key. - * @param string $raw_key The key prior to sanitization. + * @param string $sanitized_key Sanitized key. + * @param string $key The key prior to sanitization. */ - return apply_filters( 'sanitize_key', $key, $raw_key ); + return apply_filters( 'sanitize_key', $sanitized_key, $key ); } /** diff --git a/tests/phpunit/tests/formatting/sanitizeKey.php b/tests/phpunit/tests/formatting/sanitizeKey.php index 7dea43e004..0fb5d905df 100644 --- a/tests/phpunit/tests/formatting/sanitizeKey.php +++ b/tests/phpunit/tests/formatting/sanitizeKey.php @@ -7,38 +7,27 @@ class Tests_Formatting_SanitizeKey extends WP_UnitTestCase { /** - * @ticket 54160 + * @ticket 54160 * @dataProvider data_sanitize_key * - * @param mixed $key The key to sanitize. - * @param mixed $expected The expected value. + * @param string $key The key to sanitize. + * @param string $expected The expected value. */ public function test_sanitize_key( $key, $expected ) { $this->assertSame( $expected, sanitize_key( $key ) ); } + /** + * Data provider. + * + * @return array + */ public function data_sanitize_key() { return array( 'an empty string key' => array( 'key' => '', 'expected' => '', ), - 'a null key' => array( - 'key' => null, - 'expected' => '', - ), - 'an int 0 key' => array( - 'key' => 0, - 'expected' => '', - ), - 'a true key' => array( - 'key' => true, - 'expected' => '', - ), - 'an array key' => array( - 'key' => array( 'Howdy, admin!' ), - 'expected' => '', - ), 'a lowercase key with commas' => array( 'key' => 'howdy,admin', 'expected' => 'howdyadmin', @@ -71,20 +60,81 @@ class Tests_Formatting_SanitizeKey extends WP_UnitTestCase { } /** - * @ticket 54160 + * @ticket 54160 + * @dataProvider data_sanitize_key_nonstring_scalar * - * @covers WP_Hook::sanitize_key + * @param mixed $key The key to sanitize. + * @param string $expected The expected value. */ - public function test_filter_sanitize_key() { - $key = 'Howdy, admin!'; + public function test_sanitize_key_nonstring_scalar( $key, $expected ) { + $this->assertSame( $expected, sanitize_key( $key ) ); + } + /** + * Data provider. + * + * @return array + */ + public function data_sanitize_key_nonstring_scalar() { + return array( + 'integer type' => array( + 'key' => 0, + 'expected' => '0', + ), + 'boolean true' => array( + 'key' => true, + 'expected' => '1', + ), + 'boolean false' => array( + 'key' => false, + 'expected' => '', + ), + 'float type' => array( + 'key' => 0.123, + 'expected' => '0123', + ), + ); + } + + /** + * @ticket 54160 + * @dataProvider data_sanitize_key_with_non_scalars + * + * @param mixed $nonscalar_key A non-scalar data type given as a key. + */ + public function test_sanitize_key_with_non_scalars( $nonscalar_key ) { add_filter( 'sanitize_key', - static function( $key ) { - return 'Howdy, admin!'; - } + function ( $sanitized_key, $key ) use ( $nonscalar_key ) { + $this->assertEmpty( $sanitized_key, 'Empty string not passed as first filtered argument' ); + $this->assertSame( $nonscalar_key, $key, 'Given unsanitized key not passed as second filtered argument' ); + return $sanitized_key; + }, + 10, + 2 ); + $this->assertEmpty( sanitize_key( $nonscalar_key ), 'Non-scalar key did not return empty string' ); + } - $this->assertSame( $key, sanitize_key( $key ) ); + /** + * Data provider. + * + * @return array + */ + public function data_sanitize_key_with_non_scalars() { + return array( + 'array type' => array( + 'key' => array( 'key' ), + 'expected' => '', + ), + 'null' => array( + 'key' => null, + 'expected' => '', + ), + 'object' => array( + 'key' => new stdClass(), + 'expected' => '', + ), + ); } }