Ticket #22300: map_deep.diff
| File map_deep.diff, 3.5 KB (added by , 13 years ago) |
|---|
-
wp-includes/formatting.php
1400 1400 } 1401 1401 1402 1402 /** 1403 * Navigates through a n arrayand removes slashes from the values.1403 * Navigates through a scalar, array, or an object and removes slashes from the values. 1404 1404 * 1405 * If an array is passed, the array_map() function causes a callback to pass the1406 * value back to the function. The slashes from this value will removed.1407 *1408 1405 * @since 2.0.0 1409 1406 * 1410 1407 * @param mixed $value The value to be stripped. 1411 1408 * @return mixed Stripped value. 1412 1409 */ 1413 1410 function stripslashes_deep($value) { 1414 if ( is_array($value) ) { 1415 $value = array_map('stripslashes_deep', $value); 1416 } elseif ( is_object($value) ) { 1417 $vars = get_object_vars( $value ); 1418 foreach ($vars as $key=>$data) { 1419 $value->{$key} = stripslashes_deep( $data ); 1420 } 1421 } elseif ( is_string( $value ) ) { 1422 $value = stripslashes($value); 1423 } 1411 return map_deep( 'stripslashes_from_strings_only', $value ); 1412 } 1424 1413 1425 return $value; 1414 function stripslashes_from_strings_only( $value ) { 1415 return is_string( $value ) ? stripslashes( $value ) : $value; 1426 1416 } 1427 1417 1428 1418 /** 1429 * Navigates through a n arrayand encodes the values to be used in a URL.1419 * Navigates through a scalar, array, or an object and encodes the values to be used in a URL. 1430 1420 * 1431 *1432 1421 * @since 2.2.0 1433 1422 * 1434 * @param array|string$value The array or string to be encoded.1435 * @return array|string $value The encoded array (or string from the callback).1423 * @param mixed $value The array or string to be encoded. 1424 * @return mixed $value The encoded value 1436 1425 */ 1437 1426 function urlencode_deep($value) { 1438 $value = is_array($value) ? array_map('urlencode_deep', $value) : urlencode($value); 1439 return $value; 1427 return map_deep( 'urlencode', $value ); 1440 1428 } 1441 1429 1442 1430 /** 1443 * Navigates through a n array and rawencodes the values to be used in a URL.1431 * Navigates through a scalar, array, or an object and raw-encodes the values to be used in a URL. 1444 1432 * 1445 1433 * @since 3.4.0 1446 1434 * 1447 * @param array|string$value The array or string to be encoded.1448 * @return array|string $value The encoded array (or string from the callback).1435 * @param mixed $value The array or string to be encoded. 1436 * @return mixed $value The encoded value 1449 1437 */ 1450 1438 function rawurlencode_deep( $value ) { 1451 return is_array( $value ) ? array_map( 'rawurlencode_deep', $value ) : rawurlencode($value );1439 return map_deep( 'rawurlencode', $value ); 1452 1440 } 1453 1441 1454 1442 /** 1443 * Navigates through a scalar, array, or an object and decodes URL-encoded values 1444 * 1445 * @since 3.5.0 1446 * 1447 * @param mixed $value The array or string to be decoded. 1448 * @return mixed $value The decoded value 1449 */ 1450 function urldecode_deep( $value ) { 1451 return map_deep( 'urldecode', $value ); 1452 } 1453 1454 /** 1455 1455 * Converts email addresses characters to HTML entities to block spam bots. 1456 1456 * 1457 1457 * @since 0.71 … … 2915 2915 } 2916 2916 2917 2917 /** 2918 * Maps a function to all non-iterable elements of an array or an object 2919 * 2920 * @param callback $f The function to map onto $value 2921 * @param mixed $value 2922 * @return $value with applied to all non-arrays and non-objects inside it 2923 */ 2924 function map_deep( $f, $value) { 2925 if ( is_array( $value ) || is_object( $value ) ) { 2926 foreach( $value as &$item ) { 2927 $item = map_deep( $f, $item ); 2928 } 2929 return $value; 2930 } else { 2931 return call_user_func( $f, $value ); 2932 } 2933 } 2934 2935 /** 2918 2936 * Parses a string into variables to be stored in an array. 2919 2937 * 2920 2938 * Uses {@link http://www.php.net/parse_str parse_str()} and stripslashes if