feat: add filter `tainacan-api-items-${filter_type}-filter-arguments` #576

This commit is contained in:
vnmedeiros 2021-11-10 14:10:24 -03:00
parent e67e66ce4a
commit 602c089897
4 changed files with 112 additions and 50 deletions

View File

@ -479,6 +479,7 @@ class REST_Items_Controller extends REST_Controller {
$m = false;
if ( !empty($filter) ) {
$f = $filter[0]->_toArray();
$filter_type_component = $filter[0]->get_filter_type_object()->get_component();
$m = $f['metadatum'];
} else {
$metadatum = $this->metadatum_repository->fetch($meta_id, 'OBJECT');
@ -518,14 +519,18 @@ class REST_Items_Controller extends REST_Controller {
}
}
$filters_arguments[] = array(
$filter_name = is_string($filter_type_component)
? "tainacan-api-items-${filter_type_component}-filter-arguments"
: 'tainacan-api-items-filter-arguments';
$filters_arguments[] = apply_filters($filter_name, array(
'filter' => $f,
'metadatum' => $m,
'arg_type' => 'meta',
'value' => $meta_value,
'label' => $meta_label,
'compare' => isset($meta['compare']) ? $meta['compare'] : '='
);
));
}
if(isset($args['post__in'])) {

View File

@ -57,7 +57,7 @@ abstract class Repository {
*/
protected function __construct() {
add_action( 'init', array( &$this, 'register_post_type' ) );
add_action( 'init', array( &$this, 'init_objects' ) );
add_action( 'init', array( &$this, 'init_objects' ) );
add_filter( 'tainacan-get-map-' . $this->get_name(), array( $this, 'get_default_properties' ) );
}

View File

@ -0,0 +1,26 @@
<?php
namespace Tainacan\Traits;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Class for formatter texts
* @author Vinicius Nunus - L3P/Medialab
*
*/
trait Singleton_Instance {
private static $instance = null;
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
$this->init();
}
}

View File

@ -9,50 +9,81 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
*/
class Numeric_List_Interval extends Filter_Type {
function __construct(){
$this->set_name( __('Numeric Interval List', 'tainacan') );
$this->set_supported_types(['float']);
$this->set_component('tainacan-filter-numeric-list-interval');
$this->set_form_component('tainacan-filter-form-numeric-list-interval');
$this->set_use_max_options(false);
$this->set_default_options([
'intervals' => [],
'showIntervalOnTag' => false
]);
$this->set_preview_template('
<div class="collapse show">
<div class="dropdown is-active">
<div role="button" class="dropdown-trigger">
<button class="button is-white">
List
<span class="icon">
<i class="tainacan-icon tainacan-icon-20px tainacan-icon-arrowdown"></i>
</span>
</button>
</div>
<div class="background"></div>
<div class="dropdown-menu">
<div role="list" class="dropdown-content">
<a class="dropdown-item is-active">Top 10</a>
<a class="dropdown-item">Top 20</a>
<a class="dropdown-item">Top 30</a>
</div>
</div>
</div>
</div>
');
}
public function get_form_labels() {
return [
'intervals' => [
'title' => __('Predefined intervals','tainacan'),
'description' => __('A list of predefined intervals that the filter will offer on a select box.','tainacan')
],
'showIntervalOnTag' => [
'title' => __('Interval on tags', 'tainacan'),
'description' => __('Whether the applyed interval values should appear on filter tags.')
]
];
}
}
function __construct(){
$this->set_name( __('Numeric Interval List', 'tainacan') );
$this->set_supported_types(['float']);
$this->set_component('tainacan-filter-numeric-list-interval');
$this->set_form_component('tainacan-filter-form-numeric-list-interval');
$this->set_use_max_options(false);
$this->set_default_options([
'intervals' => [],
'showIntervalOnTag' => false
]);
$this->set_preview_template('
<div class="collapse show">
<div class="dropdown is-active">
<div role="button" class="dropdown-trigger">
<button class="button is-white">
List
<span class="icon">
<i class="tainacan-icon tainacan-icon-20px tainacan-icon-arrowdown"></i>
</span>
</button>
</div>
<div class="background"></div>
<div class="dropdown-menu">
<div role="list" class="dropdown-content">
<a class="dropdown-item is-active">Top 10</a>
<a class="dropdown-item">Top 20</a>
<a class="dropdown-item">Top 30</a>
</div>
</div>
</div>
</div>
');
}
public function get_form_labels() {
return [
'intervals' => [
'title' => __('Predefined intervals','tainacan'),
'description' => __('A list of predefined intervals that the filter will offer on a select box.','tainacan')
],
'showIntervalOnTag' => [
'title' => __('Interval on tags', 'tainacan'),
'description' => __('Whether the applyed interval values should appear on filter tags.')
]
];
}
}
class Numeric_List_Interval_Helper {
use \Tainacan\Traits\Singleton_Instance;
protected function init() {
add_filter( 'tainacan-api-items-tainacan-filter-numeric-list-interval-filter-arguments', [$this, 'format_filter_arguments']);
}
function format_filter_arguments( $filter_arguments ) {
if(
!isset($filter_arguments['filter']) ||
!isset($filter_arguments['filter']['filter_type_options']) ||
!isset($filter_arguments['filter']['filter_type_options']['intervals'])
) {
return $filter_arguments;
}
$intervals = $filter_arguments['filter']['filter_type_options']['intervals'];
foreach($intervals as $interval) {
if (
$interval['from'] == $filter_arguments['value'][0] &&
$interval['to'] == $filter_arguments['value'][1]
) {
$filter_arguments['label'] = $interval['label'];
break;
}
}
return $filter_arguments;
}
}
Numeric_List_Interval_Helper::get_instance();