diff --git a/src/classes/filter-types/class-tainacan-filter-type.php b/src/classes/filter-types/class-tainacan-filter-type.php index 953d98c70..3405b7365 100644 --- a/src/classes/filter-types/class-tainacan-filter-type.php +++ b/src/classes/filter-types/class-tainacan-filter-type.php @@ -8,9 +8,21 @@ abstract class Filter_Type { private $supported_types = []; public $options; + public $component; + + public function __construct(){ + add_action('register_filter_types', array(&$this, 'register_filter_type')); + } abstract function render( $field ); + /** + * generate the fields for this field type + */ + public function form(){ + + } + /** * @return array Supported types by the filter */ @@ -27,6 +39,25 @@ abstract class Filter_Type { $this->supported_types = $supported_types; } + /** + * @return mixed + */ + public function get_component() { + return $this->component; + } + /** + * @return array + */ + public function __toArray(){ + $attributes = []; + + $attributes['className'] = get_class($this); + $attributes['component'] = $this->get_component(); + $attributes['supported_types'] = $this->get_supported_types(); + + return $attributes; + } + /** * @param $options */ diff --git a/src/classes/filter-types/class-tainacan-list-filter.php b/src/classes/filter-types/class-tainacan-list-filter.php index ffe3fff21..9946a1117 100644 --- a/src/classes/filter-types/class-tainacan-list-filter.php +++ b/src/classes/filter-types/class-tainacan-list-filter.php @@ -11,6 +11,7 @@ class List_Filter extends Filter_Type { function __construct(){ parent::set_supported_types(['string']); + $this->component = 'tainacan-filter-list'; } /** diff --git a/src/classes/filter-types/class-tainacan-range.php b/src/classes/filter-types/class-tainacan-range.php index 8e61b9c84..a978281a7 100644 --- a/src/classes/filter-types/class-tainacan-range.php +++ b/src/classes/filter-types/class-tainacan-range.php @@ -11,6 +11,7 @@ class Range extends Filter_Type { function __construct(){ parent::set_supported_types(['float','date']); + $this->component = 'tainacan-filter-range'; } /** diff --git a/src/classes/repositories/class-tainacan-filters.php b/src/classes/repositories/class-tainacan-filters.php index b2b44c4c5..5428a004f 100644 --- a/src/classes/repositories/class-tainacan-filters.php +++ b/src/classes/repositories/class-tainacan-filters.php @@ -8,6 +8,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); use \Respect\Validation\Validator as v; class Filters extends Repository { public $entities_type = '\Tainacan\Entities\Filter'; + public $filters_types = []; public function get_map() { return apply_filters('tainacan-get-map-'.$this->get_name(), [ @@ -87,6 +88,7 @@ class Filters extends Repository { 'parent_item_colon' => __('Parent Filter:', 'tainacan'), 'menu_name' => __('Filters', 'tainacan') ); + $args = array( 'labels' => $labels, 'hierarchical' => true, @@ -104,7 +106,7 @@ class Filters extends Repository { 'can_export' => true, 'rewrite' => true, 'map_meta_cap' => true, - 'capability_type' => 'tainacan-filter', + 'capability_type' => Entities\Field::get_post_type(), 'supports' => [ 'title', 'editor', @@ -209,21 +211,62 @@ class Filters extends Repository { } } - /** - * fetch all declared filter type classes - * - * @return Array of Entities\Filter_Types\Filter_Type objects - */ - public function fetch_filter_types(){ - $filters = array(); - foreach (get_declared_classes() as $class) { - if (is_subclass_of($class, '\Tainacan\Filter_Types\Filter_Type')){ - $filters[] = new $class(); - } + /** + * register field types class on array of types + * + * @param $class_name string | object The class name or the instance + */ + public function register_filter_type( $class_name ){ + if( is_object( $class_name ) ){ + $class_name = get_class( $class_name ); } - return $filters; + if(!in_array( $class_name, $this->filters_types)){ + $this->filters_types[] = $class_name; + } + } + + /** + * register field types class on array of types + * + * @param $class_name string | object The class name or the instance + */ + public function deregister_filter_type( $class_name ){ + if (is_object($class_name)) { + $class_name = get_class($class_name); + } + + $key = array_search($class_name, $this->filters_types); + if ($key !== false) { + unset($this->filters_types[$key]); + } + } + + /** + * fetch all registered filter type classes + * + * Possible outputs are: + * CLASS (default) - returns the Class name of of filter types registered + * NAME - return an Array of the names of filter types registered + * + * @param $output string CLASS | NAME + * @return array of Entities\Filter_Types\Filter_Type classes path name + */ + public function fetch_filter_types( $output = 'CLASS'){ + $return = []; + + do_action('register_filter_types'); + + if( $output === 'NAME' ){ + foreach ($this->filters_types as $filter_type) { + $return[] = str_replace('Tainacan\Filter_Types\\','', $filter_type); + } + + return $return; + } + + return $this->filters_types; } /** diff --git a/src/classes/tainacan-creator.php b/src/classes/tainacan-creator.php index a710bb035..b390fe349 100644 --- a/src/classes/tainacan-creator.php +++ b/src/classes/tainacan-creator.php @@ -93,6 +93,10 @@ $Tainacan_Fields->register_field_type('Tainacan\Field_Types\Checkbox'); global $Tainacan_Filters; $Tainacan_Filters = new \Tainacan\Repositories\Filters(); +//register filter type +$Tainacan_Filters->register_filter_type('Tainacan\Filter_Types\Range'); +$Tainacan_Filters->register_filter_type('Tainacan\Filter_Types\List_Filter'); + global $Tainacan_Taxonomies; $Tainacan_Taxonomies = new \Tainacan\Repositories\Taxonomies(); diff --git a/src/dev-interface/class-tainacan-dev-interface.php b/src/dev-interface/class-tainacan-dev-interface.php index ac8af9117..ba6129aeb 100644 --- a/src/dev-interface/class-tainacan-dev-interface.php +++ b/src/dev-interface/class-tainacan-dev-interface.php @@ -186,10 +186,12 @@ class DevInterface { - + field_type_dropdown($post->ID,$value); ?> + + filter_type_dropdown($post->ID,$value); ?> @@ -378,6 +380,33 @@ class DevInterface { ?> fetch_filter_types('NAME'); + ?> + + set_options($options); + echo $class->form(); + } + ?> + insert_metadata($entity, $prop); }