WP_REST_Search_Handler
云策文档标注
概述
WP_REST_Search_Handler 是 WordPress REST API 中用于处理对象类型搜索的核心抽象基类。它定义了搜索处理器的基本结构和方法,包括获取对象类型和子类型、搜索项目、准备搜索结果和链接等。
关键要点
- WP_REST_Search_Handler 是一个抽象类,用于为特定对象类型(如文章、术语)提供 REST API 搜索功能。
- 类中包含常量 RESULT_IDS 和 RESULT_TOTAL,分别用于标识搜索结果中的 ID 数组和总数。
- 主要方法包括 get_type() 和 get_subtypes() 用于获取对象类型和子类型,search_items() 用于执行搜索,prepare_item() 和 prepare_item_links() 用于准备搜索结果和链接。
- 该类自 WordPress 5.0.0 版本引入,是其他搜索处理器(如 WP_REST_Post_Search_Handler)的基础。
代码示例
abstract class WP_REST_Search_Handler {
const RESULT_IDS = 'ids';
const RESULT_TOTAL = 'total';
protected $type = '';
protected $subtypes = array();
public function get_type() {
return $this->type;
}
public function get_subtypes() {
return $this->subtypes;
}
abstract public function search_items( WP_REST_Request $request );
abstract public function prepare_item( $id, array $fields );
abstract public function prepare_item_links( $id );
}
原文内容
Core base class representing a search handler for an object type in the REST API.
Methods
| Name | Description |
|---|---|
| WP_REST_Search_Handler::get_subtypes | Gets the object subtypes managed by this search handler. |
| WP_REST_Search_Handler::get_type | Gets the object type managed by this search handler. |
| WP_REST_Search_Handler::prepare_item | Prepares the search result for a given ID. |
| WP_REST_Search_Handler::prepare_item_links | Prepares links for the search result of a given ID. |
| WP_REST_Search_Handler::search_items | Searches the object type content for a given search request. |
Source
abstract class WP_REST_Search_Handler {
/**
* Field containing the IDs in the search result.
*/
const RESULT_IDS = 'ids';
/**
* Field containing the total count in the search result.
*/
const RESULT_TOTAL = 'total';
/**
* Object type managed by this search handler.
*
* @since 5.0.0
* @var string
*/
protected $type = '';
/**
* Object subtypes managed by this search handler.
*
* @since 5.0.0
* @var string[]
*/
protected $subtypes = array();
/**
* Gets the object type managed by this search handler.
*
* @since 5.0.0
*
* @return string Object type identifier.
*/
public function get_type() {
return $this->type;
}
/**
* Gets the object subtypes managed by this search handler.
*
* @since 5.0.0
*
* @return string[] Array of object subtype identifiers.
*/
public function get_subtypes() {
return $this->subtypes;
}
/**
* Searches the object type content for a given search request.
*
* @since 5.0.0
*
* @param WP_REST_Request $request Full REST request.
* @return array Associative array containing an `WP_REST_Search_Handler::RESULT_IDS` containing
* an array of found IDs and `WP_REST_Search_Handler::RESULT_TOTAL` containing the
* total count for the matching search results.
*/
abstract public function search_items( WP_REST_Request $request );
/**
* Prepares the search result for a given ID.
*
* @since 5.0.0
* @since 5.6.0 The `$id` parameter can accept a string.
*
* @param int|string $id Item ID.
* @param array $fields Fields to include for the item.
* @return array Associative array containing all fields for the item.
*/
abstract public function prepare_item( $id, array $fields );
/**
* Prepares links for the search result of a given ID.
*
* @since 5.0.0
* @since 5.6.0 The `$id` parameter can accept a string.
*
* @param int|string $id Item ID.
* @return array Links for the given item.
*/
abstract public function prepare_item_links( $id );
}
Changelog
| Version | Description |
|---|---|
| 5.0.0 | Introduced. |