mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-12 04:40:26 +00:00
General: Fix problematic string to array parsing.
WordPress has historically often used code like `preg_split( '/[\s,]+/', $var )` to parse a string of comma-separated values into an array. However, this approach was causing an empty string to not be parsed into an empty array as expected, but rather into an array with the empty string as its sole element. This was among other areas causing problems in the REST API where passing an empty request parameter could cause that request to fail because, instead of it being ignored, that parameter would be compared against the valid values for it, which typically do not include an empty string. Props david.binda, sstoqnov. Fixes #43977. git-svn-id: https://develop.svn.wordpress.org/trunk@44546 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -534,6 +534,30 @@ class Tests_Functions extends WP_UnitTestCase {
|
||||
update_option( 'blog_charset', $orig_blog_charset );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 43977
|
||||
* @dataProvider data_wp_parse_list
|
||||
*/
|
||||
function test_wp_parse_list( $expected, $actual ) {
|
||||
$this->assertSame( $expected, array_values( wp_parse_list( $actual ) ) );
|
||||
}
|
||||
|
||||
function data_wp_parse_list() {
|
||||
return array(
|
||||
array( array( '1', '2', '3', '4' ), '1,2,3,4' ),
|
||||
array( array( 'apple', 'banana', 'carrot', 'dog' ), 'apple,banana,carrot,dog' ),
|
||||
array( array( '1', '2', 'apple', 'banana' ), '1,2,apple,banana' ),
|
||||
array( array( '1', '2', 'apple', 'banana' ), '1, 2,apple,banana' ),
|
||||
array( array( '1', '2', 'apple', 'banana' ), '1,2,apple,,banana' ),
|
||||
array( array( '1', '2', 'apple', 'banana' ), ',1,2,apple,banana' ),
|
||||
array( array( '1', '2', 'apple', 'banana' ), '1,2,apple,banana,' ),
|
||||
array( array( '1', '2', 'apple', 'banana' ), '1,2 ,apple,banana' ),
|
||||
array( array(), '' ),
|
||||
array( array(), ',' ),
|
||||
array( array(), ',,' ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider data_wp_parse_id_list
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user