WordPress函数get_post_type_capabilities()获取自定义文章类型的权限信息

描述:

获取自定义文章类型的权限信息

用法:

<?php get_post_type_capabilities( $args ) ?>

参数:

$args

(array or string) (必填)  所需的权限类型(例如“post”)。将[“capability_type”]设置为数组,以便在使用此参数作为基础来构造功能时允许使用替代复数,例如array(’story’,’stories’)。将“[‘map_meta_cap”]设置为true也可以获得这些功能。

默认值: None

默认权限键值:

  • edit_postread_post, 和 delete_post 根据上下文通常映射到相应的基本功能,即正在编辑/读取/删除的帖子和正在检查的用户或角色。因此,这些功能通常不会直接授予用户或角色。
  • edit_posts – 控制是否可以编辑此文章类型的对象。
  • edit_others_posts – 控制是否可以编辑其他用户拥有的此类型的对象。如果文章类型不支持作者,则其行为将类似于编辑文章.
  • publish_posts – 控制发布此文章类型的对象.
  • read_private_posts – 控制是否可以读取私有对象。

这四个基本功能在不同的位置签入核心。
另外还有七个基本功能没有在核心中直接引用,除了map_meta_cap(),它接受前面提到的三个基本功能,并将它们转换为一个或多个基本功能,然后必须根据上下文针对用户或角色进行检查。

  • read – 控制是否可以读取此自定义文章类型下的文章。
  • delete_posts – 控制是否可以删除此文章类型的文章。
  • delete_private_posts – 控制是否可以删除私有文章。
  • delete_published_posts – 控制是否可以删除已发布的文章。
  • delete_others_posts – 控制是否可以删除其他用户拥有的文章。如果文章类型不支持作者,则此操作将类似于删除文章。
  • edit_private_posts – 控制是否可以编辑私有文章。
  • edit_published_posts – 控制是否可以编辑已发布的文章。

这些附加功能仅在map_meta_cap()中使用。因此,只有当post类型注册为’map_meta_cap’参数设置为true(默认值为false)时,才会默认分配它们。

源文件:

/**
 * Build an object with all post type capabilities out of a post type object
 *
 * 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).
 *
 * @since 3.0.0
 *
 * @see register_post_type()
 * @see map_meta_cap()
 *
 * @param object $args Post type registration arguments.
 * @return object object with all the capabilities as member variables.
 */
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;
}