Return attachments with collection and item
This commit is contained in:
parent
ab5b5464b3
commit
0af3aacf2b
|
@ -131,10 +131,10 @@ 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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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'),
|
||||
|
|
|
@ -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...
|
||||
]);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue