From 86d45910d429d8bb445c07c04c84f38b944c7e7b Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Fri, 19 Jan 2018 20:39:32 -0200 Subject: [PATCH 1/3] add test group "api" --- tests/test-api-filters.php | 3 +++ tests/test-api-items.php | 3 +++ tests/test-api-metadata.php | 3 +++ tests/test-api-taxonomies.php | 3 +++ tests/test-api-terms.php | 3 +++ 5 files changed, 15 insertions(+) diff --git a/tests/test-api-filters.php b/tests/test-api-filters.php index 8028a660e..3c8d0e65e 100644 --- a/tests/test-api-filters.php +++ b/tests/test-api-filters.php @@ -2,6 +2,9 @@ namespace Tainacan\Tests; +/** + * @group api + */ class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase { public function test_create_filter(){ diff --git a/tests/test-api-items.php b/tests/test-api-items.php index d891542ef..82849ba96 100644 --- a/tests/test-api-items.php +++ b/tests/test-api-items.php @@ -2,6 +2,9 @@ namespace Tainacan\Tests; +/** + * @group api + */ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { public function test_create_item_in_a_collection(){ diff --git a/tests/test-api-metadata.php b/tests/test-api-metadata.php index bd22804b5..1d3a838c6 100644 --- a/tests/test-api-metadata.php +++ b/tests/test-api-metadata.php @@ -4,6 +4,9 @@ namespace Tainacan\Tests; use Tainacan\Repositories; +/** + * @group api + */ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { public function test_insert_metadata() { diff --git a/tests/test-api-taxonomies.php b/tests/test-api-taxonomies.php index 55d7a2cf9..ce3d9f19a 100644 --- a/tests/test-api-taxonomies.php +++ b/tests/test-api-taxonomies.php @@ -2,6 +2,9 @@ namespace Tainacan\Tests; +/** + * @group api + */ class TAINACAN_REST_Taxonomies_Controller extends TAINACAN_UnitApiTestCase { public function test_delete_or_trash_a_taxonomy(){ diff --git a/tests/test-api-terms.php b/tests/test-api-terms.php index bdc1f89dd..54ac49fb5 100644 --- a/tests/test-api-terms.php +++ b/tests/test-api-terms.php @@ -2,6 +2,9 @@ namespace Tainacan\Tests; +/** + * @group api + */ class TAINACAN_REST_Terms extends TAINACAN_UnitApiTestCase { public function test_create_term(){ From 8c740ee5f7defa46c4cb1a70d5b3ccf812494f8d Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Fri, 19 Jan 2018 20:43:51 -0200 Subject: [PATCH 2/3] set controller entity prop at init, not at _construct, because we need post_type registered --- .../class-tainacan-rest-collections-controller.php | 11 +++++++++-- .../class-tainacan-rest-filters-controller.php | 14 ++++++++++---- .../class-tainacan-rest-items-controller.php | 11 +++++++++-- .../class-tainacan-rest-metadata-controller.php | 11 ++++++++--- .../class-tainacan-rest-taxonomies-controller.php | 12 +++++++++--- .../class-tainacan-rest-terms-controller.php | 10 ++++++++-- 6 files changed, 53 insertions(+), 16 deletions(-) diff --git a/src/api/endpoints/class-tainacan-rest-collections-controller.php b/src/api/endpoints/class-tainacan-rest-collections-controller.php index f8883d683..fd69223de 100644 --- a/src/api/endpoints/class-tainacan-rest-collections-controller.php +++ b/src/api/endpoints/class-tainacan-rest-collections-controller.php @@ -19,10 +19,17 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller { public function __construct(){ $this->namespace = 'tainacan/v2'; $this->rest_base = 'collections'; - $this->collections_repository = new Repositories\Collections(); - $this->collection = new Entities\Collection(); add_action('rest_api_init', array($this, 'register_routes')); + add_action('init', array(&$this, 'init_objects'), 11); + } + + /** + * Initialize objects after post_type register + */ + public function init_objects() { + $this->collections_repository = new Repositories\Collections(); + $this->collection = new Entities\Collection(); } /** diff --git a/src/api/endpoints/class-tainacan-rest-filters-controller.php b/src/api/endpoints/class-tainacan-rest-filters-controller.php index 56bf27ebc..38cfacc0e 100644 --- a/src/api/endpoints/class-tainacan-rest-filters-controller.php +++ b/src/api/endpoints/class-tainacan-rest-filters-controller.php @@ -20,16 +20,22 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller { $this->namespace = '/tainacan/v2'; $this->rest_base = 'filters'; + add_action('rest_api_init', array($this, 'register_routes')); + add_action('init', array(&$this, 'init_objects'), 11); + } + + /** + * Initialize objects after post_type register + */ + public function init_objects() { $this->collection = new Entities\Collection(); $this->collection_repository = new Repositories\Collections(); - + $this->metadata = new Entities\Metadata(); $this->metadata_repository = new Repositories\Metadatas(); - + $this->filter = new Entities\Filter(); $this->filter_repository = new Repositories\Filters(); - - add_action('rest_api_init', array($this, 'register_routes')); } public function register_routes() { diff --git a/src/api/endpoints/class-tainacan-rest-items-controller.php b/src/api/endpoints/class-tainacan-rest-items-controller.php index fa40cbb4a..22ec10b19 100644 --- a/src/api/endpoints/class-tainacan-rest-items-controller.php +++ b/src/api/endpoints/class-tainacan-rest-items-controller.php @@ -21,12 +21,19 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller { public function __construct() { $this->namespace = 'tainacan/v2'; $this->rest_base = 'items'; + + add_action('rest_api_init', array($this, 'register_routes')); + add_action('init', array(&$this, 'init_objects'), 11); + } + + /** + * Initialize objects after post_type register + */ + public function init_objects() { $this->items_repository = new Repositories\Items(); $this->item = new Entities\Item(); $this->item_metadata = new Repositories\Item_Metadata(); $this->collections_repository = new Repositories\Collections(); - - add_action('rest_api_init', array($this, 'register_routes')); } /** diff --git a/src/api/endpoints/class-tainacan-rest-metadata-controller.php b/src/api/endpoints/class-tainacan-rest-metadata-controller.php index e0263e7e6..5f3af2672 100644 --- a/src/api/endpoints/class-tainacan-rest-metadata-controller.php +++ b/src/api/endpoints/class-tainacan-rest-metadata-controller.php @@ -14,16 +14,21 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller { $this->namespace = 'tainacan/v2'; $this->rest_base = 'metadata'; + add_action('rest_api_init', array($this, 'register_routes')); + add_action('init', array(&$this, 'init_objects'), 11); + } + + /** + * Initialize objects after post_type register + */ + public function init_objects() { $this->metadata = new Entities\Metadata(); $this->metadata_repository = new Repositories\Metadatas(); $this->item_metadata_repository = new Repositories\Item_Metadata(); $this->item_repository = new Repositories\Items(); $this->collection_repository = new Repositories\Collections(); - - add_action('rest_api_init', array($this, 'register_routes')); } - /** * If POST on metadata/collection/, then * a metadata will be created in matched collection and all your item will receive this metadata diff --git a/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php b/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php index 240f1a67b..00ae74f5a 100644 --- a/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php +++ b/src/api/endpoints/class-tainacan-rest-taxonomies-controller.php @@ -3,7 +3,7 @@ use Tainacan\Entities; use Tainacan\Repositories; -class TAINACAN_REST_Taxonomies_Controller extends WP_REST_Controller { +class TAINACAN_REST_Taxonomies_Controller extends \WP_REST_Controller { private $taxonomy; private $taxonomy_repository; @@ -14,10 +14,16 @@ class TAINACAN_REST_Taxonomies_Controller extends WP_REST_Controller { $this->namespace = 'tainacan/v2'; $this->rest_base = 'taxonomies'; + add_action('rest_api_init', array($this, 'register_routes')); + add_action('init', array(&$this, 'init_objects'), 11); + } + + /** + * Initialize objects after post_type register + */ + public function init_objects() { $this->taxonomy = new Entities\Taxonomy(); $this->taxonomy_repository = new Repositories\Taxonomies(); - - add_action('rest_api_init', array($this, 'register_routes')); } public function register_routes() { diff --git a/src/api/endpoints/class-tainacan-rest-terms-controller.php b/src/api/endpoints/class-tainacan-rest-terms-controller.php index e7edfc1bd..a3248122c 100644 --- a/src/api/endpoints/class-tainacan-rest-terms-controller.php +++ b/src/api/endpoints/class-tainacan-rest-terms-controller.php @@ -16,12 +16,18 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller { $this->namespace = 'tainacan/v2'; $this->rest_base = 'terms'; + add_action('rest_api_init', array($this, 'register_routes')); + add_action('init', array(&$this, 'init_objects'), 11); + } + + /** + * Initialize objects after post_type register + */ + public function init_objects() { $this->term = new Entities\Term(); $this->terms_repository = new Repositories\Terms(); $this->taxonomy = new Entities\Taxonomy(); $this->taxonomy_repository = new Repositories\Taxonomies(); - - add_action('rest_api_init', array($this, 'register_routes')); } public function register_routes() { From 3a21a7ff1bfaa76f102c410a05f005045e67ca15 Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Fri, 19 Jan 2018 20:45:51 -0200 Subject: [PATCH 3/3] move moderator meta_cap filter to collections for now and disable it because of post_type error --- .../class-tainacan-collections.php | 35 +++++++++++++++++++ .../class-tainacan-repository.php | 28 --------------- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/classes/repositories/class-tainacan-collections.php b/src/classes/repositories/class-tainacan-collections.php index 64c7d8e29..f13cc58db 100644 --- a/src/classes/repositories/class-tainacan-collections.php +++ b/src/classes/repositories/class-tainacan-collections.php @@ -10,6 +10,15 @@ use Tainacan\Entities\Collection; class Collections extends Repository { public $entities_type = '\Tainacan\Entities\Collection'; + + public function __construct() { + parent::__construct(); +// add_filter('map_meta_cap', array($this, 'map_meta_cap'), 10, 4); + } + /** + * {@inheritDoc} + * @see \Tainacan\Repositories\Repository::get_map() + */ public function get_map() { return apply_filters('tainacan-get-map-'.$this->get_name(), [ 'name' => [ @@ -233,4 +242,30 @@ 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 + $collection_cpt = get_post_type_object(Entities\Collection::get_post_type()); + if ($cap == $collection_cpt->cap->edit_post) { + $entity = new Entities\Collection($args[0]); + if ($entity) { + $moderators = $entity->get_moderators_ids(); + if (in_array($user_id, $moderators)) { + // if user is moderator, we clear the current caps + // (that might fave edit_others_posts) and leave only edit_posts + $caps = [$collection_cpt->cap->edit_posts]; + } + } + } + + return $caps; + + } + } \ No newline at end of file diff --git a/src/classes/repositories/class-tainacan-repository.php b/src/classes/repositories/class-tainacan-repository.php index bef106730..5a7067ec1 100644 --- a/src/classes/repositories/class-tainacan-repository.php +++ b/src/classes/repositories/class-tainacan-repository.php @@ -17,8 +17,6 @@ abstract class Repository { function __construct() { add_action('init', array(&$this, 'register_post_type')); add_filter('tainacan-get-map-'.$this->get_name(), array($this, 'get_default_properties')); - - add_filter('map_meta_cap', array($this, 'map_meta_cap'), 10, 4); } /** @@ -487,32 +485,6 @@ abstract class Repository { return user_can($user, $entity->cap->publish_posts, $entity); } - /** - * Filter to handle special permissions - * - * @see https://developer.wordpress.org/reference/hooks/map_meta_cap/ - * - */ - public function map_meta_cap($caps, $cap, $user_id, $args) { - - // Filters meta caps edit_tainacan-collection and check if user is moderator - $collection_cpt = get_post_type_object(Entities\Collection::get_post_type()); - if ($cap == $collection_cpt->cap->edit_post) { - $entity = new Entities\Collection($args[0]); - if ($entity) { - $moderators = $entity->get_moderators_ids(); - if (in_array($user_id, $moderators)) { - // if user is moderator, we clear the current caps - // (that might fave edit_others_posts) and leave only edit_posts - $caps = [$collection_cpt->cap->edit_posts]; - } - } - } - - return $caps; - - } - } ?> \ No newline at end of file