This commit is contained in:
weryques 2018-01-22 09:50:31 -02:00
commit 1d473b1b6c
13 changed files with 103 additions and 44 deletions

View File

@ -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();
}
/**

View File

@ -20,6 +20,14 @@ 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();
@ -28,8 +36,6 @@ class TAINACAN_REST_Filters_Controller extends WP_REST_Controller {
$this->filter = new Entities\Filter();
$this->filter_repository = new Repositories\Filters();
add_action('rest_api_init', array($this, 'register_routes'));
}
public function register_routes() {

View File

@ -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'));
}
/**

View File

@ -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/<collection_id>, then
* a metadata will be created in matched collection and all your item will receive this metadata

View File

@ -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() {

View File

@ -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() {

View File

@ -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;
}
}

View File

@ -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;
}
}
?>

View File

@ -2,6 +2,9 @@
namespace Tainacan\Tests;
/**
* @group api
*/
class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase {
public function test_create_filter(){

View File

@ -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(){

View File

@ -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() {

View File

@ -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(){

View File

@ -2,6 +2,9 @@
namespace Tainacan\Tests;
/**
* @group api
*/
class TAINACAN_REST_Terms extends TAINACAN_UnitApiTestCase {
public function test_create_term(){