feat: using enabled metadata section to get metadatas

This commit is contained in:
vnmedeiros 2022-05-03 17:20:45 -03:00
parent d837f1eec8
commit 1c6f0f131d
3 changed files with 29 additions and 1 deletions

View File

@ -279,6 +279,15 @@ class Metadata_Sections extends Repository {
*/
public function insert( $metadata_section ) {
$new_metadata_section = parent::insert( $metadata_section );
$metadata_list = $new_metadata_section->get_metadata_list();
$id = strval($new_metadata_section->get_id());
foreach($metadata_list as $metadata_id) {
// $metadata_section_ids = get_post_meta( $metadata_id, 'metadata_section_id');
// $metadata_section_ids = array_values(array_filter($metadata_section_ids, function($value) { return !is_null($value) && !empty($value); }));
// $metadata_section_ids = $metadata_section_ids === false ? [$id] : array_merge($metadata_section_ids, [$id]) ;
//add_post_meta($metadata_id, 'metadata_section_id', $id);
update_post_meta($metadata_id, 'metadata_section_id', $id);
}
return $new_metadata_section;
}

View File

@ -230,6 +230,7 @@ class Metadata extends Repository {
'title' => __( 'Metadata section', 'tainacan' ),
'type' => ['integer', 'string', 'array'],
'description' => __( 'The metadata section ID', 'tainacan' ),
'default' => null
],
] );
}
@ -605,6 +606,7 @@ class Metadata extends Repository {
*/
public function order_result( $result, Entities\Collection $collection, $include_disabled = false ) {
$order = $collection->get_metadata_order();
$section_order = $collection->get_metadata_section_order();
if ( $order ) {
$order = ( is_array( $order ) ) ? $order : unserialize( $order );
@ -616,11 +618,23 @@ class Metadata extends Repository {
foreach ( $result as $item ) {
$id = $item->WP_Post->ID;
$index = array_search( $id, array_column( $order, 'id' ) );
$metadata_section_ids = get_post_meta( $id, 'metadata_section_id');
$enabled_metadata_section = true;
if(!empty($metadata_section_ids) && $metadata_section_ids !== false && !empty($section_order)) {
foreach( $metadata_section_ids as $metadata_section_id) {
$section_order_index = array_search( $metadata_section_id, array_column( $section_order, 'id' ) );
if ( $section_order_index !== false ) {
$enabled_metadata_section = boolval($section_order[$section_order_index]['enabled']);
break;
}
}
}
if ( $index !== false ) {
// skipping metadata disabled if the arg is set
if ( ! $include_disabled && isset( $order[ $index ]['enabled'] ) && ! $order[ $index ]['enabled'] ) {
if ( ! $include_disabled && (!$enabled_metadata_section || isset( $order[ $index ]['enabled'] ) && ! $order[ $index ]['enabled'] )) {
continue;
}

View File

@ -223,7 +223,12 @@ class MetadataSection extends TAINACAN_UnitTestCase {
$update_collection = $Tainacan_Collections->update( $collection );
$metadata_ordinate = $Tainacan_Metadata->fetch_by_collection( $update_collection );
$metadata_ordinate_enabled = $Tainacan_Metadata->fetch_by_collection( $update_collection, [ 'include_disabled' => true ] );
$this->assertEquals( 'metadatum2', $metadata_ordinate[0]->get_name() );
$this->assertEquals( 'metadatum1', $metadata_ordinate[1]->get_name() );
$this->assertEquals( 4, count($metadata_ordinate) );
$this->assertEquals( 8, count($metadata_ordinate_enabled) );
$this->assertEquals( 'metadatum3', $metadata_ordinate_enabled[1]->get_name() );
$this->assertTrue($metadata_ordinate_enabled[0]->get_enabled_for_collection());
$this->assertFalse($metadata_ordinate_enabled[1]->get_enabled_for_collection());