Merge branch 'feature/user-admin-interface' of https://github.com/tainacan/tainacan into feature/user-admin-interface

This commit is contained in:
mateuswetah 2018-01-24 14:37:13 -02:00
commit 62e7069088
6 changed files with 139 additions and 25 deletions

View File

@ -101,6 +101,20 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
return new WP_REST_Response($response, 200); 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 * Receive a WP_Query or a Collection object and return both in JSON
@ -112,22 +126,25 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
*/ */
public function prepare_item_for_response($item, $request){ public function prepare_item_for_response($item, $request){
if($item instanceof WP_Query){ if($item instanceof WP_Query){
$collections_as_json = []; $collections = [];
if ($item->have_posts()) { if ($item->have_posts()) {
while ( $item->have_posts() ) { while ( $item->have_posts() ) {
$item->the_post(); $item->the_post();
$collection = new Entities\Collection($item->post); $collection = new Entities\Collection($item->post);
array_push($collections_as_json, $collection->__toArray());
$collection_resumed = $this->get_only_needed_attributes($collection);
array_push($collections, $collection_resumed);
} }
wp_reset_postdata(); wp_reset_postdata();
} }
return $collections_as_json; return $collections;
} }
elseif(!empty($item)){ elseif(!empty($item)){
return $item->__toArray(); return $this->get_only_needed_attributes($item);
} }
return $item; return $item;

View File

@ -101,6 +101,49 @@ class Collection extends Entity {
return register_post_type($cpt_slug, $args); return register_post_type($cpt_slug, $args);
} }
/**
* @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(){
return $this->get_mapped_property('author');
}
/**
* @return mixed|null
*/
function get_url(){
return $this->get_mapped_property('url');
}
/** /**
* Get collection name * Get collection name
* *

View File

@ -21,30 +21,60 @@ class Collections extends Repository {
*/ */
public function get_map() { public function get_map() {
return apply_filters('tainacan-get-map-'.$this->get_name(), [ return apply_filters('tainacan-get-map-'.$this->get_name(), [
'name' => [ 'name' => [
'map' => 'post_title', 'map' => 'post_title',
'title' => __('Name', 'tainacan'), 'title' => __('Name', 'tainacan'),
'type' => 'string', 'type' => 'string',
'description'=> __('Name of the collection', 'tainacan'), 'description' => __('Name of the collection', 'tainacan'),
'validation' => v::stringType(), 'validation' => v::stringType(),
],
'author' => [
'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')
], ],
'order' => [ 'order' => [
'map' => 'menu_order', 'map' => 'menu_order',
'title' => __('Order', 'tainacan'), 'title' => __('Order', 'tainacan'),
'type' => 'string', 'type' => 'string',
'description'=> __('Collection order. Field used if collections are manually ordered', 'tainacan'), 'description'=> __('Collection order. Field used if collections are manually ordered', 'tainacan'),
//'validation' => v::stringType(), //'validation' => v::stringType(),
], ],
'parent' => [ 'parent' => [
'map' => 'post_parent', 'map' => 'post_parent',
'title' => __('Parent Collection', 'tainacan'), 'title' => __('Parent Collection', 'tainacan'),
'type' => 'integer', 'type' => 'integer',
'description'=> __('Parent collection ID', 'tainacan'), 'description'=> __('Parent collection ID', 'tainacan'),
//'validation' => v::stringType(), //'validation' => v::stringType(),
], ],
'description' => [ 'description' => [
'map' => 'post_content', 'map' => 'post_content',
'title' => __('Description', 'tainacan'), 'title' => __('Description', 'tainacan'),
'type' => 'string', 'type' => 'string',
'description'=> __('Collection description', 'tainacan'), 'description'=> __('Collection description', 'tainacan'),
'default' => '', 'default' => '',
@ -52,7 +82,7 @@ class Collections extends Repository {
], ],
'slug' => [ 'slug' => [
'map' => 'post_name', 'map' => 'post_name',
'title' => __('Slug', 'tainacan'), 'title' => __('Slug', 'tainacan'),
'type' => 'string', 'type' => 'string',
'description'=> __('A unique and santized string representation of the collection, used to build the collection URL', 'tainacan'), 'description'=> __('A unique and santized string representation of the collection, used to build the collection URL', 'tainacan'),
//'validation' => v::stringType(), //'validation' => v::stringType(),
@ -60,7 +90,7 @@ class Collections extends Repository {
'default_orderby' => [ 'default_orderby' => [
'map' => 'meta', 'map' => 'meta',
'title' => __('Default Order field', 'tainacan'), 'title' => __('Default Order field', 'tainacan'),
'type' => 'string', 'type' => 'string',
'description'=> __('Default property items in this collections will be ordered by', 'tainacan'), 'description'=> __('Default property items in this collections will be ordered by', 'tainacan'),
'default' => 'name', 'default' => 'name',
@ -68,7 +98,7 @@ class Collections extends Repository {
], ],
'default_order' => [ 'default_order' => [
'map' => 'meta', 'map' => 'meta',
'title' => __('Default order', 'tainacan'), 'title' => __('Default order', 'tainacan'),
'description'=> __('Default order for items in this collection. ASC or DESC', 'tainacan'), 'description'=> __('Default order for items in this collection. ASC or DESC', 'tainacan'),
'type' => 'string', 'type' => 'string',
'default' => 'ASC', 'default' => 'ASC',
@ -76,14 +106,14 @@ class Collections extends Repository {
], ],
'columns' => [ 'columns' => [
'map' => 'meta', 'map' => 'meta',
'title' => __('Columns', 'tainacan'), 'title' => __('Columns', 'tainacan'),
'type' => 'string', 'type' => 'string',
'description'=> __('List of collections property that will be displayed in the table view', 'tainacan'), 'description'=> __('List of collections property that will be displayed in the table view', 'tainacan'),
//'validation' => v::stringType(), //'validation' => v::stringType(),
], ],
'default_view_mode' => [ 'default_view_mode' => [
'map' => 'meta', 'map' => 'meta',
'title' => __('Default view mode', 'tainacan'), 'title' => __('Default view mode', 'tainacan'),
'type' => 'string', 'type' => 'string',
'description'=> __('Collection default visualization mode', 'tainacan'), 'description'=> __('Collection default visualization mode', 'tainacan'),
//'validation' => v::stringType(), //'validation' => v::stringType(),

View File

@ -281,7 +281,12 @@ abstract class Repository {
} elseif ( $mapped == 'termmeta' ){ } elseif ( $mapped == 'termmeta' ){
$property = get_term_meta($entity->WP_Term->term_id, $prop, true); $property = get_term_meta($entity->WP_Term->term_id, $prop, true);
} elseif ( isset( $entity->WP_Post )) { } elseif ( isset( $entity->WP_Post )) {
$property = isset($entity->WP_Post->$mapped) ? $entity->WP_Post->$mapped : null; if($mapped == 'thumbnail'){
$property = get_the_post_thumbnail_url($entity->WP_Post->ID, 'full');
}
else {
$property = isset($entity->WP_Post->$mapped) ? $entity->WP_Post->$mapped : null;
}
} elseif ( isset( $entity->WP_Term )) { } elseif ( isset( $entity->WP_Term )) {
$property = isset($entity->WP_Term->$mapped) ? $entity->WP_Term->$mapped : null; $property = isset($entity->WP_Term->$mapped) ? $entity->WP_Term->$mapped : null;
} }

View File

@ -9,7 +9,7 @@ const REPOSITORIES_DIR = __DIR__ . '/repositories/';
const TRAITS_DIR = __DIR__ . '/traits/'; const TRAITS_DIR = __DIR__ . '/traits/';
const VENDOR_DIR = __DIR__ . '/../vendor/'; const VENDOR_DIR = __DIR__ . '/../vendor/';
const ENDPOINTS_DIR = __DIR__ . '/../api/endpoints/'; const ENDPOINTS_DIR = __DIR__ . '/../api/endpoints/';
const HELPERS_DIR = __DIR__ . '/../helpers/'; const HELPERS_DIR = __DIR__ . '/../helpers/';
const DIRS = [ const DIRS = [
CLASSES_DIR, CLASSES_DIR,
@ -41,11 +41,16 @@ function tainacan_autoload($class_name){
} }
elseif ($class_path[0] == 'Tainacan') { elseif ($class_path[0] == 'Tainacan') {
$sliced = array_slice($class_path, 1, count($class_path) -2); $sliced = array_slice($class_path, 1, count($class_path) -2);
$lower = $sliced[0];
$sliced[0] = strtolower($lower);
$dir = CLASSES_DIR.implode(DIRECTORY_SEPARATOR, $sliced ) . '/'; if($sliced) {
$dir = str_replace('_', '-', $dir); $lower = $sliced[0];
$sliced[0] = strtolower( $lower );
$dir = CLASSES_DIR . implode( DIRECTORY_SEPARATOR, $sliced ) . '/';
$dir = str_replace( '_', '-', $dir );
} else {
$dir = CLASSES_DIR;
}
if( in_array('Field_Types', $class_path) ){ if( in_array('Field_Types', $class_path) ){
$dir.= strtolower(str_replace('_', '-' , $class_name)).'/'; $dir.= strtolower(str_replace('_', '-' , $class_name)).'/';

View File

@ -55,7 +55,7 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase {
} }
public function test_fetch_collections(){ public function test_fetch_collections(){
$x = $this->tainacan_entity_factory->create_entity( $this->tainacan_entity_factory->create_entity(
'collection', 'collection',
array( array(
'name' => 'testeApi', 'name' => 'testeApi',
@ -66,6 +66,17 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase {
true true
); );
$this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'Other',
'description' => 'adasdasdsa',
'default_order' => 'DESC',
'status' => 'publish'
),
true
);
$request = new \WP_REST_Request( 'GET', $this->namespace . '/collections' ); $request = new \WP_REST_Request( 'GET', $this->namespace . '/collections' );
$response = $this->server->dispatch( $request ); $response = $this->server->dispatch( $request );
@ -76,8 +87,11 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase {
//$data is a valid json? //$data is a valid json?
//$this->assertTrue(json_last_error() === JSON_ERROR_NONE); //$this->assertTrue(json_last_error() === JSON_ERROR_NONE);
$one_collection = $data[0]; $other_collection = $data[0];
$one_collection = $data[1];
$this->assertEquals('testeApi', $one_collection['name']); $this->assertEquals('testeApi', $one_collection['name']);
$this->assertEquals('Other', $other_collection['name']);
} }
public function test_delete_or_trash_a_collection(){ public function test_delete_or_trash_a_collection(){