diff --git a/src/views/admin/components/metadata-types/compound/class-tainacan-compound.php b/src/views/admin/components/metadata-types/compound/class-tainacan-compound.php index 8318ea155..2dbb868b2 100644 --- a/src/views/admin/components/metadata-types/compound/class-tainacan-compound.php +++ b/src/views/admin/components/metadata-types/compound/class-tainacan-compound.php @@ -169,7 +169,7 @@ class Compound extends Metadata_Type { public function get_value_as_html(Item_Metadata_Entity $item_metadata) { $value = $item_metadata->get_value(); $separator = $item_metadata->get_multivalue_separator(); - $options = $item_metadata->get_metadatum()->get_metadata_type_object()->get_options(); + $options = $item_metadata->get_metadatum()->get_metadata_type_options(); $order = $options['children_order']; $return = ''; diff --git a/src/views/admin/components/metadata-types/taxonomy/class-tainacan-taxonomy.php b/src/views/admin/components/metadata-types/taxonomy/class-tainacan-taxonomy.php index bc4d577be..d099be10f 100644 --- a/src/views/admin/components/metadata-types/taxonomy/class-tainacan-taxonomy.php +++ b/src/views/admin/components/metadata-types/taxonomy/class-tainacan-taxonomy.php @@ -74,7 +74,6 @@ class Taxonomy extends Metadata_Type { '); - add_filter( 'tainacan-term-to-html', [$this, 'term_to_html'], 10, 2 ); } /** @@ -231,37 +230,6 @@ class Taxonomy extends Metadata_Type { } - public function term_to_html($return, $term) { - - $collections = $this->get_option( 'link_filtered_by_collections' ); - - if ( !empty( $collections ) ) { - $return = ''; - $id = $term->get_id(); - - if ( $id ) { - $link = get_term_link( (int) $id ); - if (is_string($link)) { - $meta_query = [ - 'metaquery' => [ - [ - 'key' => 'collection_id', - 'compare' => 'IN', - 'value' => $collections - ] - ] - ]; - $link = $link . '?' . http_build_query( $meta_query ); - $return = ""; - $return.= $term->get_name(); - $return .= ""; - } - } - return $return; - } - return $return; - } - /** * Return the value of an Item_Metadata_Entity using a metadatum of this metadatum type as an html string * @param Item_Metadata_Entity $item_metadata @@ -320,11 +288,11 @@ class Taxonomy extends Metadata_Type { $terms = []; - $terms[] = $term->_toHtml(); + $terms[] = $this->term_to_html($term); while ($term->get_parent() > 0) { $term = \Tainacan\Repositories\Terms::get_instance()->fetch( (int) $term->get_parent(), $term->get_taxonomy() ); - $terms[] = $term->_toHtml(); + $terms[] = $this->term_to_html($term); } $terms = \array_reverse($terms); @@ -335,6 +303,35 @@ class Taxonomy extends Metadata_Type { } + private function term_to_html($term) { + $collections = $this->get_option( 'link_filtered_by_collections' ); + if ( !empty( $collections ) ) { + $return = ''; + $id = $term->get_id(); + + if ( $id ) { + $link = get_term_link( (int) $id ); + if (is_string($link)) { + $meta_query = [ + 'metaquery' => [ + [ + 'key' => 'collection_id', + 'compare' => 'IN', + 'value' => $collections + ] + ] + ]; + $link = $link . '?' . http_build_query( $meta_query ); + $return = ""; + $return.= $term->get_name(); + $return .= ""; + } + } + return $return; + } + return $term->_toHtml(); + } + public function _toArray() { $array = parent::_toArray(); diff --git a/tests/test-taxonomies.php b/tests/test-taxonomies.php index e30d6a5d7..e02ab29ba 100644 --- a/tests/test-taxonomies.php +++ b/tests/test-taxonomies.php @@ -585,4 +585,140 @@ class Taxonomies extends TAINACAN_UnitTestCase { $this->assertEquals(2, $term_repo->count); } + function test_term_taxonomy_filtered_by_collections() { + $Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance(); + $Tainacan_Terms = \Tainacan\Repositories\Terms::get_instance(); + $Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance(); + + $tax = $this->tainacan_entity_factory->create_entity( + 'taxonomy', + array( + 'name' => 'Tax', + 'description' => 'Tax', + 'allow_insert' => 'no', + 'status' => 'publish' + ), + true + ); + + //retorna a taxonomia + $tax = $Tainacan_Taxonomies->fetch($tax->get_id()); + + $term_1 = $this->tainacan_entity_factory->create_entity( + 'term', + array( + 'taxonomy' => $tax->get_db_identifier(), + 'name' => 'preto', + 'user' => get_current_user_id(), + ), + true + ); + + $term_2 = $this->tainacan_entity_factory->create_entity( + 'term', + array( + 'taxonomy' => $tax->get_db_identifier(), + 'name' => 'branco', + 'user' => get_current_user_id(), + ), + true + ); + + $collectionOnly = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'testA', + ), + true + ); + + $collectionAll = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'testB', + ), + true + ); + + $metadatumOnly = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'meta', + 'description' => 'description', + 'collection' => $collectionOnly, + 'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy', + 'status' => 'publish', + 'metadata_type_options' => [ + 'taxonomy_id' => $tax->get_id(), + 'allow_new_terms' => 'no', + 'link_filtered_by_collections' => [$collectionOnly->get_id()] + ] + ), + true + ); + + $metadatumAll = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'meta', + 'description' => 'description', + 'collection' => $collectionAll, + 'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy', + 'status' => 'publish', + 'metadata_type_options' => [ + 'taxonomy_id' => $tax->get_id(), + 'allow_new_terms' => 'no' + ] + ), + true + ); + + $itemOnly = $this->tainacan_entity_factory->create_entity( + 'item', + array( + 'title' => 'item teste', + 'description' => 'description', + 'collection' => $collectionOnly, + 'status' => 'publish' + ), + true + ); + + $itemAll = $this->tainacan_entity_factory->create_entity( + 'item', + array( + 'title' => 'item teste', + 'description' => 'description', + 'collection' => $collectionAll, + 'status' => 'publish' + ), + true + ); + + $item_metadata_only = $this->tainacan_item_metadata_factory->create_item_metadata($itemOnly, $metadatumOnly, $term_1); + $item_metadata_all = $this->tainacan_item_metadata_factory->create_item_metadata($itemAll, $metadatumAll, $term_2); + + $expected_response = get_term_link($term_2->get_id()); + $text = $item_metadata_all->get_value_as_html(); + preg_match_all('~~', $text, $matches); + $response = $matches[3][0]; + $this->assertEquals($response, $expected_response); + + + $meta_query = [ + 'metaquery' => [ + [ + 'key' => 'collection_id', + 'compare' => 'IN', + 'value' => [$collectionOnly->get_id()] + ] + ] + ]; + $expected_response = get_term_link($term_1->get_id()) . '?' . http_build_query( $meta_query ); + $text = $item_metadata_only->get_value_as_html(); + preg_match_all('~~', $text, $matches); + $response = $matches[3][0]; + $this->assertEquals($response, $expected_response); + } + }