Fixes some bugs and adds Filters args

This commit is contained in:
weryques 2018-03-15 12:51:25 -03:00
parent 7c315a7dfd
commit 09bfdffe36
4 changed files with 58 additions and 17 deletions

View File

@ -225,8 +225,8 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
} else { } else {
return new WP_REST_Response([ return new WP_REST_Response([
'error_message' => __('One or more values are invalid.', 'tainacan'), 'error_message' => __('One or more values are invalid.', 'tainacan'),
'errors' => $this->field->get_errors(), 'errors' => $prepared->get_errors(),
'field' => $this->prepare_item_for_response($this->field, $request), 'field' => $this->prepare_item_for_response($prepared, $request),
], 400); ], 400);
} }
@ -372,7 +372,7 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
* @return WP_Error|WP_REST_Response * @return WP_Error|WP_REST_Response
*/ */
public function update_item( $request ) { public function update_item( $request ) {
$collection_id = $request['collection_id']; $collection_id = is_numeric($request['collection_id']) ? $request['collection_id'] : null;
$body = json_decode($request->get_body(), true); $body = json_decode($request->get_body(), true);
if(!empty($body)){ if(!empty($body)){

View File

@ -30,7 +30,6 @@ class TAINACAN_REST_Filters_Controller extends TAINACAN_REST_Controller {
$this->collection = new Entities\Collection(); $this->collection = new Entities\Collection();
$this->collection_repository = new Repositories\Collections(); $this->collection_repository = new Repositories\Collections();
$this->field = new Entities\Field();
$this->field_repository = new Repositories\Fields(); $this->field_repository = new Repositories\Fields();
$this->filter_repository = new Repositories\Filters(); $this->filter_repository = new Repositories\Filters();
@ -41,7 +40,8 @@ class TAINACAN_REST_Filters_Controller extends TAINACAN_REST_Controller {
array( array(
'methods' => WP_REST_Server::CREATABLE, 'methods' => WP_REST_Server::CREATABLE,
'callback' => array($this, 'create_item'), '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)
), ),
)); ));
register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base, array( register_rest_route($this->namespace, '/collection/(?P<collection_id>[\d]+)/' . $this->rest_base, array(
@ -54,7 +54,8 @@ class TAINACAN_REST_Filters_Controller extends TAINACAN_REST_Controller {
array( array(
'methods' => WP_REST_Server::CREATABLE, 'methods' => WP_REST_Server::CREATABLE,
'callback' => array($this, 'create_item'), '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)
) )
)); ));
register_rest_route($this->namespace, '/' . $this->rest_base, array( register_rest_route($this->namespace, '/' . $this->rest_base, array(
@ -67,24 +68,33 @@ class TAINACAN_REST_Filters_Controller extends TAINACAN_REST_Controller {
array( array(
'methods' => WP_REST_Server::CREATABLE, 'methods' => WP_REST_Server::CREATABLE,
'callback' => array($this, 'create_item'), '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)
) )
)); ));
register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<filter_id>[\d]+)', array( register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<filter_id>[\d]+)', array(
array( array(
'methods' => WP_REST_Server::DELETABLE, 'methods' => WP_REST_Server::DELETABLE,
'callback' => array($this, 'delete_item'), 'callback' => array($this, 'delete_item'),
'permission_callback' => array($this, 'delete_item_permissions_check') 'permission_callback' => array($this, 'delete_item_permissions_check'),
'args' => array(
'body_args' => array(
'description' => __('To delete permanently, in body you can pass \'is_permanently\' as true. By default this will only trash collection'),
'default' => 'false'
),
)
), ),
array( array(
'methods' => WP_REST_Server::EDITABLE, 'methods' => WP_REST_Server::EDITABLE,
'callback' => array($this, 'update_item'), '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)
), ),
array( array(
'methods' => WP_REST_Server::READABLE, 'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_item'), '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)
) )
)); ));
} }
@ -413,6 +423,37 @@ class TAINACAN_REST_Filters_Controller extends TAINACAN_REST_Controller {
return false; return false;
} }
/**
* @param string $method
*
* @return array|mixed
*/
public function get_endpoint_args_for_item_schema( $method = null ) {
$endpoint_args = [];
if($method === WP_REST_Server::READABLE) {
$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->filter_repository->get_map();
foreach ($map as $mapped => $value){
$set_ = 'set_'. $mapped;
// Show only args that has a method set
if( !method_exists(new Entities\Filter(), "$set_") ){
unset($map[$mapped]);
}
}
$endpoint_args = $map;
}
return $endpoint_args;
}
/** /**
* @param null $object_name * @param null $object_name
* *

View File

@ -24,21 +24,21 @@ abstract class Field_Type {
private $primitive_type; private $primitive_type;
/** /**
* Array of options spececific to this field type. Stored in field_type_options property of the Field object * Array of options specific to this field type. Stored in field_type_options property of the Field object
* @var Array * @var array
*/ */
private $options = []; private $options = [];
/** /**
* The default values for the field type options array * The default values for the field type options array
* @var Array * @var array
*/ */
private $default_options = []; private $default_options = [];
private $errors; private $errors;
/** /**
* Indicates wether this is a core Field Type or not * Indicates whether this is a core Field Type or not
* *
* Core field types are used by Title and description fields. These fields: * 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 * * Can only be used once, they belong to the repository and can not be deleted
@ -118,7 +118,7 @@ abstract class Field_Type {
* @param $options * @param $options
*/ */
public function set_options( $options ){ public function set_options( $options ){
$this->options = ( is_array( $options ) ) ? $options : unserialize( $options ); $this->options = ( is_array( $options ) ) ? $options : (!is_array(unserialize( $options )) ? [] : unserialize( $options ));
} }
public function set_default_options(Array $options) { public function set_default_options(Array $options) {
@ -128,7 +128,7 @@ abstract class Field_Type {
/** /**
* Gets the options for this field types, including default values for options * Gets the options for this field types, including default values for options
* that were not set yet. * that were not set yet.
* @return Array Fielt type options * @return array Field type options
*/ */
public function get_options() { public function get_options() {
return array_merge($this->default_options, $this->options); return array_merge($this->default_options, $this->options);

View File

@ -38,7 +38,7 @@ class Filters extends Repository {
'filter_type_options' => [ 'filter_type_options' => [
'map' => 'meta', 'map' => 'meta',
'title' => __('Filter type options', 'tainacan'), 'title' => __('Filter type options', 'tainacan'),
'type' => 'string', 'type' => 'object',
'description'=> __('The filter type options', 'tainacan'), 'description'=> __('The filter type options', 'tainacan'),
'validation' => '' 'validation' => ''
], ],