我最近注意到古腾堡编辑器中的一个新功能(我猜是在Wordpress 5.8中引入的),它似乎允许任何用户以任何其他用户的身份发布:
我确认,使用此新选项,属于作者角色的用户能够成功地以属于管理员角色的用户身份发布。究竟为什么引入此功能时似乎没有权限检查,并且可以禁用它?如何在古腾堡的状态和可见性面板中禁用作者下拉菜单
这在块编辑器,甚至是经典编辑器之前就已经存在了,可以在WordPress 2.0中看到。它甚至早于适当的代谢箱!
在块编辑器中,author字段的存在取决于用户是否有操作wp:action-assign-author
, 但没有直接的能力来决定这一点。
如果我们看看authorship plugin 我们看到它可以被删除:
/**
* Filters the post data for a REST API response.
*
* This removes the `wp:action-assign-author` rel from the response so the default post author
* control doesn\'t get shown on the block editor post editing screen.
*
* This also adds a new `authorship:action-assign-authorship` rel so custom clients can refer to this.
*
* @param WP_REST_Response $response The response object.
* @param WP_Post $post Post object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response The response object.
*/
function rest_prepare_post( WP_REST_Response $response, WP_Post $post, WP_REST_Request $request ) : WP_REST_Response {
$links = $response->get_links();
if ( isset( $links[\'https://api.w.org/action-assign-author\'] ) ) {
$response->remove_link( \'https://api.w.org/action-assign-author\' );
$response->add_link( REST_REL_LINK_ID, $links[\'self\'][0][\'href\'] );
}
return $response;
}
因此,在编辑帖子时,类似这样的操作可能会为所有用户删除它:/**
* Filters the post data for a REST API response.
*
* This removes the `wp:action-assign-author` rel from the response so the default post author
* control doesn\'t get shown on the block editor post editing screen.
*
* @param WP_REST_Response $response The response object.
* @param WP_Post $post Post object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response The response object.
*/
function rest_prepare_post_remove_author_action( WP_REST_Response $response, WP_Post $post, WP_REST_Request $request ) : WP_REST_Response {
$links = $response->get_links();
if ( isset( $links[\'https://api.w.org/action-assign-author\'] ) ) {
$response->remove_link( \'https://api.w.org/action-assign-author\' );
}
return $response;
}
add_filter( \'rest_prepare_post\', \'rest_prepare_post_remove_author_action\', 10, 3 );
然后,您可以将其调整为包含角色/能力检查。请注意,这可能只是表面现象,直接向API发出请求或使用classic编辑器的用户仍然可以设置作者。这只是愚弄了块编辑器和其他行为良好的RESTAPI应用程序,使其相信这是不可能的
经过大量测试后(因为我记得任何cap添加到具有add_cap
必须使用remove_cap
而不仅仅是移除add_cap
打电话,甚至wp-cli
我最终决定。。。
此下拉列表是edit_others_posts
能力我最初为我的作者角色启用了此上限,因为它的另一个副作用是能够缓和(即批准、垃圾邮件或Bin)评论,甚至当moderate_comments
功能也已启用。启用edit_others_posts
当时是允许我的作者角色缓和评论的唯一方法,就像现在禁用它是阻止我的作者角色以其他用户身份发布评论的唯一方法一样。
换句话说,Wordpress的cap系统是如此混乱和相互依赖,如果我想阻止一个角色假装以另一个用户的身份发布,我也不能允许该角色调节评论-这两个完全不同的功能应该是不相关的。
我甚至理解为什么它们是相关的——因为一篇文章需要编辑才能发表评论——但这些大写字母应该真正分开,因为编辑一篇文章和控制其上的垃圾邮件是两个不同的逻辑域,因此是两个不同的角色。这在某种程度上是可以理解的,因为moderate_comments
cap是存在的,但很明显,Wordpress将其作为唯一一个允许用户。。。温和的评论。