_wp_json_sanity_check

The timeline below displays how wordpress function _wp_json_sanity_check has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.5

/**
 * Performs confidence checks on data that shall be encoded to JSON.
 *
 * @ignore
 * @since 4.1.0
 * @access private
 *
 * @see wp_json_encode()
 *
 * @throws Exception If depth limit is reached.
 *
 * @param mixed $value Variable (usually an array or object) to encode as JSON.
 * @param int   $depth Maximum depth to walk through $value. Must be greater than 0.
 * @return mixed The sanitized data that shall be encoded to JSON.
 */
function _wp_json_sanity_check($value, $depth)
{
    if ($depth < 0) {
        throw new Exception('Reached depth limit');
    }
    if (is_array($value)) {
        $output = array();
        foreach ($value as $id => $el) {
            // Don't forget to sanitize the ID!
            if (is_string($id)) {
                $clean_id = _wp_json_convert_string($id);
            } else {
                $clean_id = $id;
            }
            // Check the element type, so that we're only recursing if we really have to.
            if (is_array($el) || is_object($el)) {
                $output[$clean_id] = _wp_json_sanity_check($el, $depth - 1);
            } elseif (is_string($el)) {
                $output[$clean_id] = _wp_json_convert_string($el);
            } else {
                $output[$clean_id] = $el;
            }
        }
    } elseif (is_object($value)) {
        $output = new stdClass();
        foreach ($value as $id => $el) {
            if (is_string($id)) {
                $clean_id = _wp_json_convert_string($id);
            } else {
                $clean_id = $id;
            }
            if (is_array($el) || is_object($el)) {
                $output->{$clean_id} = _wp_json_sanity_check($el, $depth - 1);
            } elseif (is_string($el)) {
                $output->{$clean_id} = _wp_json_convert_string($el);
            } else {
                $output->{$clean_id} = $el;
            }
        }
    } elseif (is_string($value)) {
        return _wp_json_convert_string($value);
    } else {
        return $value;
    }
    return $output;
}

WordPress Version: 6.1

/**
 * Performs sanity checks on data that shall be encoded to JSON.
 *
 * @ignore
 * @since 4.1.0
 * @access private
 *
 * @see wp_json_encode()
 *
 * @throws Exception If depth limit is reached.
 *
 * @param mixed $data  Variable (usually an array or object) to encode as JSON.
 * @param int   $depth Maximum depth to walk through $data. Must be greater than 0.
 * @return mixed The sanitized data that shall be encoded to JSON.
 */
function _wp_json_sanity_check($data, $depth)
{
    if ($depth < 0) {
        throw new Exception('Reached depth limit');
    }
    if (is_array($data)) {
        $output = array();
        foreach ($data as $id => $el) {
            // Don't forget to sanitize the ID!
            if (is_string($id)) {
                $clean_id = _wp_json_convert_string($id);
            } else {
                $clean_id = $id;
            }
            // Check the element type, so that we're only recursing if we really have to.
            if (is_array($el) || is_object($el)) {
                $output[$clean_id] = _wp_json_sanity_check($el, $depth - 1);
            } elseif (is_string($el)) {
                $output[$clean_id] = _wp_json_convert_string($el);
            } else {
                $output[$clean_id] = $el;
            }
        }
    } elseif (is_object($data)) {
        $output = new stdClass();
        foreach ($data as $id => $el) {
            if (is_string($id)) {
                $clean_id = _wp_json_convert_string($id);
            } else {
                $clean_id = $id;
            }
            if (is_array($el) || is_object($el)) {
                $output->{$clean_id} = _wp_json_sanity_check($el, $depth - 1);
            } elseif (is_string($el)) {
                $output->{$clean_id} = _wp_json_convert_string($el);
            } else {
                $output->{$clean_id} = $el;
            }
        }
    } elseif (is_string($data)) {
        return _wp_json_convert_string($data);
    } else {
        return $data;
    }
    return $output;
}

WordPress Version: 5.5

/**
 * Perform sanity checks on data that shall be encoded to JSON.
 *
 * @ignore
 * @since 4.1.0
 * @access private
 *
 * @see wp_json_encode()
 *
 * @throws Exception If depth limit is reached.
 *
 * @param mixed $data  Variable (usually an array or object) to encode as JSON.
 * @param int   $depth Maximum depth to walk through $data. Must be greater than 0.
 * @return mixed The sanitized data that shall be encoded to JSON.
 */
