Merge branch 'master' of github.com:tainacan/tainacan
This commit is contained in:
commit
1b8c7f63ca
|
@ -161,3 +161,25 @@ A REST API for Tainacan Plugin. This API uses the Wordpress REST API.
|
|||
To delete pass in body of requisition the parameter is_permanently as true.
|
||||
To only trash pass false.
|
||||
```
|
||||
|
||||
#### Filters
|
||||
|
||||
1. Route `wp-json/tainacan/v2/filters`
|
||||
1.1 Endpoints supported:
|
||||
|
||||
1.1.1 POST (Create a filter)
|
||||
|
||||
Example of JSON passed in body for creating a filter:
|
||||
|
||||
```javascript
|
||||
{
|
||||
"collection_id": "int",
|
||||
"metadata_id": "int",
|
||||
"filter": {
|
||||
"name": "string",
|
||||
"description": "string",
|
||||
"filter_type": "string",
|
||||
...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
|
|
@ -133,11 +133,7 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
|
|||
* @return bool|WP_Error
|
||||
*/
|
||||
public function get_items_permissions_check($request){
|
||||
if(current_user_can('read')){
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return $this->collections_repository->can_read($this->collection);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -147,11 +143,8 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
|
|||
* @return bool|WP_Error
|
||||
*/
|
||||
public function get_item_permissions_check($request){
|
||||
if(current_user_can('read')){
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
$collection = $this->collections_repository->fetch($request['collection_id']);
|
||||
return $this->collections_repository->can_read($collection);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -199,11 +192,7 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
|
|||
* @return bool|WP_Error
|
||||
*/
|
||||
public function create_item_permissions_check( $request ) {
|
||||
if(current_user_can('edit_posts')){
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return $this->collections_repository->can_edit($this->collection);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -251,11 +240,8 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
|
|||
* @return bool|WP_Error
|
||||
*/
|
||||
public function delete_item_permissions_check( $request ) {
|
||||
if(current_user_can('delete_posts')){
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
$collection = $this->collections_repository->fetch($request['collection_id']);
|
||||
return $this->collections_repository->can_delete($collection);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -285,12 +271,20 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
|
|||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|mixed|void
|
||||
*/
|
||||
public function get_collection_params() {
|
||||
$query_params = $this->collections_repository->get_map();
|
||||
|
||||
return apply_filters("rest_{$this->collection->get_post_type()}_collection_params", $query_params, $this->collection->get_post_type());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
*
|
||||
* @return array|mixed|void
|
||||
*/
|
||||
public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CREATABLE ) {
|
||||
$args = [
|
||||
'Object' => [
|
||||
|
@ -302,6 +296,9 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
|
|||
return apply_filters("rest_{$this->collection->get_post_type()}_collection_params", $args, $this->collection->get_post_type());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|mixed|void
|
||||
*/
|
||||
public function get_item_schema() {
|
||||
$args = $this->collections_repository->get_map();
|
||||
|
||||
|
|
|
@ -0,0 +1,109 @@
|
|||
<?php
|
||||
|
||||
use Tainacan\Entities;
|
||||
use Tainacan\Repositories;
|
||||
|
||||
class TAINACAN_REST_Filters_Controller extends WP_REST_Controller {
|
||||
private $collection;
|
||||
private $collection_repository;
|
||||
|
||||
private $metadata;
|
||||
private $metadata_repository;
|
||||
|
||||
private $filter;
|
||||
private $filter_repository;
|
||||
|
||||
/**
|
||||
* TAINACAN_REST_Filters_Controller constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->namespace = '/tainacan/v2';
|
||||
$this->rest_base = 'filters';
|
||||
|
||||
$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() {
|
||||
register_rest_route($this->namespace, '/' . $this->rest_base, array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array($this, 'create_item'),
|
||||
'permission_callback' => array($this, 'create_item_permissions_check')
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return object|void|WP_Error
|
||||
*/
|
||||
public function prepare_item_for_database( $request ) {
|
||||
$body = json_decode($request->get_body(), true);
|
||||
|
||||
$collection_id = $body['collection_id'];
|
||||
$metadata_id = $body['metadata_id'];
|
||||
$filter = $body['filter'];
|
||||
|
||||
foreach ($filter as $attribute => $value){
|
||||
try {
|
||||
$set_ = 'set_'. $attribute;
|
||||
$this->filter->$set_($value);
|
||||
} catch (\Error $error){
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
$this->filter->set_collection_id($collection_id);
|
||||
$this->filter->set_metadata($metadata_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function create_item( $request ) {
|
||||
|
||||
if(!empty($request->get_body())){
|
||||
$this->prepare_item_for_database($request);
|
||||
|
||||
if ($this->filter->validate()){
|
||||
$filter_inserted = $this->filter_repository->insert($this->filter);
|
||||
|
||||
return new WP_REST_Response($filter_inserted->__toArray(), 200);
|
||||
}
|
||||
|
||||
return new WP_REST_Response([
|
||||
'error_message' => 'One or more attributes are invalid',
|
||||
'error' => $this->filter->get_errors()
|
||||
], 400);
|
||||
}
|
||||
|
||||
return new WP_REST_Response([
|
||||
'error_message' => 'The body could not be empty.',
|
||||
'body' => $request->get_body()
|
||||
], 400);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
public function create_item_permissions_check( $request ) {
|
||||
return $this->filter_repository->can_edit($this->filter);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -12,6 +12,7 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
|
|||
private $items_repository;
|
||||
private $item;
|
||||
private $item_metadata;
|
||||
private $collections_repository;
|
||||
|
||||
/**
|
||||
* TAINACAN_REST_Items_Controller constructor.
|
||||
|
@ -23,6 +24,7 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
|
|||
$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'));
|
||||
}
|
||||
|
@ -132,19 +134,13 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
|
|||
* @return bool|WP_Error
|
||||
*/
|
||||
public function get_item_permissions_check( $request ) {
|
||||
if(current_user_can('read')){
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
$item = $this->items_repository->fetch($request['item_id']);
|
||||
return $this->items_repository->can_read($item);
|
||||
}
|
||||
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if(current_user_can('read')){
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
$collection = $this->collections_repository->fetch($request['collection_id']);
|
||||
return $this->collections_repository->can_read($collection);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -161,7 +157,7 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
|
|||
$this->item->$set_($value);
|
||||
}
|
||||
|
||||
$collection = new Entities\Collection($request[1]);
|
||||
$collection = $this->collections_repository->fetch($request[1]);
|
||||
|
||||
$this->item->set_collection($collection);
|
||||
|
||||
|
@ -229,11 +225,7 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
|
|||
* @return bool|WP_Error
|
||||
*/
|
||||
public function create_item_permissions_check( $request ) {
|
||||
if(current_user_can('edit_posts')){
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return $this->items_repository->can_edit($this->item);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -260,11 +252,8 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
|
|||
* @return bool|WP_Error
|
||||
*/
|
||||
public function delete_item_permissions_check( $request ) {
|
||||
if(current_user_can('delete_posts')){
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
$item = $this->items_repository->fetch($request['item_id']);
|
||||
return $this->items_repository->can_delete($item);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
|
|||
private $metadata_repository;
|
||||
private $item_metadata_repository;
|
||||
private $item_repository;
|
||||
private $collection_repository;
|
||||
|
||||
public function __construct() {
|
||||
$this->namespace = 'tainacan/v2';
|
||||
|
@ -17,6 +18,7 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
|
|||
$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'));
|
||||
}
|
||||
|
@ -48,7 +50,7 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
|
|||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array($this, 'delete_item'),
|
||||
'permission_callback' => array($this, 'delete_teim_permissions_check')
|
||||
'permission_callback' => array($this, 'delete_item_permissions_check')
|
||||
)
|
||||
)
|
||||
);
|
||||
|
@ -168,11 +170,11 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
|
|||
* @return bool|WP_Error
|
||||
*/
|
||||
public function create_item_permissions_check( $request ) {
|
||||
if(current_user_can('edit_posts')){
|
||||
return true;
|
||||
if(!empty($request['item_id'])){
|
||||
return $this->item_repository->can_edit(new Entities\Item());
|
||||
}
|
||||
|
||||
return false;
|
||||
return $this->collection_repository->can_edit(new Entities\Collection());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -227,30 +229,17 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
|
|||
return new WP_REST_Response($prepared_item, 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
public function get_item_permissions_check( $request ) {
|
||||
if(current_user_can('read')){
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if(current_user_can('read')){
|
||||
return true;
|
||||
if(!empty($request['item_id'])){
|
||||
return $this->item_repository->can_read(new Entities\Item());
|
||||
}
|
||||
|
||||
return false;
|
||||
return $this->collection_repository->can_read(new Entities\Collection());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -282,11 +271,11 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
|
|||
* @return bool|WP_Error
|
||||
*/
|
||||
public function delete_item_permissions_check( $request ) {
|
||||
if(current_user_can('delete_posts')){
|
||||
return true;
|
||||
if(!empty($request['item_id'])){
|
||||
return $this->item_repository->can_delete(new Entities\Item());
|
||||
}
|
||||
|
||||
return false;
|
||||
return $this->collection_repository->can_delete(new Entities\Collection());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -106,13 +106,15 @@ class TAINACAN_REST_Taxonomies_Controller extends WP_REST_Controller {
|
|||
* @return bool|WP_Error
|
||||
*/
|
||||
public function get_item_permissions_check( $request ) {
|
||||
if(current_user_can('read')){
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
$taxonomy = $this->taxonomy_repository->fetch($request['taxonomy_id']);
|
||||
return $this->taxonomy_repository->can_read($taxonomy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function delete_item( $request ) {
|
||||
$taxonomy_id = $request['taxonomy_id'];
|
||||
|
||||
|
@ -160,11 +162,8 @@ class TAINACAN_REST_Taxonomies_Controller extends WP_REST_Controller {
|
|||
* @return bool|WP_Error
|
||||
*/
|
||||
public function delete_item_permissions_check( $request ) {
|
||||
if (current_user_can('delete_posts')){
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
$taxonomy = $this->taxonomy_repository->fetch($request['taxonomy_id']);
|
||||
return $this->taxonomy_repository->can_delete($taxonomy);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -186,11 +185,7 @@ class TAINACAN_REST_Taxonomies_Controller extends WP_REST_Controller {
|
|||
* @return bool|WP_Error
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
if (current_user_can('read')){
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return $this->taxonomy_repository->can_read($this->taxonomy);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -229,11 +224,7 @@ class TAINACAN_REST_Taxonomies_Controller extends WP_REST_Controller {
|
|||
* @return bool|WP_Error
|
||||
*/
|
||||
public function create_item_permissions_check( $request ) {
|
||||
if(current_user_can('edit_posts')){
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return $this->taxonomy_repository->can_edit($this->taxonomy);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -36,6 +36,11 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $to_prepare
|
||||
*
|
||||
* @return object|void|WP_Error
|
||||
*/
|
||||
public function prepare_item_for_database( $to_prepare ) {
|
||||
$attributes = $to_prepare[0];
|
||||
$taxonomy = $to_prepare[1];
|
||||
|
@ -53,6 +58,11 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller {
|
|||
$this->term->set_taxonomy($taxonomy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WP_REST_Request $request
|
||||
*
|
||||
* @return WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function create_item( $request ) {
|
||||
$taxonomy_id = $request['taxonomy_id'];
|
||||
$body = json_decode($request->get_body(), true);
|
||||
|
@ -90,11 +100,7 @@ class TAINACAN_REST_Terms_Controller extends WP_REST_Controller {
|
|||
* @return bool|WP_Error
|
||||
*/
|
||||
public function create_item_permissions_check( $request ) {
|
||||
if(current_user_can('edit_posts')){
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return $this->terms_repository->can_edit($this->term);
|
||||
}
|
||||
|
||||
public function delete_item( $request ) {
|
||||
|
|
|
@ -5,6 +5,7 @@ $rest_items_controller = new TAINACAN_REST_Items_Controller();
|
|||
$rest_metadata_controller = new TAINACAN_REST_Metadata_Controller();
|
||||
$rest_taxonomies_controller = new TAINACAN_REST_Taxonomies_Controller();
|
||||
$rest_terms_controller = new TAINACAN_REST_Terms_Controller();
|
||||
$rest_filters_controller = new TAINACAN_REST_Filters_Controller();
|
||||
// Add here other endpoints imports
|
||||
|
||||
?>
|
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
namespace Tainacan\Tests;
|
||||
|
||||
class TAINACAN_REST_Terms_Controller extends TAINACAN_UnitApiTestCase {
|
||||
|
||||
public function test_create_filter(){
|
||||
$collection = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array(
|
||||
'name' => 'Collection filtered',
|
||||
'description' => 'Is filtered'
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
$metadata = $this->tainacan_entity_factory->create_entity(
|
||||
'metadata',
|
||||
array(
|
||||
'name' => 'Metadata filtered',
|
||||
'description' => 'Is filtered'
|
||||
)
|
||||
);
|
||||
|
||||
$request_body = json_encode(
|
||||
array(
|
||||
'collection_id' => $collection->get_id(),
|
||||
'metadata_id' => $metadata->get_id(),
|
||||
'filter' => [
|
||||
'name' => 'Filter name',
|
||||
'description' => 'This is RANGE!',
|
||||
'filter_type' => 'range'
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
$request = new \WP_REST_Request('POST', $this->namespace . '/filters');
|
||||
|
||||
$request->set_body($request_body);
|
||||
|
||||
$response = $this->server->dispatch($request);
|
||||
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals('range', $data['filter_type']);
|
||||
$this->assertEquals('Filter name', $data['name']);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in New Issue