diff --git a/src/classes/api/endpoints/class-tainacan-rest-metadata-sections-controller.php b/src/classes/api/endpoints/class-tainacan-rest-metadata-sections-controller.php index 12982768f..eb0d02b57 100644 --- a/src/classes/api/endpoints/class-tainacan-rest-metadata-sections-controller.php +++ b/src/classes/api/endpoints/class-tainacan-rest-metadata-sections-controller.php @@ -140,6 +140,7 @@ class REST_Metadata_Sections_Controller extends REST_Controller { * @return array|\WP_Error|\WP_REST_Response */ public function prepare_item_for_response( $item, $request ) { + if (!empty($item)){ $item_arr = $item->_toArray(); if ($request['context'] === 'edit') { @@ -148,7 +149,9 @@ class REST_Metadata_Sections_Controller extends REST_Controller { $item_arr['enabled'] = $item->get_enabled_for_collection(); } - $metadata_list = $item->get_metadata_object_list(); + $metadata_list = $item->get_id() == 'default_section' + ? $this->metadata_sections_repository->get_default_section_metadata_object_list($item->get_collection()) + : $item->get_metadata_object_list(); $item_arr['metadata_object_list'] = []; if($metadata_list != false) { foreach($metadata_list as $metadata) { diff --git a/src/classes/entities/class-tainacan-metadata-section.php b/src/classes/entities/class-tainacan-metadata-section.php index 2930e0e62..70deb79c0 100644 --- a/src/classes/entities/class-tainacan-metadata-section.php +++ b/src/classes/entities/class-tainacan-metadata-section.php @@ -30,6 +30,16 @@ class Metadata_Section extends Entity { return apply_filters("tainacan-metadata-section-to-string", $this->get_name(), $this); } + /** + * Get the entity ID + * + * @return integer + */ + public function get_id() { + $id = $this->get_mapped_property('id'); + return isset($id) ? $id : 'default_section'; + } + /** * Return the metadata section name * diff --git a/src/classes/repositories/class-tainacan-metadata-sections.php b/src/classes/repositories/class-tainacan-metadata-sections.php index f40e5dcf4..7686c0f5d 100644 --- a/src/classes/repositories/class-tainacan-metadata-sections.php +++ b/src/classes/repositories/class-tainacan-metadata-sections.php @@ -119,6 +119,22 @@ class Metadata_Sections extends Repository { register_post_type( Entities\Metadata_Section::get_post_type(), $args ); } + public function get_default_section ($collection) { + if($collection instanceof Entities\Collection) { + $collection_id = $collection->get_id(); + } else if (is_int($collection)) { + $collection_id = $collection; + } else { + return false; + } + $defaul_section = new Entities\Metadata_Section(); + $defaul_section->set_slug('default_section'); + $defaul_section->set_name(__('Metadata', 'tainacan')); + $defaul_section->set_description(__('Metadata section', 'tainacan')); + $defaul_section->set_collection_id($collection_id); + return $defaul_section; + } + /** * fetch metadata section based on ID or WP_Query args * @@ -326,6 +342,36 @@ class Metadata_Sections extends Repository { return false; } + public function get_default_section_metadata_object_list (Entities\Collection $collection) { + $metadata_sections_ids = $this->fetch_ids(); + $args = array( + 'meta_query' => array( + array( + 'relation' => 'OR', + array( + 'key' => 'metadata_section_id', + 'value' => 'default_section', + 'compare' => '=' + ), + array( + array( + 'key' => 'metadata_section_id', + 'compare' => 'NOT EXISTS' + ) + ), + array( + 'key' => 'metadata_section_id', + 'value' => $metadata_sections_ids, + 'compare' => 'NOT IN' + ), + ) + ) + ); + $metadata_repository = \Tainacan\Repositories\Metadata::get_instance(); + $metadata_list = $metadata_repository->fetch_by_collection($collection, $args); + return $metadata_list; + } + /** * @inheritDoc */ @@ -366,12 +412,12 @@ class Metadata_Sections extends Repository { ksort( $result_ordinate ); $result_ordinate = array_merge( $result_ordinate, $not_ordinate ); - + $result_ordinate[] = $this->get_default_section($collection->get_id()); return $result_ordinate; } } - + $result[] = $this->get_default_section($collection->get_id()); return $result; } } diff --git a/src/classes/repositories/class-tainacan-metadata.php b/src/classes/repositories/class-tainacan-metadata.php index 5d305e74e..c1e5bd2e3 100644 --- a/src/classes/repositories/class-tainacan-metadata.php +++ b/src/classes/repositories/class-tainacan-metadata.php @@ -230,7 +230,7 @@ class Metadata extends Repository { 'title' => __( 'Metadata section', 'tainacan' ), 'type' => ['integer', 'string', 'array'], 'description' => __( 'The metadata section ID', 'tainacan' ), - 'default' => null + 'default' => 'default_section' ], ] ); } diff --git a/tests/test-api-metadata-section.php b/tests/test-api-metadata-section.php index 60d91e0e6..9f4cefe0b 100644 --- a/tests/test-api-metadata-section.php +++ b/tests/test-api-metadata-section.php @@ -440,9 +440,9 @@ class TAINACAN_REST_Metadata_Sections_Controller extends TAINACAN_UnitApiTestCas $metadata_list_1 = $response_data[0]['metadata_object_list']; $metadata_list_2 = $response_data[1]['metadata_object_list']; - $this->assertContains($metadatum_1->get_id(), array_column($metadata_list_1, 'id')); - $this->assertContains($metadatum_2->get_id(), array_column($metadata_list_1, 'id')); - $this->assertContains($metadatum_3->get_id(), array_column($metadata_list_2, 'id')); - $this->assertContains($metadatum_4->get_id(), array_column($metadata_list_2, 'id')); + $this->assertContains($metadatum_1->get_id(), array_column($metadata_list_1, 'id'), sprintf('Id: %s not found in array: %s', $metadatum_1->get_id(), print_r(array_column($metadata_list_1, 'id'), true))); + $this->assertContains($metadatum_2->get_id(), array_column($metadata_list_1, 'id'), sprintf('Id: %s not found in array: %s', $metadatum_2->get_id(), print_r(array_column($metadata_list_1, 'id'), true))); + $this->assertContains($metadatum_3->get_id(), array_column($metadata_list_2, 'id'), sprintf('Id: %s not found in array: %s', $metadatum_3->get_id(), print_r(array_column($metadata_list_2, 'id'), true))); + $this->assertContains($metadatum_4->get_id(), array_column($metadata_list_2, 'id'), sprintf('Id: %s not found in array: %s', $metadatum_4->get_id(), print_r(array_column($metadata_list_2, 'id'), true))); } }