WordPress.org

Make WordPress Core

Changeset 36100


Ignore:
Timestamp:
12/26/2015 05:21:14 AM (5 years ago)
Author:
dd32
Message:

Allow map_deep() to work with object properties containing a reference. Restores the previous behaviour of stripslashes_deep().

Props jeff@…, swissspidy.
See #22300, [35252].
Fixes #35058.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/formatting.php

    r36097 r36100  
    38853885 */
    38863886function map_deep( $value, $callback ) {
    3887     if ( is_array( $value ) || is_object( $value ) ) {
    3888         foreach ( $value as &$item ) {
    3889             $item = map_deep( $item, $callback );
    3890         }
    3891         return $value;
     3887    if ( is_array( $value ) ) {
     3888        foreach ( $value as $index => $item ) {
     3889            $value[ $index ] = map_deep( $item, $callback );
     3890        }
     3891    } elseif ( is_object( $value ) ) {
     3892        $object_vars = get_object_vars( $value );
     3893        foreach ( $object_vars as $property_name => $property_value ) {
     3894            $value->$property_name = map_deep( $property_value, $callback );
     3895        }
    38923896    } else {
    3893         return call_user_func( $callback, $value );
    3894     }
     3897        $value = call_user_func( $callback, $value );
     3898    }
     3899
     3900    return $value;
    38953901}
    38963902
  • trunk/tests/phpunit/tests/formatting/MapDeep.php

    r35252 r36100  
    9595    }
    9696
     97    /**
     98     * @ticket 35058
     99     */
     100    public function test_map_deep_should_map_object_properties_passed_by_reference() {
     101        $object_a = (object) array( 'var0' => 'a' );
     102        $object_b = (object) array( 'var0' => &$object_a->var0, 'var1' => 'x' );
     103        $this->assertEquals( (object) array(
     104            'var0' => 'ababa',
     105            'var1' => 'xbaba',
     106        ), map_deep( $object_b, array( $this, 'append_baba' ) ) );
     107    }
     108
     109    /**
     110     * @ticket 35058
     111     */
     112    public function test_map_deep_should_map_array_elements_passed_by_reference() {
     113        $array_a = array( 'var0' => 'a' );
     114        $array_b = array( 'var0' => &$array_a['var0'], 'var1' => 'x' );
     115        $this->assertEquals( array(
     116            'var0' => 'ababa',
     117            'var1' => 'xbaba',
     118        ), map_deep( $array_b, array( $this, 'append_baba' ) ) );
     119    }
     120
    97121    public function append_baba( $value ) {
    98122        return $value . 'baba';
Note: See TracChangeset for help on using the changeset viewer.