90 lines
No EOL
1.8 KiB
Markdown
90 lines
No EOL
1.8 KiB
Markdown
## 扩展
|
|
|
|
如果您想使用自己插件的测试设置代码来扩展测试助手,有两个客户端过滤器可用。
|
|
|
|
此示例添加一个新标签页:
|
|
|
|
```
|
|
import { addFilter } from '@wordpress/hooks';
|
|
|
|
const SuperSekret = () => (
|
|
<>
|
|
<h2>超级秘密</h2>
|
|
<p>此部分包含超级秘密工具。</p>
|
|
<NewTool/>
|
|
</>
|
|
);
|
|
addFilter(
|
|
'woocommerce_admin_test_helper_tabs',
|
|
'wath',
|
|
( tabs ) => [
|
|
...tabs,
|
|
{
|
|
name: 'super-sekret',
|
|
title: '超级秘密',
|
|
content: <SuperSekret/>,
|
|
}
|
|
]
|
|
);
|
|
```
|
|
|
|
此示例向现有的"选项"标签页添加一个新工具:
|
|
|
|
```
|
|
import { addFilter } from '@wordpress/hooks';
|
|
|
|
const NewTool = () => (
|
|
<>
|
|
<strong>新工具</strong>
|
|
<p>描述</p>
|
|
<button>执行</button>
|
|
</>
|
|
);
|
|
addFilter(
|
|
'woocommerce_admin_test_helper_tab_options',
|
|
'wath',
|
|
( entries ) => [
|
|
...entries,
|
|
<NewTool/>
|
|
]
|
|
);
|
|
```
|
|
|
|
以通常方式注册一个 REST API 端点来执行服务器端操作:
|
|
|
|
```
|
|
add_action( 'rest_api_init', function() {
|
|
register_rest_route(
|
|
'your-plugin/v1',
|
|
'/area/action',
|
|
array(
|
|
'methods' => 'POST',
|
|
'callback' => 'your_plugin_area_action',
|
|
'permission_callback' => function( $request ) {
|
|
if ( ! wc_rest_check_manager_permissions( 'settings', 'edit ) ) {
|
|
return new \WP_Error(
|
|
'woocommerce_rest_cannot_edit',
|
|
__( '抱歉,您无法执行此操作', 'your-plugin' )
|
|
);
|
|
}
|
|
return true;
|
|
}
|
|
)
|
|
);
|
|
} );
|
|
|
|
function your_plugin_area_action() {
|
|
return [];
|
|
}
|
|
```
|
|
|
|
在客户端上可以这样使用:
|
|
|
|
```
|
|
import apiFetch from '@wordpress/api-fetch';
|
|
...
|
|
const response = await apiFetch( {
|
|
path: '/your-plugin/v1/area/action',
|
|
method: 'POST',
|
|
} );
|
|
``` |