Merge branch 'Item_moderation' of github.com:tainacan/tainacan into Item_moderation

This commit is contained in:
Jacson Passold 2018-01-29 18:22:56 -02:00
commit 0e01a2bff7
12 changed files with 96 additions and 27 deletions

View File

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

View File

@ -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<collection_id>[\d]+)',
/*
Removing this undocumented endpoint.. it seems wrong.
register_rest_route($this->namespace, '/' . $this->rest_base . '/collection/(?P<collection_id>[\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<item_id>[\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);
}
}

View File

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

View File

@ -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() ) ||
@ -114,6 +115,11 @@ class Entity {
throw new \Exception(sprintf("The post type %s need to be registered, need the init hook!"));
}
$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();
}
}
}

View File

@ -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,

View File

@ -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',
@ -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([

View File

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

View File

@ -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',

View File

@ -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([

View File

@ -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\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());
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);
}
}
/**
@ -486,4 +492,4 @@ abstract class Repository {
}
?>
?>

View File

@ -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([

View File

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