Atributes for pagination are visible and initiated the refactoring of prepare item for response
This commit is contained in:
parent
4847f07db6
commit
ecd453b3cf
|
@ -74,22 +74,24 @@ class TAINACAN_REST_Controller extends WP_REST_Controller {
|
|||
*/
|
||||
protected function prepare_filters($request){
|
||||
$map = [
|
||||
'name' => 'title',
|
||||
'title' => 'title',
|
||||
'id' => 'p',
|
||||
'pageid' => 'page_id',
|
||||
'authorid' => 'author_id',
|
||||
'authorname' => 'author_name',
|
||||
'search' => 's',
|
||||
'posttype' => 'post_type',
|
||||
'poststatus' => 'post_status',
|
||||
'offset' => 'offset',
|
||||
'metaquery' => 'meta_query',
|
||||
'datequery' => 'date_query',
|
||||
'order' => 'order',
|
||||
'orderby' => 'orderby',
|
||||
'metakey' => 'meta_key',
|
||||
'hide_empty' => 'hide_empty',
|
||||
'name' => 'title',
|
||||
'title' => 'title',
|
||||
'id' => 'p',
|
||||
'pageid' => 'page_id',
|
||||
'authorid' => 'author_id',
|
||||
'authorname' => 'author_name',
|
||||
'search' => 's',
|
||||
'posttype' => 'post_type',
|
||||
'status' => 'post_status',
|
||||
'offset' => 'offset',
|
||||
'metaquery' => 'meta_query',
|
||||
'datequery' => 'date_query',
|
||||
'order' => 'order',
|
||||
'orderby' => 'orderby',
|
||||
'metakey' => 'meta_key',
|
||||
'hideempty' => 'hide_empty',
|
||||
'perpage' => 'posts_per_page',
|
||||
'paged' => 'paged'
|
||||
];
|
||||
|
||||
$meta_query = [
|
||||
|
|
|
@ -82,9 +82,31 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_REST_Controller {
|
|||
|
||||
$collections = $this->collections_repository->fetch($args);
|
||||
|
||||
$response = $this->prepare_item_for_response($collections, $request);
|
||||
$map = $this->collections_repository->get_map();
|
||||
|
||||
return new WP_REST_Response($response, 200);
|
||||
|
||||
$response = [];
|
||||
if($collections->have_posts()){
|
||||
while ($collections->have_posts()){
|
||||
$collections->the_post();
|
||||
|
||||
$collection = new Entities\Collection($collections->post);
|
||||
|
||||
array_push($response, $this->get_only_needed_attributes($collection, $map));
|
||||
}
|
||||
|
||||
wp_reset_postdata();
|
||||
}
|
||||
|
||||
$total_collections = $collections->found_posts;
|
||||
$max_pages = ceil($total_collections / (int) $collections->query_vars['posts_per_page']);
|
||||
|
||||
$rest_response = new WP_REST_Response($response, 200);
|
||||
|
||||
$rest_response->header('X-WP-Total', (int) $total_collections);
|
||||
$rest_response->header('X-WP-TotalPages', (int) $max_pages);
|
||||
|
||||
return $rest_response;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -113,28 +135,8 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_REST_Controller {
|
|||
* @return mixed|string|void|WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function prepare_item_for_response($item, $request){
|
||||
$map = $this->collections_repository->get_map();
|
||||
|
||||
if($item instanceof WP_Query){
|
||||
$collections = [];
|
||||
|
||||
if ($item->have_posts()) {
|
||||
while ( $item->have_posts() ) {
|
||||
$item->the_post();
|
||||
$collection = new Entities\Collection($item->post->ID);
|
||||
|
||||
$collection_resumed = $this->get_only_needed_attributes($collection, $map);
|
||||
|
||||
array_push($collections, $collection_resumed);
|
||||
|
||||
}
|
||||
wp_reset_postdata();
|
||||
}
|
||||
|
||||
return $collections;
|
||||
}
|
||||
elseif(!empty($item)){
|
||||
return $this->get_only_needed_attributes($item, $map);
|
||||
if(!empty($item)){
|
||||
return $item->__toArray();
|
||||
}
|
||||
|
||||
return $item;
|
||||
|
@ -177,17 +179,17 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_REST_Controller {
|
|||
* @return array|WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function create_item( $request ) {
|
||||
$request = json_decode($request->get_body(), true);
|
||||
$body = json_decode($request->get_body(), true);
|
||||
|
||||
if(empty($request)){
|
||||
if(empty($body)){
|
||||
return new WP_REST_Response([
|
||||
'error_message' => __('Body can not be empty.', 'tainacan'),
|
||||
'collection' => $request
|
||||
'collection' => $body
|
||||
], 400);
|
||||
}
|
||||
|
||||
try {
|
||||
$prepared_post = $this->prepare_item_for_database( $request );
|
||||
$prepared_post = $this->prepare_item_for_database( $body );
|
||||
} catch (\Error $exception){
|
||||
return new WP_REST_Response($exception->getMessage(), 400);
|
||||
}
|
||||
|
@ -195,13 +197,15 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_REST_Controller {
|
|||
if($prepared_post->validate()) {
|
||||
$collection = $this->collections_repository->insert( $prepared_post );
|
||||
|
||||
return new WP_REST_Response($collection->__toArray(), 201);
|
||||
$response = $this->prepare_item_for_response($collection, $request);
|
||||
|
||||
return new WP_REST_Response($response, 201);
|
||||
}
|
||||
|
||||
return new WP_REST_Response([
|
||||
'error_message' => __('One or more values are invalid.', 'tainacan'),
|
||||
'errors' => $prepared_post->get_errors(),
|
||||
'collection' => $prepared_post->__toArray()
|
||||
'collection' => $this->prepare_item_for_response($prepared_post, $request)
|
||||
], 400);
|
||||
}
|
||||
|
||||
|
@ -294,18 +298,20 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_REST_Controller {
|
|||
$collection = $this->collections_repository->fetch($collection_id);
|
||||
|
||||
if($collection) {
|
||||
$collection_prepared = $this->prepare_item_for_updating( $collection, $attributes );
|
||||
$prepared_collection = $this->prepare_item_for_updating( $collection, $attributes );
|
||||
|
||||
if ( $collection_prepared->validate() ) {
|
||||
if ( $prepared_collection->validate() ) {
|
||||
$updated_collection = $this->collections_repository->update( $collection );
|
||||
|
||||
return new WP_REST_Response( $updated_collection->__toArray(), 200 );
|
||||
$response = $this->prepare_item_for_response($updated_collection, $request);
|
||||
|
||||
return new WP_REST_Response( $response, 200 );
|
||||
}
|
||||
|
||||
return new WP_REST_Response([
|
||||
'error_message' => __('One or more values are invalid.', 'tainacan'),
|
||||
'errors' => $collection_prepared->get_errors(),
|
||||
'collection' => $collection_prepared->__toArray()
|
||||
'errors' => $prepared_collection->get_errors(),
|
||||
'collection' => $this->prepare_item_for_response($prepared_collection, $request)
|
||||
], 400);
|
||||
}
|
||||
|
||||
|
@ -346,7 +352,7 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_REST_Controller {
|
|||
public function get_collection_params() {
|
||||
$query_params = $this->collections_repository->get_map();
|
||||
|
||||
return apply_filters("rest_{$this->collection->get_post_type()}_collection_params", $query_params, $this->collection->get_post_type());
|
||||
return $query_params;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -362,7 +368,7 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_REST_Controller {
|
|||
]
|
||||
];
|
||||
|
||||
return apply_filters("rest_{$this->collection->get_post_type()}_collection_params", $args, $this->collection->get_post_type());
|
||||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -119,16 +119,20 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
|
|||
$field_added = $this->item_metadata_repository->insert($item_meta);
|
||||
}
|
||||
|
||||
return new WP_REST_Response($field_added->get_field()->__toArray(), 201);
|
||||
$response = $this->prepare_item_for_response($field_added->get_field(), $request);
|
||||
|
||||
return new WP_REST_Response($response, 201);
|
||||
}
|
||||
else {
|
||||
return new WP_REST_Response($this->field->__toArray(), 201);
|
||||
$response = $this->prepare_item_for_response($this->field, $request);
|
||||
|
||||
return new WP_REST_Response($response, 201);
|
||||
}
|
||||
} else {
|
||||
return new WP_REST_Response([
|
||||
'error_message' => __('One or more values are invalid.', 'tainacan'),
|
||||
'errors' => $this->field->get_errors(),
|
||||
'field' => $this->field->__toArray(),
|
||||
'field' => $this->prepare_item_for_response($this->field, $request),
|
||||
], 400);
|
||||
}
|
||||
}
|
||||
|
@ -157,13 +161,11 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
|
|||
* @return array|WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function prepare_item_for_response( $item, $request ) {
|
||||
$field_as = [];
|
||||
|
||||
foreach ( $item as $field ) {
|
||||
$field_as[] = $field->__toArray();
|
||||
if(!empty($item)){
|
||||
return $item->__toArray();
|
||||
}
|
||||
|
||||
return $field_as;
|
||||
return $item;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -180,7 +182,10 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
|
|||
|
||||
$collection_metadata = $this->field_repository->fetch_by_collection($collection, $args, 'OBJECT');
|
||||
|
||||
$prepared_item = $this->prepare_item_for_response($collection_metadata, $request);
|
||||
$prepared_item = [];
|
||||
foreach ($collection_metadata as $item){
|
||||
$prepared_item[] = $this->prepare_item_for_response($item, $request);
|
||||
}
|
||||
|
||||
return new WP_REST_Response($prepared_item, 200);
|
||||
}
|
||||
|
@ -284,16 +289,20 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
|
|||
$up_metadata = $this->item_metadata_repository->update($item_meta);
|
||||
}
|
||||
|
||||
return new WP_REST_Response($up_metadata->get_field()->__toArray(), 201);
|
||||
$response = $this->prepare_item_for_response($up_metadata->get_field(), $request);
|
||||
|
||||
return new WP_REST_Response($response, 200);
|
||||
}
|
||||
|
||||
return new WP_REST_Response($updated_metadata->__toArray(), 201);
|
||||
$response = $this->prepare_item_for_response($updated_metadata, $request);
|
||||
|
||||
return new WP_REST_Response($response, 200);
|
||||
}
|
||||
|
||||
return new WP_REST_Response([
|
||||
'error_message' => __('One or more values are invalid.', 'tainacan'),
|
||||
'errors' => $prepared_metadata->get_errors(),
|
||||
'metadata' => $prepared_metadata->__toArray()
|
||||
'metadata' => $this->prepare_item_for_response($prepared_metadata, $request)
|
||||
], 400);
|
||||
}
|
||||
|
||||
|
|
|
@ -85,26 +85,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
|
|||
* @return mixed|string|void|WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function prepare_item_for_response( $item, $request ) {
|
||||
$map = $this->items_repository->get_map();
|
||||
|
||||
if (!empty($item) && $item instanceof WP_Query){
|
||||
$items = [];
|
||||
|
||||
if ($item->have_posts()) {
|
||||
while ( $item->have_posts() ) {
|
||||
$item->the_post();
|
||||
$ite = new Entities\Item($item->post);
|
||||
|
||||
$item_prepared = $this->get_only_needed_attributes($ite, $map);
|
||||
|
||||
array_push($items, $item_prepared);
|
||||
|
||||
}
|
||||
wp_reset_postdata();
|
||||
}
|
||||
|
||||
return $items;
|
||||
} elseif(!empty($item)){
|
||||
if(!empty($item)){
|
||||
return $item->__toArray();
|
||||
}
|
||||
|
||||
|
@ -137,9 +118,30 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
|
|||
$collection_id = $request['collection_id'];
|
||||
$items = $this->items_repository->fetch($args, $collection_id, 'WP_Query');
|
||||
|
||||
$response = $this->prepare_item_for_response($items, $request);
|
||||
$map = $this->items_repository->get_map();
|
||||
|
||||
return new WP_REST_Response($response, 200);
|
||||
$response = [];
|
||||
if ($items->have_posts()) {
|
||||
while ( $items->have_posts() ) {
|
||||
$items->the_post();
|
||||
|
||||
$item = new Entities\Item($items->post);
|
||||
|
||||
array_push($response, $this->get_only_needed_attributes($item, $map));
|
||||
}
|
||||
|
||||
wp_reset_postdata();
|
||||
}
|
||||
|
||||
$total_items = $items->found_posts;
|
||||
$max_pages = ceil($total_items / (int) $items->query_vars['posts_per_page']);
|
||||
|
||||
$rest_response = new WP_REST_Response($response, 200);
|
||||
|
||||
$rest_response->header('X-WP-Total', (int) $total_items);
|
||||
$rest_response->header('X-WP-TotalPages', (int) $max_pages);
|
||||
|
||||
return $rest_response;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -215,14 +217,14 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
|
|||
if($this->item->validate()) {
|
||||
$item = $this->items_repository->insert($this->item );
|
||||
|
||||
return new WP_REST_Response($this->item->__toArray(), 201 );
|
||||
return new WP_REST_Response($this->prepare_item_for_response($item, $request), 201 );
|
||||
}
|
||||
|
||||
|
||||
return new WP_REST_Response([
|
||||
'error_message' => __('One or more values are invalid.', 'tainacan'),
|
||||
'errors' => $this->item->get_errors(),
|
||||
'item' => $this->item->__toArray()
|
||||
'item' => $this->prepare_item_for_response($this->item, $request)
|
||||
], 400);
|
||||
}
|
||||
|
||||
|
@ -301,13 +303,13 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
|
|||
if($prepared_item->validate()){
|
||||
$updated_item = $this->items_repository->update($prepared_item);
|
||||
|
||||
return new WP_REST_Response($updated_item->__toArray(), 200);
|
||||
return new WP_REST_Response($this->prepare_item_for_response($updated_item, $request), 200);
|
||||
}
|
||||
|
||||
return new WP_REST_Response([
|
||||
'error_message' => __('One or more values are invalid.', 'tainacan'),
|
||||
'errors' => $prepared_item->get_errors(),
|
||||
'item' => $prepared_item->__toArray()
|
||||
'item' => $this->prepare_item_for_response($prepared_item, $request)
|
||||
], 400);
|
||||
}
|
||||
|
||||
|
|
|
@ -153,7 +153,7 @@ class TAINACAN_REST_Terms extends TAINACAN_UnitApiTestCase {
|
|||
);
|
||||
|
||||
$request->set_query_params([
|
||||
'hide_empty' => false
|
||||
'hideempty' => false
|
||||
]);
|
||||
|
||||
$response = $this->server->dispatch($request);
|
||||
|
|
Loading…
Reference in New Issue