diff --git a/src/api/endpoints/class-tainacan-rest-fields-controller.php b/src/api/endpoints/class-tainacan-rest-fields-controller.php index e9a0aa340..1ccaed305 100644 --- a/src/api/endpoints/class-tainacan-rest-fields-controller.php +++ b/src/api/endpoints/class-tainacan-rest-fields-controller.php @@ -225,8 +225,8 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller { } else { return new WP_REST_Response([ 'error_message' => __('One or more values are invalid.', 'tainacan'), - 'errors' => $this->field->get_errors(), - 'field' => $this->prepare_item_for_response($this->field, $request), + 'errors' => $prepared->get_errors(), + 'field' => $this->prepare_item_for_response($prepared, $request), ], 400); } @@ -372,7 +372,7 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller { * @return WP_Error|WP_REST_Response */ 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); if(!empty($body)){ diff --git a/src/api/endpoints/class-tainacan-rest-filters-controller.php b/src/api/endpoints/class-tainacan-rest-filters-controller.php index cc8f10c55..2f0a879a0 100644 --- a/src/api/endpoints/class-tainacan-rest-filters-controller.php +++ b/src/api/endpoints/class-tainacan-rest-filters-controller.php @@ -30,7 +30,6 @@ class TAINACAN_REST_Filters_Controller extends TAINACAN_REST_Controller { $this->collection = new Entities\Collection(); $this->collection_repository = new Repositories\Collections(); - $this->field = new Entities\Field(); $this->field_repository = new Repositories\Fields(); $this->filter_repository = new Repositories\Filters(); @@ -41,7 +40,8 @@ class TAINACAN_REST_Filters_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) ), )); register_rest_route($this->namespace, '/collection/(?P[\d]+)/' . $this->rest_base, array( @@ -54,7 +54,8 @@ class TAINACAN_REST_Filters_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) ) )); register_rest_route($this->namespace, '/' . $this->rest_base, array( @@ -67,24 +68,33 @@ class TAINACAN_REST_Filters_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) ) )); register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P[\d]+)', array( 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'), + '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( '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) ), 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) ) )); } @@ -413,6 +423,37 @@ class TAINACAN_REST_Filters_Controller extends TAINACAN_REST_Controller { 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 * diff --git a/src/classes/field-types/field-type/class-tainacan-field-type.php b/src/classes/field-types/field-type/class-tainacan-field-type.php index 73334d7d9..c6c14ddaf 100644 --- a/src/classes/field-types/field-type/class-tainacan-field-type.php +++ b/src/classes/field-types/field-type/class-tainacan-field-type.php @@ -24,21 +24,21 @@ abstract class Field_Type { private $primitive_type; /** - * Array of options spececific to this field type. Stored in field_type_options property of the Field object - * @var Array + * Array of options specific to this field type. Stored in field_type_options property of the Field object + * @var array */ private $options = []; /** * The default values for the field type options array - * @var Array + * @var array */ private $default_options = []; 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: * * 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 */ 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) { @@ -128,7 +128,7 @@ abstract class Field_Type { /** * Gets the options for this field types, including default values for options * that were not set yet. - * @return Array Fielt type options + * @return array Field type options */ public function get_options() { return array_merge($this->default_options, $this->options); diff --git a/src/classes/repositories/class-tainacan-filters.php b/src/classes/repositories/class-tainacan-filters.php index 7015a29f9..ca2aa6264 100644 --- a/src/classes/repositories/class-tainacan-filters.php +++ b/src/classes/repositories/class-tainacan-filters.php @@ -38,7 +38,7 @@ class Filters extends Repository { 'filter_type_options' => [ 'map' => 'meta', 'title' => __('Filter type options', 'tainacan'), - 'type' => 'string', + 'type' => 'object', 'description'=> __('The filter type options', 'tainacan'), 'validation' => '' ],