WP_Block_Editor_Context
云策文档标注
概述
WP_Block_Editor_Context 类用于存储块编辑器渲染时的上下文信息,包括编辑器名称和正在编辑的文章。它通过构造函数初始化可选设置。
关键要点
- WP_Block_Editor_Context 类包含两个主要属性:$name(标识块编辑器类型,默认为 'core/edit-post')和 $post(可选,表示正在编辑的 WP_Post 对象)。
- 构造函数 __construct 接受一个数组参数 $settings,用于设置 $name 和 $post 属性,支持自定义编辑器上下文。
- 该类自 WordPress 5.8.0 版本引入,用于在块编辑器环境中传递和管理上下文数据。
原文内容
Contains information about a block editor being rendered.
Methods
| Name | Description |
|---|---|
| WP_Block_Editor_Context::__construct | Constructor. |
Source
final class WP_Block_Editor_Context {
/**
* String that identifies the block editor being rendered. Can be one of:
*
* - `'core/edit-post'` - The post editor at `/wp-admin/edit.php`.
* - `'core/edit-widgets'` - The widgets editor at `/wp-admin/widgets.php`.
* - `'core/customize-widgets'` - The widgets editor at `/wp-admin/customize.php`.
* - `'core/edit-site'` - The site editor at `/wp-admin/site-editor.php`.
*
* Defaults to 'core/edit-post'.
*
* @since 6.0.0
*
* @var string
*/
public $name = 'core/edit-post';
/**
* The post being edited by the block editor. Optional.
*
* @since 5.8.0
*
* @var WP_Post|null
*/
public $post = null;
/**
* Constructor.
*
* Populates optional properties for a given block editor context.
*
* @since 5.8.0
*
* @param array $settings The list of optional settings to expose in a given context.
*/
public function __construct( array $settings = array() ) {
if ( isset( $settings['name'] ) ) {
$this->name = $settings['name'];
}
if ( isset( $settings['post'] ) ) {
$this->post = $settings['post'];
}
}
}
Changelog
| Version | Description |
|---|---|
| 5.8.0 | Introduced. |