set collection items creation caps

This commit is contained in:
Jacson Passold 2018-01-18 12:23:13 -02:00
parent 4b2103f800
commit 14a7c2651f
1 changed files with 89 additions and 11 deletions

View File

@ -224,7 +224,51 @@ class Capabilities {
"subscriber"=> [
"read"
]
]
],
"tainacan-items"=> [ // abstract, will apply to collection Items
"administrator"=> [
"delete_posts",
"delete_private_posts",
"edit_posts",
"edit_private_posts",
"publish_posts",
"read_private_posts",
"delete_published_posts",
"edit_published_posts",
"edit_others_posts",
"delete_others_posts",
"read"
],
"editor"=> [
"delete_posts",
"delete_private_posts",
"edit_posts",
"edit_private_posts",
"publish_posts",
"read_private_posts",
"delete_published_posts",
"edit_published_posts",
"edit_others_posts",
"delete_others_posts",
"read"
],
"author"=> [
"delete_posts",
"edit_posts",
"publish_posts",
"delete_published_posts",
"edit_published_posts",
"read"
],
"contributor"=> [
"delete_posts",
"edit_posts",
"read"
],
"subscriber"=> [
"read"
]
],
];
/**
@ -232,6 +276,7 @@ class Capabilities {
*/
function __construct() {
add_action('init', array(&$this, 'init'), 11);
add_action('tainacan-insert-tainacan-collections', array(&$this, 'new_collection'));
}
/**
@ -241,8 +286,8 @@ class Capabilities {
public function init() {
$defaults_caps = apply_filters('tainacan-defaults-capabilities', $this->defaults);
foreach ($defaults_caps as $post_type => $wp_append_roles)
{
foreach ($defaults_caps as $post_type => $wp_append_roles) {
if($post_type == 'tainacan-items') continue;
$entity = Repository::get_entity_by_post_type($post_type);
// append new capabilities to WordPress default roles
foreach ($wp_append_roles as $role_name => $caps) {
@ -256,23 +301,56 @@ class Capabilities {
}
}
}
global $Tainacan_Collections;
$collections = $Tainacan_Collections->fetch([], 'OBJECT');
foreach ($collections as $collection) {
$this->set_items_capabilities($collection, $defaults_caps);
}
}
/**
* Set config roles for items
* @param \Tainacan\Entities\Collection $collection
*/
public function set_items_capabilities($collection, $defaults_caps = null) {
if(is_null($defaults_caps)) $defaults_caps = apply_filters('tainacan-defaults-capabilities', $this->defaults); // External Call
$item = new \Tainacan\Entities\Item();
$item->set_collection($collection);
foreach ($defaults_caps['tainacan-items'] as $role_name => $caps) {
$role = get_role($role_name);
if(!is_object($role)) {
throw new \Exception(sprintf('Role "%s" not found', $role_name));
}
foreach ($caps as $cap) {
$role->add_cap($collection->cap->$cap);
}
}
}
/**
* Return list of editable roles
* @return array List of roles
*/
public static function get_editable_roles()
{
public static function get_editable_roles() {
// TODO $this->security_check();
global $wp_roles;
if(! isset($wp_roles))
$wp_roles = new \WP_Roles();
if(! isset($wp_roles)) $wp_roles = new \WP_Roles();
$all_roles = $wp_roles->get_names();
$editable_roles = apply_filters('tainacan-editable-roles', $all_roles);
return $all_roles;
$all_roles = $wp_roles->get_names();
$editable_roles = apply_filters('tainacan-editable-roles', $all_roles);
return $all_roles;
}
/**
* Hook to set capabilities to the new created collection
* @param \Tainacan\Entities\Collection $collection
*/
public function new_collection($collection)
{
$this->set_items_capabilities($collection);
}
}