FIX Filters inheritance!
This commit is contained in:
parent
0e0d2ce7bd
commit
6998dc4297
|
@ -351,6 +351,25 @@ class Filters extends Repository {
|
||||||
|
|
||||||
return $supported_filter_types;
|
return $supported_filter_types;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* fetch filters IDs based on WP_Query args
|
||||||
|
*
|
||||||
|
* to learn all args accepted in the $args parameter (@see https://developer.wordpress.org/reference/classes/wp_query/)
|
||||||
|
* You can also use a mapped property, such as name and description, as an argument and it will be mapped to the
|
||||||
|
* appropriate WP_Query argument
|
||||||
|
*
|
||||||
|
* @param array $args WP_Query args || int $args the item id
|
||||||
|
*
|
||||||
|
* @return Array array of IDs;
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function fetch_ids( $args = [] ) {
|
||||||
|
|
||||||
|
$args['fields'] = 'ids';
|
||||||
|
|
||||||
|
return $this->fetch( $args )->get_posts();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* fetch filters by collection, searches all filters available
|
* fetch filters by collection, searches all filters available
|
||||||
|
@ -372,13 +391,17 @@ class Filters extends Repository {
|
||||||
$parents[] = $collection_id;
|
$parents[] = $collection_id;
|
||||||
|
|
||||||
//search for default metadatum
|
//search for default metadatum
|
||||||
//$parents[] = $this->get_default_metadata_attribute();
|
$parents[] = 'filter_in_repository';
|
||||||
|
|
||||||
$meta_query = array(
|
$meta_query = array(
|
||||||
'key' => 'collection_id',
|
'key' => 'collection_id',
|
||||||
'value' => $parents,
|
'value' => $parents,
|
||||||
'compare' => 'IN',
|
'compare' => 'IN',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$args = array_merge( [
|
||||||
|
'parent' => 0
|
||||||
|
], $args );
|
||||||
|
|
||||||
if ( isset( $args['meta_query'] ) ) {
|
if ( isset( $args['meta_query'] ) ) {
|
||||||
$args['meta_query'][] = $meta_query;
|
$args['meta_query'][] = $meta_query;
|
||||||
|
@ -392,6 +415,53 @@ class Filters extends Repository {
|
||||||
isset( $args['include_disabled'] ) ? $args['include_disabled'] : false
|
isset( $args['include_disabled'] ) ? $args['include_disabled'] : false
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* fetch filters IDs by collection, considering inheritance
|
||||||
|
*
|
||||||
|
* @param Entities\Collection|int $collection object or ID
|
||||||
|
* @param array $args WP_Query args plus disabled_metadata
|
||||||
|
*
|
||||||
|
* @return array List of metadata IDs
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function fetch_ids_by_collection( $collection, $args = [] ) {
|
||||||
|
|
||||||
|
if ( $collection instanceof Entities\Collection ) {
|
||||||
|
$collection_id = $collection->get_id();
|
||||||
|
} elseif ( is_integer( $collection ) ) {
|
||||||
|
$collection_id = $collection;
|
||||||
|
} else {
|
||||||
|
throw new \InvalidArgumentException( 'fetch_ids_by_collection expects paramater 1 to be a integer or a \Tainacan\Entities\Collection object. ' . gettype( $collection ) . ' given' );
|
||||||
|
}
|
||||||
|
|
||||||
|
//get parent collections
|
||||||
|
$parents = get_post_ancestors( $collection_id );
|
||||||
|
|
||||||
|
//insert the actual collection
|
||||||
|
$parents[] = $collection_id;
|
||||||
|
|
||||||
|
//search for default metadatum
|
||||||
|
$parents[] = 'filter_in_repository';
|
||||||
|
|
||||||
|
$meta_query = array(
|
||||||
|
'key' => 'collection_id',
|
||||||
|
'value' => $parents,
|
||||||
|
'compare' => 'IN',
|
||||||
|
);
|
||||||
|
|
||||||
|
$args = array_merge( [
|
||||||
|
'parent' => 0
|
||||||
|
], $args );
|
||||||
|
|
||||||
|
if ( isset( $args['meta_query'] ) ) {
|
||||||
|
$args['meta_query'][] = $meta_query;
|
||||||
|
} elseif ( is_array( $args ) ) {
|
||||||
|
$args['meta_query'] = array( $meta_query );
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->fetch_ids( $args );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ordinate the result from fetch response if $collection has an ordination,
|
* Ordinate the result from fetch response if $collection has an ordination,
|
||||||
|
|
|
@ -161,4 +161,139 @@ class Filters extends TAINACAN_UnitTestCase {
|
||||||
$this->assertFalse($filter2->validate(), 'filter with a metadatum with unsupported primitive type should not validate');
|
$this->assertFalse($filter2->validate(), 'filter with a metadatum with unsupported primitive type should not validate');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test if parent metadatum are visible for children collection
|
||||||
|
*/
|
||||||
|
function test_hierarchy_filters(){
|
||||||
|
$Tainacan_Filters = \Tainacan\Repositories\Filters::get_instance();
|
||||||
|
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
||||||
|
|
||||||
|
$meta_repo = $this->tainacan_entity_factory->create_entity(
|
||||||
|
'metadatum',
|
||||||
|
array(
|
||||||
|
'name' => 'metadatum default',
|
||||||
|
'collection_id' => $Tainacan_Metadata->get_default_metadata_attribute(),
|
||||||
|
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
||||||
|
'status' => 'publish'
|
||||||
|
),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->tainacan_entity_factory->create_entity(
|
||||||
|
'filter',
|
||||||
|
array(
|
||||||
|
'name' => 'filter default',
|
||||||
|
'collection_id' => 'filter_in_repository',
|
||||||
|
'filter_type' => 'Tainacan\Filter_Types\Selectbox',
|
||||||
|
'metadatum' => $meta_repo->get_id(),
|
||||||
|
'status' => 'publish'
|
||||||
|
),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
$collection_grandfather = $this->tainacan_entity_factory->create_entity(
|
||||||
|
'collection',
|
||||||
|
array(
|
||||||
|
'name' => 'collection grandfather'
|
||||||
|
),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
$meta_grand = $this->tainacan_entity_factory->create_entity(
|
||||||
|
'metadatum',
|
||||||
|
array(
|
||||||
|
'name' => 'metadatum grandfather',
|
||||||
|
'collection_id' => $collection_grandfather->get_id(),
|
||||||
|
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
||||||
|
'status' => 'publish'
|
||||||
|
),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
$this->tainacan_entity_factory->create_entity(
|
||||||
|
'filter',
|
||||||
|
array(
|
||||||
|
'name' => 'filter grandfather',
|
||||||
|
'collection_id' => $collection_grandfather->get_id(),
|
||||||
|
'filter_type' => 'Tainacan\Filter_Types\Selectbox',
|
||||||
|
'metadatum' => $meta_grand->get_id(),
|
||||||
|
'status' => 'publish'
|
||||||
|
),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
$collection_father = $this->tainacan_entity_factory->create_entity(
|
||||||
|
'collection',
|
||||||
|
array(
|
||||||
|
'name' => 'collection father',
|
||||||
|
'parent' => $collection_grandfather->get_id()
|
||||||
|
),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
$meta_father = $this->tainacan_entity_factory->create_entity(
|
||||||
|
'metadatum',
|
||||||
|
array(
|
||||||
|
'name' => 'metadatum father',
|
||||||
|
'collection_id' => $collection_father->get_id(),
|
||||||
|
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
||||||
|
'status' => 'publish'
|
||||||
|
),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
$this->tainacan_entity_factory->create_entity(
|
||||||
|
'filter',
|
||||||
|
array(
|
||||||
|
'name' => 'filter father',
|
||||||
|
'collection_id' => $collection_father->get_id(),
|
||||||
|
'filter_type' => 'Tainacan\Filter_Types\Selectbox',
|
||||||
|
'metadatum' => $meta_father->get_id(),
|
||||||
|
'status' => 'publish'
|
||||||
|
),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
$collection_son = $this->tainacan_entity_factory->create_entity(
|
||||||
|
'collection',
|
||||||
|
array(
|
||||||
|
'name' => 'collection son',
|
||||||
|
'parent' => $collection_father->get_id()
|
||||||
|
),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals( $collection_grandfather->get_id(), $collection_father->get_parent() );
|
||||||
|
$this->assertEquals( $collection_father->get_id(), $collection_son->get_parent() );
|
||||||
|
|
||||||
|
$meta_son = $this->tainacan_entity_factory->create_entity(
|
||||||
|
'metadatum',
|
||||||
|
array(
|
||||||
|
'name' => 'metadatum son',
|
||||||
|
'collection_id' => $collection_son->get_id(),
|
||||||
|
'metadata_type' => 'Tainacan\Metadata_Types\Text',
|
||||||
|
'status' => 'publish'
|
||||||
|
),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
$this->tainacan_entity_factory->create_entity(
|
||||||
|
'filter',
|
||||||
|
array(
|
||||||
|
'name' => 'filter son',
|
||||||
|
'collection_id' => $collection_son->get_id(),
|
||||||
|
'filter_type' => 'Tainacan\Filter_Types\Selectbox',
|
||||||
|
'metadatum' => $meta_son->get_id(),
|
||||||
|
'status' => 'publish'
|
||||||
|
),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
$retrieve_filters = $Tainacan_Filters->fetch_by_collection( $collection_son, [], 'OBJECT' );
|
||||||
|
|
||||||
|
$retrieve_filters_ids = $Tainacan_Filters->fetch_ids_by_collection( $collection_son, [] );
|
||||||
|
|
||||||
|
// should return 4
|
||||||
|
$this->assertEquals( 4, sizeof( $retrieve_filters ) );
|
||||||
|
$this->assertEquals( 4, sizeof( $retrieve_filters_ids ) );
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue