/** * Fallback filter the API response to inject the Customized REST resources. * * This filter cannot apply on embedded resources since they get injected * after the rest_post_dispatch filter is called. For this reason, it serves * as a fallback if the customize_rest_server_response_data filter can't be * used in our WP_REST_Server subclass. * * @param \WP_HTTP_Response $result Result to send to the client. Usually a \WP_REST_Response. * @param \WP_REST_Server $server Server instance. * @param \WP_REST_Request $request Request used to generate the response. * @return \WP_REST_Response */ public static function filter_rest_post_dispatch($result, $server, $request) { // Skip filtering on rest_post_dispatch if our server subclass is used. if ($server instanceof WP_Customize_REST_Server) { return $result; } unset($request); $data = $result->get_data(); $links = null; if ($result instanceof \WP_REST_Response) { $links = $result->get_links(); } if (!empty($links)) { $data = static::filter_single_resource($data, $links); } else { if (isset($data[0])) { $data = array_map(array(__CLASS__, 'filter_single_resource'), $data); } } $result->set_data($data); return $result; }
/** * Make sure that the context for the request is made known so we know whether it can be customized. * * @param \WP_HTTP_Response $response Result to send to the client. Usually a WP_REST_Response. * @param \WP_REST_Server $server Server instance. * @param \WP_REST_Request $request Request used to generate the response. * @return \WP_HTTP_Response Response. */ public function export_context_with_response($response, $server, $request) { unset($server); if ('edit' === $request['context']) { $response->header('X-Customize-REST-Resources-Context', $request['context']); } return $response; }
/** * @param WP_HTTP_Response $result Result to send to the client. Usually a WP_REST_Response. * @param WP_REST_Server $this Server instance. * @param WP_REST_Request $request Request used to generate the response. * * @return WP_HTTP_Response */ function epoch_post_dispatch($result, $server, $request) { if (isset($_GET['epoch'], $_GET['post']) && 0 < absint($_GET['post'])) { $query = new WP_Comment_Query(array('post_ID' => $_GET['post'], 'number' => 1, 'order' => 'DESC')); $highest = 0; if (0 != $query->found_comments) { $highest = wp_list_pluck($query->comments, 'comment_ID'); } $result->header('X-EPOCH-HIGHEST', $highest); } return $result; }