Skip to content

Commit a052d7a

Browse files
authored
Merge branch 'WordPress:trunk' into trunk
2 parents 76e5206 + c72064d commit a052d7a

4 files changed

Lines changed: 64 additions & 15 deletions

File tree

src/wp-includes/functions.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5027,18 +5027,21 @@ function wp_parse_args( $args, $defaults = array() ) {
50275027
* Converts a comma- or space-separated list of scalar values to an array.
50285028
*
50295029
* @since 5.1.0
5030+
* @since 7.2.0 Added explicit support for passing an integer.
50305031
*
5031-
* @param mixed[]|string $input_list List of values.
5032+
* @param mixed[]|string|int $input_list List of values.
50325033
* @return array Array of scalar values. A string is split into a list, while an array
50335034
* keeps its keys, so the result is not necessarily a list.
50345035
* @phpstan-return (
5035-
* $input_list is string ? list<string> : (
5036+
* $input_list is string|int ? list<string> : (
50365037
* $input_list is array<string> ? array<string> : array<scalar>
50375038
* )
50385039
* )
50395040
*/
50405041
function wp_parse_list( $input_list ): array {
5041-
if ( ! is_array( $input_list ) ) {
5042+
if ( is_int( $input_list ) ) {
5043+
$input_list = array( (string) $input_list );
5044+
} elseif ( ! is_array( $input_list ) ) {
50425045
$parsed_list = preg_split( '/[\s,]+/', $input_list, -1, PREG_SPLIT_NO_EMPTY );
50435046
return is_array( $parsed_list ) ? $parsed_list : array();
50445047
}
@@ -5053,9 +5056,10 @@ function wp_parse_list( $input_list ): array {
50535056
* Cleans up an array, comma- or space-separated list of IDs.
50545057
*
50555058
* @since 3.0.0
5056-
* @since 5.1.0 Refactored to use wp_parse_list().
5059+
* @since 5.1.0 Refactored to use {@see wp_parse_list()}.
5060+
* @since 7.2.0 Added explicit support for passing an integer.
50575061
*
5058-
* @param mixed[]|string $input_list List of IDs.
5062+
* @param mixed[]|string|int $input_list List of IDs.
50595063
* @return int[] Sanitized array of IDs. May include zero. Keys are preserved
50605064
* from the input and `array_unique()` may leave gaps, so the
50615065
* result is not necessarily a list.
@@ -5071,9 +5075,10 @@ function wp_parse_id_list( $input_list ): array {
50715075
* Cleans up an array, comma- or space-separated list of slugs.
50725076
*
50735077
* @since 4.7.0
5074-
* @since 5.1.0 Refactored to use wp_parse_list().
5078+
* @since 5.1.0 Refactored to use {@see wp_parse_list()}.
5079+
* @since 7.2.0 Added explicit support for passing an integer.
50755080
*
5076-
* @param mixed[]|string $input_list List of slugs.
5081+
* @param mixed[]|string|int $input_list List of slugs.
50775082
* @return string[] Sanitized array of slugs. May include an empty string. Keys
50785083
* are preserved from the input and `array_unique()` may leave
50795084
* gaps, so the result is not necessarily a list.

tests/phpunit/tests/functions/wpParseIdList.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Tests_Functions_wpParseIdList extends WP_UnitTestCase {
1616
* @dataProvider data_wp_parse_id_list
1717
* @dataProvider data_unexpected_input
1818
*
19-
* @param mixed[]|string $input_list
19+
* @param mixed[]|string|int $input_list
2020
* @param array<non-negative-int> $expected
2121
*/
2222
public function test_wp_parse_id_list( $input_list, array $expected ): void {
@@ -37,7 +37,7 @@ public function test_wp_parse_id_list( $input_list, array $expected ): void {
3737
/**
3838
* Data provider.
3939
*
40-
* @return array<string, array{ input_list: mixed[]|string, expected: array<non-negative-int> }>
40+
* @return array<string, array{ input_list: mixed[]|string|int, expected: array<non-negative-int> }>
4141
*/
4242
public function data_wp_parse_id_list(): array {
4343
return array(
@@ -74,13 +74,25 @@ public function data_wp_parse_id_list(): array {
7474
'input_list' => array( -1, 2, '-3', '4' ),
7575
'expected' => array( 1, 2, 3, 4 ),
7676
),
77+
'positive int' => array(
78+
'input_list' => 5,
79+
'expected' => array( 5 ),
80+
),
81+
'negative int' => array(
82+
'input_list' => -5,
83+
'expected' => array( 5 ),
84+
),
85+
'zero' => array(
86+
'input_list' => 0,
87+
'expected' => array( 0 ),
88+
),
7789
);
7890
}
7991

8092
/**
8193
* Data provider.
8294
*
83-
* @return array<string, array{ input_list: mixed[]|string, expected: array<non-negative-int> }>
95+
* @return array<string, array{ input_list: mixed[]|string|int, expected: array<non-negative-int> }>
8496
*/
8597
public function data_unexpected_input(): array {
8698
return array(
@@ -100,6 +112,10 @@ public function data_unexpected_input(): array {
100112
'input_list' => array( '1 2 string with spaces' ),
101113
'expected' => array( 1 ),
102114
),
115+
'comma in array' => array(
116+
'input_list' => array( '1,2' ),
117+
'expected' => array( 1 ),
118+
),
103119
'string with html' => array(
104120
'input_list' => '1 2 string <strong>with</strong> <h1>HEADING</h1>',
105121
'expected' => array( 1, 2, 0 ),

tests/phpunit/tests/functions/wpParseList.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Tests_Functions_wpParseList extends WP_UnitTestCase {
1414
*
1515
* @dataProvider data_wp_parse_list
1616
*
17-
* @param mixed[]|string $input_list
17+
* @param mixed[]|string|int $input_list
1818
* @param array<scalar> $expected
1919
*/
2020
public function test_wp_parse_list( $input_list, array $expected ): void {
@@ -35,7 +35,7 @@ public function test_wp_parse_list( $input_list, array $expected ): void {
3535
/**
3636
* Data provider.
3737
*
38-
* @return array<string, array{ input_list: mixed[]|string, expected: array<scalar> }>
38+
* @return array<string, array{ input_list: mixed[]|string|int, expected: array<scalar> }>
3939
*/
4040
public function data_wp_parse_list(): array {
4141
return array(
@@ -83,6 +83,22 @@ public function data_wp_parse_list(): array {
8383
'input_list' => ',,',
8484
'expected' => array(),
8585
),
86+
'positive int' => array(
87+
'input_list' => 5,
88+
'expected' => array( '5' ),
89+
),
90+
'negative int' => array(
91+
'input_list' => -5,
92+
'expected' => array( '-5' ),
93+
),
94+
'zero' => array(
95+
'input_list' => 0,
96+
'expected' => array( '0' ),
97+
),
98+
'comma in array item' => array(
99+
'input_list' => array( '1,2' ),
100+
'expected' => array( '1,2' ),
101+
),
86102
'passed scalar array' => array(
87103
'input_list' => array( 'foo', true, false, 1, 3.14 ),
88104
'expected' => array( 'foo', true, false, 1, 3.14 ),

tests/phpunit/tests/functions/wpParseSlugList.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Tests_Functions_WpParseSlugList extends WP_UnitTestCase {
1616
* @dataProvider data_wp_parse_slug_list
1717
* @dataProvider data_unexpected_input
1818
*
19-
* @param mixed[]|string $input_list
19+
* @param mixed[]|string|int $input_list
2020
* @param array<string> $expected
2121
*/
2222
public function test_wp_parse_slug_list( $input_list, array $expected ): void {
@@ -37,7 +37,7 @@ public function test_wp_parse_slug_list( $input_list, array $expected ): void {
3737
/**
3838
* Data provider.
3939
*
40-
* @return array<string, array{ input_list: mixed[]|string, expected: array<string> }>
40+
* @return array<string, array{ input_list: mixed[]|string|int, expected: array<string> }>
4141
*/
4242
public function data_wp_parse_slug_list(): array {
4343
return array(
@@ -75,6 +75,18 @@ public function data_wp_parse_slug_list(): array {
7575
'input_list' => array( 'apple ', 'banana carrot', 'd o g' ),
7676
'expected' => array( 'apple', 'banana-carrot', 'd-o-g' ),
7777
),
78+
'positive int' => array(
79+
'input_list' => 5,
80+
'expected' => array( '5' ),
81+
),
82+
'negative int' => array(
83+
'input_list' => -5,
84+
'expected' => array( '5' ),
85+
),
86+
'zero' => array(
87+
'input_list' => 0,
88+
'expected' => array( '0' ),
89+
),
7890
'passed assoc array' => array(
7991
'input_list' => array(
8092
'one' => 'foo',
@@ -93,7 +105,7 @@ public function data_wp_parse_slug_list(): array {
93105
/**
94106
* Data provider.
95107
*
96-
* @return array<string, array{ input_list: mixed[]|string, expected: array<string> }>
108+
* @return array<string, array{ input_list: mixed[]|string|int, expected: array<string> }>
97109
*/
98110
public function data_unexpected_input(): array {
99111
return array(

0 commit comments

Comments
 (0)