diff --git a/src/api/endpoints/class-tainacan-rest-collections-controller.php b/src/api/endpoints/class-tainacan-rest-collections-controller.php index cb5ae1454..8a151eeb3 100644 --- a/src/api/endpoints/class-tainacan-rest-collections-controller.php +++ b/src/api/endpoints/class-tainacan-rest-collections-controller.php @@ -47,7 +47,7 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_REST_Controller { 'methods' => WP_REST_Server::CREATABLE, 'callback' => array($this, 'create_item'), 'permission_callback' => array($this, 'create_item_permissions_check'), - 'args' => $this->get_item_schema() + 'args' => $this->get_endpoint_args_for_item_schema(WP_REST_Server::CREATABLE), ), )); register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P[\d]+)', array( @@ -55,13 +55,13 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_REST_Controller { 'methods' => WP_REST_Server::READABLE, 'callback' => array($this, 'get_item'), 'permission_callback' => array($this, 'get_item_permissions_check'), - 'args' => $this->get_item_schema() + 'args' => $this->get_endpoint_args_for_item_schema(WP_REST_Server::READABLE), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array($this, 'update_item'), 'permission_callback' => array($this, 'update_item_permissions_check'), - 'args' => $this->get_item_schema() + 'args' => $this->get_endpoint_args_for_item_schema(WP_REST_Server::EDITABLE), ), array( 'methods' => WP_REST_Server::DELETABLE, @@ -374,15 +374,49 @@ class TAINACAN_REST_Collections_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['fetch_only'] = array( + 'type' => 'string/array', + 'description' => __( 'Fetch only specific attribute. The specifics attributes are the same in schema.' ), + ); + + $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->collections_repository->get_map(); + + foreach ($map as $mapped => $value){ + $set_ = 'set_'. $mapped; + + // Show only args that has a method set + if( !method_exists($this->collection, "$set_") ){ + unset($map[$mapped]); + } + } + + $endpoint_args = $map; + } + + return $endpoint_args; + } + /** * @return array|mixed|void */ public function get_item_schema() { - $schema['$schema'] = 'http://json-schema.org/draft-07/schema#'; - $schema['title'] = $this->collection->get_post_type(); - $schema['type'] = 'object'; - - $schema['properties'] = $this->collections_repository->get_map(); + $schema['collection']['type'] = 'array/object'; + $schema['collection']['description'] = __('Passed in body.'); + $schema['collection']['properties'] = $this->collections_repository->get_map(); return $schema; } diff --git a/src/api/endpoints/class-tainacan-rest-items-controller.php b/src/api/endpoints/class-tainacan-rest-items-controller.php index 66778ac74..56cecc324 100644 --- a/src/api/endpoints/class-tainacan-rest-items-controller.php +++ b/src/api/endpoints/class-tainacan-rest-items-controller.php @@ -53,7 +53,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller { 'methods' => WP_REST_Server::CREATABLE, 'callback' => array($this, 'create_item'), 'permission_callback' => array($this, 'create_item_permissions_check'), - 'args' => $this->get_item_schema(), + 'args' => $this->get_endpoint_args_for_item_schema(WP_REST_Server::CREATABLE), ), ) ); @@ -64,13 +64,13 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller { 'methods' => WP_REST_Server::READABLE, 'callback' => array($this, 'get_item'), 'permission_callback' => array($this, 'get_item_permissions_check'), - 'args' => $this->get_item_schema(), + 'args' => $this->get_endpoint_args_for_item_schema(WP_REST_Server::READABLE), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array($this, 'update_item'), 'permission_callback' => array($this, 'update_item_permissions_check'), - 'args' => $this->get_item_schema(), + 'args' => $this->get_endpoint_args_for_item_schema(WP_REST_Server::EDITABLE), ), array( 'methods' => WP_REST_Server::DELETABLE, @@ -397,15 +397,51 @@ class TAINACAN_REST_Items_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['fetch_only'] = array( + 'type' => 'string/array', + 'description' => __( 'Fetch only specific attribute. The specifics attributes are the same in schema.' ), + ); + + $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->items_repository->get_map(); + + foreach ($map as $mapped => $value){ + $set_ = 'set_'. $mapped; + + // Show only args that has a method set + if( !method_exists($this->item, "$set_") ){ + unset($map[$mapped]); + } + } + + $endpoint_args = $map; + } + + return $endpoint_args; + } + /** * @return array */ public function get_item_schema() { - $schema['$schema'] = 'http://json-schema.org/draft-07/schema#'; - $schema['title'] = $this->item->get_post_type(); - $schema['type'] = 'object'; - - $schema['properties'] = $this->items_repository->get_map(); + $schema['item']['type'] = 'object'; + $schema['item']['description'] = __('Passed in body.'); + $schema['item']['properties'] = $this->items_repository->get_map(); return $schema; } diff --git a/src/classes/entities/class-tainacan-collection.php b/src/classes/entities/class-tainacan-collection.php index dd136985e..827726121 100644 --- a/src/classes/entities/class-tainacan-collection.php +++ b/src/classes/entities/class-tainacan-collection.php @@ -121,6 +121,13 @@ class Collection extends Entity { return $this->get_mapped_property('featured_image'); } + /** + * @param $id + */ + function set_featured_img_by_id($id){ + $this->set_mapped_property('featured_image_id', $id ); + } + /** * @return mixed|null */ @@ -290,6 +297,7 @@ class Collection extends Entity { * @see \Tainacan\Repositories\Fields->fetch() * * @return [\Tainacan\Entities\Field] array + * @throws \Exception */ function get_fields() { $Tainacan_Fields = new \Tainacan\Repositories\Fields(); diff --git a/src/classes/entities/class-tainacan-item.php b/src/classes/entities/class-tainacan-item.php index d00d0b722..f62ce3190 100644 --- a/src/classes/entities/class-tainacan-item.php +++ b/src/classes/entities/class-tainacan-item.php @@ -49,6 +49,13 @@ class Item extends Entity { return $this->get_mapped_property('featured_image'); } + /** + * @param $id + */ + function set_featured_img_by_id($id){ + $this->set_mapped_property('featured_image_id', $id ); + } + /** * @return mixed|null */ diff --git a/src/classes/repositories/class-tainacan-collections.php b/src/classes/repositories/class-tainacan-collections.php index b22a19217..a07ff6d77 100644 --- a/src/classes/repositories/class-tainacan-collections.php +++ b/src/classes/repositories/class-tainacan-collections.php @@ -71,6 +71,12 @@ class Collections extends Repository { 'type' => 'string', 'description' => __('The collection thumbnail URL') ], + 'featured_image_id' => [ + 'map' => 'thumbnail_id', + 'title' => __('Featured Image ID', 'tainacan'), + 'type' => 'string', + 'description' => __('The collection thumbnail') + ], 'attachments' => [ 'map' => 'attachments', 'title' => __('Attachments', 'tainacan'), diff --git a/src/classes/repositories/class-tainacan-items.php b/src/classes/repositories/class-tainacan-items.php index 5cc250890..ac81b1d6c 100644 --- a/src/classes/repositories/class-tainacan-items.php +++ b/src/classes/repositories/class-tainacan-items.php @@ -76,6 +76,12 @@ class Items extends Repository { 'type' => 'string', 'description' => __('The item thumbnail URL') ], + 'featured_image_id' => [ + 'map' => 'thumbnail_id', + 'title' => __('Featured Image ID', 'tainacan'), + 'type' => 'string', + 'description' => __('The item thumbnail') + ], 'attachments' => [ 'map' => 'attachments', 'title' => __('Attachments', 'tainacan'), diff --git a/src/classes/repositories/class-tainacan-repository.php b/src/classes/repositories/class-tainacan-repository.php index 2411c344d..ea7db4291 100644 --- a/src/classes/repositories/class-tainacan-repository.php +++ b/src/classes/repositories/class-tainacan-repository.php @@ -74,7 +74,7 @@ abstract class Repository { // First iterate through the native post properties foreach ($map as $prop => $mapped) { - if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') { + if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi' && $mapped['map'] != 'thumbnail_id') { $obj->WP_Post->{$mapped['map']} = $obj->get_mapped_property($prop); } } @@ -94,8 +94,9 @@ abstract class Repository { foreach ($map as $prop => $mapped) { if ($mapped['map'] == 'meta' || $mapped['map'] == 'meta_multi') { $this->insert_metadata($obj, $prop); + } elseif ($mapped['map'] === 'thumbnail_id'){ + set_post_thumbnail($obj->WP_Post, $obj->get_mapped_property('thumbnail_id')); } - } do_action('tainacan-insert', $obj);