mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 12:20:22 +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:
@@ -3822,6 +3822,22 @@ function wp_parse_args( $args, $defaults = '' ) {
|
||||
return $r;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleans up an array, comma- or space-separated list of scalar values.
|
||||
*
|
||||
* @since 5.1.0
|
||||
*
|
||||
* @param array|string $list List of values.
|
||||
* @return array Sanitized array of values.
|
||||
*/
|
||||
function wp_parse_list( $list ) {
|
||||
if ( ! is_array( $list ) ) {
|
||||
return preg_split( '/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY );
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up an array, comma- or space-separated list of IDs.
|
||||
*
|
||||
@@ -3831,9 +3847,7 @@ function wp_parse_args( $args, $defaults = '' ) {
|
||||
* @return array Sanitized array of IDs.
|
||||
*/
|
||||
function wp_parse_id_list( $list ) {
|
||||
if ( ! is_array( $list ) ) {
|
||||
$list = preg_split( '/[\s,]+/', $list );
|
||||
}
|
||||
$list = wp_parse_list( $list );
|
||||
|
||||
return array_unique( array_map( 'absint', $list ) );
|
||||
}
|
||||
@@ -3847,15 +3861,9 @@ function wp_parse_id_list( $list ) {
|
||||
* @return array Sanitized array of slugs.
|
||||
*/
|
||||
function wp_parse_slug_list( $list ) {
|
||||
if ( ! is_array( $list ) ) {
|
||||
$list = preg_split( '/[\s,]+/', $list );
|
||||
}
|
||||
$list = wp_parse_list( $list );
|
||||
|
||||
foreach ( $list as $key => $value ) {
|
||||
$list[ $key ] = sanitize_title( $value );
|
||||
}
|
||||
|
||||
return array_unique( $list );
|
||||
return array_unique( array_map( 'sanitize_title', $list ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user