Itens endpoint

Image are now returned;
In case of GET all Itens the quantity of attibutes are limited;
Now all endpoints Extends TAINACAN_REST_Controller
This commit is contained in:
weryques 2018-01-25 11:14:20 -02:00
parent 7094f96bc2
commit 5e2e6ac3ae
13 changed files with 138 additions and 30 deletions

View File

@ -0,0 +1,40 @@
<?php
class TAINACAN_REST_Controller extends WP_REST_Controller {
/**
* @param $entity
*
* @return array
*/
protected function get_only_needed_attributes($entity, $map){
$entity_prepared = [
'id' => $entity->get_id(),
'description' => $entity->get_description(),
'author_id' => $entity->get_author_id(),
'creation_date' => $entity->get_creation_date(),
'modification_date' => $entity->get_modification_date(),
];
if(array_key_exists('name', $map)){
$entity_prepared['name'] = $entity->get_name();
} elseif(array_key_exists('title', $map)){
$entity_prepared['title'] = $entity->get_title();
}
if(array_key_exists('featured_image', $map)){
$entity_prepared['featured_image'] = $entity->get_featured_img();
}
if(array_key_exists('columns', $map)){
$entity_prepared['featured_image'] = $entity->get_columns();
}
return $entity_prepared;
}
}
?>

View File

