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
This commit is contained in:
Dion Hulse 2015-12-26 05:21:14 +00:00
parent 42aeb0af8b
commit 0ce64dd122
2 changed files with 35 additions and 5 deletions

View File

@ -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;
}
/**

View File

@ -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';
}