剖析 WordPress `WP_REST_Response` 类的源码:它如何构建 REST API 的响应。

嘿,各位代码界的探险家们,咱们今天来聊聊WordPress REST API 的幕后英雄——WP_REST_Response 类。 别担心,咱们不会像啃砖头一样啃源码,我会尽量用大白话加上一些小幽默,让这趟源码之旅变得轻松愉快。准备好了吗? Let’s dive in!

开场白:响应,一切的终点

在REST API的世界里,请求是开始,而响应则是终点。 你向服务器提出问题(发送请求),服务器思考一番后,给你一个答案(返回响应)。 这个答案,就是由WP_REST_Response类精心构造出来的。

WP_REST_Response:响应的骨架

WP_REST_Response 类,顾名思义,就是WordPress REST API 响应的核心类。 它负责将你的数据打包成一个标准的、可读性强的响应,让前端可以轻松解析并展示。 咱们先来看一下这个类的基本结构(为了简化,我省略了一些不太常用的属性和方法):

<?php

/**
 * Core class used to implement a REST response.
 *
 *
 */
class WP_REST_Response {

    /**
     * Response data.
     *
     * @var mixed
     */
    protected $data;

    /**
     * Response status code.
     *
     * @var int
     */
    protected $status = 200;

    /**
     * Response headers.
     *
     * @var array
     */
    protected $headers = array();

    /**
     * Response link headers.
     *
     * @var array
     */
    protected $links = array();

    /**
     * Constructor.
     *
     * Sets up the values needed.
     *
     * @param mixed $data Response data.
     * @param int   $status Optional. Status code for the response. Default 200.
     * @param array $headers Optional. Array of headers to set. Default empty array.
     */
    public function __construct( $data = null, $status = 200, $headers = array() ) {
        $this->data    = $data;
        $this->status  = $status;
        $this->headers = $headers;
    }

    /**
     * Gets the response data.
     *
     * @return mixed Response data.
     */
    public function get_data() {
        return $this->data;
    }

    /**
     * Sets the response data.
     *
     * @param mixed $data Response data.
     * @return WP_REST_Response The response object.
     */
    public function set_data( $data ) {
        $this->data = $data;
        return $this;
    }

    /**
     * Gets the response status code.
     *
     * @return int Response status code.
     */
    public function get_status() {
        return $this->status;
    }

    /**
     * Sets the response status code.
     *
     * @param int $status Response status code.
     * @return WP_REST_Response The response object.
     */
    public function set_status( $status ) {
        $this->status = (int) $status;
        return $this;
    }

    /**
     * Gets all the response headers.
     *
     * @return array Response headers.
     */
    public function get_headers() {
        return $this->headers;
    }

    /**
     * Gets a specific response header.
     *
     * @param string $key Header name to retrieve.
     * @return string|null Header value, or null if it isn't set.
     */
    public function get_header( $key ) {
        if ( isset( $this->headers[ $key ] ) ) {
            return $this->headers[ $key ];
        }

        return null;
    }

    /**
     * Sets a response header.
     *
     * @param string $key   Header name to set.
     * @param string $value Header value.
     * @return WP_REST_Response The response object.
     */
    public function set_header( $key, $value ) {
        $this->headers[ $key ] = $value;
        return $this;
    }

    /**
     * Sets all the response headers.
     *
     * @param array $headers Response headers.
     * @return WP_REST_Response The response object.
     */
    public function set_headers( $headers ) {
        $this->headers = $headers;
        return $this;
    }

    /**
     * Removes a response header.
     *
     * @param string $key Header name to remove.
     * @return WP_REST_Response The response object.
     */
    public function remove_header( $key ) {
        unset( $this->headers[ $key ] );
        return $this;
    }

    /**
     * Retrieves all the links associated with the response.
     *
     * @return array Links related to the response.
     */
    public function get_links() {
        return $this->links;
    }

    /**
     * Retrieves all the links associated with the response, in a specific relation.
     *
     * @param string $rel Relation to retrieve links for.
     * @return array Links related to the response.
     */
    public function get_links_by_rel( $rel ) {
        if ( ! isset( $this->links[ $rel ] ) ) {
            return array();
        }

        return $this->links[ $rel ];
    }

    /**
     * Add a link to the response.
     *
     * @param string $rel  Relation type.
     * @param string $href URI of the link.
     * @param array  $attributes Optional. Link attributes.
     * @return WP_REST_Response The response object.
     */
    public function add_link( $rel, $href, $attributes = array() ) {
        if ( empty( $this->links[ $rel ] ) ) {
            $this->links[ $rel ] = array();
        }

        $this->links[ $rel ][] = array_merge(
            array(
                'href' => $href,
            ),
            $attributes
        );

        return $this;
    }

    /**
     * Remove a link.
     *
     * @param string $rel  Relation type.
     * @param string $href URI of the link.
     * @return WP_REST_Response The response object.
     */
    public function remove_link( $rel, $href ) {
        if ( empty( $this->links[ $rel ] ) ) {
            return $this;
        }

        foreach ( $this->links[ $rel ] as $key => $link ) {
            if ( $link['href'] === $href ) {
                unset( $this->links[ $rel ][ $key ] );
            }
        }

        return $this;
    }

    /**
     * Embeds an item from the request.
     *
     * @param WP_REST_Request $request Request context.
     * @param array           $item    Data to embed.
     * @param array           $links   Links to embed.
     */
    protected function embed_links( $request, &$item, $links ) {
        if ( empty( $links ) ) {
            return;
        }

        $item['_links'] = array();

        foreach ( $links as $rel => $rel_links ) {
            $item['_links'][ $rel ] = array_map( array( $this, 'prepare_link' ), $rel_links );
        }
    }

    /**
     * Prepares a link for output.
     *
     * @param array $link Link details.
     * @return array Prepared link details.
     */
    protected function prepare_link( $link ) {
        return $link;
    }
}

咱们把它拆解一下:

属性 类型 描述

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注