Ticket #22300: 22300.4.diff
| File 22300.4.diff, 7.9 KB (added by , 10 years ago) |
|---|
-
src/wp-includes/formatting.php
2009 2009 } 2010 2010 2011 2011 /** 2012 * Navigates through an array and removes slashes from the values. 2013 * 2014 * If an array is passed, the array_map() function causes a callback to pass the 2015 * value back to the function. The slashes from this value will removed. 2012 * Navigates through an array, object, or scalar, and removes slashes from the values. 2016 2013 * 2017 2014 * @since 2.0.0 2018 2015 * … … 2020 2017 * @return mixed Stripped value. 2021 2018 */ 2022 2019 function stripslashes_deep( $value ) { 2023 if ( is_array($value) ) { 2024 $value = array_map('stripslashes_deep', $value); 2025 } elseif ( is_object($value) ) { 2026 $vars = get_object_vars( $value ); 2027 foreach ($vars as $key=>$data) { 2028 $value->{$key} = stripslashes_deep( $data ); 2029 } 2030 } elseif ( is_string( $value ) ) { 2031 $value = stripslashes($value); 2032 } 2033 2034 return $value; 2020 return map_deep( $value, 'stripslashes_from_strings_only' ); 2035 2021 } 2036 2022 2037 2023 /** 2038 * Navigates through an array and encodes the values to be used in a URL. 2024 * Callback function for `stripslashes_deep()` which strips slashes from strings. 2025 * 2026 * @since 4.4.0 2039 2027 * 2028 * @param mixed $value The array or string to be stripped. 2029 * @return mixed $value The stripped value. 2030 */ 2031 function stripslashes_from_strings_only( $value ) { 2032 return is_string( $value ) ? stripslashes( $value ) : $value; 2033 } 2034 2035 /** 2036 * Navigates through an array, object, or scalar, and encodes the values to be used in a URL. 2040 2037 * 2041 2038 * @since 2.2.0 2042 2039 * 2043 * @param array|string$value The array or string to be encoded.2044 * @return array|string $value The encoded array (or string from the callback).2040 * @param mixed $value The array or string to be encoded. 2041 * @return mixed $value The encoded value. 2045 2042 */ 2046 2043 function urlencode_deep( $value ) { 2047 return is_array( $value ) ? array_map( 'urlencode_deep', $value ) : urlencode( $value);2044 return map_deep( $value, 'urlencode' ); 2048 2045 } 2049 2046 2050 2047 /** 2051 * Navigates through an array and rawencodes the values to be used in a URL.2048 * Navigates through an array, object, or scalar, and raw-encodes the values to be used in a URL. 2052 2049 * 2053 2050 * @since 3.4.0 2054 2051 * 2055 * @param array|string$value The array or string to be encoded.2056 * @return array|string $value The encoded array (or string from the callback).2052 * @param mixed $value The array or string to be encoded. 2053 * @return mixed $value The encoded value. 2057 2054 */ 2058 2055 function rawurlencode_deep( $value ) { 2059 return is_array( $value ) ? array_map( 'rawurlencode_deep', $value ) : rawurlencode( $value ); 2056 return map_deep( $value, 'rawurlencode' ); 2057 } 2058 2059 /** 2060 * Navigates through an array, object, or scalar, and decodes URL-encoded values 2061 * 2062 * @since 4.4.0 2063 * 2064 * @param mixed $value The array or string to be decoded. 2065 * @return mixed $value The decoded value. 2066 */ 2067 function urldecode_deep( $value ) { 2068 return map_deep( $value, 'urldecode' ); 2060 2069 } 2061 2070 2062 2071 /** … … 3863 3872 } 3864 3873 3865 3874 /** 3875 * Maps a function to all non-iterable elements of an array or an object. 3876 * 3877 * This is similar to `array_walk_recursive()` but acts upon objects too. 3878 * 3879 * @since 4.4.0 3880 * 3881 * @param mixed $value The array, object, or scalar. 3882 * @param callable $function The function to map onto $value. 3883 * @return The value with the callback applied to all non-arrays and non-objects inside it. 3884 */ 3885 function map_deep( $value, $callback ) { 3886 if ( is_array( $value ) || is_object( $value ) ) { 3887 foreach ( $value as &$item ) { 3888 $item = map_deep( $item, $callback ); 3889 } 3890 return $value; 3891 } else { 3892 return call_user_func( $callback, $value ); 3893 } 3894 } 3895 3896 /** 3866 3897 * Parses a string into variables to be stored in an array. 3867 3898 * 3868 3899 * Uses {@link http://www.php.net/parse_str parse_str()} and stripslashes if -
tests/phpunit/tests/formatting/MapDeep.php
1 <?php 2 3 /** 4 * @group formatting 5 * @ticket 22300 6 */ 7 class Tests_Formatting_MapDeep extends WP_UnitTestCase { 8 9 public function test_map_deep_with_any_function_over_empty_array_should_return_empty_array() { 10 $this->assertEquals( array(), map_deep( array(), array( $this, 'append_baba' ) ) ); 11 } 12 13 public function test_map_deep_should_map_each_element_of_array_one_level_deep() { 14 $this->assertEquals( array( 15 'ababa', 16 'xbaba', 17 ), map_deep( array( 18 'a', 19 'x', 20 ), array( $this, 'append_baba' ) ) ); 21 } 22 23 public function test_map_deep_should_map_each_element_of_array_two_levels_deep() { 24 $this->assertEquals( array( 25 'ababa', 26 array( 27 'xbaba', 28 ), 29 ), map_deep( array( 30 'a', 31 array( 32 'x', 33 ), 34 ), array( $this, 'append_baba' ) ) ); 35 } 36 37 public function test_map_deep_should_map_each_object_element_of_an_array() { 38 $this->assertEquals( array( 39 'var0' => 'ababa', 40 'var1' => (object) array( 41 'xbaba', 42 ), 43 ), map_deep( array( 44 'var0' => 'a', 45 'var1' => (object) array( 46 'x', 47 ), 48 ), array( $this, 'append_baba' ) ) ); 49 } 50 51 public function test_map_deep_should_apply_the_function_to_a_string() { 52 $this->assertEquals( 'xbaba', map_deep( 'x', array( $this, 'append_baba' ) ) ); 53 } 54 55 public function test_map_deep_should_apply_the_function_to_an_integer() { 56 $this->assertEquals( '5baba' , map_deep( 5, array( $this, 'append_baba' ) ) ); 57 } 58 59 public function test_map_deep_should_map_each_property_of_an_object() { 60 $this->assertEquals( (object) array( 61 'var0' => 'ababa', 62 'var1' => 'xbaba', 63 ), map_deep( (object) array( 64 'var0' => 'a', 65 'var1' => 'x', 66 ), array( $this, 'append_baba' ) ) ); 67 } 68 69 public function test_map_deep_should_map_each_array_property_of_an_object() { 70 $this->assertEquals( (object) array( 71 'var0' => 'ababa', 72 'var1' => array( 73 'xbaba', 74 ), 75 ), map_deep( (object) array( 76 'var0' => 'a', 77 'var1' => array( 78 'x', 79 ), 80 ), array( $this, 'append_baba' ) ) ); 81 } 82 83 public function test_map_deep_should_map_each_object_property_of_an_object() { 84 $this->assertEquals( (object) array( 85 'var0' => 'ababa', 86 'var1' => (object) array( 87 'xbaba', 88 ), 89 ), map_deep( (object) array( 90 'var0' => 'a', 91 'var1' => (object) array( 92 'x', 93 ), 94 ), array( $this, 'append_baba' ) ) ); 95 } 96 97 public function append_baba( $value ) { 98 return $value . 'baba'; 99 } 100 101 } -
tests/phpunit/tests/formatting/UrlencodeDeep.php
1 <?php 2 3 /** 4 * @group formatting 5 * @ticket 22300 6 */ 7 class Tests_Formatting_UrlencodeDeep extends WP_UnitTestCase { 8 9 /** 10 * Data Provider 11 */ 12 public function data_test_values() { 13 return array( 14 array( 'qwerty123456', 'qwerty123456' ), 15 array( '|!"£$%&/()=?', '%7C%21%22%C2%A3%24%25%26%2F%28%29%3D%3F' ), 16 array( '^é*ç°§;:_-.,', '%5E%C3%A9%2A%C3%A7%C2%B0%C2%A7%3B%3A_-.%2C' ), 17 array( 'abc123 @#[]€', 'abc123+%40%23%5B%5D%E2%82%AC' ), 18 array( 'abc123 @#[]€', urlencode( 'abc123 @#[]€' ) ), 19 ); 20 } 21 22 /** 23 * Validate the urlencode_deep function pair by pair 24 * 25 * @dataProvider data_test_values 26 * 27 * @param string $actual 28 * @param string $expected 29 */ 30 public function test_urlencode_deep_should_encode_individual_value( $actual, $expected ) { 31 $this->assertEquals( $expected, urlencode_deep( $actual ) ); 32 } 33 34 /** 35 * Test the whole array as input 36 */ 37 public function test_urlencode_deep_should_encode_all_values_in_array() { 38 $data = $this->data_test_values(); 39 40 $actual = wp_list_pluck( $data, 0 ); 41 $expected = wp_list_pluck( $data, 1 ); 42 43 $this->assertEquals( $expected, urlencode_deep( $actual ) ); 44 } 45 46 }