返回介绍

get_post_type_capabilities()

发布于 2017-09-10 23:59:17 字数 5918 浏览 1143 评论 0 收藏 0

get_post_type_capabilities( object $args )

Build 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, seven 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.
  • publish_posts – Controls publishing objects of this post type.
  • read_private_posts – Controls whether private objects can be read.

These four primitive capabilities are checked in core in various locations. There are also seven 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_posts – Controls whether objects of this post type can be deleted.
  • 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).


参数

$args

(object) (Required) Post type registration arguments.


返回值

(object) object with all the capabilities as member variables.


源代码

File: wp-includes/post.php

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,
		'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_posts'           => 'delete_'           . $plural_base,
			'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;
}

更新日志

Versiondescription
3.0.0Introduced.

相关函数

Uses

  • wp-includes/post.php: _post_type_meta_capabilities()

Used By

  • wp-includes/class-wp-post-type.php: WP_Post_Type::set_props()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文