remove collections moderators feature #274
This commit is contained in:
parent
87520fd410
commit
31ef06a8dd
|
@ -22,14 +22,6 @@ class Collections extends Repository {
|
|||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collections constructor.
|
||||
*/
|
||||
protected function __construct() {
|
||||
parent::__construct();
|
||||
add_filter( 'map_meta_cap', array( $this, 'map_meta_cap' ), 10, 4 );
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @see \Tainacan\Repositories\Repository::get_map()
|
||||
|
@ -182,6 +174,7 @@ class Collections extends Repository {
|
|||
//'validation' => v::numeric(),
|
||||
'default' => ''
|
||||
],
|
||||
// deprecated
|
||||
'moderators_ids' => [
|
||||
'map' => 'meta_multi',
|
||||
'title' => __( 'Moderators', 'tainacan' ),
|
||||
|
@ -285,7 +278,6 @@ class Collections extends Repository {
|
|||
|
||||
$collection->register_collection_item_post_type();
|
||||
flush_rewrite_rules( false ); // needed to activate items post type archive url
|
||||
$this->update_moderators( $new_collection );
|
||||
|
||||
return $new_collection;
|
||||
}
|
||||
|
@ -361,9 +353,6 @@ class Collections extends Repository {
|
|||
}
|
||||
|
||||
function pre_process( $collection ) {
|
||||
// make sure we get the current value from database
|
||||
$current_moderators = $this->get_mapped_property( $collection, 'moderators_ids' );
|
||||
$this->current_moderators = is_array( $current_moderators ) ? $current_moderators : [];
|
||||
|
||||
$this->old_collection = $this->fetch( $collection->get_id() );
|
||||
$this->old_core_title = $collection->get_core_title_metadatum();
|
||||
|
@ -372,16 +361,6 @@ class Collections extends Repository {
|
|||
|
||||
}
|
||||
|
||||
function update_moderators( $collection ) {
|
||||
$moderators = $collection->get_moderators_ids();
|
||||
|
||||
$deleted = array_diff( $this->current_moderators, $moderators );
|
||||
$added = array_diff( $moderators, $this->current_moderators );
|
||||
|
||||
do_action( 'tainacan-add-collection-moderators', $collection, $added );
|
||||
do_action( 'tainacan-remove-collection-moderators', $collection, $deleted );
|
||||
}
|
||||
|
||||
function handle_core_metadata( $collection ) {
|
||||
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
||||
|
||||
|
@ -396,44 +375,6 @@ class Collections extends Repository {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
if ( $cap == 'edit_post' && is_array( $args ) && array_key_exists( 0, $args ) ) { // edit_tainacan-colletion is mapped to edit_post
|
||||
|
||||
$entity = $args[0];
|
||||
|
||||
if ( is_numeric( $entity ) || $entity instanceof Entities\Collection ) {
|
||||
|
||||
if ( is_numeric( $entity ) ) {
|
||||
$post = get_post( $entity );
|
||||
if ( $post instanceof \WP_Post && $post->post_type == Entities\Collection::get_post_type() ) {
|
||||
$entity = new Entities\Collection( $post );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( $entity instanceof Entities\Collection ) {
|
||||
$moderators = $entity->get_moderators_ids();
|
||||
if ( is_array( $moderators ) && in_array( $user_id, $moderators ) ) {
|
||||
|
||||
// if user is moderator, we clear the current caps
|
||||
// (that might fave edit_others_posts) and leave only read, that everybody has
|
||||
$collection_cpt = get_post_type_object( Entities\Collection::get_post_type() );
|
||||
$caps = [ 'read' ];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $caps;
|
||||
}
|
||||
|
||||
}
|
|
@ -61,94 +61,6 @@ class Collections extends TAINACAN_UnitTestCase {
|
|||
$this->assertTrue(current_user_can($collection_test2->cap->edit_post, $collection_test2->WP_Post->ID));
|
||||
$this->assertFalse(user_can($autor2, $collection_test->cap->edit_post, $collection_test->WP_Post->ID));
|
||||
|
||||
// add current user to moderators list of collection test.
|
||||
// Test add_moderator method and granting permissions
|
||||
|
||||
$collection_test->add_moderator_id($current_user_id);
|
||||
$collection_test->validate();
|
||||
$Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance();
|
||||
|
||||
$collection_test = $Tainacan_Collections->insert($collection_test);
|
||||
|
||||
$this->assertContains($current_user_id, $collection_test->get_moderators_ids());
|
||||
$this->assertTrue(current_user_can($collection_test->cap->edit_post, $collection_test->WP_Post->ID));
|
||||
|
||||
wp_set_current_user($this->user_id);
|
||||
$collection_test_moderator = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array(
|
||||
'name' => 'testeModerator',
|
||||
'description' => 'adasdasdsa',
|
||||
'default_order' => 'DESC',
|
||||
'moderators_ids' => [$autor2]
|
||||
),
|
||||
true
|
||||
);
|
||||
$this->assertEquals([$autor2], $collection_test_moderator->get_moderators_ids());
|
||||
|
||||
wp_set_current_user($autor2);
|
||||
$this->assertTrue(current_user_can($collection_test_moderator->cap->edit_post, $collection_test_moderator->WP_Post->ID));
|
||||
$this->assertTrue($collection_test_moderator->can_edit($autor2), 'Moderators cannot edit a collection!');
|
||||
|
||||
|
||||
// now lets test adding a moderator in a collection that already has one
|
||||
// and then lets test remove_moderator_id method
|
||||
|
||||
// first, subscriber user should not be able to edit the collection
|
||||
$this->assertFalse(user_can($new_user, $collection_test_moderator->cap->edit_post, $collection_test_moderator->WP_Post->ID));
|
||||
|
||||
// lets add him as moderator
|
||||
$collection_test_moderator->add_moderator_id($new_user);
|
||||
$collection_test_moderator->validate();
|
||||
$collection_test_moderator = $Tainacan_Collections->insert($collection_test_moderator);
|
||||
$this->assertContains($new_user, $collection_test_moderator->get_moderators_ids());
|
||||
|
||||
|
||||
// now he can edit
|
||||
$this->assertTrue(user_can($new_user, $collection_test_moderator->cap->edit_post, $collection_test_moderator->WP_Post->ID));
|
||||
|
||||
// lets remove him and check if he can no longer edit
|
||||
$collection_test_moderator->remove_moderator_id($new_user);
|
||||
$collection_test_moderator->validate();
|
||||
$collection_test_moderator = $Tainacan_Collections->insert($collection_test_moderator);
|
||||
$this->assertNotContains($new_user, $collection_test_moderator->get_moderators_ids());
|
||||
|
||||
|
||||
// now he can edit
|
||||
$this->assertFalse(user_can($new_user, $collection_test_moderator->cap->edit_post, $collection_test_moderator->WP_Post->ID));
|
||||
|
||||
}
|
||||
|
||||
function test_avoid_duplicated_moderator () {
|
||||
$collection_test = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array(
|
||||
'name' => 'testeCaps',
|
||||
'description' => 'adasdasdsa',
|
||||
'default_order' => 'DESC'
|
||||
),
|
||||
true
|
||||
);
|
||||
$new_user = $this->factory()->user->create(array( 'role' => 'subscriber' ));
|
||||
wp_set_current_user($new_user);
|
||||
$user_id = get_current_user_id();
|
||||
$this->assertEquals($new_user, $user_id);
|
||||
|
||||
$autor1 = $this->factory()->user->create(array( 'role' => 'author' ));
|
||||
wp_set_current_user($autor1);
|
||||
$autor1_id = get_current_user_id();
|
||||
|
||||
$moderators_ids = [
|
||||
$user_id,
|
||||
$autor1_id,
|
||||
$user_id,
|
||||
$autor1_id,
|
||||
];
|
||||
|
||||
$collection_test->set('moderators_ids', $moderators_ids);
|
||||
|
||||
$this->assertEquals(2, sizeof( $collection_test->get_moderators_ids() ));
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -47,31 +47,6 @@ class Items extends TAINACAN_UnitTestCase {
|
|||
$this->assertTrue($item->can_edit(), 'Administrator cannot edit the Item');
|
||||
$this->assertTrue(current_user_can($collection->get_items_capabilities()->edit_post, $item->get_id()), 'Administrator cannot edit an item!');
|
||||
|
||||
$sub = $this->factory()->user->create(array( 'role' => 'subscriber', 'display_name' => 'Sub' ));
|
||||
|
||||
$collectionM = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array(
|
||||
'name' => 'testePermModerator',
|
||||
'moderators_ids' => [$sub]
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
$itemM = $this->tainacan_entity_factory->create_entity(
|
||||
'item',
|
||||
array(
|
||||
'title' => 'testeItemModerator',
|
||||
'collection' => $collectionM,
|
||||
),
|
||||
true
|
||||
);
|
||||
$this->assertEquals([$sub], $collectionM->get_moderators_ids());
|
||||
|
||||
wp_set_current_user($sub);
|
||||
$this->assertTrue(current_user_can($collectionM->get_items_capabilities()->edit_post, $itemM->get_id()), 'Moderators cannot edit an item!');
|
||||
$this->assertTrue($itemM->can_edit($sub), 'Moderators cannot edit an item!');
|
||||
|
||||
}
|
||||
|
||||
function teste_query(){
|
||||
|
|
Loading…
Reference in New Issue