WP_REST_Blocks_Controller
云策文档标注
概述
WP_REST_Blocks_Controller 是一个 REST API 控制器,为编辑器提供读取、创建、编辑和删除同步模式(原可重用块)的端点。它扩展自 WP_REST_Posts_Controller,将模式存储为 wp_block 文章类型。
关键要点
- 提供 REST 端点管理同步模式(原可重用块),基于 wp_block 文章类型。
- 扩展 WP_REST_Posts_Controller,继承其核心功能。
- 包含关键方法:check_read_permission(检查读取权限)、filter_response_by_context(基于上下文过滤响应)、get_item_schema(获取模式架构)。
- 在响应中移除 title.rendered 和 content.rendered,添加 wp_pattern_sync_status 元数据。
- 架构中允许所有上下文访问 title.raw 和 content.raw,移除 rendered 属性。
代码示例
public function check_read_permission( $post ) {
if ( ! current_user_can( 'read_post', $post->ID ) ) {
return false;
}
return parent::check_read_permission( $post );
}
public function filter_response_by_context( $data, $context ) {
$data = parent::filter_response_by_context( $data, $context );
unset( $data['title']['rendered'] );
unset( $data['content']['rendered'] );
$data['wp_pattern_sync_status'] = isset( $data['meta']['wp_pattern_sync_status'] ) ? $data['meta']['wp_pattern_sync_status'] : '';
unset( $data['meta']['wp_pattern_sync_status'] );
return $data;
}
public function get_item_schema() {
if ( $this->schema ) {
return $this->add_additional_fields_schema( $this->schema );
}
$schema = parent::get_item_schema();
$schema['properties']['title']['properties']['raw']['context'] = array( 'view', 'edit' );
$schema['properties']['content']['properties']['raw']['context'] = array( 'view', 'edit' );
unset( $schema['properties']['title']['properties']['rendered'] );
unset( $schema['properties']['content']['properties']['rendered'] );
$this->schema = $schema;
return $this->add_additional_fields_schema( $this->schema );
}
原文内容
Controller which provides a REST endpoint for the editor to read, create, edit, and delete synced patterns (formerly called reusable blocks).
Description
Patterns are stored as posts with the wp_block post type.
See also
Methods
| Name | Description |
|---|---|
| WP_REST_Blocks_Controller::check_read_permission | Checks if a pattern can be read. |
| WP_REST_Blocks_Controller::filter_response_by_context | Filters a response based on the context defined in the schema. |
| WP_REST_Blocks_Controller::get_item_schema | Retrieves the pattern’s schema, conforming to JSON Schema. |
Source
class WP_REST_Blocks_Controller extends WP_REST_Posts_Controller {
/**
* Checks if a pattern can be read.
*
* @since 5.0.0
*
* @param WP_Post $post Post object that backs the block.
* @return bool Whether the pattern can be read.
*/
public function check_read_permission( $post ) {
// By default the read_post capability is mapped to edit_posts.
if ( ! current_user_can( 'read_post', $post->ID ) ) {
return false;
}
return parent::check_read_permission( $post );
}
/**
* Filters a response based on the context defined in the schema.
*
* @since 5.0.0
* @since 6.3.0 Adds the `wp_pattern_sync_status` postmeta property to the top level of response.
*
* @param array $data Response data to filter.
* @param string $context Context defined in the schema.
* @return array Filtered response.
*/
public function filter_response_by_context( $data, $context ) {
$data = parent::filter_response_by_context( $data, $context );
/*
* Remove `title.rendered` and `content.rendered` from the response.
* It doesn't make sense for a pattern to have rendered content on its own,
* since rendering a block requires it to be inside a post or a page.
*/
unset( $data['title']['rendered'] );
unset( $data['content']['rendered'] );
// Add the core wp_pattern_sync_status meta as top level property to the response.
$data['wp_pattern_sync_status'] = isset( $data['meta']['wp_pattern_sync_status'] ) ? $data['meta']['wp_pattern_sync_status'] : '';
unset( $data['meta']['wp_pattern_sync_status'] );
return $data;
}
/**
* Retrieves the pattern's schema, conforming to JSON Schema.
*
* @since 5.0.0
*
* @return array Item schema data.
*/
public function get_item_schema() {
if ( $this->schema ) {
return $this->add_additional_fields_schema( $this->schema );
}
$schema = parent::get_item_schema();
/*
* Allow all contexts to access `title.raw` and `content.raw`.
* Clients always need the raw markup of a pattern to do anything useful,
* e.g. parse it or display it in an editor.
*/
$schema['properties']['title']['properties']['raw']['context'] = array( 'view', 'edit' );
$schema['properties']['content']['properties']['raw']['context'] = array( 'view', 'edit' );
/*
* Remove `title.rendered` and `content.rendered` from the schema.
* It doesn't make sense for a pattern to have rendered content on its own,
* since rendering a block requires it to be inside a post or a page.
*/
unset( $schema['properties']['title']['properties']['rendered'] );
unset( $schema['properties']['content']['properties']['rendered'] );
$this->schema = $schema;
return $this->add_additional_fields_schema( $this->schema );
}
}
Changelog
| Version | Description |
|---|---|
| 5.0.0 | Introduced. |