get_post_type_capabilities()
概述
get_post_type_capabilities() 函数基于文章类型对象构建包含所有权限能力的对象。它使用 capability_type 参数作为基础,自动生成元能力和原始能力,并支持通过 map_meta_cap 参数控制额外能力的分配。
关键要点
- 函数基于 capability_type 参数生成权限能力,可接受数组形式以指定单复数形式。
- 默认生成的能力包括 edit_post、read_post、delete_post 等元能力,以及 edit_posts、publish_posts 等原始能力。
- 当 map_meta_cap 参数设置为 true 时,会额外生成 read、delete_private_posts 等用于 map_meta_cap() 的能力。
- create_posts 能力默认映射到 edit_posts,除非在 capabilities 参数中显式设置。
- 函数返回一个对象,包含所有能力作为成员变量,便于在权限检查中使用。
代码示例
function get_post_type_capabilities( $args ) {
if ( ! is_array( $args->capability_type ) ) {
$args->capability_type = array( $args->capability_type, $args->capability_type . 's' );
}
list( $singular_base, $plural_base ) = $args->capability_type;
$default_capabilities = array(
'edit_post' => 'edit_' . $singular_base,
'read_post' => 'read_' . $singular_base,
'delete_post' => 'delete_' . $singular_base,
'edit_posts' => 'edit_' . $plural_base,
'edit_others_posts' => 'edit_others_' . $plural_base,
'delete_posts' => 'delete_' . $plural_base,
'publish_posts' => 'publish_' . $plural_base,
'read_private_posts' => 'read_private_' . $plural_base,
);
if ( $args->map_meta_cap ) {
$default_capabilities_for_mapping = array(
'read' => 'read',
'delete_private_posts' => 'delete_private_' . $plural_base,
'delete_published_posts' => 'delete_published_' . $plural_base,
'delete_others_posts' => 'delete_others_' . $plural_base,
'edit_private_posts' => 'edit_private_' . $plural_base,
'edit_published_posts' => 'edit_published_' . $plural_base,
);
$default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping );
}
$capabilities = array_merge( $default_capabilities, $args->capabilities );
if ( ! isset( $capabilities['create_posts'] ) ) {
$capabilities['create_posts'] = $capabilities['edit_posts'];
}
if ( $args->map_meta_cap ) {
_post_type_meta_capabilities( $capabilities );
}
return (object) $capabilities;
}注意事项
- capability_type 参数注册后始终为单数形式的字符串。
- 元能力如 edit_post 通常不直接授予用户或角色,而是通过 map_meta_cap() 映射到原始能力。
- 如果文章类型不支持作者,edit_others_posts 和 delete_others_posts 的行为会分别类似于 edit_posts 和 delete_posts。
- map_meta_cap 参数默认为 false,设置为 true 以启用额外能力生成。
Builds an object with all post type capabilities out of a post type object
Description
Post type capabilities use the ‘capability_type’ argument as a base, if the capability is not set in the ‘capabilities’ argument array or if the ‘capabilities’ argument is not supplied.
The capability_type argument can optionally be registered as an array, with the first value being singular and the second plural, e.g. array(‘story, ‘stories’) Otherwise, an ‘s’ will be added to the value for the plural form. After registration, capability_type will always be a string of the singular value.
By default, the following keys are accepted as part of the capabilities array:
-
edit_post, read_post, and delete_post are meta capabilities, which are then generally mapped to corresponding primitive capabilities depending on the context, which would be the post being edited/read/deleted and the user or role being checked. Thus these capabilities would generally not be granted directly to users or roles.
-
edit_posts – Controls whether objects of this post type can be edited.
-
edit_others_posts – Controls whether objects of this type owned by other users can be edited. If the post type does not support an author, then this will behave like edit_posts.
-
delete_posts – Controls whether objects of this post type can be deleted.
-
publish_posts – Controls publishing objects of this post type.
-
read_private_posts – Controls whether private objects can be read.
-
create_posts – Controls whether objects of this post type can be created.
These primitive capabilities are checked in core in various locations.
There are also six other primitive capabilities which are not referenced directly in core, except in map_meta_cap() , which takes the three aforementioned meta capabilities and translates them into one or more primitive capabilities that must then be checked against the user or role, depending on the context.
- read – Controls whether objects of this post type can be read.
- delete_private_posts – Controls whether private objects can be deleted.
- delete_published_posts – Controls whether published objects can be deleted.
- delete_others_posts – Controls whether objects owned by other users can be can be deleted. If the post type does not support an author, then this will behave like delete_posts.
- edit_private_posts – Controls whether private objects can be edited.
- edit_published_posts – Controls whether published objects can be edited.
These additional capabilities are only used in map_meta_cap() . Thus, they are only assigned by default if the post type is registered with the ‘map_meta_cap’ argument set to true (default is false).
See also
Parameters
$argsobjectrequired-
Post type registration arguments.
Source
function get_post_type_capabilities( $args ) {
if ( ! is_array( $args->capability_type ) ) {
$args->capability_type = array( $args->capability_type, $args->capability_type . 's' );
}
// Singular base for meta capabilities, plural base for primitive capabilities.
list( $singular_base, $plural_base ) = $args->capability_type;
$default_capabilities = array(
// Meta capabilities.
'edit_post' => 'edit_' . $singular_base,
'read_post' => 'read_' . $singular_base,
'delete_post' => 'delete_' . $singular_base,
// Primitive capabilities used outside of map_meta_cap():
'edit_posts' => 'edit_' . $plural_base,
'edit_others_posts' => 'edit_others_' . $plural_base,
'delete_posts' => 'delete_' . $plural_base,
'publish_posts' => 'publish_' . $plural_base,
'read_private_posts' => 'read_private_' . $plural_base,
);
// Primitive capabilities used within map_meta_cap():
if ( $args->map_meta_cap ) {
$default_capabilities_for_mapping = array(
'read' => 'read',
'delete_private_posts' => 'delete_private_' . $plural_base,
'delete_published_posts' => 'delete_published_' . $plural_base,
'delete_others_posts' => 'delete_others_' . $plural_base,
'edit_private_posts' => 'edit_private_' . $plural_base,
'edit_published_posts' => 'edit_published_' . $plural_base,
);
$default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping );
}
$capabilities = array_merge( $default_capabilities, $args->capabilities );
// Post creation capability simply maps to edit_posts by default:
if ( ! isset( $capabilities['create_posts'] ) ) {
$capabilities['create_posts'] = $capabilities['edit_posts'];
}
// Remember meta capabilities for future reference.
if ( $args->map_meta_cap ) {
_post_type_meta_capabilities( $capabilities );
}
return (object) $capabilities;
}
Skip to note 2 content
MakeWebBetter
Usage of get_post_type_capabilities
$args = array( 'capability_type' => 'edit_post' ); // If not set, default to the setting for 'show_ui'. if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) { $args['show_in_menu'] = $args['show_ui']; } $this->cap = get_post_type_capabilities( (object) $args ); unset( $args['capabilities'] );