@ -8,7 +8,7 @@ use Tainacan\Entities;
*
* @uses Entities\Collection and Repositories\Collections
* */
class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
class TAINACAN_REST_Collections_Controller extends TAINACAN_REST_Controller {
private $collections_repository;
private $collection;
@ -101,20 +101,6 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
return new WP_REST_Response($response, 200);
}
protected function get_only_needed_attributes($collection){
$collection_temp = [
'id' => $collection->get_id(),
'name' => $collection->get_name(),
'description' => $collection->get_description(),
'featured_image' => $collection->get_featured_img(),
'columns' => $collection->get_columns(),
'creation_date' => $collection->get_creation_date(),
'modification_date' => $collection->get_modification_date(),
];
return $collection_temp;
}
/**
*
* Receive a WP_Query or a Collection object and return both in JSON
@ -125,7 +111,9 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
* @return mixed|string|void|WP_Error|WP_REST_Response
*/
public function prepare_item_for_response($item, $request){
if($item instanceof WP_Query){
$map = $this->collections_repository->get_map();
if($item instanceof WP_Query){
$collections = [];
if ($item->have_posts()) {
@ -133,7 +121,7 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
$item->the_post();
$collection = new Entities\Collection($item->post);
$collection_resumed = $this->get_only_needed_attributes($collection);
$collection_resumed = $this->get_only_needed_attributes($collection, $map);
array_push($collections, $collection_resumed);
@ -144,7 +132,7 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
return $collections;
}
elseif(!empty($item)){
return $this->get_only_needed_attributes($item);
return $this->get_only_needed_attributes($item, $map);
}
return $item;

View File

@ -3,7 +3,7 @@
use Tainacan\Entities;
use Tainacan\Repositories;
class TAINACAN_REST_Filters_Controller extends WP_REST_Controller {
class TAINACAN_REST_Filters_Controller extends TAINACAN_REST_Controller {
private $collection;
private $collection_repository;

View File

@ -8,7 +8,7 @@ use Tainacan\Entities;
* @uses Tainacan\Repositories\
* @uses Tainacan\Entities\
*/
class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
private $items_repository;
private $item;
private $item_metadata;
@ -85,22 +85,27 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
* @return mixed|string|void|WP_Error|WP_REST_Response
*/
public function prepare_item_for_response( $item, $request ) {
$map = $this->items_repository->get_map();
if (!empty($item) && $item instanceof WP_Query){
$items_as_array = [];
$items = [];
if ($item->have_posts()) {
while ( $item->have_posts() ) {
$item->the_post();
$ite = new Entities\Item($item->post);
array_push($items_as_array, $ite->__toArray());
$item_prepared = $this->get_only_needed_attributes($ite, $map);
array_push($items, $item_prepared);
}
wp_reset_postdata();
}
return $items_as_array;
return $items;
} elseif(!empty($item)){
return $item->__toArray();
return $this->get_only_needed_attributes($item, $map);
}
return $item;

View File

@ -3,7 +3,7 @@
use Tainacan\Entities;
use Tainacan\Repositories;
class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
class TAINACAN_REST_Metadata_Controller extends TAINACAN_REST_Controller {
private $metadata;
private $metadata_repository;
private $item_metadata_repository;

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 TAINACAN_REST_Controller {
private $taxonomy;
private $taxonomy_repository;

View File

@ -3,7 +3,7 @@
use Tainacan\Entities;
use Tainacan\Repositories;
class TAINACAN_REST_Terms_Controller extends WP_REST_Controller {
class TAINACAN_REST_Terms_Controller extends TAINACAN_REST_Controller {
private $term;
private $terms_repository;
private $taxonomy;

View File

@ -1,5 +1,6 @@
<?php
$rest_controller = new TAINACAN_REST_Controller();
$rest_collections_controller = new TAINACAN_REST_Collections_Controller();
$rest_items_controller = new TAINACAN_REST_Items_Controller();
$rest_metadata_controller = new TAINACAN_REST_Metadata_Controller();

View File

@ -133,8 +133,8 @@ class Collection extends Entity {
/**
* @return mixed|null
*/
function get_author(){
return $this->get_mapped_property('author');
function get_author_id(){
return $this->get_mapped_property('author_id');
}
/**

View File

@ -28,6 +28,48 @@ class Item extends Entity {
return 'Hello, my name is '. $this->get_title();
}
/**
* @return mixed|null
*/
function get_featured_img(){
return $this->get_mapped_property('featured_image');
}
/**
* @param $value
*/
function set_featured_img($value){
$this->set_mapped_property('featured_image', $value);
}
/**
* @return mixed|null
*/
function get_modification_date(){
return $this->get_mapped_property('modification_date');
}
/**
* @return mixed|null
*/
function get_creation_date(){
return $this->get_mapped_property('creation_date');
}
/**
* @return mixed|null
*/
function get_author_id(){
return $this->get_mapped_property('author_id');
}
/**
* @return mixed|null
*/
function get_url(){
return $this->get_mapped_property('url');
}
/**
* Return the item ID
*

View File

@ -28,7 +28,7 @@ class Collections extends Repository {
'description' => __('Name of the collection', 'tainacan'),
'validation' => v::stringType(),
],
'author' => [
'author_id' => [
'map' => 'post_author',
'title' => __('Author', 'tainacan'),
'type' => 'string',

View File

@ -33,6 +33,36 @@ class Items extends Repository {
'description'=> __('The collection ID', 'tainacan'),
'validation' => ''
],
'author_id' => [
'map' => 'post_author',
'title' => __('Author', 'tainacan'),
'type' => 'string',
'description' => __('The collection author\'s user ID (numeric string)', 'tainacan')
],
'creation_date' => [
'map' => 'post_date',
'title' => __('Creation Date', 'tainacan'),
'type' => 'string',
'description' => __('The collection creation date', 'tainacan')
],
'modification_date' => [
'map' => 'post_modified',
'title' => __('Modification Date', 'tainacan'),
'type' => 'string',
'description' => __('The collection modification date', 'tainacan')
],
'url' => [
'map' => 'guid',
'title' => __('Collection URL', 'tainacan'),
'type' => 'string',
'description' => __('The collection URL', 'tainacan')
],
'featured_image' => [
'map' => 'thumbnail',
'title' => __('Featured Image', 'tainacan'),
'type' => 'string',
'description' => __('The collection thumbnail URL')
],
//'collection' => 'relation...',
// metadata .. metadata...
]);

View File

@ -8,6 +8,7 @@ const FILTER_TYPES_DIR = __DIR__ . '/filter-types/';
const REPOSITORIES_DIR = __DIR__ . '/repositories/';
const TRAITS_DIR = __DIR__ . '/traits/';
const VENDOR_DIR = __DIR__ . '/../vendor/';
const TAPI_DIR = __DIR__ . '/../api/';
const ENDPOINTS_DIR = __DIR__ . '/../api/endpoints/';
const HELPERS_DIR = __DIR__ . '/../helpers/';
@ -18,6 +19,7 @@ const DIRS = [
FILTER_TYPES_DIR,
REPOSITORIES_DIR,
TRAITS_DIR,
TAPI_DIR,
ENDPOINTS_DIR,
];