function _wp_json_sanity_check($data, $depth)
{
    if ($depth < 0) {
        throw new Exception('Reached depth limit');
    }
    if (is_array($data)) {
        $output = array();
        foreach ($data as $id => $el) {
            // Don't forget to sanitize the ID!
            if (is_string($id)) {
                $clean_id = _wp_json_convert_string($id);
            } else {
                $clean_id = $id;
            }
            // Check the element type, so that we're only recursing if we really have to.
            if (is_array($el) || is_object($el)) {
                $output[$clean_id] = _wp_json_sanity_check($el, $depth - 1);
            } elseif (is_string($el)) {
                $output[$clean_id] = _wp_json_convert_string($el);
            } else {
                $output[$clean_id] = $el;
            }
        }
    } elseif (is_object($data)) {
        $output = new stdClass();
        foreach ($data as $id => $el) {
            if (is_string($id)) {
                $clean_id = _wp_json_convert_string($id);
            } else {
                $clean_id = $id;
            }
            if (is_array($el) || is_object($el)) {
                $output->{$clean_id} = _wp_json_sanity_check($el, $depth - 1);
            } elseif (is_string($el)) {
                $output->{$clean_id} = _wp_json_convert_string($el);
            } else {
                $output->{$clean_id} = $el;
            }
        }
    } elseif (is_string($data)) {
        return _wp_json_convert_string($data);
    } else {
        return $data;
    }
    return $output;
}

WordPress Version: 4.2

/**
 * Perform sanity checks on data that shall be encoded to JSON.
 *
 * @ignore
 * @since 4.1.0
 * @access private
 *
 * @see wp_json_encode()
 *
 * @param mixed $data  Variable (usually an array or object) to encode as JSON.
 * @param int   $depth Maximum depth to walk through $data. Must be greater than 0.
 * @return mixed The sanitized data that shall be encoded to JSON.
 */
function _wp_json_sanity_check($data, $depth)
{
    if ($depth < 0) {
        throw new Exception('Reached depth limit');
    }
    if (is_array($data)) {
        $output = array();
        foreach ($data as $id => $el) {
            // Don't forget to sanitize the ID!
            if (is_string($id)) {
                $clean_id = _wp_json_convert_string($id);
            } else {
                $clean_id = $id;
            }
            // Check the element type, so that we're only recursing if we really have to.
            if (is_array($el) || is_object($el)) {
                $output[$clean_id] = _wp_json_sanity_check($el, $depth - 1);
            } elseif (is_string($el)) {
                $output[$clean_id] = _wp_json_convert_string($el);
            } else {
                $output[$clean_id] = $el;
            }
        }
    } elseif (is_object($data)) {
        $output = new stdClass();
        foreach ($data as $id => $el) {
            if (is_string($id)) {
                $clean_id = _wp_json_convert_string($id);
            } else {
                $clean_id = $id;
            }
            if (is_array($el) || is_object($el)) {
                $output->{$clean_id} = _wp_json_sanity_check($el, $depth - 1);
            } elseif (is_string($el)) {
                $output->{$clean_id} = _wp_json_convert_string($el);
            } else {
                $output->{$clean_id} = $el;
            }
        }
    } elseif (is_string($data)) {
        return _wp_json_convert_string($data);
    } else {
        return $data;
    }
    return $output;
}

WordPress Version: 4.1

/**
 * Perform sanity checks on data that shall be encoded to JSON.
 *
 * @see wp_json_encode()
 *
 * @since 4.1.0
 * @access private
 * @internal
 *
 * @param mixed $data  Variable (usually an array or object) to encode as JSON.
 * @param int   $depth Maximum depth to walk through $data. Must be greater than 0.
 * @return mixed The sanitized data that shall be encoded to JSON.
 */
function _wp_json_sanity_check($data, $depth)
{
    if ($depth < 0) {
        throw new Exception('Reached depth limit');
    }
    if (is_array($data)) {
        $output = array();
        foreach ($data as $id => $el) {
            // Don't forget to sanitize the ID!
            if (is_string($id)) {
                $clean_id = _wp_json_convert_string($id);
            } else {
                $clean_id = $id;
            }
            // Check the element type, so that we're only recursing if we really have to.
            if (is_array($el) || is_object($el)) {
                $output[$clean_id] = _wp_json_sanity_check($el, $depth - 1);
            } elseif (is_string($el)) {
                $output[$clean_id] = _wp_json_convert_string($el);
            } else {
                $output[$clean_id] = $el;
            }
        }
    } elseif (is_object($data)) {
        $output = new stdClass();
        foreach ($data as $id => $el) {
            if (is_string($id)) {
                $clean_id = _wp_json_convert_string($id);
            } else {
                $clean_id = $id;
            }
            if (is_array($el) || is_object($el)) {
                $output->{$clean_id} = _wp_json_sanity_check($el, $depth - 1);
            } elseif (is_string($el)) {
                $output->{$clean_id} = _wp_json_convert_string($el);
            } else {
                $output->{$clean_id} = $el;
            }
        }
    } elseif (is_string($data)) {
        return _wp_json_convert_string($data);
    } else {
        return $data;
    }
    return $output;
}