深入理解 WordPress `WP_REST_Request` 类的源码:它如何封装 HTTP 请求数据。

各位观众老爷们,晚上好! 今天咱们来聊聊 WordPress 里一个非常重要的家伙—— WP_REST_Request 类。 这玩意儿,你别看名字长,其实就是 WordPress REST API 的“快递小哥”,专门负责把 HTTP 请求里的各种数据打包好,送到后端处理。

一、 啥是 WP_REST_Request? 简单来说就是个“请求数据收纳盒”

想象一下,你开了一家餐厅,客人通过手机 APP 点餐。APP 会向你的服务器发送一个 HTTP 请求,里面包含了客人点的菜品、数量、收货地址等等信息。 WP_REST_Request 就相当于餐厅的前台收银员,负责把这些信息都整理好,然后交给后厨(也就是你的 WordPress 插件或主题)。

更官方一点的说法: WP_REST_Request 类是 WordPress REST API 的核心组成部分,它封装了 HTTP 请求的所有数据,例如:

  • 请求方法 (GET, POST, PUT, DELETE 等)
  • 请求参数 (Query Parameters, POST Data)
  • 请求头 (Headers)
  • 文件上传 (Files)
  • 请求的 URL

二、 WP_REST_Request 类源码剖析: 看看它肚子里都装了啥

咱们直接上源码,看看这个“收纳盒”到底长啥样。 注意:这里的代码是简化后的,只包含关键部分,完整的源码在 WordPress 核心文件中。

<?php

class WP_REST_Request {

    /**
     * 请求方法 (GET, POST, PUT, DELETE 等)
     *
     * @var string
     */
    protected $method;

    /**
     * 请求参数
     *
     * @var array
     */
    protected $params = array();

    /**
     * 请求头
     *
     * @var array
     */
    protected $headers = array();

    /**
     * 上传的文件
     *
     * @var array
     */
    protected $files = array();

    /**
     * 属性。可以存储任何与请求相关的数据。
     *
     * @var array
     */
    protected $attributes = array();

    /**
     * 构造函数
     *
     * @param string $method 请求方法
     */
    public function __construct( $method = 'GET' ) {
        $this->set_method( $method );
    }

    /**
     * 设置请求方法
     *
     * @param string $method 请求方法
     */
    public function set_method( $method ) {
        $this->method = strtoupper( $method );
    }

    /**
     * 获取请求方法
     *
     * @return string 请求方法
     */
    public function get_method() {
        return $this->method;
    }

    /**
     * 设置参数
     *
     * @param string|array $key   参数名或参数数组
     * @param mixed        $value 参数值
     */
    public function set_param( $key, $value ) {
        if ( is_array( $key ) ) {
            $this->params = array_merge( $this->params, $key );
        } else {
            $this->params[ $key ] = $value;
        }
    }

    /**
     * 获取参数
     *
     * @param string $key     参数名
     * @param mixed  $default 默认值 (如果参数不存在)
     * @return mixed 参数值
     */
    public function get_param( $key, $default = null ) {
        if ( isset( $this->params[ $key ] ) ) {
            return $this->params[ $key ];
        }

        return $default;
    }

    /**
     * 获取所有参数
     *
     * @return array 所有参数
     */
    public function get_params() {
        return $this->params;
    }

    /**
     * 设置请求头
     *
     * @param string|array $key   请求头名或请求头数组
     * @param string       $value 请求头值
     */
    public function set_header( $key, $value ) {
        if ( is_array( $key ) ) {
            $this->headers = array_merge( $this->headers, $key );
        } else {
            $this->headers[ $key ] = $value;
        }
    }

    /**
     * 获取请求头
     *
     * @param string $key     请求头名
     * @param string $default 默认值 (如果请求头不存在)
     * @return string 请求头值
     */
    public function get_header( $key, $default = null ) {
        $key = strtolower( $key ); // 请求头名不区分大小写

        if ( isset( $this->headers[ $key ] ) ) {
            return $this->headers[ $key ];
        }

        return $default;
    }

    /**
     * 获取所有请求头
     *
     * @return array 所有请求头
     */
    public function get_headers() {
        return $this->headers;
    }

    /**
     * 设置文件
     *
     * @param string|array $key   文件名或文件数组
     * @param array        $value 文件信息 (通常是 $_FILES 数组中的一个元素)
     */
    public function set_file( $key, $value ) {
        if ( is_array( $key ) ) {
            $this->files = array_merge( $this->files, $key );
        } else {
            $this->files[ $key ] = $value;
        }
    }

    /**
     * 获取文件
     *
     * @param string $key     文件名
     * @param mixed  $default 默认值 (如果文件不存在)
     * @return array 文件信息
     */
    public function get_file( $key, $default = null ) {
        if ( isset( $this->files[ $key ] ) ) {
            return $this->files[ $key ];
        }

        return $default;
    }

    /**
     * 获取所有文件
     *
     * @return array 所有文件
     */
    public function get_files() {
        return $this->files;
    }

    /**
     * 设置属性
     *
     * @param string|array $key   属性名或属性数组
     * @param mixed        $value 属性值
     */
    public function set_attribute( $key, $value ) {
        if ( is_array( $key ) ) {
            $this->attributes = array_merge( $this->attributes, $key );
        } else {
            $this->attributes[ $key ] = $value;
        }
    }

    /**
     * 获取属性
     *
     * @param string $key     属性名
     * @param mixed  $default 默认值 (如果属性不存在)
     * @return mixed 属性值
     */
    public function get_attribute( $key, $default = null ) {
        if ( isset( $this->attributes[ $key ] ) ) {
            return $this->attributes[ $key ];
        }

        return $default;
    }

    /**
     * 获取所有属性
     *
     * @return array 所有属性
     */
    public function get_attributes() {
        return $this->attributes;
    }

    /**
     * 检查请求是否使用了 SSL
     *
     * @return bool
     */
    public function is_ssl() {
        return is_ssl();
    }

}

关键属性:

属性名 类型 描述
method string 请求方法,例如 'GET', 'POST', 'PUT', 'DELETE' 等。

发表回复

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