beggining of core field types
This commit is contained in:
parent
275bec0463
commit
d4eebaae64
|
@ -137,11 +137,10 @@ class Item_Metadata_Entity extends Entity {
|
|||
return false;
|
||||
}
|
||||
|
||||
$field_type = $field->get_field_type();
|
||||
if( class_exists( $field_type ) ){
|
||||
$classFieldType = new $field_type();
|
||||
$classFieldType = $field->get_field_type_object();
|
||||
if( is_object( $classFieldType ) ){
|
||||
if( method_exists ( $classFieldType , 'validate' ) ){
|
||||
if( ! $classFieldType->validate( $value ) ) {
|
||||
if( ! $classFieldType->validate( $this ) ) {
|
||||
$this->add_error('field_type_error', $classFieldType->get_errors() );
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
namespace Tainacan\Field_Types;
|
||||
|
||||
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
||||
|
||||
/**
|
||||
* Class TainacanFieldType
|
||||
*/
|
||||
class Core_Title extends Field_Type {
|
||||
|
||||
function __construct(){
|
||||
// call field type constructor
|
||||
parent::__construct();
|
||||
parent::set_primitive_type('string');
|
||||
$this->core = true;
|
||||
$this->related_mapped_prop = 'title';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $itemMetadata \Tainacan\Entities\Item_Metadata_Entity The instace of the entity itemMetadata
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function render( $itemMetadata ){
|
||||
return '<tainacan-text field_id ="'.$itemMetadata->get_field()->get_id().'"
|
||||
item_id="'.$itemMetadata->get_item()->get_id().'"
|
||||
value=\''.json_encode( $itemMetadata->get_value() ).'\'
|
||||
name="'.$itemMetadata->get_field()->get_name().'"></tainacan-text>';
|
||||
}
|
||||
|
||||
/**
|
||||
* generate the fields for this field type
|
||||
*/
|
||||
public function form(){
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Title core Field type is stored as the item title
|
||||
*
|
||||
* Lets validate it as the item title
|
||||
*
|
||||
* @param TainacanEntitiesItem_Metadata_Entity $item_metadata
|
||||
* @return bool Valid or not
|
||||
*/
|
||||
public function validate(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) {
|
||||
|
||||
$item = $item_metadata->get_item();
|
||||
|
||||
if ( !in_array($item->get_status(), apply_filters('tainacan-status-require-validation', ['publish','future','private'])) )
|
||||
return true;
|
||||
|
||||
$item->set_title($item_metadata->get_value());
|
||||
|
||||
return $item->validate_prop('title');
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -12,6 +12,21 @@ abstract class Field_Type {
|
|||
private $primitive_type;
|
||||
public $options;
|
||||
public $errors;
|
||||
|
||||
/**
|
||||
* Indicates wether this is a core Field Type or not
|
||||
*
|
||||
* Core field types are used by Title and description fields. These fields:
|
||||
* * Can only be used once, they belong to the repository and can not be deleted
|
||||
* * Its values are saved in th wp_post table, and not as post_meta
|
||||
*
|
||||
*/
|
||||
public $core = false;
|
||||
|
||||
/**
|
||||
* Used by core field types to indicate where it should be saved
|
||||
*/
|
||||
public $related_mapped_prop = false;
|
||||
|
||||
abstract function render( $itemMetadata );
|
||||
|
||||
|
@ -24,7 +39,7 @@ abstract class Field_Type {
|
|||
$Tainacan_Fields->register_field_type( $this );
|
||||
}
|
||||
|
||||
public function validate($value) {
|
||||
public function validate(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,21 @@ class Item_Metadata extends Repository {
|
|||
$unique = !$item_metadata->is_multiple();
|
||||
|
||||
if ($unique) {
|
||||
add_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $item_metadata->get_value() ) );
|
||||
$field_type = $item_metadata->get_field()->get_field_type_object();
|
||||
if ($field_type->core) {
|
||||
$item = $item_metadata->get_item();
|
||||
$set_method = 'set_' . $field_type->related_mapped_prop;
|
||||
$item->$set_method($item_metadata->get_value());
|
||||
if ($item->validate()) {
|
||||
global $TainacanItems;
|
||||
$TainacanItems->insert($item);
|
||||
} else {
|
||||
throw new \Exception('Item metadata should be validated beforehand');
|
||||
}
|
||||
} else {
|
||||
add_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $item_metadata->get_value() ) );
|
||||
}
|
||||
|
||||
} else {
|
||||
delete_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id());
|
||||
|
||||
|
|
Loading…
Reference in New Issue