Make WordPress Core

Changeset 35252


Ignore:
Timestamp:
10/17/2015 11:25:21 PM (9 years ago)
Author:
johnbillion
Message:

Introduce map_deep(), a utility function that recursively maps a callable function to every item in an array or object. Works like array_walk_recursive() but works with objects too.

Updates rawurlencode_deep(), urlencode_deep(), and stripslashes_deep() to use map_deep(). Introduces urldecode_deep() for completeness.

Props wpmuguru, nbachiyski, boonebgorges, MikeHansenMe, chriscct7, realloc, johnbillion
Fixes #22300

Location:
trunk
Files:
2 added
1 edited

Legend:

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

    r35170 r35252  
    20102010
    20112011/**
    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.
    20162013 *
    20172014 * @since 2.0.0
     
    20212018 */
    20222019function 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;
    2035 }
    2036 
    2037 /**
    2038  * Navigates through an array and encodes the values to be used in a URL.
    2039  *
     2020    return map_deep( $value, 'stripslashes_from_strings_only' );
     2021}
     2022
     2023/**
     2024 * Callback function for `stripslashes_deep()` which strips slashes from strings.
     2025 *
     2026 * @since 4.4.0
     2027 *
     2028 * @param mixed $value The array or string to be stripped.
     2029 * @return mixed $value The stripped value.
     2030 */
     2031function 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.
    20402037 *
    20412038 * @since 2.2.0
    20422039 *
    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.
    20452042 */
    20462043function urlencode_deep( $value ) {
    2047     return is_array( $value ) ? array_map( 'urlencode_deep', $value ) : urlencode( $value );
    2048 }
    2049 
    2050 /**
    2051  * Navigates through an array and raw encodes the values to be used in a URL.
     2044    return map_deep( $value, 'urlencode' );
     2045}
     2046
     2047/**
     2048 * Navigates through an array, object, or scalar, and raw-encodes the values to be used in a URL.
    20522049 *
    20532050 * @since 3.4.0
    20542051 *
    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.
    20572054 */
    20582055function 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 */
     2067function urldecode_deep( $value ) {
     2068    return map_deep( $value, 'urldecode' );
    20602069}
    20612070
     
    38643873
    38653874/**
     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 */
     3885function 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/**
    38663897 * Parses a string into variables to be stored in an array.
    38673898 *
Note: See TracChangeset for help on using the changeset viewer.