Return attachments with collection and item

This commit is contained in:
weryques 2018-02-23 13:48:33 -03:00
parent ab5b5464b3
commit 0af3aacf2b
5 changed files with 49 additions and 7 deletions

View File

@ -131,10 +131,10 @@ class Collection extends Entity {
} }
/** /**
* @param $value * @return mixed|null
*/ */
function set_featured_img($value){ function get_attachments(){
$this->set_mapped_property('featured_image', $value); return $this->get_mapped_property( 'attachments');
} }
/** /**

View File

@ -36,10 +36,10 @@ class Item extends Entity {
} }
/** /**
* @param $value * @return mixed|null
*/ */
function set_featured_img($value){ function get_attachments(){
$this->set_mapped_property('featured_image', $value); return $this->get_mapped_property('attachments');
} }
/** /**

View File

@ -65,6 +65,12 @@ class Collections extends Repository {
'type' => 'string', 'type' => 'string',
'description' => __('The collection thumbnail URL') 'description' => __('The collection thumbnail URL')
], ],
'attachments' => [
'map' => 'attachments',
'title' => __('Attachments', 'tainacan'),
'type' => 'array',
'description' => __('The collection attachments')
],
'order' => [ 'order' => [
'map' => 'menu_order', 'map' => 'menu_order',
'title' => __('Order', 'tainacan'), 'title' => __('Order', 'tainacan'),

View File

@ -70,6 +70,12 @@ class Items extends Repository {
'type' => 'string', 'type' => 'string',
'description' => __('The collection thumbnail URL') 'description' => __('The collection thumbnail URL')
], ],
'attachments' => [
'map' => 'attachments',
'title' => __('Attachments', 'tainacan'),
'type' => 'array',
'description' => __('The item attachments')
],
//'collection' => 'relation...', //'collection' => 'relation...',
// field .. field... // field .. field...
]); ]);

View File

@ -292,6 +292,36 @@ abstract class Repository {
} else { } else {
$property = isset($entity->WP_Post->$mapped) ? $entity->WP_Post->$mapped : null; $property = isset($entity->WP_Post->$mapped) ? $entity->WP_Post->$mapped : null;
} }
if($mapped == 'attachments'){
if(isset($entity->WP_Post) && isset($entity->WP_Post->ID)){
$attachments_query = [
'post_type' => 'attachment',
'post_per_page' => -1,
'post_parent' => $entity->WP_Post->ID,
'exclude' => get_post_thumbnail_id()
];
$attachments = get_posts($attachments_query);
$attachments_prepared = [];
if($attachments){
foreach ($attachments as $attachment){
$prepared = [
'id' => $attachment->ID,
'title' => $attachment->post_title,
'description' => $attachment->post_content,
'mime_type' => $attachment->post_mime_type,
'url' => $attachment->guid,
];
array_push($attachments_prepared, $prepared);
}
}
$property = $attachments_prepared;
}
}
} 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;
} }