From 0a1be6e3b3eee1f350284eb2fda75aae94f9ac3e Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Tue, 16 Aug 2022 23:59:05 -0300 Subject: [PATCH] fix: duplicate item adding support for copying compound metadata #751 --- .../class-tainacan-rest-items-controller.php | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/classes/api/endpoints/class-tainacan-rest-items-controller.php b/src/classes/api/endpoints/class-tainacan-rest-items-controller.php index febe56c70..b2a2c5d29 100644 --- a/src/classes/api/endpoints/class-tainacan-rest-items-controller.php +++ b/src/classes/api/endpoints/class-tainacan-rest-items-controller.php @@ -996,16 +996,32 @@ class REST_Items_Controller extends REST_Controller { $errors = []; foreach ($metadata as $item_metadatum) { - - $new_item_metadatum = new Entities\Item_Metadata_Entity( $new_item, $item_metadatum->get_metadatum() ); - $new_item_metadatum->set_value( $item_metadatum->get_value() ); - - if ( $new_item_metadatum->validate() ) { - Repositories\Item_Metadata::get_instance()->insert( $new_item_metadatum ); + if ( $item_metadatum->get_metadatum()->get_metadata_type() == 'Tainacan\Metadata_Types\Compound' ) { + $multiple_values = $item_metadatum->is_multiple() ? $item_metadatum->get_value() : [$item_metadatum->get_value()] ; + foreach ($multiple_values as $value) { + $parent_meta_id = null; + foreach ( $value as $meta_id => $meta ) { + if ( $meta instanceof Entities\Item_Metadata_Entity) { + $item_metadata = new Entities\Item_Metadata_Entity($new_item, $meta->get_metadatum(), null, $parent_meta_id); + $item_metadata->set_value( $meta->get_value() ); + if ( $item_metadata->validate() ) { + $item_metadata = Repositories\Item_Metadata::get_instance()->insert( $item_metadata ); + $parent_meta_id = $item_metadata->get_parent_meta_id(); + } else { + $errors[] = $item_metadata->get_errors(); + } + } + } + } } else { - $errors[] = $new_item_metadatum->get_errors(); + $new_item_metadatum = new Entities\Item_Metadata_Entity( $new_item, $item_metadatum->get_metadatum() ); + $new_item_metadatum->set_value( $item_metadatum->get_value() ); + if ( $new_item_metadatum->validate() ) { + Repositories\Item_Metadata::get_instance()->insert( $new_item_metadatum ); + } else { + $errors[] = $new_item_metadatum->get_errors(); + } } - } if ($args['status'] != 'draft') {