From 856ee25ea3f46c7150249eb694043236e9f12455 Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Tue, 15 Jun 2021 15:56:45 -0300 Subject: [PATCH] feat: add related items on API #358 --- src/classes/entities/class-tainacan-item.php | 11 ++++ .../repositories/class-tainacan-items.php | 61 ++++++++++++++++++- 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/src/classes/entities/class-tainacan-item.php b/src/classes/entities/class-tainacan-item.php index 79c14d659..bb733bd34 100644 --- a/src/classes/entities/class-tainacan-item.php +++ b/src/classes/entities/class-tainacan-item.php @@ -815,4 +815,15 @@ class Item extends Entity { return $link; } + + /** + * Return related items withs the item + * + * @return array + */ + public function get_related_items($args = []) { + $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); + $related_items = $Tainacan_Items->fetch_related_items($this, $args); + return $related_items; + } } diff --git a/src/classes/repositories/class-tainacan-items.php b/src/classes/repositories/class-tainacan-items.php index 776c627ce..2cab9f001 100644 --- a/src/classes/repositories/class-tainacan-items.php +++ b/src/classes/repositories/class-tainacan-items.php @@ -116,7 +116,13 @@ class Items extends Repository { 'description' => __( 'Item comment status: "open" means comments are allowed, "closed" means comments are not allowed.', 'tainacan' ), 'default' => get_default_comment_status(Entities\Collection::get_post_type()), 'validation' => v::optional(v::stringType()->in( [ 'open', 'closed' ] )), - ] + ], + 'related_items' => [ + 'map' => 'meta', + 'title' => __( 'Related items', 'tainacan' ), + 'type' => 'array', + 'description' => __( 'The related items whit the item', 'tainacan' ), + ], ] ); } @@ -573,5 +579,58 @@ class Items extends Repository { return $caps; } + public function fetch_related_items($item, $args=[]) { + $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); + $current_collection = $item->get_collection(); + $metadatas = $Tainacan_Metadata->fetch([ + 'meta_query' => [ + [ + 'key' => 'metadata_type', + 'value' => 'Tainacan\Metadata_Types\Relationship' + ], + [ + 'key' => '_option_collection_id', + 'value' => $current_collection->get_id() + ] + ] + ], 'OBJECT'); + + $response = array(); + + foreach($metadatas as $metadata) { + $collection = $metadata->get_collection(); + $prepared_items = array(); + $items = $this->fetch([ + 'meta_query' => [ + [ + 'key' => $metadata->get_id(), + 'value' => $item->get_id() + ] + ] + ], $collection->get_id(), 'WP_Query'); + + if ($items->have_posts()) { + while ( $items->have_posts() ) { + $items->the_post(); + $item_related = new \Tainacan\Entities\Item($items->post); + $item_arr = $item_related->_toArray(); + $item_arr['thumbnail'] = $item->get_thumbnail(); + array_push($prepared_items, $item_arr); + } + wp_reset_postdata(); + } + + $response[$metadata->get_id()] = array( + 'collection_id' => $collection->get_id(), + 'collection_name' => $collection->get_name(), + 'metadata_id' => $metadata->get_id(), + 'metadata_name' => $metadata->get_name(), + 'total_items' => $items->found_posts, + 'items' => $prepared_items + ); + } + + return $response; + } }