WordPress.org

Make WordPress Core

Ticket #22300: map_deep.diff

File map_deep.diff, 3.5 KB (added by nbachiyski, 6 years ago)
  • wp-includes/formatting.php

     
    14001400}
    14011401
    14021402/**
    1403  * Navigates through an array and removes slashes from the values.
     1403 * Navigates through a scalar, array, or an object and removes slashes from the values.
    14041404 *
    1405  * If an array is passed, the array_map() function causes a callback to pass the
    1406  * value back to the function. The slashes from this value will removed.
    1407  *
    14081405 * @since 2.0.0
    14091406 *
    14101407 * @param mixed $value The value to be stripped.
    14111408 * @return mixed Stripped value.
    14121409 */
    14131410function 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}
    14241413
    1425         return $value;
     1414function stripslashes_from_strings_only( $value ) {
     1415        return is_string( $value ) ? stripslashes( $value ) : $value;
    14261416}
    14271417
    14281418/**
    1429  * Navigates through an array and 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.
    14301420 *
    1431  *
    14321421 * @since 2.2.0
    14331422 *
    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
    14361425 */
    14371426function urlencode_deep($value) {
    1438         $value = is_array($value) ? array_map('urlencode_deep', $value) : urlencode($value);
    1439         return $value;
     1427        return map_deep( 'urlencode', $value );
    14401428}
    14411429
    14421430/**
    1443  * Navigates through an array and raw encodes 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.
    14441432 *
    14451433 * @since 3.4.0
    14461434 *
    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
    14491437 */
    14501438function rawurlencode_deep( $value ) {
    1451         return is_array( $value ) ? array_map( 'rawurlencode_deep', $value ) : rawurlencode( $value );
     1439        return map_deep( 'rawurlencode', $value );
    14521440}
    14531441
    14541442/**
     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 */
     1450function urldecode_deep( $value ) {
     1451        return map_deep( 'urldecode', $value );
     1452}
     1453
     1454/**
    14551455 * Converts email addresses characters to HTML entities to block spam bots.
    14561456 *
    14571457 * @since 0.71
     
    29152915}
    29162916
    29172917/**
     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 */
     2924function 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/**
    29182936 * Parses a string into variables to be stored in an array.
    29192937 *
    29202938 * Uses {@link http://www.php.net/parse_str parse_str()} and stripslashes if