Field endpoint args, Fixes return of field type object
This commit is contained in:
parent
20428dee50
commit
7b7a1bc3b7
|
@ -37,6 +37,8 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
|
|||
* id body of requisition
|
||||
*
|
||||
* Both of GETs return the field of matched objects
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base . '/(?P<field_id>[\d]+)',
|
||||
|
@ -44,18 +46,20 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
|
|||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array($this, 'update_item'),
|
||||
'permission_callback' => array($this, 'update_item_permissions_check')
|
||||
'permission_callback' => array($this, 'update_item_permissions_check'),
|
||||
'args' => $this->get_endpoint_args_for_item_schema(WP_REST_Server::EDITABLE)
|
||||
),
|
||||
// ENDPOINT X. THIS ENDPOINT DO THE SAME THING OF ENDPOINT Z. I hope in a brief future it function changes.
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array($this, 'delete_item'),
|
||||
'permission_callback' => array($this, 'delete_item_permissions_check')
|
||||
'permission_callback' => array($this, 'delete_item_permissions_check'),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array($this, 'get_item'),
|
||||
'permission_callback' => array($this, 'get_item_permissions_check')
|
||||
'permission_callback' => array($this, 'get_item_permissions_check'),
|
||||
'args' => $this->get_endpoint_args_for_item_schema(WP_REST_Server::READABLE),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
@ -65,12 +69,13 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
|
|||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array($this, 'get_items'),
|
||||
'permission_callback' => array($this, 'get_items_permissions_check'),
|
||||
//'args' => $this->get_collection_params(),
|
||||
'args' => $this->get_collection_params(),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array($this, 'create_item'),
|
||||
'permission_callback' => array($this, 'create_item_permissions_check')
|
||||
'permission_callback' => array($this, 'create_item_permissions_check'),
|
||||
'args' => $this->get_endpoint_args_for_item_schema(WP_REST_Server::CREATABLE),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
@ -79,12 +84,14 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
|
|||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array($this, 'create_item'),
|
||||
'permission_callback' => array($this, 'create_item_permissions_check')
|
||||
'permission_callback' => array($this, 'create_item_permissions_check'),
|
||||
'args' => $this->get_endpoint_args_for_item_schema(WP_REST_Server::CREATABLE),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array($this, 'get_items'),
|
||||
'permission_callback' => array($this, 'get_items_permissions_check')
|
||||
'permission_callback' => array($this, 'get_items_permissions_check'),
|
||||
'args' => $this->get_collection_params(),
|
||||
)
|
||||
)
|
||||
);
|
||||
|
@ -99,7 +106,8 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
|
|||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array($this, 'update_item'),
|
||||
'permission_callback' => array($this, 'update_item_permissions_check')
|
||||
'permission_callback' => array($this, 'update_item_permissions_check'),
|
||||
'args' => $this->get_endpoint_args_for_item_schema(WP_REST_Server::EDITABLE)
|
||||
),
|
||||
)
|
||||
);
|
||||
|
@ -280,7 +288,7 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
|
|||
$item->get_field_type_object()->form();
|
||||
$form = ob_get_clean();
|
||||
$item_arr['edit_form'] = $form;
|
||||
$item_arr['field_type_object'] = $item->get_field_type_object();
|
||||
$item_arr['field_type_object'] = $item->get_field_type_object()->__toArray();
|
||||
$item_arr['disabled'] = $item->get_disabled_for_collection();
|
||||
}
|
||||
|
||||
|
@ -465,6 +473,63 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
|
|||
|
||||
return $this->field_repository->can_edit(new Entities\Field());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $object_name
|
||||
*
|
||||
* @return array|void
|
||||
*/
|
||||
public function get_collection_params( $object_name = null ) {
|
||||
$query_params['context']['default'] = 'view';
|
||||
|
||||
$query_params = array_merge($query_params, parent::get_collection_params('field'));
|
||||
|
||||
$query_params['name'] = array(
|
||||
'description' => __('Limit result set to field with specific name.'),
|
||||
'type' => 'string',
|
||||
);
|
||||
|
||||
$query_params = array_merge($query_params, parent::get_meta_queries_params());
|
||||
|
||||
return $query_params;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $method
|
||||
*
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get_endpoint_args_for_item_schema( $method = null ) {
|
||||
$endpoint_args = [];
|
||||
if($method === WP_REST_Server::READABLE) {
|
||||
$endpoint_args['fetch'] = [
|
||||
'type' => 'string',
|
||||
'description' => __('Fetch all values of a field from a collection in all it collection items'),
|
||||
'enum' => ['all_field_values']
|
||||
];
|
||||
$endpoint_args['context'] = array(
|
||||
'type' => 'string',
|
||||
'default' => 'view',
|
||||
'items' => array( 'view, edit' )
|
||||
);
|
||||
} elseif ($method === WP_REST_Server::CREATABLE || $method === WP_REST_Server::EDITABLE) {
|
||||
$map = $this->field_repository->get_map();
|
||||
|
||||
foreach ($map as $mapped => $value){
|
||||
$set_ = 'set_'. $mapped;
|
||||
|
||||
// Show only args that has a method set
|
||||
if( !method_exists(new Entities\Field(), "$set_") ){
|
||||
unset($map[$mapped]);
|
||||
}
|
||||
}
|
||||
|
||||
$endpoint_args = $map;
|
||||
}
|
||||
|
||||
return $endpoint_args;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -27,15 +27,15 @@ abstract class Field_Type {
|
|||
* Array of options spececific to this field type. Stored in field_type_options property of the Field object
|
||||
* @var Array
|
||||
*/
|
||||
public $options = [];
|
||||
private $options = [];
|
||||
|
||||
/**
|
||||
* The default values for the field type options array
|
||||
* @var Array
|
||||
*/
|
||||
public $default_options = [];
|
||||
private $default_options = [];
|
||||
|
||||
public $errors;
|
||||
private $errors;
|
||||
|
||||
/**
|
||||
* Indicates wether this is a core Field Type or not
|
||||
|
@ -45,24 +45,24 @@ abstract class Field_Type {
|
|||
* * Its values are saved in th wp_post table, and not as post_meta
|
||||
*
|
||||
*/
|
||||
public $core = false;
|
||||
private $core = false;
|
||||
|
||||
/**
|
||||
* Used by core field types to indicate where it should be saved
|
||||
*/
|
||||
public $related_mapped_prop = false;
|
||||
private $related_mapped_prop = false;
|
||||
|
||||
/**
|
||||
* The name of the web component used by this field type
|
||||
* @var string
|
||||
*/
|
||||
public $component;
|
||||
private $component;
|
||||
|
||||
/**
|
||||
* The name of the web component used by the Form
|
||||
* @var bool | string
|
||||
*/
|
||||
public $form_component = false;
|
||||
private $form_component = false;
|
||||
|
||||
abstract function render( $itemMetadata );
|
||||
|
||||
|
@ -74,6 +74,10 @@ abstract class Field_Type {
|
|||
return true;
|
||||
}
|
||||
|
||||
public function get_related_mapped_prop(){
|
||||
return $this->related_mapped_prop;
|
||||
}
|
||||
|
||||
public function get_validation_errors() {
|
||||
return [];
|
||||
}
|
||||
|
@ -141,8 +145,11 @@ abstract class Field_Type {
|
|||
public function __toArray(){
|
||||
$attributes = [];
|
||||
|
||||
$attributes['errors'] = $this->get_errors();
|
||||
$attributes['related_mapped_prop'] = $this->get_related_mapped_prop();
|
||||
$attributes['options'] = $this->get_options();
|
||||
$attributes['className'] = get_class($this);
|
||||
$attributes['core'] = $this->core;
|
||||
$attributes['core'] = $this->get_core();
|
||||
$attributes['component'] = $this->get_component();
|
||||
$attributes['primitive_type'] = $this->get_primitive_type();
|
||||
$attributes['form_component'] = $this->get_form_component();
|
||||
|
@ -163,4 +170,11 @@ abstract class Field_Type {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_core() {
|
||||
return $this->core;
|
||||
}
|
||||
|
||||
}
|
|
@ -109,7 +109,7 @@ class Fields extends Repository {
|
|||
'cardinality' => [
|
||||
'map' => 'meta',
|
||||
'title' => __('Cardinality', 'tainacan'),
|
||||
'type' => 'string',
|
||||
'type' => 'string/number',
|
||||
'description'=> __('Number of multiples possible fields', 'tainacan'),
|
||||
'on_error' => __('The number of fields not allowed', 'tainacan'),
|
||||
'validation' => v::numeric()->positive(),
|
||||
|
|
|
@ -13,7 +13,7 @@ class Item_Metadata extends Repository {
|
|||
$unique = !$item_metadata->is_multiple();
|
||||
|
||||
$field_type = $item_metadata->get_field()->get_field_type_object();
|
||||
if ($field_type->core) {
|
||||
if ($field_type->get_core()) {
|
||||
$this->save_core_field_value($item_metadata);
|
||||
} elseif ($field_type->get_primitive_type() == 'term') {
|
||||
$this->save_terms_field_value($item_metadata);
|
||||
|
@ -50,9 +50,9 @@ class Item_Metadata extends Repository {
|
|||
|
||||
public function save_core_field_value(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) {
|
||||
$field_type = $item_metadata->get_field()->get_field_type_object();
|
||||
if ($field_type->core) {
|
||||
if ($field_type->get_core()) {
|
||||
$item = $item_metadata->get_item();
|
||||
$set_method = 'set_' . $field_type->related_mapped_prop;
|
||||
$set_method = 'set_' . $field_type->get_related_mapped_prop();
|
||||
$value = $item_metadata->get_value();
|
||||
$item->$set_method( is_array( $value ) ? $value[0] : $value );
|
||||
if ($item->validate_core_fields()) {
|
||||
|
@ -121,10 +121,10 @@ class Item_Metadata extends Repository {
|
|||
$unique = ! $item_metadata->is_multiple();
|
||||
|
||||
$field_type = $item_metadata->get_field()->get_field_type_object();
|
||||
if ($field_type->core) {
|
||||
if ($field_type->get_core()) {
|
||||
$item = $item_metadata->get_item();
|
||||
|
||||
$get_method = 'get_' . $field_type->related_mapped_prop;
|
||||
$get_method = 'get_' . $field_type->get_related_mapped_prop();
|
||||
return $item->$get_method();
|
||||
|
||||
} elseif ($field_type->get_primitive_type() == 'term') {
|
||||
|
|
Loading…
Reference in New Issue