Merge branch 'feature/user-admin-interface' of https://github.com/tainacan/tainacan into feature/user-admin-interface
This commit is contained in:
commit
62e7069088
|
@ -101,6 +101,20 @@ 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
|
||||
|
@ -112,22 +126,25 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
|
|||
*/
|
||||
public function prepare_item_for_response($item, $request){
|
||||
if($item instanceof WP_Query){
|
||||
$collections_as_json = [];
|
||||
$collections = [];
|
||||
|
||||
if ($item->have_posts()) {
|
||||
while ( $item->have_posts() ) {
|
||||
$item->the_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();
|
||||
}
|
||||
|
||||
return $collections_as_json;
|
||||
return $collections;
|
||||
}
|
||||
elseif(!empty($item)){
|
||||
return $item->__toArray();
|
||||
return $this->get_only_needed_attributes($item);
|
||||
}
|
||||
|
||||
return $item;
|
||||
|
|
|
@ -101,6 +101,49 @@ class Collection extends Entity {
|
|||
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
|
||||
*
|
||||
|
|
|
@ -21,30 +21,60 @@ class Collections extends Repository {
|
|||
*/
|
||||
public function get_map() {
|
||||
return apply_filters('tainacan-get-map-'.$this->get_name(), [
|
||||
'name' => [
|
||||
'map' => 'post_title',
|
||||
'name' => [
|
||||
'map' => 'post_title',
|
||||
'title' => __('Name', 'tainacan'),
|
||||
'type' => 'string',
|
||||
'description'=> __('Name of the collection', 'tainacan'),
|
||||
'validation' => v::stringType(),
|
||||
'type' => 'string',
|
||||
'description' => __('Name of the collection', 'tainacan'),
|
||||
'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' => [
|
||||
'map' => 'menu_order',
|
||||
'title' => __('Order', 'tainacan'),
|
||||
'title' => __('Order', 'tainacan'),
|
||||
'type' => 'string',
|
||||
'description'=> __('Collection order. Field used if collections are manually ordered', 'tainacan'),
|
||||
//'validation' => v::stringType(),
|
||||
],
|
||||
'parent' => [
|
||||
'map' => 'post_parent',
|
||||
'title' => __('Parent Collection', 'tainacan'),
|
||||
'title' => __('Parent Collection', 'tainacan'),
|
||||
'type' => 'integer',
|
||||
'description'=> __('Parent collection ID', 'tainacan'),
|
||||
//'validation' => v::stringType(),
|
||||
],
|
||||
'description' => [
|
||||
'map' => 'post_content',
|
||||
'title' => __('Description', 'tainacan'),
|
||||
'title' => __('Description', 'tainacan'),
|
||||
'type' => 'string',
|
||||
'description'=> __('Collection description', 'tainacan'),
|
||||
'default' => '',
|
||||
|
@ -52,7 +82,7 @@ class Collections extends Repository {
|
|||
],
|
||||
'slug' => [
|
||||
'map' => 'post_name',
|
||||
'title' => __('Slug', 'tainacan'),
|
||||
'title' => __('Slug', 'tainacan'),
|
||||
'type' => 'string',
|
||||
'description'=> __('A unique and santized string representation of the collection, used to build the collection URL', 'tainacan'),
|
||||
//'validation' => v::stringType(),
|
||||
|
@ -60,7 +90,7 @@ class Collections extends Repository {
|
|||
|
||||
'default_orderby' => [
|
||||
'map' => 'meta',
|
||||
'title' => __('Default Order field', 'tainacan'),
|
||||
'title' => __('Default Order field', 'tainacan'),
|
||||
'type' => 'string',
|
||||
'description'=> __('Default property items in this collections will be ordered by', 'tainacan'),
|
||||
'default' => 'name',
|
||||
|
@ -68,7 +98,7 @@ class Collections extends Repository {
|
|||
],
|
||||
'default_order' => [
|
||||
'map' => 'meta',
|
||||
'title' => __('Default order', 'tainacan'),
|
||||
'title' => __('Default order', 'tainacan'),
|
||||
'description'=> __('Default order for items in this collection. ASC or DESC', 'tainacan'),
|
||||
'type' => 'string',
|
||||
'default' => 'ASC',
|
||||
|
@ -76,14 +106,14 @@ class Collections extends Repository {
|
|||
],
|
||||
'columns' => [
|
||||
'map' => 'meta',
|
||||
'title' => __('Columns', 'tainacan'),
|
||||
'title' => __('Columns', 'tainacan'),
|
||||
'type' => 'string',
|
||||
'description'=> __('List of collections property that will be displayed in the table view', 'tainacan'),
|
||||
//'validation' => v::stringType(),
|
||||
],
|
||||
'default_view_mode' => [
|
||||
'map' => 'meta',
|
||||
'title' => __('Default view mode', 'tainacan'),
|
||||
'title' => __('Default view mode', 'tainacan'),
|
||||
'type' => 'string',
|
||||
'description'=> __('Collection default visualization mode', 'tainacan'),
|
||||
//'validation' => v::stringType(),
|
||||
|
|
|
@ -281,7 +281,12 @@ abstract class Repository {
|
|||
} elseif ( $mapped == 'termmeta' ){
|
||||
$property = get_term_meta($entity->WP_Term->term_id, $prop, true);
|
||||
} 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 )) {
|
||||
$property = isset($entity->WP_Term->$mapped) ? $entity->WP_Term->$mapped : null;
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ const REPOSITORIES_DIR = __DIR__ . '/repositories/';
|
|||
const TRAITS_DIR = __DIR__ . '/traits/';
|
||||
const VENDOR_DIR = __DIR__ . '/../vendor/';
|
||||
const ENDPOINTS_DIR = __DIR__ . '/../api/endpoints/';
|
||||
const HELPERS_DIR = __DIR__ . '/../helpers/';
|
||||
const HELPERS_DIR = __DIR__ . '/../helpers/';
|
||||
|
||||
const DIRS = [
|
||||
CLASSES_DIR,
|
||||
|
@ -41,11 +41,16 @@ function tainacan_autoload($class_name){
|
|||
}
|
||||
elseif ($class_path[0] == 'Tainacan') {
|
||||
$sliced = array_slice($class_path, 1, count($class_path) -2);
|
||||
$lower = $sliced[0];
|
||||
$sliced[0] = strtolower($lower);
|
||||
|
||||
$dir = CLASSES_DIR.implode(DIRECTORY_SEPARATOR, $sliced ) . '/';
|
||||
$dir = str_replace('_', '-', $dir);
|
||||
if($sliced) {
|
||||
$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) ){
|
||||
$dir.= strtolower(str_replace('_', '-' , $class_name)).'/';
|
||||
|
|
|
@ -55,7 +55,7 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase {
|
|||
}
|
||||
|
||||
public function test_fetch_collections(){
|
||||
$x = $this->tainacan_entity_factory->create_entity(
|
||||
$this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array(
|
||||
'name' => 'testeApi',
|
||||
|
@ -66,6 +66,17 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase {
|
|||
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' );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
@ -76,8 +87,11 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase {
|
|||
//$data is a valid json?
|
||||
//$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('Other', $other_collection['name']);
|
||||
}
|
||||
|
||||
public function test_delete_or_trash_a_collection(){
|
||||
|
|
Loading…
Reference in New Issue