From 0ce64dd12236cfc1ff69d9b8c8e8939d51590f39 Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Sat, 26 Dec 2015 05:21:14 +0000 Subject: [PATCH] Allow `map_deep()` to work with object properties containing a reference. Restores the previous behaviour of `stripslashes_deep()`. Props jeff@pyebrook.com, swissspidy. See #22300, [35252]. Fixes #35058. git-svn-id: https://develop.svn.wordpress.org/trunk@36100 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/formatting.php | 16 ++++++++++----- tests/phpunit/tests/formatting/MapDeep.php | 24 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index e9adb438f5..ac28d347f0 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -3884,14 +3884,20 @@ function sanitize_option( $option, $value ) { * @return The value with the callback applied to all non-arrays and non-objects inside it. */ function map_deep( $value, $callback ) { - if ( is_array( $value ) || is_object( $value ) ) { - foreach ( $value as &$item ) { - $item = map_deep( $item, $callback ); + if ( is_array( $value ) ) { + foreach ( $value as $index => $item ) { + $value[ $index ] = map_deep( $item, $callback ); + } + } elseif ( is_object( $value ) ) { + $object_vars = get_object_vars( $value ); + foreach ( $object_vars as $property_name => $property_value ) { + $value->$property_name = map_deep( $property_value, $callback ); } - return $value; } else { - return call_user_func( $callback, $value ); + $value = call_user_func( $callback, $value ); } + + return $value; } /** diff --git a/tests/phpunit/tests/formatting/MapDeep.php b/tests/phpunit/tests/formatting/MapDeep.php index 6fbd946a48..eb5914f357 100644 --- a/tests/phpunit/tests/formatting/MapDeep.php +++ b/tests/phpunit/tests/formatting/MapDeep.php @@ -94,6 +94,30 @@ class Tests_Formatting_MapDeep extends WP_UnitTestCase { ), array( $this, 'append_baba' ) ) ); } + /** + * @ticket 35058 + */ + public function test_map_deep_should_map_object_properties_passed_by_reference() { + $object_a = (object) array( 'var0' => 'a' ); + $object_b = (object) array( 'var0' => &$object_a->var0, 'var1' => 'x' ); + $this->assertEquals( (object) array( + 'var0' => 'ababa', + 'var1' => 'xbaba', + ), map_deep( $object_b, array( $this, 'append_baba' ) ) ); + } + + /** + * @ticket 35058 + */ + public function test_map_deep_should_map_array_elements_passed_by_reference() { + $array_a = array( 'var0' => 'a' ); + $array_b = array( 'var0' => &$array_a['var0'], 'var1' => 'x' ); + $this->assertEquals( array( + 'var0' => 'ababa', + 'var1' => 'xbaba', + ), map_deep( $array_b, array( $this, 'append_baba' ) ) ); + } + public function append_baba( $value ) { return $value . 'baba'; }