Collections Controller, Items Controller, Collection and Item
1. Update collection params and endpoints args of Item and Collection Controller 2. Support of set thumnail for Item and Collection
This commit is contained in:
parent
48b9122bcc
commit
f3f0079112
|
@ -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<collection_id>[\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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -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'),
|
||||
|
|
|
@ -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'),
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue