From e71a1a5079beae5f155909c18c24ab0c0bef7257 Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Fri, 26 Jan 2018 16:40:24 -0200 Subject: [PATCH 01/29] get metadata collection --- src/classes/entities/class-tainacan-metadata.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/classes/entities/class-tainacan-metadata.php b/src/classes/entities/class-tainacan-metadata.php index f93f2a99d..3e5d06d35 100644 --- a/src/classes/entities/class-tainacan-metadata.php +++ b/src/classes/entities/class-tainacan-metadata.php @@ -152,6 +152,14 @@ class Metadata extends Entity { function get_field_options(){ return $this->get_mapped_property('field_type_options'); } + + /** + * return the collection_id + * @return integer + */ + function get_collection_id() { + return $this->get_mapped_property('collection_id'); + } /** * Set the metadata name From 1cd21a30801007712c333babaf84dba85bf45599 Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Fri, 26 Jan 2018 16:41:04 -0200 Subject: [PATCH 02/29] support metadata data moderator --- .../repositories/class-tainacan-collections.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/classes/repositories/class-tainacan-collections.php b/src/classes/repositories/class-tainacan-collections.php index e60166f40..24ba0738f 100644 --- a/src/classes/repositories/class-tainacan-collections.php +++ b/src/classes/repositories/class-tainacan-collections.php @@ -258,7 +258,6 @@ class Collections extends Repository { * [2] Associated object ID */ public function user_has_cap($allcaps, $cap, $args) { - if(count($args) > 2) { $entity = Repository::get_entity_by_post($args[2]); $collection = false; @@ -266,13 +265,18 @@ class Collections extends Repository { if($entity instanceof Entities\Collection) { // TODO others entity types $collection = $entity; } - if($entity instanceof Entities\Item) { // TODO others entity types + elseif($entity instanceof Entities\Item) { $collection = $entity->get_collection(); } + elseif($entity instanceof Entities\Metadata) + { + $col_id = $entity->get_collection_id(); + if($col_id) $collection = Collections::fetch($col_id); + } if($collection) { $moderators = $collection->get_moderators_ids(); if (is_array($moderators) && in_array($args[1], $moderators)) { - $allcaps[$cap[0]] = true; + $allcaps[$cap[0]] = 1; } } } From 11cf82fb9ba13e8e5e7285c0311d88147f902f14 Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Fri, 26 Jan 2018 16:42:03 -0200 Subject: [PATCH 03/29] item does not have a fixed post_type --- .../repositories/class-tainacan-repository.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/classes/repositories/class-tainacan-repository.php b/src/classes/repositories/class-tainacan-repository.php index 5a7067ec1..4255ef01c 100644 --- a/src/classes/repositories/class-tainacan-repository.php +++ b/src/classes/repositories/class-tainacan-repository.php @@ -403,10 +403,9 @@ abstract class Repository { $entity = self::get_entity_by_post($entity); $post_type = $entity::get_post_type(); - if($post_type === false) { // There is no post + if($post_type === false || $entity instanceof Entities\Entity ) { // There is no post return user_can($user, 'edit_posts'); } - return user_can($user, $entity->cap->edit_post, $entity); } @@ -428,8 +427,8 @@ abstract class Repository { } $entity = self::get_entity_by_post($entity); - $post_type = $entity::get_post_type(); - if($post_type === false) { // There is no post + $post_type = $entity::get_post_type(); + if($post_type === false || $entity instanceof Entities\Entity ) { // There is no post return user_can($user, 'read'); } @@ -453,8 +452,8 @@ abstract class Repository { $user = $user->ID; } $entity = self::get_entity_by_post($entity); - $post_type = $entity::get_post_type(); - if($post_type === false) { // There is no post + $post_type = $entity::get_post_type(); + if($post_type === false || $entity instanceof Entities\Entity ) { // There is no post return user_can($user, 'delete_posts'); } @@ -479,7 +478,7 @@ abstract class Repository { } $entity = self::get_entity_by_post($entity); $post_type = $entity::get_post_type(); - if($post_type === false) { // There is no post + if($post_type === false || $entity instanceof Entities\Entity ) { // There is no post return user_can($user, 'publish_posts'); } return user_can($user, $entity->cap->publish_posts, $entity); From 35ba99d8825d3638e4318b599d129d1cc5122f4b Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Fri, 26 Jan 2018 16:44:45 -0200 Subject: [PATCH 04/29] test item moderation --- tests/test-items.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/tests/test-items.php b/tests/test-items.php index 522be3455..4c9664208 100644 --- a/tests/test-items.php +++ b/tests/test-items.php @@ -16,7 +16,7 @@ use Tainacan\Entities; class Items extends TAINACAN_UnitTestCase { /** - * @group permissions + * @group permissions2 */ public function test_permissions () { $collection = $this->tainacan_entity_factory->create_entity( @@ -36,6 +36,28 @@ class Items extends TAINACAN_UnitTestCase { ); $this->assertTrue($item->can_read(), 'Administrator cannot read the Item'); $this->assertTrue($item->can_edit(), 'Administrator cannot edit the 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()); + $this->assertTrue($itemM->can_edit($sub), 'Moderators cannot edit a item!'); + } function teste_query(){ From 979c80eb9226b4173f6dbcf64418e5216a4e96bd Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Sat, 27 Jan 2018 00:23:25 -0200 Subject: [PATCH 05/29] fix permissions for moderators. still have to fix can_* methods in repoitory --- src/classes/class-tainacan-capabilities.php | 45 +++++++++++ .../class-tainacan-collections.php | 76 +++++++++++++++++-- tests/test-collections.php | 3 + tests/test-items.php | 5 ++ 4 files changed, 124 insertions(+), 5 deletions(-) diff --git a/src/classes/class-tainacan-capabilities.php b/src/classes/class-tainacan-capabilities.php index 2b2216dc4..b6e499ca7 100644 --- a/src/classes/class-tainacan-capabilities.php +++ b/src/classes/class-tainacan-capabilities.php @@ -277,6 +277,11 @@ class Capabilities { function __construct() { add_action('init', array(&$this, 'init'), 11); add_action('tainacan-insert-tainacan-collections', array(&$this, 'new_collection')); + + add_action('tainacan-add-collection-moderators', array(&$this, 'add_moderators'), 10, 2); + add_action('tainacan-remove-collection-moderators', array(&$this, 'remove_moderators'), 10, 2); + + } /** @@ -353,4 +358,44 @@ class Capabilities { { $this->set_items_capabilities($collection); } + + + public function remove_moderators($collection, $moderators) { + $defaults_caps = apply_filters('tainacan-defaults-capabilities', $this->defaults); + if (is_array($moderators)) { + foreach ($moderators as $moderator) { + $user = get_userdata($moderator); + $cpt_object = get_post_type_object($collection->get_db_identifier()); + + if ($user instanceof \WP_User && $cpt_object instanceof \WP_Post_Type) { + $caps = $defaults_caps['tainacan-items']['editor']; + foreach ($caps as $cap) { + $user->remove_cap($cpt_object->cap->$cap); + } + } + } + } + } + + public function add_moderators($collection, $moderators) { + $defaults_caps = apply_filters('tainacan-defaults-capabilities', $this->defaults); + if (is_array($moderators)) { + foreach ($moderators as $moderator) { + $user = get_userdata($moderator); + $cpt_object = get_post_type_object($collection->get_db_identifier()); + + + + if ($user instanceof \WP_User && $cpt_object instanceof \WP_Post_Type) { + $caps = $defaults_caps['tainacan-items']['editor']; + foreach ($caps as $cap) { + $user->add_cap($cpt_object->cap->$cap); + } + } + } + } + } + + + } diff --git a/src/classes/repositories/class-tainacan-collections.php b/src/classes/repositories/class-tainacan-collections.php index 24ba0738f..a3b36f413 100644 --- a/src/classes/repositories/class-tainacan-collections.php +++ b/src/classes/repositories/class-tainacan-collections.php @@ -13,7 +13,8 @@ class Collections extends Repository { public function __construct() { parent::__construct(); - add_filter('user_has_cap', array($this, 'user_has_cap'), 10, 3); + //add_filter('user_has_cap', array($this, 'user_has_cap'), 10, 3); + add_filter('map_meta_cap', array($this, 'map_meta_cap'), 10, 4); } /** * {@inheritDoc} @@ -171,8 +172,11 @@ class Collections extends Repository { * @see \Tainacan\Repositories\Repository::insert() */ public function insert($collection){ - $new_collection = parent::insert($collection); - $collection->register_collection_item_post_type(); + $this->pre_update_moderators($collection); + $new_collection = parent::insert($collection); + + $collection->register_collection_item_post_type(); + $this->update_moderators($new_collection); return $new_collection; } @@ -242,6 +246,68 @@ class Collections extends Repository { } + function pre_update_moderators($collection) { + $current_moderators = $this->get_mapped_property($collection, 'moderators_ids'); + $this->current_moderators = is_array($current_moderators) ? $current_moderators : []; + + } + + 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); + } + + /** + * 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') { // 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 edit_posts + $collection_cpt = get_post_type_object(Entities\Collection::get_post_type()); + $caps = [$collection_cpt->cap->edit_posts]; + } + } + + + + + } + + } + + return $caps; + + } + /** * Filter to handle special permissions * @@ -256,7 +322,7 @@ class Collections extends Repository { * @param array $args [0] Requested capability * [1] User ID * [2] Associated object ID - */ + public function user_has_cap($allcaps, $cap, $args) { if(count($args) > 2) { $entity = Repository::get_entity_by_post($args[2]); @@ -284,5 +350,5 @@ class Collections extends Repository { return $allcaps; } - + */ } \ No newline at end of file diff --git a/tests/test-collections.php b/tests/test-collections.php index 10c9e923c..2c0da5a12 100644 --- a/tests/test-collections.php +++ b/tests/test-collections.php @@ -75,6 +75,9 @@ class Collections extends TAINACAN_UnitTestCase { 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!'); } diff --git a/tests/test-items.php b/tests/test-items.php index 4c9664208..cef76d9f5 100644 --- a/tests/test-items.php +++ b/tests/test-items.php @@ -47,6 +47,7 @@ class Items extends TAINACAN_UnitTestCase { ), true ); + $itemM = $this->tainacan_entity_factory->create_entity( 'item', array( @@ -56,6 +57,10 @@ class Items extends TAINACAN_UnitTestCase { true ); $this->assertEquals([$sub], $collectionM->get_moderators_ids()); + + wp_set_current_user($sub); + $cpt_obj = get_post_type_object($collectionM->get_db_identifier()); + $this->assertTrue(current_user_can($cpt_obj->cap->edit_post, $itemM->get_id()), 'Moderators cannot edit a item!'); $this->assertTrue($itemM->can_edit($sub), 'Moderators cannot edit a item!'); } From 1b54969c4d32f0f0ca1dbdbbc58452d349541caa Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Sun, 28 Jan 2018 23:34:12 -0200 Subject: [PATCH 06/29] Improves capabilities tests and fixes set_items_capabilities --- src/classes/class-tainacan-capabilities.php | 33 ++++++---- .../entities/class-tainacan-collection.php | 63 +++++++++++++++++-- .../class-tainacan-collections.php | 48 +------------- tests/test-collections.php | 42 ++++++++++++- tests/test-items.php | 18 ++++-- 5 files changed, 134 insertions(+), 70 deletions(-) diff --git a/src/classes/class-tainacan-capabilities.php b/src/classes/class-tainacan-capabilities.php index b6e499ca7..8b715ea08 100644 --- a/src/classes/class-tainacan-capabilities.php +++ b/src/classes/class-tainacan-capabilities.php @@ -319,9 +319,9 @@ class Capabilities { */ 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); + $collection_items_caps = $collection->get_items_capabilities(); + foreach ($defaults_caps['tainacan-items'] as $role_name => $caps) { $role = get_role($role_name); if(!is_object($role)) { @@ -329,7 +329,7 @@ class Capabilities { } foreach ($caps as $cap) { - $role->add_cap($collection->cap->$cap); + $role->add_cap($collection_items_caps->$cap); } } } @@ -359,37 +359,46 @@ class Capabilities { $this->set_items_capabilities($collection); } - + /** + * Hooke to revoke the capabilities for the items post type of the collection + * @param \Tainacan\Entities\Collection $collection The collection object + * @param array $moderators List of IDs of user IDs removed from the moderators list of the collection + * @return void + */ public function remove_moderators($collection, $moderators) { $defaults_caps = apply_filters('tainacan-defaults-capabilities', $this->defaults); if (is_array($moderators)) { + $collection_items_caps = $collection->get_items_capabilities(); foreach ($moderators as $moderator) { $user = get_userdata($moderator); - $cpt_object = get_post_type_object($collection->get_db_identifier()); - if ($user instanceof \WP_User && $cpt_object instanceof \WP_Post_Type) { + if ($user instanceof \WP_User && is_object($collection_items_caps)) { $caps = $defaults_caps['tainacan-items']['editor']; foreach ($caps as $cap) { - $user->remove_cap($cpt_object->cap->$cap); + $user->remove_cap($collection_items_caps->$cap); } } } } } + /** + * Hooke to grant the capabilities for the items post type of the collection + * @param \Tainacan\Entities\Collection $collection The collection object + * @param array $moderators List of IDs of user IDs added to the moderators list of the collection + * @return void + */ public function add_moderators($collection, $moderators) { $defaults_caps = apply_filters('tainacan-defaults-capabilities', $this->defaults); if (is_array($moderators)) { + $collection_items_caps = $collection->get_items_capabilities(); foreach ($moderators as $moderator) { $user = get_userdata($moderator); - $cpt_object = get_post_type_object($collection->get_db_identifier()); - - - if ($user instanceof \WP_User && $cpt_object instanceof \WP_Post_Type) { + if ($user instanceof \WP_User && is_object($collection_items_caps)) { $caps = $defaults_caps['tainacan-items']['editor']; foreach ($caps as $cap) { - $user->add_cap($cpt_object->cap->$cap); + $user->add_cap($collection_items_caps->$cap); } } } diff --git a/src/classes/entities/class-tainacan-collection.php b/src/classes/entities/class-tainacan-collection.php index c0dca4eff..1cd7ff088 100644 --- a/src/classes/entities/class-tainacan-collection.php +++ b/src/classes/entities/class-tainacan-collection.php @@ -64,6 +64,7 @@ class Collection extends Entity { ); $cpt_slug = $this->get_db_identifier(); + $capabilities = $this->get_items_capabilities(); $args = array( 'labels' => $cpt_labels, @@ -84,7 +85,7 @@ class Collection extends Entity { 'slug' => $this->get_slug() ], 'map_meta_cap' => true, - 'capability_type' => $this->get_db_identifier(), + 'capabilities' => (array) $capabilities, 'supports' => [ 'title', 'editor', @@ -100,6 +101,26 @@ class Collection extends Entity { return register_post_type($cpt_slug, $args); } + + /** + * Get the capabilities list for the post type of the items of this collection + * + * @uses get_post_type_capabilities to get the list. + * + * This method is usefull for getting the capabilities of the collection's items post type + * regardless if it has been already registered or not. + * + * @return [type] [description] + */ + function get_items_capabilities() { + $args = [ + 'map_meta_cap' => true, + 'capability_type' => $this->get_db_identifier(), + 'capabilities' => array() + ]; + + return get_post_type_capabilities((object) $args); + } /** * Get collection name @@ -322,6 +343,42 @@ class Collection extends Entity { $this->set_mapped_property('moderators_ids', $value); } + // Moderators methods + + /** + * Add a moderator ID to the moderators_ids list + * @param int $user_id The user ID to be added + * @return boolean Wether the ID was added or not. (if it already existed in the list it returns false) + */ + function add_moderator_id($user_id) { + if (is_integer($user_id)) { + $current_moderators = $this->get_moderators_ids(); + if (!in_array($user_id, $current_moderators)) { + $current_moderators[] = $user_id; + $this->set_moderators_ids($current_moderators); + return true; + } + } + return false; + } + + /** + * Remove a moderator ID to the moderators_ids list + * @param int $user_id The user ID to be removed + * @return boolean Wether the ID was added or not. (if it did not exist in the list it returns false) + */ + function remove_moderator_id($user_id) { + if (is_integer($user_id)) { + $current_moderators = $this->get_moderators_ids(); + if (($key = array_search($user_id, $current_moderators)) !== false) { + unset($current_moderators[$key]); + $this->set_moderators_ids($current_moderators); + return true; + } + } + return false; + } + /** * TODO implement the following methods to handle moderators_ids * @@ -329,10 +386,6 @@ class Collection extends Entity { * 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) - * */ } diff --git a/src/classes/repositories/class-tainacan-collections.php b/src/classes/repositories/class-tainacan-collections.php index a3b36f413..7425cf880 100644 --- a/src/classes/repositories/class-tainacan-collections.php +++ b/src/classes/repositories/class-tainacan-collections.php @@ -13,7 +13,6 @@ class Collections extends Repository { public function __construct() { parent::__construct(); - //add_filter('user_has_cap', array($this, 'user_has_cap'), 10, 3); add_filter('map_meta_cap', array($this, 'map_meta_cap'), 10, 4); } /** @@ -291,9 +290,9 @@ class Collections extends Repository { 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 edit_posts + // (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 = [$collection_cpt->cap->edit_posts]; + $caps = ['read']; } } @@ -308,47 +307,4 @@ class Collections extends Repository { } - /** - * Filter to handle special permissions - * - * @see https://codex.wordpress.org/Plugin_API/Filter_Reference/user_has_cap - * - * 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) { - 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; - } - elseif($entity instanceof Entities\Item) { - $collection = $entity->get_collection(); - } - elseif($entity instanceof Entities\Metadata) - { - $col_id = $entity->get_collection_id(); - if($col_id) $collection = Collections::fetch($col_id); - } - if($collection) { - $moderators = $collection->get_moderators_ids(); - if (is_array($moderators) && in_array($args[1], $moderators)) { - $allcaps[$cap[0]] = 1; - } - } - } - } - return $allcaps; - - } - */ } \ No newline at end of file diff --git a/tests/test-collections.php b/tests/test-collections.php index 2c0da5a12..9796d7554 100644 --- a/tests/test-collections.php +++ b/tests/test-collections.php @@ -31,8 +31,6 @@ class Collections extends TAINACAN_UnitTestCase { $user_id = get_current_user_id(); $this->assertEquals($new_user, $user_id); - $this->assertFalse(current_user_can('edit_collection')); - $autor1 = $this->factory()->user->create(array( 'role' => 'author' )); wp_set_current_user($autor1); $autor1_id = get_current_user_id(); @@ -63,6 +61,18 @@ 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(); + global $Tainacan_Collections; + + $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', @@ -79,6 +89,34 @@ class Collections extends TAINACAN_UnitTestCase { 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 debug_meta($user = false) diff --git a/tests/test-items.php b/tests/test-items.php index cef76d9f5..c1697656f 100644 --- a/tests/test-items.php +++ b/tests/test-items.php @@ -19,7 +19,8 @@ class Items extends TAINACAN_UnitTestCase { * @group permissions2 */ public function test_permissions () { - $collection = $this->tainacan_entity_factory->create_entity( + + $collection = $this->tainacan_entity_factory->create_entity( 'collection', array( 'name' => 'testePerm', @@ -36,7 +37,15 @@ class Items extends TAINACAN_UnitTestCase { ); $this->assertTrue($item->can_read(), 'Administrator cannot read the Item'); $this->assertTrue($item->can_edit(), 'Administrator cannot edit the Item'); - + + // another administrator should be able to edit items + $new_admin = $this->factory()->user->create(array( 'role' => 'administrator' )); + wp_set_current_user($new_admin); + + $this->assertTrue($item->can_read(), 'Administrator cannot read the Item'); + $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( @@ -59,9 +68,8 @@ class Items extends TAINACAN_UnitTestCase { $this->assertEquals([$sub], $collectionM->get_moderators_ids()); wp_set_current_user($sub); - $cpt_obj = get_post_type_object($collectionM->get_db_identifier()); - $this->assertTrue(current_user_can($cpt_obj->cap->edit_post, $itemM->get_id()), 'Moderators cannot edit a item!'); - $this->assertTrue($itemM->can_edit($sub), 'Moderators cannot edit a item!'); + $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!'); } From c251618100c26c99dc82ad9edb0a21b123ae4e0e Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Mon, 29 Jan 2018 16:21:15 -0200 Subject: [PATCH 07/29] entity id, not the object --- src/classes/repositories/class-tainacan-repository.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/classes/repositories/class-tainacan-repository.php b/src/classes/repositories/class-tainacan-repository.php index 4255ef01c..8ae11ffb4 100644 --- a/src/classes/repositories/class-tainacan-repository.php +++ b/src/classes/repositories/class-tainacan-repository.php @@ -406,7 +406,7 @@ abstract class Repository { if($post_type === false || $entity instanceof Entities\Entity ) { // There is no post return user_can($user, 'edit_posts'); } - return user_can($user, $entity->cap->edit_post, $entity); + return user_can($user, $entity->cap->edit_post, $entity->get_id()); } /** @@ -432,7 +432,7 @@ abstract class Repository { return user_can($user, 'read'); } - return user_can($user, $entity->cap->read_post, $entity); + return user_can($user, $entity->cap->read_post, $entity->get_id()); } /** @@ -457,7 +457,7 @@ abstract class Repository { return user_can($user, 'delete_posts'); } - return user_can($user, $entity->cap->delete_post, $entity); + return user_can($user, $entity->cap->delete_post, $entity->get_id()); } /** @@ -481,7 +481,7 @@ abstract class Repository { if($post_type === false || $entity instanceof Entities\Entity ) { // There is no post return user_can($user, 'publish_posts'); } - return user_can($user, $entity->cap->publish_posts, $entity); + return user_can($user, $entity->cap->publish_posts, $entity->get_id()); } } From bec1a17372ccb0a74b44ff561dc99edcdc7c288f Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Mon, 29 Jan 2018 16:21:49 -0200 Subject: [PATCH 08/29] miss spell namespace --- .../traits/class-tainacan-entity-collection-relation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/classes/traits/class-tainacan-entity-collection-relation.php b/src/classes/traits/class-tainacan-entity-collection-relation.php index cb4694487..e205f3c8c 100644 --- a/src/classes/traits/class-tainacan-entity-collection-relation.php +++ b/src/classes/traits/class-tainacan-entity-collection-relation.php @@ -24,7 +24,7 @@ trait Entity_Collection_Relation { /** * Return Collection from relation - * @return \Entities\Collection|NULL Return Collection or null on errors + * @return Entities\Collection|NULL Return Collection or null on errors */ public function get_collection() { if (isset($this->collection) && $this->collection instanceof Entities\Collection) From 3cd563c5293cc5ed7e95f149d6d06c2d4bd63fd4 Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Mon, 29 Jan 2018 16:22:32 -0200 Subject: [PATCH 09/29] remove already implemented get --- src/classes/entities/class-tainacan-metadata.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/classes/entities/class-tainacan-metadata.php b/src/classes/entities/class-tainacan-metadata.php index 3e5d06d35..534c252d0 100644 --- a/src/classes/entities/class-tainacan-metadata.php +++ b/src/classes/entities/class-tainacan-metadata.php @@ -153,14 +153,6 @@ class Metadata extends Entity { return $this->get_mapped_property('field_type_options'); } - /** - * return the collection_id - * @return integer - */ - function get_collection_id() { - return $this->get_mapped_property('collection_id'); - } - /** * Set the metadata name * From 671779c5e90530cfb9355129fb3a836a2c85908d Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Mon, 29 Jan 2018 17:07:44 -0200 Subject: [PATCH 10/29] fixing Items API permission check --- .../endpoints/class-tainacan-rest-items-controller.php | 7 ++++++- src/classes/entities/class-tainacan-entity.php | 6 ++++++ src/classes/repositories/class-tainacan-collections.php | 8 +++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/api/endpoints/class-tainacan-rest-items-controller.php b/src/api/endpoints/class-tainacan-rest-items-controller.php index 785cc0cad..abb19bd35 100644 --- a/src/api/endpoints/class-tainacan-rest-items-controller.php +++ b/src/api/endpoints/class-tainacan-rest-items-controller.php @@ -233,7 +233,12 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller { * @throws Exception */ public function create_item_permissions_check( $request ) { - return $this->items_repository->can_edit($this->item); + $collection = $this->collections_repository->fetch($request['collection_id']); + if ($collection instanceof Entities\Collection) { + return $collection->get_items_capabilities()->edit_posts; + } + return false; + } /** diff --git a/src/classes/entities/class-tainacan-entity.php b/src/classes/entities/class-tainacan-entity.php index 68b0d5485..2396c92d3 100644 --- a/src/classes/entities/class-tainacan-entity.php +++ b/src/classes/entities/class-tainacan-entity.php @@ -94,6 +94,7 @@ class Entity { } if( is_int($which) && + $this->WP_Post instanceof \WP_Post && $which != 0 && ( ( $this->get_post_type() !== false && $this->WP_Post->post_type != $this->get_post_type() ) || @@ -116,6 +117,11 @@ class Entity { $post_type_obj = get_post_type_object(self::get_post_type()); } $this->cap = $post_type_obj->cap; + } elseif ($this instanceof Item) { + $item_collection = $this->get_collection(); + if ($item_collection) { + $this->cap = $item_collection->get_items_capabilities(); + } } } diff --git a/src/classes/repositories/class-tainacan-collections.php b/src/classes/repositories/class-tainacan-collections.php index 7425cf880..f2c94ace4 100644 --- a/src/classes/repositories/class-tainacan-collections.php +++ b/src/classes/repositories/class-tainacan-collections.php @@ -222,7 +222,13 @@ class Collections extends Repository { */ public function fetch($args = [], $output = null){ if(is_numeric( $args )){ - return new Entities\Collection($args); + $existing_post = get_post($args); + if ($existing_post instanceof \WP_Post) { + return new Entities\Collection($existing_post); + } else { + return []; + } + } elseif(is_array($args)) { $args = array_merge([ 'posts_per_page' => -1, From 764ec9b681cf31473b4357c602849dd09c4e43d9 Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Mon, 29 Jan 2018 17:08:34 -0200 Subject: [PATCH 11/29] removing endpoint tha needs refactoring --- .../class-tainacan-rest-metadata-controller.php | 10 ++++++++-- src/classes/repositories/class-tainacan-filters.php | 2 +- tests/test-api-metadata.php | 10 ++++++---- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/api/endpoints/class-tainacan-rest-metadata-controller.php b/src/api/endpoints/class-tainacan-rest-metadata-controller.php index e1a0db4f2..21e5cc769 100644 --- a/src/api/endpoints/class-tainacan-rest-metadata-controller.php +++ b/src/api/endpoints/class-tainacan-rest-metadata-controller.php @@ -39,7 +39,10 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller { * Both of GETs return the metadata of matched objects */ public function register_routes() { - register_rest_route($this->namespace, '/' . $this->rest_base . '/collection/(?P[\d]+)', + /* + Removing this undocumented endpoint.. it seems wrong. + + register_rest_route($this->namespace, '/' . $this->rest_base . '/collection/(?P[\d]+)', array( array( 'methods' => WP_REST_Server::READABLE, @@ -64,6 +67,7 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller { ) ) ); + */ register_rest_route($this->namespace, '/' . $this->rest_base . '/item/(?P[\d]+)', array( array( @@ -328,6 +332,8 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller { } } + // We need to rethink this endpoint. Its confusing... + // and there is no need to iterato through all items... $collection_id = $request['collection_id']; $body = json_decode($request->get_body(), true); @@ -374,7 +380,7 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller { */ public function update_item_permissions_check( $request ) { $item = $this->item_repository->fetch($request['item_id'] ? $request['item_id'] : $request['collection_id']); - return $this->item_repository->can_edit($item); + return $this->item_repository->can_edit($item); } } diff --git a/src/classes/repositories/class-tainacan-filters.php b/src/classes/repositories/class-tainacan-filters.php index baf260b94..4fbb907a4 100644 --- a/src/classes/repositories/class-tainacan-filters.php +++ b/src/classes/repositories/class-tainacan-filters.php @@ -104,7 +104,7 @@ class Filters extends Repository { 'can_export' => true, 'rewrite' => true, 'map_meta_cap' => true, - 'capability_type' => Entities\Filter::get_post_type(), + 'capability_type' => 'tainacan-filter', 'supports' => [ 'title', 'editor', diff --git a/tests/test-api-metadata.php b/tests/test-api-metadata.php index ebc001bb2..2c47d91f6 100644 --- a/tests/test-api-metadata.php +++ b/tests/test-api-metadata.php @@ -31,7 +31,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { 'field_type' => $field->get_primitive_type(), ) ); - + /* $request = new \WP_REST_Request( 'POST', $this->namespace . '/metadata/collection/' . $collection->get_id() @@ -43,6 +43,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { $metadata_added = $response->get_data(); $this->assertEquals('Moeda', $metadata_added['name']); + */ } @@ -89,7 +90,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { $Tainacan_Item_Metadata->insert($item_metadata); #################### Get metadata of collection ###################### - +/* $request = new \WP_REST_Request( 'GET', $this->namespace . '/metadata/collection/' . $collection->get_id() @@ -102,7 +103,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { $metadata = $data[0]; $this->assertEquals('Data', $metadata['name']); - +*/ ################### Get metadata of item with value ####################### $request = new \WP_REST_Request( @@ -182,7 +183,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { #### UPDATE METADATA IN COLLECTION #### - + /* $values = json_encode([ 'metadata_id' => $metadata->get_id(), 'values' => [ @@ -209,6 +210,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { $metav = get_post_meta($item->get_id(), $data['id'], true); $this->assertEquals('19/01/2018', $metav); + */ } } From 0b10630a2d3252e5a2b364892f230351aad5c500 Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Mon, 29 Jan 2018 17:29:41 -0200 Subject: [PATCH 12/29] Starting to fix can_edit() and related repository methods --- .../class-tainacan-repository.php | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/classes/repositories/class-tainacan-repository.php b/src/classes/repositories/class-tainacan-repository.php index 4255ef01c..3aba15980 100644 --- a/src/classes/repositories/class-tainacan-repository.php +++ b/src/classes/repositories/class-tainacan-repository.php @@ -387,13 +387,13 @@ abstract class Repository { /** * Check if $user can edit/create a entity * - * @param int|array|\WP_Post|Entities\Entity $entity + * @param Entities\Entity $entity * @param int|\WP_User|null $user default is null for the current user * * @return boolean * @throws \Exception */ - public function can_edit($entity, $user = null) { + public function can_edit(Entities\Entity $entity, $user = null) { if(is_null($user)) { $user = get_current_user_id(); } @@ -402,11 +402,17 @@ abstract class Repository { } $entity = self::get_entity_by_post($entity); - $post_type = $entity::get_post_type(); - if($post_type === false || $entity instanceof Entities\Entity ) { // There is no post - return user_can($user, 'edit_posts'); - } - return user_can($user, $entity->cap->edit_post, $entity); + if (!isset($entity->cap->edit_post)) { + return false; + } + + if (is_integer($entity->get_id())) { + return user_can($user, $entity->cap->edit_post, $entity->get_id()); + } else { + // creating new + return user_can($user, $entity->cap->edit_posts); + } + } /** From 7d616c5cca3aa528f88bd7a8fd06badc73db0d7f Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Mon, 29 Jan 2018 17:30:01 -0200 Subject: [PATCH 13/29] fixing terms api permission checks --- .../class-tainacan-rest-terms-controller.php | 20 ++++++++++++++----- .../class-tainacan-taxonomies.php | 7 ++++++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/api/endpoints/class-tainacan-rest-terms-controller.php b/src/api/endpoints/class-tainacan-rest-terms-controller.php index a3248122c..1d0f61692 100644 --- a/src/api/endpoints/class-tainacan-rest-terms-controller.php +++ b/src/api/endpoints/class-tainacan-rest-terms-controller.php @@ -130,7 +130,11 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller { * @return bool|WP_Error */ public function create_item_permissions_check( $request ) { - return $this->terms_repository->can_edit($this->term); + $taxonomy = $this->taxonomy_repository->fetch($request['taxonomy_id']); + if ($taxonomy instanceof Entities\Taxonomy) { + return $taxonomy->can_edit(); + } + return false; } /** @@ -163,8 +167,11 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller { * @return bool|WP_Error */ public function delete_item_permissions_check( $request ) { - $term = new Entities\Term($this->terms_repository->fetch($request['term_id'])); - return $this->terms_repository->can_delete($term); + $taxonomy = $this->taxonomy_repository->fetch($request['taxonomy_id']); + if ($taxonomy instanceof Entities\Taxonomy) { + return $taxonomy->can_edit(); + } + return false; } /** @@ -209,8 +216,11 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller { * @return bool|WP_Error */ public function update_item_permissions_check( $request ) { - $term = new Entities\Term($this->terms_repository->fetch($request['term_id'])); - return $this->terms_repository->can_edit($term); + $taxonomy = $this->taxonomy_repository->fetch($request['taxonomy_id']); + if ($taxonomy instanceof Entities\Taxonomy) { + return $taxonomy->can_edit(); + } + return false; } /** diff --git a/src/classes/repositories/class-tainacan-taxonomies.php b/src/classes/repositories/class-tainacan-taxonomies.php index ff6ca6fc4..77ebe1ed4 100644 --- a/src/classes/repositories/class-tainacan-taxonomies.php +++ b/src/classes/repositories/class-tainacan-taxonomies.php @@ -162,7 +162,12 @@ class Taxonomies extends Repository { // TODO: Pegar taxonomias registradas via código if( is_numeric($args) ){ - return new Entities\Taxonomy($args); + $existing_post = get_post($args); + if ($existing_post instanceof \WP_Post) { + return new Entities\Taxonomy($existing_post); + } else { + return []; + } } elseif (is_array($args)) { $args = array_merge([ From 1bd548c1380f4963f89e4ee4fb2a3b897e5dde6b Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Mon, 29 Jan 2018 17:34:34 -0200 Subject: [PATCH 14/29] fetch returns empty array if invalid ID is passed --- src/classes/repositories/class-tainacan-filters.php | 8 +++++++- src/classes/repositories/class-tainacan-items.php | 8 +++++++- src/classes/repositories/class-tainacan-logs.php | 8 +++++++- src/classes/repositories/class-tainacan-metadatas.php | 7 ++++++- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/classes/repositories/class-tainacan-filters.php b/src/classes/repositories/class-tainacan-filters.php index 4fbb907a4..8517bbde1 100644 --- a/src/classes/repositories/class-tainacan-filters.php +++ b/src/classes/repositories/class-tainacan-filters.php @@ -198,7 +198,13 @@ class Filters extends Repository { */ public function fetch($args = [], $output = null){ if( is_numeric($args) ){ - return new Entities\Filter($args); + $existing_post = get_post($args); + if ($existing_post instanceof \WP_Post) { + return new Entities\Filter($existing_post); + } else { + return []; + } + } elseif (is_array($args)) { // TODO: get filters from parent collections $args = array_merge([ diff --git a/src/classes/repositories/class-tainacan-items.php b/src/classes/repositories/class-tainacan-items.php index c3559fa97..9e013edd7 100644 --- a/src/classes/repositories/class-tainacan-items.php +++ b/src/classes/repositories/class-tainacan-items.php @@ -149,7 +149,13 @@ class Items extends Repository { global $Tainacan_Collections; if(is_numeric($args)){ - return new Entities\Item($args); + $existing_post = get_post($args); + if ($existing_post instanceof \WP_Post) { + return new Entities\Item($existing_post); + } else { + return []; + } + } if (empty($collections)){ diff --git a/src/classes/repositories/class-tainacan-logs.php b/src/classes/repositories/class-tainacan-logs.php index a7eb8511f..ff6b8c6b6 100644 --- a/src/classes/repositories/class-tainacan-logs.php +++ b/src/classes/repositories/class-tainacan-logs.php @@ -157,7 +157,13 @@ class Logs extends Repository { */ public function fetch($args = [], $output = null){ if(is_numeric($args)){ - return new Entities\Log($args); + $existing_post = get_post($args); + if ($existing_post instanceof \WP_Post) { + return new Entities\Log($existing_post); + } else { + return []; + } + } elseif (is_array($args)) { $args = array_merge([ 'post_status' => 'publish', diff --git a/src/classes/repositories/class-tainacan-metadatas.php b/src/classes/repositories/class-tainacan-metadatas.php index 2a219abc6..1c2e06178 100644 --- a/src/classes/repositories/class-tainacan-metadatas.php +++ b/src/classes/repositories/class-tainacan-metadatas.php @@ -233,7 +233,12 @@ class Metadatas extends Repository { public function fetch( $args, $output = null ) { if( is_numeric($args) ){ - return new Entities\Metadata($args); + $existing_post = get_post($args); + if ($existing_post instanceof \WP_Post) { + return new Entities\Metadata($existing_post); + } else { + return []; + } } elseif (is_array($args)) { $args = array_merge([ From efe9cdc6b2f839ab0b7425db5b21b211ccb28196 Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Mon, 29 Jan 2018 18:15:16 -0200 Subject: [PATCH 15/29] doc function return --- src/classes/entities/class-tainacan-collection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/classes/entities/class-tainacan-collection.php b/src/classes/entities/class-tainacan-collection.php index 1cd7ff088..8bef3a20b 100644 --- a/src/classes/entities/class-tainacan-collection.php +++ b/src/classes/entities/class-tainacan-collection.php @@ -110,7 +110,7 @@ class Collection extends Entity { * This method is usefull for getting the capabilities of the collection's items post type * regardless if it has been already registered or not. * - * @return [type] [description] + * @return object Object with all the capabilities as member variables. */ function get_items_capabilities() { $args = [ From 31360eef229b824be5c4210dac507f402c71ebc2 Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Mon, 29 Jan 2018 18:16:49 -0200 Subject: [PATCH 16/29] do not register post, throw exp and better post_type item check --- .../entities/class-tainacan-entity.php | 4 +--- src/classes/entities/class-tainacan-item.php | 24 ++++++++----------- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/classes/entities/class-tainacan-entity.php b/src/classes/entities/class-tainacan-entity.php index 68b0d5485..7952b895f 100644 --- a/src/classes/entities/class-tainacan-entity.php +++ b/src/classes/entities/class-tainacan-entity.php @@ -111,9 +111,7 @@ class Entity { if($this->get_post_type() !== false) { $post_type_obj = get_post_type_object(self::get_post_type()); if(!is_object($post_type_obj)) { //may be called before post_type registration - global ${$this->repository}; - ${$this->repository}->register_post_type(); - $post_type_obj = get_post_type_object(self::get_post_type()); + throw new \Exception(sprintf("The post type %s need to be registered, need the init hook!")); } $this->cap = $post_type_obj->cap; } diff --git a/src/classes/entities/class-tainacan-item.php b/src/classes/entities/class-tainacan-item.php index affe90874..0c4fbfb87 100644 --- a/src/classes/entities/class-tainacan-item.php +++ b/src/classes/entities/class-tainacan-item.php @@ -179,20 +179,16 @@ class Item extends Entity { * set meta cap object */ protected function set_cap() { - $db_identifier = $this->get_db_identifier(); - if(!empty($db_identifier)) - { - $post_type_obj = get_post_type_object($db_identifier); - if(!is_object($post_type_obj)) { //may be called before post_type registration - $collection = $this->get_collection(); - if(is_object($collection)) { - $post_type_obj = $collection->register_collection_item_post_type(); - } + if(!empty($this->get_db_identifier())) { + $db_identifier = Collection::$db_identifier_prefix.$this->get_db_identifier().Collection::$db_identifier_sufix; + if(!empty($db_identifier)) + { + $post_type_obj = get_post_type_object($db_identifier); + if(!is_object($post_type_obj)) { + throw new \Exception(sprintf("Collection post type (%s) is not setted and cannot be registred", $db_identifier)); + } + $this->cap = $post_type_obj->cap; } - if(!is_object($post_type_obj)) { - throw new \Exception(sprintf("Collection post type (%s) is not setted and cannot be registred", $db_identifier)); - } - $this->cap = $post_type_obj->cap; - } //TODO Item without collection? treat as error? + } } } \ No newline at end of file From 8f439e75576f1e329bc6ed68ecf0fbb7ad5b57aa Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Mon, 29 Jan 2018 18:18:08 -0200 Subject: [PATCH 17/29] using read and not read_post, check if cap has been initialized --- src/classes/repositories/class-tainacan-repository.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/classes/repositories/class-tainacan-repository.php b/src/classes/repositories/class-tainacan-repository.php index 8ae11ffb4..6aeb60167 100644 --- a/src/classes/repositories/class-tainacan-repository.php +++ b/src/classes/repositories/class-tainacan-repository.php @@ -403,7 +403,7 @@ abstract class Repository { $entity = self::get_entity_by_post($entity); $post_type = $entity::get_post_type(); - if($post_type === false || $entity instanceof Entities\Entity ) { // There is no post + if( ($post_type === false && ! ($entity instanceof Entities\Item)) || is_null($entity->cap) ) { // There is no post return user_can($user, 'edit_posts'); } return user_can($user, $entity->cap->edit_post, $entity->get_id()); @@ -428,11 +428,11 @@ abstract class Repository { $entity = self::get_entity_by_post($entity); $post_type = $entity::get_post_type(); - if($post_type === false || $entity instanceof Entities\Entity ) { // There is no post + if( ($post_type === false && ! ($entity instanceof Entities\Item)) || is_null($entity->cap) ) { // There is no post return user_can($user, 'read'); } - return user_can($user, $entity->cap->read_post, $entity->get_id()); + return user_can($user, $entity->cap->read, $entity->get_id()); } /** @@ -453,7 +453,7 @@ abstract class Repository { } $entity = self::get_entity_by_post($entity); $post_type = $entity::get_post_type(); - if($post_type === false || $entity instanceof Entities\Entity ) { // There is no post + if( ($post_type === false && ! ($entity instanceof Entities\Item)) || is_null($entity->cap) ) { // There is no post return user_can($user, 'delete_posts'); } @@ -478,7 +478,7 @@ abstract class Repository { } $entity = self::get_entity_by_post($entity); $post_type = $entity::get_post_type(); - if($post_type === false || $entity instanceof Entities\Entity ) { // There is no post + if( ($post_type === false && ! ($entity instanceof Entities\Item)) || is_null($entity->cap) ) { // There is no post return user_can($user, 'publish_posts'); } return user_can($user, $entity->cap->publish_posts, $entity->get_id()); From a9bcd0292aec84857e8328763d50d48846ba2306 Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Mon, 29 Jan 2018 18:18:40 -0200 Subject: [PATCH 18/29] better handler response errors --- tests/test-api-collections.php | 2 +- tests/test-api-filters.php | 2 +- tests/test-api-metadata.php | 2 +- tests/test-api-taxonomies.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test-api-collections.php b/tests/test-api-collections.php index f572101f2..7cbad5239 100644 --- a/tests/test-api-collections.php +++ b/tests/test-api-collections.php @@ -39,7 +39,7 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase { $request->set_body($collection_JSON); $response = $this->server->dispatch( $request ); - $this->assertEquals( 201, $response->get_status() ); + $this->assertEquals( 201, $response->get_status(), sprintf('response: %s', print_r($response, true)) ); $collection = $response->get_data(); $id = $collection['id']; diff --git a/tests/test-api-filters.php b/tests/test-api-filters.php index 3c8d0e65e..aa68296ac 100644 --- a/tests/test-api-filters.php +++ b/tests/test-api-filters.php @@ -44,7 +44,7 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase { $response = $this->server->dispatch($request); $data = $response->get_data(); - + $this->assertTrue(is_array($data) && array_key_exists('filter_type', $data), sprintf('cannot create a range, response: %s', print_r($data, true))); $this->assertEquals('Tainacan\Filter_Types\Range', $data['filter_type']); $this->assertEquals('Filter name', $data['name']); } diff --git a/tests/test-api-metadata.php b/tests/test-api-metadata.php index ebc001bb2..da0079241 100644 --- a/tests/test-api-metadata.php +++ b/tests/test-api-metadata.php @@ -41,7 +41,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { $response = $this->server->dispatch($request); $metadata_added = $response->get_data(); - + $this->assertTrue(is_array($metadata_added) && array_key_exists('name', $metadata_added), sprintf('cannot create metadata, response: %s', print_r($metadata_added, true))); $this->assertEquals('Moeda', $metadata_added['name']); } diff --git a/tests/test-api-taxonomies.php b/tests/test-api-taxonomies.php index ce3d9f19a..39a6a8ced 100644 --- a/tests/test-api-taxonomies.php +++ b/tests/test-api-taxonomies.php @@ -65,7 +65,7 @@ class TAINACAN_REST_Taxonomies_Controller extends TAINACAN_UnitApiTestCase { $response = $this->server->dispatch($request); $data = $response->get_data(); - + $this->assertTrue(is_array($data) && array_key_exists('name', $data), sprintf('cannot create a range, response: %s', print_r($data, true))); $this->assertEquals('Nome', $data['name']); } From bf4f254df7fd7a54d9ce49c06ada8b393af4c3da Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Mon, 29 Jan 2018 18:36:15 -0200 Subject: [PATCH 19/29] better debug --- tests/test-api-metadata.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-api-metadata.php b/tests/test-api-metadata.php index c57c024c7..5509192bd 100644 --- a/tests/test-api-metadata.php +++ b/tests/test-api-metadata.php @@ -114,7 +114,7 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { $response = $this->server->dispatch($request); $data = $response->get_data(); - + $this->assertTrue(is_array($data) && array_key_exists(0, $data), sprintf('cannot read metadata, response: %s', print_r($data, true))); $item_metadata = $data[0]; $metadata = $item_metadata['metadata']; From 6aecb0af57e9ede508dabec16e73722a8f768556 Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Mon, 29 Jan 2018 18:36:40 -0200 Subject: [PATCH 20/29] using isset to check if cap is set --- .../class-tainacan-repository.php | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/classes/repositories/class-tainacan-repository.php b/src/classes/repositories/class-tainacan-repository.php index f00503419..053b7eb52 100644 --- a/src/classes/repositories/class-tainacan-repository.php +++ b/src/classes/repositories/class-tainacan-repository.php @@ -433,10 +433,12 @@ abstract class Repository { } $entity = self::get_entity_by_post($entity); - $post_type = $entity::get_post_type(); - if( ($post_type === false && ! ($entity instanceof Entities\Item)) || is_null($entity->cap) ) { // There is no post - return user_can($user, 'read'); - } + if (!isset($entity->cap->read)) { + if($entity->get_post_type() === false) { // Allow read of not post entities + return true; + } + return false; + } return user_can($user, $entity->cap->read, $entity->get_id()); } @@ -458,10 +460,10 @@ abstract class Repository { $user = $user->ID; } $entity = self::get_entity_by_post($entity); - $post_type = $entity::get_post_type(); - if( ($post_type === false && ! ($entity instanceof Entities\Item)) || is_null($entity->cap) ) { // There is no post - return user_can($user, 'delete_posts'); - } + + if (!isset($entity->cap->delete_post)) { + return false; + } return user_can($user, $entity->cap->delete_post, $entity->get_id()); } @@ -483,9 +485,8 @@ abstract class Repository { $user = $user->ID; } $entity = self::get_entity_by_post($entity); - $post_type = $entity::get_post_type(); - if( ($post_type === false && ! ($entity instanceof Entities\Item)) || is_null($entity->cap) ) { // There is no post - return user_can($user, 'publish_posts'); + if (!isset($entity->cap->publish_posts)) { + return false; } return user_can($user, $entity->cap->publish_posts, $entity->get_id()); } From dd853552a7a61b1aae44690af6615c0f766aaa26 Mon Sep 17 00:00:00 2001 From: Weryques Date: Fri, 2 Feb 2018 12:29:51 -0200 Subject: [PATCH 21/29] Update tainacan-api.md --- docs/tainacan-api.md | 335 ++++++++++++++++++++++--------------------- 1 file changed, 168 insertions(+), 167 deletions(-) diff --git a/docs/tainacan-api.md b/docs/tainacan-api.md index a1bc42fa9..ee7700823 100644 --- a/docs/tainacan-api.md +++ b/docs/tainacan-api.md @@ -10,277 +10,278 @@ A REST API for Tainacan Plugin. This API uses the Wordpress REST API. 1. Route `wp-json/tainacan/v2/collections/(?P[\d]+)` - 1.1. Endpoints supported: +1.1. Endpoints supported: - 1.1.1 GET (Fetch a collection) +1.1.1 GET (Fetch a collection) - 1.1.2 DELETE (Delete or Trash a collection and all your dependencies) +1.1.2 DELETE (Delete or Trash a collection and all your dependencies) - ``` - To delete pass in body of a requisition the parameter is_permanently as true. - To only trash pass false. - ``` - - 1.1.3 PATCH or PUT (Update a collection) - - Example of JSON passed in body for updating a collection: -     ``` - { -    "name": "string", -    "description": "string", - ... -  } +To delete pass in body of a requisition the parameter is_permanently as true. +To only trash pass false. +``` + +1.1.3 PATCH or PUT (Update a collection) + +Example of JSON passed in body for updating a collection: +     +```javascript +{ +"name": "string", +"description": "string", +... +} ``` 2. Route `wp-json/tainacan/v2/collections` - 2.1. Endpoints supported: +2.1. Endpoints supported: - 2.1.1 GET (Fetch all collections) +2.1.1 GET (Fetch all collections) - 2.1.2 POST (Create a collection). +2.1.2 POST (Create a collection). - Example of JSON passed in body for creating a collection: +Example of JSON passed in body for creating a collection: ```javascript - { -   "name": "string", -    "description": "string", -    "status": "string", -    "order": "string", -    "parent": "integer", -    "slug": "string", -    "default_orderby": "string", -    "default_order": "string", -    "columns": "string", -    "default_view_mode": "string" -  } +{ +"name": "string", +"description": "string", +"status": "string", +"order": "string", +"parent": "integer", +"slug": "string", +"default_orderby": "string", +"default_order": "string", +"columns": "string", +"default_view_mode": "string" +} ``` #### Items 1. Route `wp-json/tainacan/v2/collection/(?P[\d]+)/items` - 1.1. Endpoints supported: +1.1. Endpoints supported: - 1.1.1 GET (Fetch all items from a collection) +1.1.1 GET (Fetch all items from a collection) - 1.1.2 POST (Create a item in a collection) +1.1.2 POST (Create a item in a collection) - Example of JSON passed in body for creating a item: +Example of JSON passed in body for creating a item: ```javascript - { -   "title": "string", -    "description": "string", -    "status": "string", -  } +{ +"title": "string", +"description": "string", +"status": "string", +} ``` 2. Route `wp-json/tainacan/v2/items/(?P[\d]+)` - 2.1. Endpoints supported: +2.1. Endpoints supported: - 2.1.1 GET (Fetch a item) +2.1.1 GET (Fetch a item) - 2.1.2 DELETE (Delete or Trash a item and all your dependencies) +2.1.2 DELETE (Delete or Trash a item and all your dependencies) - ``` - To delete pass in body of a requisition the parameter is_permanently as true. - To only trash pass false. - ``` +``` +To delete pass in body of a requisition the parameter is_permanently as true. +To only trash pass false. +``` - 2.1.3 PATCH or PUT (Update a item) +2.1.3 PATCH or PUT (Update a item) - Example of JSON passed in body for updating a item: +Example of JSON passed in body for updating a item:     ``` - { -    "title": "string", -    "description": "string", - ... -  } +{ +"title": "string", +"description": "string", +... +} ``` #### Metadata 1. Route `wp-json/tainacan/v2/collection/(?P[\d]+)/metadata` - 1.1. Endpoints supported: +1.1. Endpoints supported: - 1.1.1 POST (Create a metadata in collection and all your items) +1.1.1 POST (Create a metadata in collection and all your items) - In body of requisition pass a JSON with the attributes of metadata like: +In body of requisition pass a JSON with the attributes of metadata like: ```javascript - { -       "name": "string", -       "description": "string", -       "field_type": "string", -       "order": "string", -       "parent": "integer", -       "required": "string", -       "collection_key": "string", -       "multiple": "string", -       "cardinality": "string", -       "privacy": "string", -       "mask": "string", -       "default_value": "string", -       "field_type_options": "string", -    } +{ +"name": "string", +"description": "string", +"field_type": "string", +"order": "string", +"parent": "integer", +"required": "string", +"collection_key": "string", +"multiple": "string", +"cardinality": "string", +"privacy": "string", +"mask": "string", +"default_value": "string", +"field_type_options": "string", +} ``` - 1.1.2 GET (Fetch all collection metadata) +1.1.2 GET (Fetch all collection metadata) 2. Route `wp-json/tainacan/v2/item/(?P[\d]+)/metadata` - 2.1. Endpoints supported: +2.1. Endpoints supported: - 2.1.1 POST (Set a value of item metadata) +2.1.1 POST (Set a value of item metadata) - In body of requisition pass a JSON with value e and id of metadata like: +In body of requisition pass a JSON with value e and id of metadata like: ```javascript - { -     "metadata_id": "integer", -     "values": "[any, type]" -  } +{ +"metadata_id": "integer", +"values": "[any, type]" +} ``` - - 2.1.2 GET (Fetch all item metadata, with your values) + +2.1.2 GET (Fetch all item metadata, with your values) #### Taxonomies 1. Route `wp-json/tainacan/v2/taxonomies` - 1.1. Endpoints supported: +1.1. Endpoints supported: - 1.1.1. GET (Fetch all taxonomies) +1.1.1. GET (Fetch all taxonomies) - 1.1.2. POST (Create a taxonomy) +1.1.2. POST (Create a taxonomy) - Example of JSON passed in body for creating a taxonomy: +Example of JSON passed in body for creating a taxonomy: ```javascript - { -   "name": "string", -    "description": "string", -    "status": "string", -    "parent": "string", -    "slug": "string", -    "allow_insert": "string", -    "collections_ids": "array" -  } +{ +"name": "string", +"description": "string", +"status": "string", +"parent": "string", +"slug": "string", +"allow_insert": "string", +"collections_ids": "array" +} ``` 2. Route `wp-json/tainacan/v2/taxonomies/(?P[\d]+)` - 2.1. Endpoints supported: +2.1. Endpoints supported: - 2.1.1 GET (Fetch a taxonomy) +2.1.1 GET (Fetch a taxonomy) - 2.1.2 DELETE (Delete or trash a taxonomy) +2.1.2 DELETE (Delete or trash a taxonomy) - ``` - To delete pass in body of requisition the parameter is_permanently as true. - To only trash pass false. - ``` +``` +To delete pass in body of requisition the parameter is_permanently as true. +To only trash pass false. +``` - 2.1.3 PATCH or PUT (Update a taxonomy) +2.1.3 PATCH or PUT (Update a taxonomy) - Example of JSON passed in body for updating a taxonomy: +Example of JSON passed in body for updating a taxonomy:     ```javascript - { -    "name": "string", -    "description": "string", - ... -  } +{ +"name": "string", +"description": "string", +... +} ``` #### Filters 1. Route `wp-json/tainacan/v2/filters` - 1.1 Endpoints supported: - 1.1.1 POST (Create a filter) +1.1 Endpoints supported: - Example of JSON passed in body for creating a filter: +1.1.1 POST (Create a filter) - ```javascript - { -   "collection_id": "int", -   "metadata_id": "int", - "filter_type": "string", -   "filter": { -     "name": "string", -     "description": "string", - ... -   } - } - ``` - - 1.1.2 GET (Fetch all filters) - - 2. Route `wp-json/tainacan/v2/filters/(?P[\d]+)` - - 2.1. Endpoints supported: - - 2.1.1 DELETE (Delete or trash a filter) - - ``` - To delete pass in body of requisition the parameter is_permanently as true. - To only trash pass false. - ``` - - 2.1.2 PATCH or PUT (Update a filter) - - Example of JSON passed in body for updating a filter: +Example of JSON passed in body for creating a filter: ```javascript - { - "name": "string", - ... - } +{ +"collection_id": "int", +"metadata_id": "int", +"filter_type": "string", +"filter": { +"name": "string", +"description": "string", +... +} +} +``` + +1.1.2 GET (Fetch all filters) + +2. Route `wp-json/tainacan/v2/filters/(?P[\d]+)` + +2.1. Endpoints supported: + +2.1.1 DELETE (Delete or trash a filter) + +``` +To delete pass in body of requisition the parameter is_permanently as true. +To only trash pass false. ``` - 2.1.3 GET (Fetch a filter) +2.1.2 PATCH or PUT (Update a filter) + +Example of JSON passed in body for updating a filter: + +```javascript +{ +"name": "string", +... +} +``` + +2.1.3 GET (Fetch a filter) #### Terms 1. Route `wp-json/tainacan/v2/taxonomy/(?P[\d]+)/terms` - 1.1 Endpoints supported: +1.1 Endpoints supported: - 1.1.1 POST (Create a term in a taxonomy) +1.1.1 POST (Create a term in a taxonomy) - Example of JSON passed in body for creating a term: +Example of JSON passed in body for creating a term: ```javascript - { -   "name": "string", -   "user": "int", - ... - } - ``` +{ +"name": "string", +"user": "int", +... +} +``` -    1.1.2 GET (Fetch all tems of a taxonomy) +1.1.2 GET (Fetch all tems of a taxonomy) 2. Route `wp-json/tainacan/v2/taxonomy/(?P[\d]+)/terms/(?P[\d]+)` - 2.1 Endpoints supported: +2.1 Endpoints supported: - 2.1.1 GET (Fecth a term of a taxonomy) +2.1.1 GET (Fecth a term of a taxonomy) - 2.1.2 PATCH or PUT (Update a term in a taxonomy) +2.1.2 PATCH or PUT (Update a term in a taxonomy) - Example of JSON passed in body for updating a term: +Example of JSON passed in body for updating a term: ```javascript - { -   "name": "string", - ... - } +{ +"name": "string", +... +} ``` - 2.1.3 DELETE (Delete a term of a taxonoy) +2.1.3 DELETE (Delete a term of a taxonoy) From 760de476306864ce296a8061cfc867fe7f13b26f Mon Sep 17 00:00:00 2001 From: Weryques Date: Fri, 2 Feb 2018 14:01:57 -0200 Subject: [PATCH 22/29] Update tainacan-api.md --- docs/tainacan-api.md | 126 +++++++++++++++++++++++++++++-------------- 1 file changed, 86 insertions(+), 40 deletions(-) diff --git a/docs/tainacan-api.md b/docs/tainacan-api.md index ee7700823..b1a71cea7 100644 --- a/docs/tainacan-api.md +++ b/docs/tainacan-api.md @@ -4,17 +4,18 @@ A REST API for Tainacan Plugin. This API uses the Wordpress REST API. +------ ### Routes and Endpoints - +------ #### Collections 1. Route `wp-json/tainacan/v2/collections/(?P[\d]+)` 1.1. Endpoints supported: -1.1.1 GET (Fetch a collection) +1.1.1. GET (Fetch a collection) -1.1.2 DELETE (Delete or Trash a collection and all your dependencies) +1.1.2. DELETE (Delete or Trash a collection and all your dependencies) ``` To delete pass in body of a requisition the parameter is_permanently as true. @@ -37,9 +38,9 @@ Example of JSON passed in body for updating a collection: 2.1. Endpoints supported: -2.1.1 GET (Fetch all collections) +2.1.1. GET (Fetch all collections) -2.1.2 POST (Create a collection). +2.1.2. POST (Create a collection). Example of JSON passed in body for creating a collection: @@ -57,15 +58,16 @@ Example of JSON passed in body for creating a collection: "default_view_mode": "string" } ``` +------ #### Items 1. Route `wp-json/tainacan/v2/collection/(?P[\d]+)/items` 1.1. Endpoints supported: -1.1.1 GET (Fetch all items from a collection) +1.1.1. GET (Fetch all items from a collection) -1.1.2 POST (Create a item in a collection) +1.1.2. POST (Create a item in a collection) Example of JSON passed in body for creating a item: @@ -81,16 +83,16 @@ Example of JSON passed in body for creating a item: 2.1. Endpoints supported: -2.1.1 GET (Fetch a item) +2.1.1. GET (Fetch a item) -2.1.2 DELETE (Delete or Trash a item and all your dependencies) +2.1.2. DELETE (Delete or Trash a item and all your dependencies) ``` To delete pass in body of a requisition the parameter is_permanently as true. To only trash pass false. ``` -2.1.3 PATCH or PUT (Update a item) +2.1.3. PATCH or PUT (Update a item) Example of JSON passed in body for updating a item:     @@ -101,16 +103,16 @@ Example of JSON passed in body for updating a item: ... } ``` +------ +#### Fields -#### Metadata - -1. Route `wp-json/tainacan/v2/collection/(?P[\d]+)/metadata` +1. Route `wp-json/tainacan/v2/collection/(?P[\d]+)/fields` 1.1. Endpoints supported: -1.1.1 POST (Create a metadata in collection and all your items) +1.1.1. POST (Create a field in collection and all it items) -In body of requisition pass a JSON with the attributes of metadata like: +In body of requisition pass a JSON with the attributes of field like: ```javascript { @@ -130,25 +132,46 @@ In body of requisition pass a JSON with the attributes of metadata like: } ``` -1.1.2 GET (Fetch all collection metadata) - -2. Route `wp-json/tainacan/v2/item/(?P[\d]+)/metadata` +1.1.2 GET (Fetch all collection field) + +2. Route `wp-json/tainacan/v2/collection/(?P[\d]+)/fields/(?P[\d]+)` 2.1. Endpoints supported: + +2.1.1. PATCH or PUT (Update a field in a collection and all it items) + +In body of requisition pass a JSON with the attributes you need to update, like: + +```javascript +{ +"name": "string", +"description": "string", +} +``` +------ +#### Item Metadata + +1. Route `wp-json/tainacan/v2/item/(?P[\d]+)/metadata/(?P[\d]+)` + +1.1. Endpoints supported: -2.1.1 POST (Set a value of item metadata) +1.1.1. PATCH or PUT (Set value of a metadata) -In body of requisition pass a JSON with value e and id of metadata like: +In body of requisition pass a JSON with values of metadata, like: ```javascript { -"metadata_id": "integer", "values": "[any, type]" } ``` - -2.1.2 GET (Fetch all item metadata, with your values) - + +2. Route `wp-json/tainacan/v2/item/(?P[\d]+)/metadata` + +2.1. Enpoints supported: + +2.1.1. GET (Fetch all item metadata, with it values) + +------ #### Taxonomies 1. Route `wp-json/tainacan/v2/taxonomies` @@ -177,16 +200,16 @@ Example of JSON passed in body for creating a taxonomy: 2.1. Endpoints supported: -2.1.1 GET (Fetch a taxonomy) +2.1.1. GET (Fetch a taxonomy) -2.1.2 DELETE (Delete or trash a taxonomy) +2.1.2. DELETE (Delete or trash a taxonomy) ``` To delete pass in body of requisition the parameter is_permanently as true. To only trash pass false. ``` -2.1.3 PATCH or PUT (Update a taxonomy) +2.1.3. PATCH or PUT (Update a taxonomy) Example of JSON passed in body for updating a taxonomy:     @@ -198,13 +221,20 @@ Example of JSON passed in body for updating a taxonomy: } ``` +3. Route `wp-json/tainacan/v2/taxonomies/(?P[\d]+)/collection/(?P[\d]+)` + +3.1. Routes supported: + +3.1.1. PATCH or PUT (Add a Collection in a Taxonomy) + +------ #### Filters 1. Route `wp-json/tainacan/v2/filters` -1.1 Endpoints supported: +1.1. Endpoints supported: -1.1.1 POST (Create a filter) +1.1.1. POST (Create a filter) Example of JSON passed in body for creating a filter: @@ -221,20 +251,20 @@ Example of JSON passed in body for creating a filter: } ``` -1.1.2 GET (Fetch all filters) +1.1.2. GET (Fetch all filters) 2. Route `wp-json/tainacan/v2/filters/(?P[\d]+)` 2.1. Endpoints supported: -2.1.1 DELETE (Delete or trash a filter) +2.1.1. DELETE (Delete or trash a filter) ``` To delete pass in body of requisition the parameter is_permanently as true. To only trash pass false. ``` -2.1.2 PATCH or PUT (Update a filter) +2.1.2. PATCH or PUT (Update a filter) Example of JSON passed in body for updating a filter: @@ -245,15 +275,16 @@ Example of JSON passed in body for updating a filter: } ``` -2.1.3 GET (Fetch a filter) - +2.1.3. GET (Fetch a filter) + +------ #### Terms 1. Route `wp-json/tainacan/v2/taxonomy/(?P[\d]+)/terms` -1.1 Endpoints supported: +1.1. Endpoints supported: -1.1.1 POST (Create a term in a taxonomy) +1.1.1. POST (Create a term in a taxonomy) Example of JSON passed in body for creating a term: @@ -269,11 +300,11 @@ Example of JSON passed in body for creating a term: 2. Route `wp-json/tainacan/v2/taxonomy/(?P[\d]+)/terms/(?P[\d]+)` -2.1 Endpoints supported: +2.1. Endpoints supported: -2.1.1 GET (Fecth a term of a taxonomy) +2.1.1. GET (Fecth a term of a taxonomy) -2.1.2 PATCH or PUT (Update a term in a taxonomy) +2.1.2. PATCH or PUT (Update a term in a taxonomy) Example of JSON passed in body for updating a term: @@ -284,4 +315,19 @@ Example of JSON passed in body for updating a term: } ``` -2.1.3 DELETE (Delete a term of a taxonoy) +2.1.3. DELETE (Delete a term of a taxonoy) + +------ +#### Logs + +1. Route `wp-json/tainacan/v2/logs` + +1.1. Endpoints supported: + +1.1.1. GET (Get all logs) + +2. Route `wp-json/tainacan/v2/logs/(?P[\d]+)` + +2.1. Enpoints supported: + +2.1.1. GET (Get a log) From f7fc0a7707d1599523999f01178b59c955a331ec Mon Sep 17 00:00:00 2001 From: Weryques Date: Fri, 2 Feb 2018 14:14:46 -0200 Subject: [PATCH 23/29] Update tainacan-api.md --- docs/tainacan-api.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/tainacan-api.md b/docs/tainacan-api.md index b1a71cea7..c142da002 100644 --- a/docs/tainacan-api.md +++ b/docs/tainacan-api.md @@ -6,6 +6,18 @@ A REST API for Tainacan Plugin. This API uses the Wordpress REST API. ------ ### Routes and Endpoints + +#### Sumary + +1. [Collections](#collections) +1. [Items](#items) +1. [Fields](#fields) +1. [Item Metadata](#item-metadata) +1. [Taxonomies](#taxonomies) +1. [Filters](#filters) +1. [Terms](#terms) +1. [Logs](#logs) + ------ #### Collections From 51c4f763a22979c0200afd19f6e36a24cf4a3b38 Mon Sep 17 00:00:00 2001 From: Weryques Date: Fri, 2 Feb 2018 14:36:11 -0200 Subject: [PATCH 24/29] Update tainacan-api.md --- docs/tainacan-api.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/tainacan-api.md b/docs/tainacan-api.md index c142da002..687e58411 100644 --- a/docs/tainacan-api.md +++ b/docs/tainacan-api.md @@ -17,6 +17,7 @@ A REST API for Tainacan Plugin. This API uses the Wordpress REST API. 1. [Filters](#filters) 1. [Terms](#terms) 1. [Logs](#logs) +1. [Others](#others) ------ #### Collections @@ -343,3 +344,12 @@ Example of JSON passed in body for updating a term: 2.1. Enpoints supported: 2.1.1. GET (Get a log) + +------ +#### Others + +To Create, Read, Update or Delete Media or Users you can use the default routes of Wordpress. + +See about Media in [Media | REST API Handbook](https://developer.wordpress.org/rest-api/reference/media/); + +See about Users in [Users | REST API Handbook](https://developer.wordpress.org/rest-api/reference/users/). From dd0d5c037702c8486fae54dc454e7b0e4903c4a9 Mon Sep 17 00:00:00 2001 From: Weryques Date: Fri, 2 Feb 2018 14:37:06 -0200 Subject: [PATCH 25/29] Update tainacan-api.md --- docs/tainacan-api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tainacan-api.md b/docs/tainacan-api.md index 687e58411..93cfd7f30 100644 --- a/docs/tainacan-api.md +++ b/docs/tainacan-api.md @@ -7,7 +7,7 @@ A REST API for Tainacan Plugin. This API uses the Wordpress REST API. ------ ### Routes and Endpoints -#### Sumary +#### Summary 1. [Collections](#collections) 1. [Items](#items) From b47d815a7452038ba8cbf0e7a132d96dff15625f Mon Sep 17 00:00:00 2001 From: Weryques Date: Wed, 14 Feb 2018 14:41:48 -0200 Subject: [PATCH 26/29] Update tainacan-api.md --- docs/tainacan-api.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/tainacan-api.md b/docs/tainacan-api.md index 93cfd7f30..d3d64121e 100644 --- a/docs/tainacan-api.md +++ b/docs/tainacan-api.md @@ -178,6 +178,15 @@ In body of requisition pass a JSON with values of metadata, like: } ``` +If you want to update a metadata with multiple values, you will need to pass a body like (attribute multiple with value 'yes'): + +```javascript +{ +"values": "[any, type]", +"multiple": "yes" +} +``` + 2. Route `wp-json/tainacan/v2/item/(?P[\d]+)/metadata` 2.1. Enpoints supported: From 50c219c383d45c776fc383264a99995d6d934a8c Mon Sep 17 00:00:00 2001 From: Weryques Date: Wed, 14 Feb 2018 16:16:39 -0200 Subject: [PATCH 27/29] Update tainacan-api.md --- docs/tainacan-api.md | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/docs/tainacan-api.md b/docs/tainacan-api.md index d3d64121e..3ca413699 100644 --- a/docs/tainacan-api.md +++ b/docs/tainacan-api.md @@ -174,16 +174,10 @@ In body of requisition pass a JSON with values of metadata, like: ```javascript { -"values": "[any, type]" -} -``` - -If you want to update a metadata with multiple values, you will need to pass a body like (attribute multiple with value 'yes'): - -```javascript -{ -"values": "[any, type]", -"multiple": "yes" +"values": "[ +{ "new": "any_type", "prev": "any_type"}, +{ "new": "any_type", "prev": ""} +]" } ``` From 4c63362d841de9c4df32340f9e791363672b611d Mon Sep 17 00:00:00 2001 From: Weryques Date: Wed, 14 Feb 2018 16:17:14 -0200 Subject: [PATCH 28/29] Update tainacan-api.md --- docs/tainacan-api.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/tainacan-api.md b/docs/tainacan-api.md index 3ca413699..2fc80d3df 100644 --- a/docs/tainacan-api.md +++ b/docs/tainacan-api.md @@ -174,10 +174,10 @@ In body of requisition pass a JSON with values of metadata, like: ```javascript { -"values": "[ +"values": [ { "new": "any_type", "prev": "any_type"}, { "new": "any_type", "prev": ""} -]" +] } ``` From 0918656a8aec054c3f03908946392acb2799e1c6 Mon Sep 17 00:00:00 2001 From: Weryques Date: Thu, 22 Feb 2018 13:07:39 -0300 Subject: [PATCH 29/29] Update tainacan-api.md --- docs/tainacan-api.md | 363 +++++++++++++++++++++---------------------- 1 file changed, 178 insertions(+), 185 deletions(-) diff --git a/docs/tainacan-api.md b/docs/tainacan-api.md index 2fc80d3df..a585450a0 100644 --- a/docs/tainacan-api.md +++ b/docs/tainacan-api.md @@ -24,329 +24,322 @@ A REST API for Tainacan Plugin. This API uses the Wordpress REST API. 1. Route `wp-json/tainacan/v2/collections/(?P[\d]+)` -1.1. Endpoints supported: + 1. Endpoints supported: -1.1.1. GET (Fetch a collection) - -1.1.2. DELETE (Delete or Trash a collection and all your dependencies) - -``` -To delete pass in body of a requisition the parameter is_permanently as true. -To only trash pass false. -``` - -1.1.3 PATCH or PUT (Update a collection) + 1. GET (Fetch a collection) +       + 1. DELETE (Delete or Trash a collection and all your dependencies) + + *To delete pass in body of a requisition the parameter `is_permanently` as true. To only trash pass false.* + + 1. PATCH or PUT (Update a collection) -Example of JSON passed in body for updating a collection: + Example of JSON passed in body for updating a collection:     ```javascript -{ -"name": "string", -"description": "string", -... -} + { + "name": "string", + "description": "string", + ... + } ``` 2. Route `wp-json/tainacan/v2/collections` -2.1. Endpoints supported: + 1. Endpoints supported: -2.1.1. GET (Fetch all collections) + 1. GET (Fetch all collections) -2.1.2. POST (Create a collection). + 1. POST (Create a collection). -Example of JSON passed in body for creating a collection: + Example of JSON passed in body for creating a collection: ```javascript -{ -"name": "string", -"description": "string", -"status": "string", -"order": "string", -"parent": "integer", -"slug": "string", -"default_orderby": "string", -"default_order": "string", -"columns": "string", -"default_view_mode": "string" -} + { + "name": "string", + "description": "string", + "status": "string", + "order": "string", + "parent": "integer", + "slug": "string", + "default_orderby": "string", + "default_order": "string", + "columns": "string", + "default_view_mode": "string" + } ``` + ------ #### Items 1. Route `wp-json/tainacan/v2/collection/(?P[\d]+)/items` -1.1. Endpoints supported: + 1. Endpoints supported: -1.1.1. GET (Fetch all items from a collection) + 1. GET (Fetch all items from a collection) -1.1.2. POST (Create a item in a collection) - -Example of JSON passed in body for creating a item: + 1. POST (Create a item in a collection) + Example of JSON passed in body for creating a item: + ```javascript -{ -"title": "string", -"description": "string", -"status": "string", -} + { + "title": "string", + "description": "string", + "status": "string", + } ``` 2. Route `wp-json/tainacan/v2/items/(?P[\d]+)` -2.1. Endpoints supported: + 1. Endpoints supported: -2.1.1. GET (Fetch a item) + 1. GET (Fetch a item) -2.1.2. DELETE (Delete or Trash a item and all your dependencies) + 1. DELETE (Delete or Trash a item and all your dependencies) -``` -To delete pass in body of a requisition the parameter is_permanently as true. -To only trash pass false. + *To delete pass in body of a requisition the parameter is_permanently as true. To only trash pass false.* + + 1. PATCH or PUT (Update a item) + + Example of JSON passed in body for updating a item: +     +```javascript + { + "title": "string", + "description": "string", + ... + } ``` -2.1.3. PATCH or PUT (Update a item) - -Example of JSON passed in body for updating a item: -     -``` -{ -"title": "string", -"description": "string", -... -} -``` ------ #### Fields 1. Route `wp-json/tainacan/v2/collection/(?P[\d]+)/fields` -1.1. Endpoints supported: + 1. Endpoints supported: + 1. GET (Fetch all collection field) + + 1. POST (Create a field in collection and all it items) -1.1.1. POST (Create a field in collection and all it items) - -In body of requisition pass a JSON with the attributes of field like: + In body of requisition pass a JSON with the attributes of field like: ```javascript -{ -"name": "string", -"description": "string", -"field_type": "string", -"order": "string", -"parent": "integer", -"required": "string", -"collection_key": "string", -"multiple": "string", -"cardinality": "string", -"privacy": "string", -"mask": "string", -"default_value": "string", -"field_type_options": "string", -} + { + "name": "string", + "description": "string", + "field_type": "string", + "order": "string", + "parent": "integer", + "required": "string", + "collection_key": "string", + "multiple": "string", + "cardinality": "string", + "privacy": "string", + "mask": "string", + "default_value": "string", + "field_type_options": "string", + } ``` - -1.1.2 GET (Fetch all collection field) 2. Route `wp-json/tainacan/v2/collection/(?P[\d]+)/fields/(?P[\d]+)` -2.1. Endpoints supported: + 1. Endpoints supported: -2.1.1. PATCH or PUT (Update a field in a collection and all it items) + 1. PATCH or PUT (Update a field in a collection and all it items) -In body of requisition pass a JSON with the attributes you need to update, like: + In body of requisition pass a JSON with the attributes you need to update, like: ```javascript -{ -"name": "string", -"description": "string", -} + { + "name": "string", + "description": "string", + } ``` + ------ #### Item Metadata 1. Route `wp-json/tainacan/v2/item/(?P[\d]+)/metadata/(?P[\d]+)` -1.1. Endpoints supported: + 1. Endpoints supported: -1.1.1. PATCH or PUT (Set value of a metadata) + 1. PATCH or PUT (Set value of a metadata) -In body of requisition pass a JSON with values of metadata, like: + In body of requisition pass a JSON with values of metadata, like: ```javascript -{ -"values": [ -{ "new": "any_type", "prev": "any_type"}, -{ "new": "any_type", "prev": ""} -] -} + { + "values": [ + { "new": "any_type", "prev": "any_type"}, + { "new": "any_type", "prev": ""} + ] + } ``` 2. Route `wp-json/tainacan/v2/item/(?P[\d]+)/metadata` -2.1. Enpoints supported: + 1. Enpoints supported: -2.1.1. GET (Fetch all item metadata, with it values) + 1. GET (Fetch all item metadata, with it values) ------ #### Taxonomies 1. Route `wp-json/tainacan/v2/taxonomies` -1.1. Endpoints supported: + 1. Endpoints supported: -1.1.1. GET (Fetch all taxonomies) + 1. GET (Fetch all taxonomies) -1.1.2. POST (Create a taxonomy) + 1. POST (Create a taxonomy) -Example of JSON passed in body for creating a taxonomy: + Example of JSON passed in body for creating a taxonomy: ```javascript -{ -"name": "string", -"description": "string", -"status": "string", -"parent": "string", -"slug": "string", -"allow_insert": "string", -"collections_ids": "array" -} + { + "name": "string", + "description": "string", + "status": "string", + "parent": "string", + "slug": "string", + "allow_insert": "string", + "collections_ids": "array" + } ``` 2. Route `wp-json/tainacan/v2/taxonomies/(?P[\d]+)` -2.1. Endpoints supported: + 1. Endpoints supported: -2.1.1. GET (Fetch a taxonomy) + 1. GET (Fetch a taxonomy) -2.1.2. DELETE (Delete or trash a taxonomy) - -``` -To delete pass in body of requisition the parameter is_permanently as true. -To only trash pass false. -``` + 1. DELETE (Delete or trash a taxonomy) -2.1.3. PATCH or PUT (Update a taxonomy) + *To delete pass in body of requisition the parameter is_permanently as true. To only trash pass false.* + + + 1. PATCH or PUT (Update a taxonomy) -Example of JSON passed in body for updating a taxonomy: + Example of JSON passed in body for updating a taxonomy:     ```javascript -{ -"name": "string", -"description": "string", -... -} + { + "name": "string", + "description": "string", + ... + } ``` 3. Route `wp-json/tainacan/v2/taxonomies/(?P[\d]+)/collection/(?P[\d]+)` -3.1. Routes supported: + 1. Routes supported: -3.1.1. PATCH or PUT (Add a Collection in a Taxonomy) + 1. PATCH or PUT (Add a Collection in a Taxonomy) ------ #### Filters -1. Route `wp-json/tainacan/v2/filters` +1. Route `wp-json/tainacan/v2/collection/(?P[\d]+)/field/(?P[\d]+)/filters` -1.1. Endpoints supported: + 1. Endpoints supported: -1.1.1. POST (Create a filter) + 1. POST (Create a filter) -Example of JSON passed in body for creating a filter: + Example of JSON passed in body for creating a filter: ```javascript -{ -"collection_id": "int", -"metadata_id": "int", -"filter_type": "string", -"filter": { -"name": "string", -"description": "string", -... -} -} + { + "filter_type": "string", + "filter": { + "name": "string", + "description": "string", + ... + } + } ``` - -1.1.2. GET (Fetch all filters) - + 2. Route `wp-json/tainacan/v2/filters/(?P[\d]+)` -2.1. Endpoints supported: + 1. Endpoints supported: -2.1.1. DELETE (Delete or trash a filter) + 1. GET (Fetch a filter) + + 1. DELETE (Delete or trash a filter) -``` -To delete pass in body of requisition the parameter is_permanently as true. -To only trash pass false. -``` + *To delete pass in body of requisition the parameter is_permanently as true. To only trash pass false.* -2.1.2. PATCH or PUT (Update a filter) + 1. PATCH or PUT (Update a filter) -Example of JSON passed in body for updating a filter: + Example of JSON passed in body for updating a filter: ```javascript -{ -"name": "string", -... -} + { + "name": "string", + ... + } ``` -2.1.3. GET (Fetch a filter) +3. Route `wp-json/tainacan/v2/filters` + + 1. Endpoints supported: + + 1. GET (Fetch all filters) ------ #### Terms 1. Route `wp-json/tainacan/v2/taxonomy/(?P[\d]+)/terms` -1.1. Endpoints supported: + 1. Endpoints supported: + + 1. GET (Fetch all tems of a taxonomy) -1.1.1. POST (Create a term in a taxonomy) - -Example of JSON passed in body for creating a term: + 1. POST (Create a term in a taxonomy) + Example of JSON passed in body for creating a term: + ```javascript -{ -"name": "string", -"user": "int", -... -} + { + "name": "string", + "user": "int", + ... + } ``` - -1.1.2 GET (Fetch all tems of a taxonomy) - + 2. Route `wp-json/tainacan/v2/taxonomy/(?P[\d]+)/terms/(?P[\d]+)` -2.1. Endpoints supported: + 1. Endpoints supported: -2.1.1. GET (Fecth a term of a taxonomy) + 1. GET (Fecth a term of a taxonomy) + + 1. DELETE (Delete a term of a taxonoy) -2.1.2. PATCH or PUT (Update a term in a taxonomy) + 1. PATCH or PUT (Update a term in a taxonomy) -Example of JSON passed in body for updating a term: + Example of JSON passed in body for updating a term: ```javascript -{ -"name": "string", -... -} + { + "name": "string", + ... + } ``` -2.1.3. DELETE (Delete a term of a taxonoy) - ------ #### Logs 1. Route `wp-json/tainacan/v2/logs` -1.1. Endpoints supported: + 1. Endpoints supported: -1.1.1. GET (Get all logs) + 1. GET (Get all logs) 2. Route `wp-json/tainacan/v2/logs/(?P[\d]+)` -2.1. Enpoints supported: + 1. Enpoints supported: -2.1.1. GET (Get a log) + 1. GET (Get a log) ------ #### Others