diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 0c6f44591f..fc1dd011e1 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -4636,6 +4636,7 @@ function _wp_array_get( $array, $path, $default = null ) { * * $array = array(); * _wp_array_set( $array, array( 'a', 'b', 'c', 1 ); + * * $array becomes: * array( * 'a' => array( @@ -4645,9 +4646,14 @@ function _wp_array_get( $array, $path, $default = null ) { * ), * ); * - * @param array $array An array that we want to mutate to include a specific value in a path. - * @param array $path An array of keys describing the path that we want to mutate. - * @param mixed $value The value that will be set. + * @internal + * + * @since 5.8.0 + * @access private + * + * @param array $array An array that we want to mutate to include a specific value in a path. + * @param array $path An array of keys describing the path that we want to mutate. + * @param mixed $value The value that will be set. */ function _wp_array_set( &$array, $path, $value = null ) { // Confirm $array is valid. @@ -4659,10 +4665,13 @@ function _wp_array_set( &$array, $path, $value = null ) { if ( ! is_array( $path ) ) { return; } + $path_length = count( $path ); + if ( 0 === $path_length ) { return; } + foreach ( $path as $path_element ) { if ( ! is_string( $path_element ) && ! is_integer( $path_element ) && @@ -4682,6 +4691,7 @@ function _wp_array_set( &$array, $path, $value = null ) { } $array = &$array[ $path_element ]; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.VariableRedeclaration } + $array[ $path[ $i ] ] = $value; } diff --git a/tests/phpunit/tests/functions/wpArrayGet.php b/tests/phpunit/tests/functions/wpArrayGet.php index 8ebd9c4386..e9e8a2b27d 100644 --- a/tests/phpunit/tests/functions/wpArrayGet.php +++ b/tests/phpunit/tests/functions/wpArrayGet.php @@ -57,12 +57,12 @@ class Tests_Functions_wpArrayGet extends WP_UnitTestCase { } /** - * Test _wp_array_get() with non subtree paths. + * Test _wp_array_get() with non-subtree paths. * * @ticket 51720 */ public function test_wp_array_get_simple_non_subtree() { - // Simple non sub tree test. + // Simple non-subtree test. $this->assertSame( _wp_array_get( array( @@ -73,7 +73,7 @@ class Tests_Functions_wpArrayGet extends WP_UnitTestCase { 4 ); - // Simple non sub tree not found. + // Simple non-subtree not found. $this->assertSame( _wp_array_get( array( @@ -84,7 +84,7 @@ class Tests_Functions_wpArrayGet extends WP_UnitTestCase { null ); - // Simple non sub tree not found with a default. + // Simple non-subtree not found with a default. $this->assertSame( _wp_array_get( array( @@ -96,7 +96,7 @@ class Tests_Functions_wpArrayGet extends WP_UnitTestCase { 1 ); - // Simple non sub tree integer path. + // Simple non-subtree integer path. $this->assertSame( _wp_array_get( array( @@ -110,6 +110,55 @@ class Tests_Functions_wpArrayGet extends WP_UnitTestCase { ); } + /** + * Test _wp_array_get() with subtrees. + * + * @ticket 51720 + */ + public function test_wp_array_get_subtree() { + $this->assertSame( + _wp_array_get( + array( + 'a' => array( + 'b' => array( + 'c' => 1, + ), + ), + ), + array( 'a', 'b' ) + ), + array( 'c' => 1 ) + ); + + $this->assertSame( + _wp_array_get( + array( + 'a' => array( + 'b' => array( + 'c' => 1, + ), + ), + ), + array( 'a', 'b', 'c' ) + ), + 1 + ); + + $this->assertSame( + _wp_array_get( + array( + 'a' => array( + 'b' => array( + 'c' => 1, + ), + ), + ), + array( 'a', 'b', 'c', 'd' ) + ), + null + ); + } + /** * Test _wp_array_get() with zero strings. * @@ -161,56 +210,6 @@ class Tests_Functions_wpArrayGet extends WP_UnitTestCase { ); } - - /** - * Test _wp_array_get() with subtrees. - * - * @ticket 51720 - */ - public function test_wp_array_get_subtree() { - $this->assertSame( - _wp_array_get( - array( - 'a' => array( - 'b' => array( - 'c' => 1, - ), - ), - ), - array( 'a', 'b' ) - ), - array( 'c' => 1 ) - ); - - $this->assertSame( - _wp_array_get( - array( - 'a' => array( - 'b' => array( - 'c' => 1, - ), - ), - ), - array( 'a', 'b', 'c' ) - ), - 1 - ); - - $this->assertSame( - _wp_array_get( - array( - 'a' => array( - 'b' => array( - 'c' => 1, - ), - ), - ), - array( 'a', 'b', 'c', 'd' ) - ), - null - ); - } - /** * Test _wp_array_get() with null values. * diff --git a/tests/phpunit/tests/wp-array-set.php b/tests/phpunit/tests/functions/wpArraySet.php similarity index 85% rename from tests/phpunit/tests/wp-array-set.php rename to tests/phpunit/tests/functions/wpArraySet.php index d1e8a9bf8b..9ed16253e6 100644 --- a/tests/phpunit/tests/wp-array-set.php +++ b/tests/phpunit/tests/functions/wpArraySet.php @@ -1,22 +1,63 @@ assertSame( + $test, + 3 + ); + + $test_array = array( 'a' => 2 ); + _wp_array_set( $test_array, 'a', 3 ); + $this->assertSame( + $test_array, + array( 'a' => 2 ) + ); + + $test_array = array( 'a' => 2 ); + _wp_array_set( $test_array, null, 3 ); + $this->assertSame( + $test_array, + array( 'a' => 2 ) + ); + + $test_array = array( 'a' => 2 ); + _wp_array_set( $test_array, array(), 3 ); + $this->assertSame( + $test_array, + array( 'a' => 2 ) + ); + + $test_array = array( 'a' => 2 ); + _wp_array_set( $test_array, array( 'a', array() ), 3 ); + $this->assertSame( + $test_array, + array( 'a' => 2 ) + ); + } + + /** + * Test _wp_array_set() with simple non-subtree path. + * + * @ticket 53175 + */ + public function test_wp_array_set_simple_non_subtree() { $test_array = array(); _wp_array_set( $test_array, array( 'a' ), 1 ); $this->assertSame( @@ -47,7 +88,7 @@ class WP_Array_Set_Test extends WP_UnitTestCase { * * @ticket 53175 */ - public function test_subtree_set() { + public function test_wp_array_set_subtree() { $test_array = array(); _wp_array_set( $test_array, array( 'a', 'b', 'c' ), 1 ); $this->assertSame( @@ -91,46 +132,4 @@ class WP_Array_Set_Test extends WP_UnitTestCase { ) ); } - - /** - * Test _wp_array_set() with invalid parameters. - * - * @ticket 53175 - */ - public function test_invalid_parameters_set() { - $test = 3; - _wp_array_set( $test, array( 'a' ), 1 ); - $this->assertSame( - $test, - 3 - ); - - $test_array = array( 'a' => 2 ); - _wp_array_set( $test_array, 'a', 3 ); - $this->assertSame( - $test_array, - array( 'a' => 2 ) - ); - - $test_array = array( 'a' => 2 ); - _wp_array_set( $test_array, null, 3 ); - $this->assertSame( - $test_array, - array( 'a' => 2 ) - ); - - $test_array = array( 'a' => 2 ); - _wp_array_set( $test_array, array(), 3 ); - $this->assertSame( - $test_array, - array( 'a' => 2 ) - ); - - $test_array = array( 'a' => 2 ); - _wp_array_set( $test_array, array( 'a', array() ), 3 ); - $this->assertSame( - $test_array, - array( 'a' => 2 ) - ); - } }