Make WordPress Core

Ticket #35058: 35058.diff

File 35058.diff, 2.2 KB (added by swissspidy, 8 years ago)
  • src/wp-includes/formatting.php

    diff --git src/wp-includes/formatting.php src/wp-includes/formatting.php
    index 91690d8..3c688b9 100644
    function sanitize_option( $option, $value ) { 
    38803880 * @return The value with the callback applied to all non-arrays and non-objects inside it.
    38813881 */
    38823882function map_deep( $value, $callback ) {
    3883         if ( is_array( $value ) || is_object( $value ) ) {
    3884                 foreach ( $value as &$item ) {
    3885                         $item = map_deep( $item, $callback );
     3883        if ( is_array( $value ) ) {
     3884                foreach ( $value as $index => $item ) {
     3885                        $value[ $index ] = map_deep( $item, $callback );
     3886                }
     3887        } elseif ( is_object( $value ) ) {
     3888                $object_vars = get_object_vars( $value );
     3889                foreach ( $object_vars as $property_name => $property_value ) {
     3890                        $value->$property_name = map_deep( $property_value, $callback );
    38863891                }
    3887                 return $value;
    38883892        } else {
    3889                 return call_user_func( $callback, $value );
     3893                $value = call_user_func( $callback, $value );
    38903894        }
     3895
     3896        return $value;
    38913897}
    38923898
     3899
    38933900/**
    38943901 * Parses a string into variables to be stored in an array.
    38953902 *
  • tests/phpunit/tests/formatting/MapDeep.php

    diff --git tests/phpunit/tests/formatting/MapDeep.php tests/phpunit/tests/formatting/MapDeep.php
    index 6fbd946..eb5914f 100644
    class Tests_Formatting_MapDeep extends WP_UnitTestCase { 
    9494                ), array( $this, 'append_baba' ) ) );
    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';
    99123        }