allow filters crud in dev interface

This commit is contained in:
Eduardo humberto 2018-02-26 14:49:09 -03:00
parent 5aa78c9a4a
commit 2b2c4537d1
6 changed files with 129 additions and 16 deletions

View File

@ -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
*/

View File

@ -11,6 +11,7 @@ class List_Filter extends Filter_Type {
function __construct(){
parent::set_supported_types(['string']);
$this->component = 'tainacan-filter-list';
}
/**

View File

@ -11,6 +11,7 @@ class Range extends Filter_Type {
function __construct(){
parent::set_supported_types(['float','date']);
$this->component = 'tainacan-filter-range';
}
/**

View File

@ -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;
}
/**

View File

@ -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();

View File

@ -186,10 +186,12 @@ class DevInterface {
<?php Helpers\HtmlHelpers::collections_dropdown( $value ); ?>
<?php elseif ($prop == 'collections_ids'): ?>
<?php Helpers\HtmlHelpers::collections_checkbox_list( $value ); ?>
<?php elseif ($prop == 'field_type_options'): ?>
<?php elseif ($prop == 'field_type_options' || $prop == 'filter_type_options'): ?>
<?php echo $value; ?>
<?php elseif ($prop == 'field_type'): ?>
<?php echo $this->field_type_dropdown($post->ID,$value); ?>
<?php elseif ($prop == 'filter_type'): ?>
<?php echo $this->filter_type_dropdown($post->ID,$value); ?>
<?php else: ?>
<textarea name="tnc_prop_<?php echo $prop; ?>"><?php echo htmlspecialchars($value); ?></textarea>
<?php endif; ?>
@ -378,6 +380,33 @@ class DevInterface {
?>
<?php
}
function filter_type_dropdown($id,$selected) {
global $Tainacan_Filters;
$class = ( class_exists( $selected ) ) ? new $selected() : '';
if(is_object( $class )){
$selected = str_replace('Tainacan\Filter_Types\\','', get_class( $class ) );
}
$types = $Tainacan_Filters->fetch_filter_types('NAME');
?>
<select name="tnc_prop_filter_type">
<?php foreach ($types as $type): ?>
<option value="<?php echo $type; ?>" <?php selected($type, $selected) ?>><?php echo $type; ?></option>
<?php endforeach; ?>
</select>
<?php
if( $class ){
$options = get_post_meta($id,'filter_type_options',true);
$class->set_options($options);
echo $class->form();
}
?>
<?php
}
function collections_checkbox_list($selected) {
global $Tainacan_Collections;
@ -432,10 +461,14 @@ class DevInterface {
$class = '\Tainacan\Field_Types\\'.$value;
update_post_meta($post_id, 'field_type_options', $_POST['field_type_'.strtolower( $value ) ] );
update_post_meta($post_id, 'field_type', wp_slash( get_class( new $class() ) ) );
} elseif($prop == 'field_type_options') {
} elseif($prop == 'field_type_options' || $prop == 'filter_type_options') {
continue;
} elseif ($prop == 'filter_type') {
$class = '\Tainacan\Filter_Types\\'.$value;
update_post_meta($post_id, 'filter_type_options', $_POST['filter_type_'.strtolower( $value ) ] );
update_post_meta($post_id, 'filter_type', wp_slash( get_class( new $class() ) ) );
} elseif ($mapped['map'] == 'meta' || $mapped['map'] == 'meta_multi') {
$repo->insert_metadata($entity, $prop);
}