diff --git a/src/classes/repositories/class-tainacan-collections.php b/src/classes/repositories/class-tainacan-collections.php index f13cc58db..e60166f40 100644 --- a/src/classes/repositories/class-tainacan-collections.php +++ b/src/classes/repositories/class-tainacan-collections.php @@ -13,7 +13,7 @@ class Collections extends Repository { public function __construct() { parent::__construct(); -// add_filter('map_meta_cap', array($this, 'map_meta_cap'), 10, 4); + add_filter('user_has_cap', array($this, 'user_has_cap'), 10, 3); } /** * {@inheritDoc} @@ -245,26 +245,39 @@ class Collections extends Repository { /** * Filter to handle special permissions * - * @see https://developer.wordpress.org/reference/hooks/map_meta_cap/ + * @see https://codex.wordpress.org/Plugin_API/Filter_Reference/user_has_cap * - */ - public function map_meta_cap($caps, $cap, $user_id, $args) { + * Filter on the current_user_can() function. + * This function is used to explicitly allow authors to edit contributors and other + * authors posts if they are published or pending. + * + * @param array $allcaps All the capabilities of the user + * @param array $cap [0] Required capability + * @param array $args [0] Requested capability + * [1] User ID + * [2] Associated object ID + */ + public function user_has_cap($allcaps, $cap, $args) { - // Filters meta caps edit_tainacan-collection and check if user is moderator - $collection_cpt = get_post_type_object(Entities\Collection::get_post_type()); - if ($cap == $collection_cpt->cap->edit_post) { - $entity = new Entities\Collection($args[0]); - if ($entity) { - $moderators = $entity->get_moderators_ids(); - if (in_array($user_id, $moderators)) { - // if user is moderator, we clear the current caps - // (that might fave edit_others_posts) and leave only edit_posts - $caps = [$collection_cpt->cap->edit_posts]; + if(count($args) > 2) { + $entity = Repository::get_entity_by_post($args[2]); + $collection = false; + if($entity) { + if($entity instanceof Entities\Collection) { // TODO others entity types + $collection = $entity; } + if($entity instanceof Entities\Item) { // TODO others entity types + $collection = $entity->get_collection(); + } + if($collection) { + $moderators = $collection->get_moderators_ids(); + if (is_array($moderators) && in_array($args[1], $moderators)) { + $allcaps[$cap[0]] = true; + } + } } } - - return $caps; + return $allcaps; }