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

@ -25,8 +25,38 @@ class Collections extends Repository {
'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',

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 )) {
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; $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

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