From d2f41560d3fa070460287cc1e7932fcf439fdf01 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Thu, 7 Feb 2019 23:34:22 +0000 Subject: [PATCH] Formatting: Loosen the type checking in `_sanitize_text_fields()`. [44618] added strict type checking to `_sanitize_text_fields()`, which has caused some compat issues with plugins. We can loosen the type checking to only reject objects and arrays, and cast other types to string. Props Nick_theGeek, pento. Fixes #41450. git-svn-id: https://develop.svn.wordpress.org/trunk@44731 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/formatting.php | 4 +++- .../tests/formatting/SanitizeTextField.php | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index 4beb33cb8f..b5c4982141 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -5153,10 +5153,12 @@ function sanitize_textarea_field( $str ) { * @return string Sanitized string. */ function _sanitize_text_fields( $str, $keep_newlines = false ) { - if ( ! is_string( $str ) ) { + if ( is_object( $str ) || is_array( $str ) ) { return ''; } + $str = (string) $str; + $filtered = wp_check_invalid_utf8( $str ); if ( strpos( $filtered, '<' ) !== false ) { diff --git a/tests/phpunit/tests/formatting/SanitizeTextField.php b/tests/phpunit/tests/formatting/SanitizeTextField.php index 62ee5573c3..c6f978484b 100644 --- a/tests/phpunit/tests/formatting/SanitizeTextField.php +++ b/tests/phpunit/tests/formatting/SanitizeTextField.php @@ -97,6 +97,30 @@ class Tests_Formatting_SanitizeTextField extends WP_UnitTestCase { array(), '', ), + array( + array( 1, 2, 'foo' ), + '', + ), + array( + new WP_Query, + '', + ), + array( + 2, + '2', + ), + array( + false, + '', + ), + array( + true, + '1', + ), + array( + 10.1, + '10.1', + ), ); }