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 = []; private $supported_types = [];
public $options; public $options;
public $component;
public function __construct(){
add_action('register_filter_types', array(&$this, 'register_filter_type'));
}
abstract function render( $field ); abstract function render( $field );
/**
* generate the fields for this field type
*/
public function form(){
}
/** /**
* @return array Supported types by the filter * @return array Supported types by the filter
*/ */
@ -27,6 +39,25 @@ abstract class Filter_Type {
$this->supported_types = $supported_types; $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 * @param $options
*/ */

View File

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

View File

@ -11,6 +11,7 @@ class Range extends Filter_Type {
function __construct(){ function __construct(){
parent::set_supported_types(['float','date']); 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; use \Respect\Validation\Validator as v;
class Filters extends Repository { class Filters extends Repository {
public $entities_type = '\Tainacan\Entities\Filter'; public $entities_type = '\Tainacan\Entities\Filter';
public $filters_types = [];
public function get_map() { public function get_map() {
return apply_filters('tainacan-get-map-'.$this->get_name(), [ return apply_filters('tainacan-get-map-'.$this->get_name(), [
@ -87,6 +88,7 @@ class Filters extends Repository {
'parent_item_colon' => __('Parent Filter:', 'tainacan'), 'parent_item_colon' => __('Parent Filter:', 'tainacan'),
'menu_name' => __('Filters', 'tainacan') 'menu_name' => __('Filters', 'tainacan')
); );
$args = array( $args = array(
'labels' => $labels, 'labels' => $labels,
'hierarchical' => true, 'hierarchical' => true,
@ -104,7 +106,7 @@ class Filters extends Repository {
'can_export' => true, 'can_export' => true,
'rewrite' => true, 'rewrite' => true,
'map_meta_cap' => true, 'map_meta_cap' => true,
'capability_type' => 'tainacan-filter', 'capability_type' => Entities\Field::get_post_type(),
'supports' => [ 'supports' => [
'title', 'title',
'editor', 'editor',
@ -209,21 +211,62 @@ class Filters extends Repository {
} }
} }
/** /**
* fetch all declared filter type classes * register field types class on array of types
* *
* @return Array of Entities\Filter_Types\Filter_Type objects * @param $class_name string | object The class name or the instance
*/ */
public function fetch_filter_types(){ public function register_filter_type( $class_name ){
$filters = array(); if( is_object( $class_name ) ){
$class_name = get_class( $class_name );
}
foreach (get_declared_classes() as $class) { if(!in_array( $class_name, $this->filters_types)){
if (is_subclass_of($class, '\Tainacan\Filter_Types\Filter_Type')){ $this->filters_types[] = $class_name;
$filters[] = new $class();
} }
} }
return $filters; /**
* 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; global $Tainacan_Filters;
$Tainacan_Filters = new \Tainacan\Repositories\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; global $Tainacan_Taxonomies;
$Tainacan_Taxonomies = new \Tainacan\Repositories\Taxonomies(); $Tainacan_Taxonomies = new \Tainacan\Repositories\Taxonomies();

View File

@ -186,10 +186,12 @@ class DevInterface {
<?php Helpers\HtmlHelpers::collections_dropdown( $value ); ?> <?php Helpers\HtmlHelpers::collections_dropdown( $value ); ?>
<?php elseif ($prop == 'collections_ids'): ?> <?php elseif ($prop == 'collections_ids'): ?>
<?php Helpers\HtmlHelpers::collections_checkbox_list( $value ); ?> <?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 echo $value; ?>
<?php elseif ($prop == 'field_type'): ?> <?php elseif ($prop == 'field_type'): ?>
<?php echo $this->field_type_dropdown($post->ID,$value); ?> <?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: ?> <?php else: ?>
<textarea name="tnc_prop_<?php echo $prop; ?>"><?php echo htmlspecialchars($value); ?></textarea> <textarea name="tnc_prop_<?php echo $prop; ?>"><?php echo htmlspecialchars($value); ?></textarea>
<?php endif; ?> <?php endif; ?>
@ -379,6 +381,33 @@ class DevInterface {
<?php <?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) { function collections_checkbox_list($selected) {
global $Tainacan_Collections; global $Tainacan_Collections;
$collections = $Tainacan_Collections->fetch([], 'OBJECT'); $collections = $Tainacan_Collections->fetch([], 'OBJECT');
@ -432,8 +461,12 @@ class DevInterface {
$class = '\Tainacan\Field_Types\\'.$value; $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_options', $_POST['field_type_'.strtolower( $value ) ] );
update_post_meta($post_id, 'field_type', wp_slash( get_class( new $class() ) ) ); 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; 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') { } elseif ($mapped['map'] == 'meta' || $mapped['map'] == 'meta_multi') {
$repo->insert_metadata($entity, $prop); $repo->insert_metadata($entity, $prop);