facets endpoints - priority for selected items ( text and relationship )

This commit is contained in:
eduardohumberto 2018-09-14 20:10:13 -03:00
parent 080664b2d1
commit 855c5b6e04
1 changed files with 32 additions and 25 deletions

View File

@ -100,41 +100,41 @@ class REST_Facets_Controller extends REST_Controller {
if(isset($request['number'])){
$args['posts_per_page'] = $request['number'];
}
$items = $this->items_repository->fetch($args, $options['collection_id'], 'WP_Query');
$ids = [];
if ($items->have_posts()) {
while ( $items->have_posts() ) {
$items->the_post();
$ids[] = (string) $items->post->ID;
$item = new Entities\Item($items->post);
$prepared_item = $restItemsClass->prepare_item_for_response($item, $request);
array_push($response, $prepared_item);
}
wp_reset_postdata();
}
// retrieve selected items
if( $selected && $request['getSelected'] && $request['getSelected'] === '1' ){
foreach( $selected as $index => $item_id ){
if( in_array($item_id,$ids) ){
continue;
}
$item = new Entities\Item($item_id);
$prepared_item = $restItemsClass->prepare_item_for_response($item, $request);
$response[$index] = $prepared_item;
$response[] = $prepared_item;
$ids[] = $item_id;
}
}
if( isset($request['number']) && ($index+1) >= $request['number']){
if ($items->have_posts()) {
while ( $items->have_posts() ) {
$items->the_post();
$item = new Entities\Item($items->post);
$prepared_item = $restItemsClass->prepare_item_for_response($item, $request);
if( in_array((string) $items->post->ID,$ids) ){
continue;
}
if( isset($request['number']) && count($response) >= $request['number']){
break;
}
array_push($response, $prepared_item);
}
wp_reset_postdata();
}
$this->total_items = $items->found_posts;
@ -234,19 +234,26 @@ class REST_Facets_Controller extends REST_Controller {
$rawValues = $this->get_values( $response );
foreach( $selected as $index => $value ){
$row = ['mvalue' => $value, 'metadatum_id' => $metadatum_id ];
$realResponse[] = $row;
if( in_array($value,$rawValues) ){
}
foreach( $rawValues as $index => $row ){
if( in_array($row['mvalue'],$selected) ){
continue;
}
$row = ['mvalue' => $value, 'metadatum_id' => $metadatum_id ];
$response[$index] = $row;
$realResponse[] = $row;
if( isset($request['number']) && ($index+1) >= $request['number']){
if( isset($request['number']) && count($realResponse) >= $request['number']){
break;
}
}
$response = $realResponse;
}
$this->set_pagination_properties_text_type( $collection_id, $metadatum_id, ($request['search']) ? $request['search'] : '' , $offset, $number );