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

View File

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

View File

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

View File

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

View File

@ -292,6 +292,36 @@ abstract class Repository {
} else {
$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 )) {
$property = isset($entity->WP_Term->$mapped) ? $entity->WP_Term->$mapped : null;
}