rest_preload_api_request

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

WordPress Version: 6.3

/**
 * Append result of internal request to REST API for purpose of preloading data to be attached to a page.
 * Expected to be called in the context of `array_reduce`.
 *
 * @since 5.0.0
 *
 * @param array  $memo Reduce accumulator.
 * @param string $path REST API path to preload.
 * @return array Modified reduce accumulator.
 */
function rest_preload_api_request($memo, $path)
{
    /*
     * array_reduce() doesn't support passing an array in PHP 5.2,
     * so we need to make sure we start with one.
     */
    if (!is_array($memo)) {
        $memo = array();
    }
    if (empty($path)) {
        return $memo;
    }
    $method = 'GET';
    if (is_array($path) && 2 === count($path)) {
        $method = end($path);
        $path = reset($path);
        if (!in_array($method, array('GET', 'OPTIONS'), true)) {
            $method = 'GET';
        }
    }
    $path = untrailingslashit($path);
    if (empty($path)) {
        $path = '/';
    }
    $path_parts = parse_url($path);
    if (false === $path_parts) {
        return $memo;
    }
    $request = new WP_REST_Request($method, $path_parts['path']);
    if (!empty($path_parts['query'])) {
        parse_str($path_parts['query'], $query_params);
        $request->set_query_params($query_params);
    }
    $response = rest_do_request($request);
    if (200 === $response->status) {
        $server = rest_get_server();
        /** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
        $response = apply_filters('rest_post_dispatch', rest_ensure_response($response), $server, $request);
        $embed = $request->has_param('_embed') ? rest_parse_embed_param($request['_embed']) : false;
        $data = (array) $server->response_to_data($response, $embed);
        if ('OPTIONS' === $method) {
            $memo[$method][$path] = array('body' => $data, 'headers' => $response->headers);
        } else {
            $memo[$path] = array('body' => $data, 'headers' => $response->headers);
        }
    }
    return $memo;
}

WordPress Version: 6.1

/**
 * Append result of internal request to REST API for purpose of preloading data to be attached to a page.
 * Expected to be called in the context of `array_reduce`.
 *
 * @since 5.0.0
 *
 * @param array  $memo Reduce accumulator.
 * @param string $path REST API path to preload.
 * @return array Modified reduce accumulator.
 */
function rest_preload_api_request($memo, $path)
{
    // array_reduce() doesn't support passing an array in PHP 5.2,
    // so we need to make sure we start with one.
    if (!is_array($memo)) {
        $memo = array();
    }
    if (empty($path)) {
        return $memo;
    }
    $method = 'GET';
    if (is_array($path) && 2 === count($path)) {
        $method = end($path);
        $path = reset($path);
        if (!in_array($method, array('GET', 'OPTIONS'), true)) {
            $method = 'GET';
        }
    }
    $path = untrailingslashit($path);
    if (empty($path)) {
        $path = '/';
    }
    $path_parts = parse_url($path);
    if (false === $path_parts) {
        return $memo;
    }
    $request = new WP_REST_Request($method, $path_parts['path']);
    if (!empty($path_parts['query'])) {
        parse_str($path_parts['query'], $query_params);
        $request->set_query_params($query_params);
    }
    $response = rest_do_request($request);
    if (200 === $response->status) {
        $server = rest_get_server();
        /** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
        $response = apply_filters('rest_post_dispatch', rest_ensure_response($response), $server, $request);
        $embed = $request->has_param('_embed') ? rest_parse_embed_param($request['_embed']) : false;
        $data = (array) $server->response_to_data($response, $embed);
        if ('OPTIONS' === $method) {
            $memo[$method][$path] = array('body' => $data, 'headers' => $response->headers);
        } else {
            $memo[$path] = array('body' => $data, 'headers' => $response->headers);
        }
    }
    return $memo;
}

WordPress Version: 5.9

/**
 * Append result of internal request to REST API for purpose of preloading data to be attached to a page.
 * Expected to be called in the context of `array_reduce`.
 *
 * @since 5.0.0
 *
 * @param array  $memo Reduce accumulator.
 * @param string $path REST API path to preload.
 * @return array Modified reduce accumulator.
 */
function rest_preload_api_request($memo, $path)
{
    // array_reduce() doesn't support passing an array in PHP 5.2,
    // so we need to make sure we start with one.
    if (!is_array($memo)) {
        $memo = array();
    }
    if (empty($path)) {
        return $memo;
    }
    $method = 'GET';
    if (is_array($path) && 2 === count($path)) {
        $method = end($path);
        $path = reset($path);
        if (!in_array($method, array('GET', 'OPTIONS'), true)) {
            $method = 'GET';
        }
    }
    $path = untrailingslashit($path);
    if (empty($path)) {
        $path = '/';
    }
    $path_parts = parse_url($path);
    if (false === $path_parts) {
        return $memo;
    }
    $request = new WP_REST_Request($method, $path_parts['path']);
    if (!empty($path_parts['query'])) {
        parse_str($path_parts['query'], $query_params);
        $request->set_query_params($query_params);
    }
    $response = rest_do_request($request);
    if (200 === $response->status) {
        $server = rest_get_server();
        $embed = $request->has_param('_embed') ? rest_parse_embed_param($request['_embed']) : false;
        $data = (array) $server->response_to_data($response, $embed);
        if ('OPTIONS' === $method) {
            $response = rest_send_allow_header($response, $server, $request);
            $memo[$method][$path] = array('body' => $data, 'headers' => $response->headers);
        } else {
            $memo[$path] = array('body' => $data, 'headers' => $response->headers);
        }
    }
    return $memo;
}

WordPress Version: 5.7

/**
 * Append result of internal request to REST API for purpose of preloading data to be attached to a page.
 * Expected to be called in the context of `array_reduce`.
 *
 * @since 5.0.0
 *
 * @param array  $memo Reduce accumulator.
 * @param string $path REST API path to preload.
 * @return array Modified reduce accumulator.
 */
function rest_preload_api_request($memo, $path)
{
    // array_reduce() doesn't support passing an array in PHP 5.2,
    // so we need to make sure we start with one.
    if (!is_array($memo)) {
        $memo = array();
    }
    if (empty($path)) {
        return $memo;
    }
    $method = 'GET';
    if (is_array($path) && 2 === count($path)) {
        $method = end($path);
        $path = reset($path);
        if (!in_array($method, array('GET', 'OPTIONS'), true)) {
            $method = 'GET';
        }
    }
    $path_parts = parse_url($path);
    if (false === $path_parts) {
        return $memo;
    }
    $request = new WP_REST_Request($method, $path_parts['path']);
    if (!empty($path_parts['query'])) {
        parse_str($path_parts['query'], $query_params);
        $request->set_query_params($query_params);
    }
    $response = rest_do_request($request);
    if (200 === $response->status) {
        $server = rest_get_server();
        $embed = $request->has_param('_embed') ? rest_parse_embed_param($request['_embed']) : false;
        $data = (array) $server->response_to_data($response, $embed);
        if ('OPTIONS' === $method) {
            $response = rest_send_allow_header($response, $server, $request);
            $memo[$method][$path] = array('body' => $data, 'headers' => $response->headers);
        } else {
            $memo[$path] = array('body' => $data, 'headers' => $response->headers);
        }
    }
    return $memo;
}

WordPress Version: 5.5

/**
 * Append result of internal request to REST API for purpose of preloading data to be attached to a page.
 * Expected to be called in the context of `array_reduce`.
 *
 * @since 5.0.0
 *
 * @param array  $memo Reduce accumulator.
 * @param string $path REST API path to preload.
 * @return array Modified reduce accumulator.
 */
function rest_preload_api_request($memo, $path)
{
    // array_reduce() doesn't support passing an array in PHP 5.2,
    // so we need to make sure we start with one.
    if (!is_array($memo)) {
        $memo = array();
    }
    if (empty($path)) {
        return $memo;
    }
    $method = 'GET';
    if (is_array($path) && 2 === count($path)) {
        $method = end($path);
        $path = reset($path);
        if (!in_array($method, array('GET', 'OPTIONS'), true)) {
            $method = 'GET';
        }
    }
    $path_parts = parse_url($path);
    if (false === $path_parts) {
        return $memo;
    }
    $request = new WP_REST_Request($method, $path_parts['path']);
    if (!empty($path_parts['query'])) {
        parse_str($path_parts['query'], $query_params);
        $request->set_query_params($query_params);
    }
    $response = rest_do_request($request);
    if (200 === $response->status) {
        $server = rest_get_server();
        $data = (array) $response->get_data();
        $links = $server::get_compact_response_links($response);
        if (!empty($links)) {
            $data['_links'] = $links;
        }
        if ('OPTIONS' === $method) {
            $response = rest_send_allow_header($response, $server, $request);
            $memo[$method][$path] = array('body' => $data, 'headers' => $response->headers);
        } else {
            $memo[$path] = array('body' => $data, 'headers' => $response->headers);
        }
    }
    return $memo;
}

WordPress Version: 5.4

/**
 * Append result of internal request to REST API for purpose of preloading data to be attached to a page.
 * Expected to be called in the context of `array_reduce`.
 *
 * @since 5.0.0
 *
 * @param  array  $memo Reduce accumulator.
 * @param  string $path REST API path to preload.
 * @return array        Modified reduce accumulator.
 */
function rest_preload_api_request($memo, $path)
{
    // array_reduce() doesn't support passing an array in PHP 5.2,
    // so we need to make sure we start with one.
    if (!is_array($memo)) {
        $memo = array();
    }
    if (empty($path)) {
        return $memo;
    }
    $method = 'GET';
    if (is_array($path) && 2 === count($path)) {
        $method = end($path);
        $path = reset($path);
        if (!in_array($method, array('GET', 'OPTIONS'), true)) {
            $method = 'GET';
        }
    }
    $path_parts = parse_url($path);
    if (false === $path_parts) {
        return $memo;
    }
    $request = new WP_REST_Request($method, $path_parts['path']);
    if (!empty($path_parts['query'])) {
        parse_str($path_parts['query'], $query_params);
        $request->set_query_params($query_params);
    }
    $response = rest_do_request($request);
    if (200 === $response->status) {
        $server = rest_get_server();
        $data = (array) $response->get_data();
        $links = $server::get_compact_response_links($response);
        if (!empty($links)) {
            $data['_links'] = $links;
        }
        if ('OPTIONS' === $method) {
            $response = rest_send_allow_header($response, $server, $request);
            $memo[$method][$path] = array('body' => $data, 'headers' => $response->headers);
        } else {
            $memo[$path] = array('body' => $data, 'headers' => $response->headers);
        }
    }
    return $memo;
}

WordPress Version: 5.3

/**
 * Append result of internal request to REST API for purpose of preloading data to be attached to a page.
 * Expected to be called in the context of `array_reduce`.
 *
 * @since 5.0.0
 *
 * @param  array  $memo Reduce accumulator.
 * @param  string $path REST API path to preload.
 * @return array        Modified reduce accumulator.
 */
function rest_preload_api_request($memo, $path)
{
    // array_reduce() doesn't support passing an array in PHP 5.2, so we need to make sure we start with one.
    if (!is_array($memo)) {
        $memo = array();
    }
    if (empty($path)) {
        return $memo;
    }
    $method = 'GET';
    if (is_array($path) && 2 === count($path)) {
        $method = end($path);
        $path = reset($path);
        if (!in_array($method, array('GET', 'OPTIONS'), true)) {
            $method = 'GET';
        }
    }
    $path_parts = parse_url($path);
    if (false === $path_parts) {
        return $memo;
    }
    $request = new WP_REST_Request($method, $path_parts['path']);
    if (!empty($path_parts['query'])) {
        parse_str($path_parts['query'], $query_params);
        $request->set_query_params($query_params);
    }
    $response = rest_do_request($request);
    if (200 === $response->status) {
        $server = rest_get_server();
        $data = (array) $response->get_data();
        $links = $server::get_compact_response_links($response);
        if (!empty($links)) {
            $data['_links'] = $links;
        }
        if ('OPTIONS' === $method) {
            $response = rest_send_allow_header($response, $server, $request);
            $memo[$method][$path] = array('body' => $data, 'headers' => $response->headers);
        } else {
            $memo[$path] = array('body' => $data, 'headers' => $response->headers);
        }
    }
    return $memo;
}

WordPress Version: 5.0

/**
 * Append result of internal request to REST API for purpose of preloading data to be attached to a page.
 * Expected to be called in the context of `array_reduce`.
 *
 * @since 5.0.0
 *
 * @param  array  $memo Reduce accumulator.
 * @param  string $path REST API path to preload.
 * @return array        Modified reduce accumulator.
 */
function rest_preload_api_request($memo, $path)
{
    // array_reduce() doesn't support passing an array in PHP 5.2, so we need to make sure we start with one.
    if (!is_array($memo)) {
        $memo = array();
    }
    if (empty($path)) {
        return $memo;
    }
    $method = 'GET';
    if (is_array($path) && 2 === count($path)) {
        $method = end($path);
        $path = reset($path);
        if (!in_array($method, array('GET', 'OPTIONS'), true)) {
            $method = 'GET';
        }
    }
    $path_parts = parse_url($path);
    if (false === $path_parts) {
        return $memo;
    }
    $request = new WP_REST_Request($method, $path_parts['path']);
    if (!empty($path_parts['query'])) {
        parse_str($path_parts['query'], $query_params);
        $request->set_query_params($query_params);
    }
    $response = rest_do_request($request);
    if (200 === $response->status) {
        $server = rest_get_server();
        $data = (array) $response->get_data();
        $links = $server->get_compact_response_links($response);
        if (!empty($links)) {
            $data['_links'] = $links;
        }
        if ('OPTIONS' === $method) {
            $response = rest_send_allow_header($response, $server, $request);
            $memo[$method][$path] = array('body' => $data, 'headers' => $response->headers);
        } else {
            $memo[$path] = array('body' => $data, 'headers' => $response->headers);
        }
    }
    return $memo;
}