starting to develop all the features for collections moderators

This commit is contained in:
Leo Germani 2018-01-12 18:28:49 -02:00
parent 73b27ac97a
commit 836878092a
3 changed files with 69 additions and 0 deletions

View File

@ -192,6 +192,15 @@ class Collection extends Entity {
return $this->get_mapped_property('default_view_mode');
}
/**
* Get collection moderators ids
*
* @return array
*/
function get_moderators_ids() {
return $this->get_mapped_property('moderators_ids');
}
/**
* Get collection DB identifier
*
@ -313,4 +322,27 @@ class Collection extends Entity {
$this->set_mapped_property('default_view_mode', $value);
}
/**
* Set collection moderators ids
*
* @param [string] $value
* @return void
*/
function set_moderators_ids($value) {
$this->set_mapped_property('moderators_ids', $value);
}
/**
* TODO implement the following methods to handle moderators_ids
*
* set_moderators
* get_moderators
* (the same as moderators_ids but gets and sets WP_User objects)
*
* add_moderator_id
* remove moderator_id
* (add or remove one moderator from the moderators_ids array)
*
*/
}

View File

@ -100,6 +100,13 @@ class Collections extends Repository {
*
*
*/
'moderators_ids' => [
'map' => 'meta_multi',
'title' => __('Moderators', 'tainacan'),
'type' => 'string',
'description' => __('The IDs of users assigned as moderators of this collection', 'tainacan'),
'validation' => ''
],
]);
}

View File

@ -19,6 +19,9 @@ abstract class Repository {
//add_action('admin_init', array(&$this, 'init_caps'));
add_action('init', array(&$this, 'init_capabilities'), 11);
add_filter('tainacan-get-map-'.$this->get_name(), array($this, 'get_default_properties'));
add_filter('map_meta_cap', array($this, 'map_meta_cap'), 10, 4);
}
/**
@ -532,6 +535,33 @@ abstract class Repository {
return user_can($user, 'publish_post', $entity);
}
/**
* Filter to handle special permissions
*
* @see https://developer.wordpress.org/reference/hooks/map_meta_cap/
*
*/
public function map_meta_cap($caps, $cap, $user_id, $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]
}
}
}
return $caps;
}
}
?>