API Getting all items for a especified collection

This commit is contained in:
weryques 2017-12-06 15:56:16 -02:00
parent 3d2301d416
commit a7e60976a5
4 changed files with 36 additions and 9 deletions

2
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "e0eb272f68b3132a64ef299d34cf9700",
"content-hash": "64fbb4d01225b482970da995b00dccfb",
"packages": [
{
"name": "respect/validation",

View File

@ -69,8 +69,35 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
));
}
public function prepare_item_for_response( $item, $request ) {
if (!empty($item) && $item instanceof WP_Query){
$items_as_array = [];
if ($item->have_posts()) {
while ( $item->have_posts() ) {
$item->the_post();
$ite = new Entities\Item($item->post);
array_push($items_as_array, $ite->__toJSON());
}
wp_reset_postdata();
}
return json_encode($items_as_array);
} elseif(!empty($item)){
return $item->__toJSON();
}
return $item;
}
public function get_items( $request ) {
return parent::get_items( $request ); // TODO: Change the autogenerated stub
$collection_id = $request['collection_id'];
$items = $this->items_repository->fetch([], $collection_id, 'WP_Query');
$response = $this->prepare_item_for_response($items, $request);
return new WP_REST_Response($response, 201);
}
public function get_item_permissions_check( $request ) {

View File

@ -130,10 +130,10 @@ abstract class Repository {
if( $output === 'WP_Query'){
return $WP_Query;
}else if( $output === 'OBJECT' ) {
} else if( $output === 'OBJECT' ) {
$result = [];
if ( $WP_Query->have_posts() ){
if ( $WP_Query->have_posts() ){
/**
* Using WordPress Loop here would cause problems
* @see https://core.trac.wordpress.org/ticket/18408
@ -154,14 +154,14 @@ abstract class Repository {
* and the mapped properties for the repository.
*
* For example, you can use any of the following methods to browse collections by name:
* $TaincanCollections->fetch(['title' => 'test']);
* $TaincanCollections->fetch(['name' => 'test']);
* $TainacanCollections->fetch(['title' => 'test']);
* $TainacanCollections->fetch(['name' => 'test']);
*
* The property `name` is transformed into the native WordPress property `post_title`. (actually only title for query purpouses)
*
* Example 2, this also works with properties mapped to postmeta. The following methods are the same:
* $TaincanMetadatas->fetch(['required' => 'yes']);
* $TaincanMetadatas->fetch(['meta_query' => [
* $TainacanMetadatas->fetch(['required' => 'yes']);
* $TainacanMetadatas->fetch(['meta_query' => [
* [
* 'key' => 'required',
* 'value' => 'yes'

View File

@ -21,5 +21,5 @@ require_once('dev-interface/class-tainacan-dev-interface.php');
$Tainacan_Dev_interface = new \Tainacan\DevInterface\DevInterface();
function tnc_enable_dev_wp_interface() {
return defined('TNC_ENABLE_DEV_WP_INTERFACE') && true === TNC_ENABLE_DEV_WP_INTERFACE ? true : false;
//return defined('TNC_ENABLE_DEV_WP_INTERFACE') && true === TNC_ENABLE_DEV_WP_INTERFACE ? true : false;
}