From a7e60976a5e04ab9b80f14dd77e62a07c7a49806 Mon Sep 17 00:00:00 2001 From: weryques Date: Wed, 6 Dec 2017 15:56:16 -0200 Subject: [PATCH] API Getting all items for a especified collection --- composer.lock | 2 +- .../class-tainacan-rest-items-controller.php | 29 ++++++++++++++++++- .../class-tainacan-repository.php | 12 ++++---- src/tainacan.php | 2 +- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/composer.lock b/composer.lock index 21706ee0d..76bfecf3f 100644 --- a/composer.lock +++ b/composer.lock @@ -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", diff --git a/src/api/endpoints/class-tainacan-rest-items-controller.php b/src/api/endpoints/class-tainacan-rest-items-controller.php index 446ec95be..6f52df0a6 100644 --- a/src/api/endpoints/class-tainacan-rest-items-controller.php +++ b/src/api/endpoints/class-tainacan-rest-items-controller.php @@ -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 ) { diff --git a/src/classes/repositories/class-tainacan-repository.php b/src/classes/repositories/class-tainacan-repository.php index 4b5fbb74c..9703dac6f 100644 --- a/src/classes/repositories/class-tainacan-repository.php +++ b/src/classes/repositories/class-tainacan-repository.php @@ -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' diff --git a/src/tainacan.php b/src/tainacan.php index 7cff76a53..81eb5a8ef 100644 --- a/src/tainacan.php +++ b/src/tainacan.php @@ -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; }