diff --git a/src/classes/entities/class-tainacan-filter.php b/src/classes/entities/class-tainacan-filter.php index 76ea74548..45e8e2c86 100644 --- a/src/classes/entities/class-tainacan-filter.php +++ b/src/classes/entities/class-tainacan-filter.php @@ -82,23 +82,35 @@ class Filter extends Entity { } /** - * Retorna o objeto filtro + * Retorna o objeto Filter Type * - * @return Filter + * @return array || object */ function get_filter_type_object(){ - return unserialize( base64_decode( $this->get_mapped_property('filter_type_object') ) ); + $class_name = $this->get_filter_type(); + $object_type = new $class_name(); + $object_type->set_options( $this->get_filter_options() ); + return $object_type; } /** - * Retorna o filtro + * Retorna o objeto field type * - * @return string + * @return array || object */ function get_filter_type(){ return $this->get_mapped_property('filter_type'); } + /** + * Retorna o objeto Metadado + * + * @return array || object + */ + function get_filter_options(){ + return $this->get_mapped_property('filter_type_options'); + } + /** * Atribui nome ao filtro * @@ -157,14 +169,14 @@ class Filter extends Entity { * @param \Tainacan\Filter_Types\Filter_Type $value * @return void */ - function set_filter_type_object( \Tainacan\Filter_Types\Filter_Type $value ){ - // TODO: validate primitive type with filter - //if filter matches the metadata type - //if( in_array( $type->get_primitive_type(), $value->get_supported_types() ) ){ - $this->set_filter_type( get_class( $value ) ); - $this->set_mapped_property('filter_type_object', base64_encode( serialize($value) ) ); - //} - } +// function set_filter_type_object( \Tainacan\Filter_Types\Filter_Type $value ){ +// // TODO: validate primitive type with filter +// //if filter matches the metadata type +// //if( in_array( $type->get_primitive_type(), $value->get_supported_types() ) ){ +// $this->set_filter_type( get_class( $value ) ); +// $this->set_mapped_property('filter_type_object', base64_encode( serialize($value) ) ); +// //} +// } /** * Atribui o filter type. @@ -173,7 +185,7 @@ class Filter extends Entity { * @param string * */ - private function set_filter_type($value){ - $this->set_mapped_property('filter_type', $value ); + public function set_filter_type($value){ + $this->set_mapped_property('filter_type', get_class( $value ) ); } } \ No newline at end of file diff --git a/src/classes/entities/class-tainacan-metadata.php b/src/classes/entities/class-tainacan-metadata.php index d58da42be..505e760a8 100644 --- a/src/classes/entities/class-tainacan-metadata.php +++ b/src/classes/entities/class-tainacan-metadata.php @@ -150,7 +150,10 @@ class Metadata extends Entity { * @return array || object */ function get_field_type_object(){ - return unserialize(base64_decode( $this->get_mapped_property('field_type_object') ) ); + $class_name = $this->get_field_type(); + $object_type = new $class_name(); + $object_type->set_options( $this->get_field_options() ); + return $object_type; } /** @@ -159,7 +162,16 @@ class Metadata extends Entity { * @return array || object */ function get_field_type(){ - return base64_decode($this->get_mapped_property('field_type')); + return $this->get_mapped_property('field_type'); + } + + /** + * Retorna o objeto Metadado + * + * @return array || object + */ + function get_field_options(){ + return $this->get_mapped_property('get_field_options'); } /** @@ -272,18 +284,13 @@ class Metadata extends Entity { $this->set_mapped_property('default_property', $value); } - function set_field_type_object(\Tainacan\Field_Types\Field_Type $value){ - $this->set_field_type( get_class( $value ) ); - $this->set_mapped_property('field_type_object', base64_encode( serialize($value) ) ); // Encode to avoid backslaches removal - } - /** - * Este metodo é privado, porque é utilizado apenas neste contexto pelo @method set_field_type_object() + * save the class field class name * * @param $value */ - private function set_field_type($value){ - $this->set_mapped_property('field_type', base64_encode($value) ) ; // Encode to avoid backslaches removal + public function set_field_type($value){ + $this->set_mapped_property('field_type', get_class( $value ) ) ; // Encode to avoid backslaches removal } // helpers diff --git a/src/classes/filter-types/class-tainacan-filter-type.php b/src/classes/filter-types/class-tainacan-filter-type.php index c00efd9ab..0a1310676 100644 --- a/src/classes/filter-types/class-tainacan-filter-type.php +++ b/src/classes/filter-types/class-tainacan-filter-type.php @@ -7,6 +7,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); abstract class Filter_Type { private $supported_types = []; + public $options; abstract function render( $metadata ); @@ -17,4 +18,11 @@ abstract class Filter_Type { public function set_supported_types($supported_types){ $this->supported_types = $supported_types; } + + /** + * @param $options + */ + public function set_options( $options ){ + $this->options = ( is_array( $options ) ) ? $options : unserialize( $options ); + } } \ No newline at end of file diff --git a/src/classes/repositories/class-tainacan-filters.php b/src/classes/repositories/class-tainacan-filters.php index 23063f20e..04e2a735a 100644 --- a/src/classes/repositories/class-tainacan-filters.php +++ b/src/classes/repositories/class-tainacan-filters.php @@ -40,11 +40,11 @@ class Filters extends Repository { 'description'=> __('The filter description', 'tainacan'), 'validation' => '' ], - 'filter_type_object' => [ + 'filter_type_options' => [ 'map' => 'meta', - 'title' => __('Type', 'tainacan'), + 'title' => __('Filter type options', 'tainacan'), 'type' => 'string', - 'description'=> __('The filter type object', 'tainacan'), + 'description'=> __('The filter type options', 'tainacan'), 'validation' => '' ], 'filter_type' => [ diff --git a/src/classes/repositories/class-tainacan-metadatas.php b/src/classes/repositories/class-tainacan-metadatas.php index 33af396ad..e4d8e3127 100644 --- a/src/classes/repositories/class-tainacan-metadatas.php +++ b/src/classes/repositories/class-tainacan-metadatas.php @@ -124,11 +124,11 @@ class Metadatas extends Repository { 'type' => 'string', 'description'=> __('The value default fot the metadata', 'tainacan'), ], - 'field_type_object' => [ // not showed in form + 'field_type_options' => [ // not showed in form 'map' => 'meta', - 'title' => __('Type', 'tainacan'), + 'title' => __('Field Type options', 'tainacan'), 'type' => 'string', - 'description'=> __('The object type', 'tainacan'), + 'description'=> __('Options specific for field type', 'tainacan'), // 'validation' => '' ], 'collection_id' => [ // not showed in form diff --git a/src/dev-interface/class-tainacan-dev-interface.php b/src/dev-interface/class-tainacan-dev-interface.php index fc462c87e..bf4980fa6 100644 --- a/src/dev-interface/class-tainacan-dev-interface.php +++ b/src/dev-interface/class-tainacan-dev-interface.php @@ -138,10 +138,10 @@ class DevInterface { collections_dropdown($value); ?> collections_checkbox_list($value); ?> - + - - field_type_dropdown($value); ?> + + field_type_dropdown($post->ID,$value); ?> @@ -315,11 +315,11 @@ class DevInterface { fetch_field_types('NAME'); ?> - set_options($options); echo $class->form(); } ?> @@ -388,12 +390,12 @@ class DevInterface { if ($entity->validate_prop($prop)) { - if ($prop == 'field_type_object') { + if ($prop == 'field_type') { $class = '\Tainacan\Field_Types\\'.$value; - $ft = new $class(); - $ft->set_options( $_POST['field_type_'.strtolower( $value )] ); - update_post_meta($post_id, 'field_type_object', base64_encode(serialize($ft)) ); - update_post_meta($post_id, 'field_type', base64_encode(get_class( $ft )) ); + 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() ) ) ); + }else if($prop == 'field_type_options'){ + continue; } // we cannot user repository->insert here, it would create an infinite loop else if ($mapped['map'] == 'meta') { diff --git a/tests/test-filters.php b/tests/test-filters.php index 126fe43e4..d0ce0a192 100644 --- a/tests/test-filters.php +++ b/tests/test-filters.php @@ -58,7 +58,7 @@ class Filters extends TAINACAN_UnitTestCase { array( 'name' => 'metadado', 'collection_id' => $collection->get_id(), - 'field_type_object' => $type + 'field_type' => $type ), true ); @@ -71,7 +71,7 @@ class Filters extends TAINACAN_UnitTestCase { 'name' => 'filtro', 'collection' => $collection, 'metadata' => $metadata, - 'filter_type_object' => $filter_list_type + 'filter_type' => $filter_list_type ), true ); @@ -79,7 +79,7 @@ class Filters extends TAINACAN_UnitTestCase { $filter_range_type = $this->tainacan_filter_factory->create_filter('range'); //nao devera permitir um filtro Range para o tipo string - $this->assertTrue( $filter->set_filter_type_object( $filter_range_type ) === null ); + $this->assertTrue( $filter->set_filter_type( $filter_range_type ) === null ); $test = $Tainacan_Filters->fetch( $filter->get_id() ); diff --git a/tests/test-item-metadata.php b/tests/test-item-metadata.php index fdd4d680b..b8e7f78d9 100644 --- a/tests/test-item-metadata.php +++ b/tests/test-item-metadata.php @@ -36,7 +36,7 @@ class Item_Metadata extends TAINACAN_UnitTestCase { 'name' => 'metadado', 'description' => 'descricao', 'collection' => $collection, - 'field_type_object' => $type + 'field_type' => $type ), true ); @@ -88,7 +88,7 @@ class Item_Metadata extends TAINACAN_UnitTestCase { 'description' => 'descricao', 'collection' => $collection, 'required' => 'yes', - 'field_type_object' => $type + 'field_type' => $type ), true ); @@ -142,7 +142,7 @@ class Item_Metadata extends TAINACAN_UnitTestCase { 'description' => 'descricao', 'collection' => $collection, 'collection_key' => 'yes', - 'field_type_object' => $type + 'field_type' => $type ), true ); @@ -200,7 +200,7 @@ class Item_Metadata extends TAINACAN_UnitTestCase { 'description' => 'descricao', 'collection' => $collection, 'status' => 'publish', - 'field_type_object' => $type + 'field_type' => $type ), true ); diff --git a/tests/test-items.php b/tests/test-items.php index 297d97146..2c6f11e8a 100644 --- a/tests/test-items.php +++ b/tests/test-items.php @@ -43,7 +43,7 @@ class Items extends TAINACAN_UnitTestCase { 'name' => 'metadado', 'status' => 'publish', 'collection' => $collection, - 'field_type_object' => $type + 'field_type' => $type ), true ); @@ -54,7 +54,7 @@ class Items extends TAINACAN_UnitTestCase { 'name' => 'metadado2', 'status' => 'publish', 'collection' => $collection, - 'field_type_object' => $type + 'field_type' => $type ), true ); @@ -65,7 +65,7 @@ class Items extends TAINACAN_UnitTestCase { 'name' => 'metadado3', 'status' => 'publish', 'collection' => $collection, - 'field_type_object' => $type + 'field_type' => $type ), true ); diff --git a/tests/test-metadata.php b/tests/test-metadata.php index 826767f2e..2331287a7 100644 --- a/tests/test-metadata.php +++ b/tests/test-metadata.php @@ -35,7 +35,7 @@ class Metadata extends TAINACAN_UnitTestCase { 'name' => 'metadado', 'description' => 'descricao', 'collection' => $collection, - 'field_type_object' => $type + 'field_type' => $type ), true ); @@ -69,7 +69,7 @@ class Metadata extends TAINACAN_UnitTestCase { 'name' => 'metadado', 'description' => 'descricao', 'collection_id' => $collection->get_id(), - 'field_type_object' => $type + 'field_type' => $type ), true ); @@ -95,7 +95,7 @@ class Metadata extends TAINACAN_UnitTestCase { array( 'name' => 'metadata default', 'collection_id' => $Tainacan_Metadatas->get_default_metadata_attribute(), - 'field_type_object' => $type, + 'field_type' => $type, 'status' => 'publish' ), true @@ -114,7 +114,7 @@ class Metadata extends TAINACAN_UnitTestCase { array( 'name' => 'metadata grandfather', 'collection_id' => $collection_grandfather->get_id(), - 'field_type_object' => $type, + 'field_type' => $type, 'status' => 'publish' ), true @@ -134,7 +134,7 @@ class Metadata extends TAINACAN_UnitTestCase { array( 'name' => 'metadata father', 'collection_id' => $collection_father->get_id(), - 'field_type_object' => $type, + 'field_type' => $type, 'status' => 'publish' ), true @@ -157,7 +157,7 @@ class Metadata extends TAINACAN_UnitTestCase { array( 'name' => 'metadata son', 'collection_id' => $collection_son->get_id(), - 'field_type_object' => $type, + 'field_type' => $type, 'status' => 'publish' ), true