From ca3ab329d63b9cb76d9726a8689f4f001badc7d3 Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Fri, 29 Jul 2022 11:29:49 -0300 Subject: [PATCH 1/5] feat add funciton `tainacan_metadata_sections` #184 --- src/classes/tainacan-loaders.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/classes/tainacan-loaders.php b/src/classes/tainacan-loaders.php index 161c31549..651a37c44 100644 --- a/src/classes/tainacan-loaders.php +++ b/src/classes/tainacan-loaders.php @@ -71,3 +71,12 @@ function tainacan_terms() { function tainacan_roles() { return \Tainacan\Roles::get_instance(); } + + +/** + * Retrieve the singleton Metadata Repository instance + * @return \Tainacan\Repositories\Metadata The Tainacan Metadata Repository + */ +function tainacan_metadata_sections() { + return \Tainacan\Repositories\Metadata_Sections::get_instance(); +} From f69073e735f75c6a540ef773f503cb6ebd434414 Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Fri, 29 Jul 2022 11:30:17 -0300 Subject: [PATCH 2/5] test: update `test_ordenation_metadata` #184 --- tests/test-metadata-section.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test-metadata-section.php b/tests/test-metadata-section.php index eda036282..c3efac2f3 100644 --- a/tests/test-metadata-section.php +++ b/tests/test-metadata-section.php @@ -319,6 +319,22 @@ class MetadataSection extends TAINACAN_UnitTestCase { $this->assertFalse($metadata_ordinate_enabled[1]->get_enabled_for_collection()); $this->assertTrue($metadata_ordinate_enabled[2]->get_enabled_for_collection()); + //changing the metadata section of the metadata without changing the ordering + $metadatum2->set_metadata_section_id($metadata_section_a->get_id()); + $this->assertTrue($metadatum2->validate(), json_encode($metadatum2->get_errors())); + $Tainacan_Metadata->update($metadatum2); + + $metadatum_c->set_metadata_section_id($metadata_section_1->get_id()); + $this->assertTrue($metadatum_c->validate(), json_encode($metadatum_c->get_errors())); + $Tainacan_Metadata->update($metadatum_c); + + $metadata_ordinate_enabled = $Tainacan_Metadata->fetch_by_collection( $update_collection, [ 'include_disabled' => true ] ); + $this->assertEquals( 8, count($metadata_ordinate_enabled) ); + $this->assertEquals( 'metadatum3', $metadata_ordinate_enabled[0]->get_name() ); + $this->assertEquals( 'metadatum_b', $metadata_ordinate_enabled[3]->get_name() ); + $this->assertEquals( 'metadatum2', $metadata_ordinate_enabled[5]->get_name() ); + $this->assertEquals( 'metadatum_c', $metadata_ordinate_enabled[2]->get_name() ); + } } \ No newline at end of file From 28ddf605b134c9bd79a0b00d95be4f5c43a77dd2 Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Fri, 29 Jul 2022 11:31:10 -0300 Subject: [PATCH 3/5] feat: add hook `hook_metadata_update_order` to fix ordination #184 --- .../repositories/class-tainacan-metadata.php | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/classes/repositories/class-tainacan-metadata.php b/src/classes/repositories/class-tainacan-metadata.php index c1e5bd2e3..4237528bf 100644 --- a/src/classes/repositories/class-tainacan-metadata.php +++ b/src/classes/repositories/class-tainacan-metadata.php @@ -42,6 +42,7 @@ class Metadata extends Repository { add_action('tainacan-insert-tainacan-taxonomy', [$this, 'hook_taxonomies_saved_as_private']); add_action('tainacan-insert-tainacan-taxonomy', [$this, 'hook_taxonomies_saved_not_allow_insert_new_terms']); + add_action('tainacan-insert-tainacan-metadatum', [$this, 'hook_metadata_update_order']); } @@ -1734,4 +1735,38 @@ class Metadata extends Repository { return false; } + /** + * When a metadata is saved, if the metadata section changes, the ordering needs to be updated + * + * @param \Tainacan\Entities\Metadatum $metadata + * @return void + */ + public function hook_metadata_update_order($metadata) { + $tainacan_metadata_sections_repository = \tainacan_metadata_sections(); + $tainacan_collections_repository = \tainacan_collections(); + $metadata_section_id = $metadata->get_metadata_section_id(); + $metadata_section = $tainacan_metadata_sections_repository->fetch($metadata_section_id); + if ( $metadata_section instanceof \Tainacan\Entities\Metadata_Section ) { + $collection = $metadata_section->get_collection(); + $metadata_sections_order = $collection->get_metadata_section_order(); + if( empty($metadata_sections_order) ) { + return; + } + foreach( $metadata_sections_order as &$metadata_section_order ) { + $pos = array_search($metadata->get_id(), array_column($metadata_section_order['metadata_order'], 'id')); + if($pos !== false) { + if( $metadata_section_id != $metadata_section_order['id']) { + array_splice($metadata_section_order['metadata_order'], $pos, 1); + } + } else if($metadata_section_id == $metadata_section_order['id']) { + $metadata_section_order['metadata_order'][] = ["id" => $metadata->get_id(), "enabled" => $metadata->get_enabled_for_collection()]; + } + } + $collection->set_metadata_section_order($metadata_sections_order); + if($collection->validate()) { + $tainacan_collections_repository->update($collection); + } + } + } + } From 6d8c75f6401e608f767a29d4a4553bb6cb91eae7 Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Fri, 29 Jul 2022 11:31:46 -0300 Subject: [PATCH 4/5] fix: space formatting --- .../class-tainacan-collections.php | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/classes/repositories/class-tainacan-collections.php b/src/classes/repositories/class-tainacan-collections.php index edb3c559d..a923c4a7d 100644 --- a/src/classes/repositories/class-tainacan-collections.php +++ b/src/classes/repositories/class-tainacan-collections.php @@ -186,21 +186,21 @@ class Collections extends Repository { 'title' => __( 'Thumbnail', 'tainacan' ), 'description' => __( 'Squared reduced-size version of a picture that helps recognizing and organizing files', 'tainacan' ) ], - 'comment_status' => [ - 'map' => 'comment_status', - 'title' => __( 'Comment Status', 'tainacan' ), - 'type' => 'string', - 'description' => __( 'Collection 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' ] )), - ], - 'allow_comments' => [ - 'map' => 'meta', + 'comment_status' => [ + 'map' => 'comment_status', + 'title' => __( 'Comment Status', 'tainacan' ), + 'type' => 'string', + 'description' => __( 'Collection 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' ] )), + ], + 'allow_comments' => [ + 'map' => 'meta', 'title' => __( 'Allow enabling comments on items', 'tainacan' ), - 'type' => 'string', - 'description' => __( 'If this option is enabled, items of this collection can be set to enable a comments section on their page. "open" means comments are allowed, "closed" means comments are not allowed.', 'tainacan' ), - 'default' => 'closed', - 'validation' => v::optional(v::stringType()->in( [ 'open', 'closed' ] )), + 'type' => 'string', + 'description' => __( 'If this option is enabled, items of this collection can be set to enable a comments section on their page. "open" means comments are allowed, "closed" means comments are not allowed.', 'tainacan' ), + 'default' => 'closed', + 'validation' => v::optional(v::stringType()->in( [ 'open', 'closed' ] )), ], 'submission_anonymous_user' => [ 'map' => 'meta', @@ -311,7 +311,7 @@ class Collections extends Repository { * @see \Tainacan\Repositories\Repository::insert() */ public function insert( $collection ) { - $this->pre_process( $collection ); + $this->pre_process( $collection ); $this->handle_parent_order_clone( $collection ); $new_collection = parent::insert( $collection ); From 9f3ce863a0e705c1e69e724d94e1a8b8dd9d8316 Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Fri, 29 Jul 2022 11:32:22 -0300 Subject: [PATCH 5/5] fix: delete metadata section #184 --- .../class-tainacan-rest-metadata-sections-controller.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 eb0d02b57..3e4641986 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 @@ -390,14 +390,14 @@ class REST_Metadata_Sections_Controller extends REST_Controller { public function delete_item( $request ) { $metadata_section_id = $request['metadata_section_id']; $metadatum_section = $this->metadata_sections_repository->fetch($metadata_section_id); - if (! $metadatum_section instanceof Entities\Metadatum) { + if (! $metadatum_section instanceof Entities\Metadata_Section) { return new \WP_REST_Response([ - 'error_message' => __('Metadata with this ID was not found', 'tainacan'), + 'error_message' => __('Metadata section with this ID was not found', 'tainacan'), 'item_id' => $metadata_section_id ], 400); } - $metadatum_section_trashed = $this->metadata_sections_repository->trash($metadatum_section); + $metadatum_section_trashed = $this->metadata_sections_repository->delete($metadatum_section); $prepared = $this->prepare_item_for_response($metadatum_section_trashed, $request); return new \WP_REST_Response($prepared, 200); }