diff --git a/build-config-sample.cfg b/build-config-sample.cfg index 7cb0cea98..caac95d41 100755 --- a/build-config-sample.cfg +++ b/build-config-sample.cfg @@ -1,2 +1,2 @@ # the destination folder, inside the plugins folder of some WordPress installation -destination=~/devel/wordpress/wp-content/plugins/test-tainacan \ No newline at end of file +destination=~/devel/wordpress/wp-content/plugins/tainacan \ No newline at end of file diff --git a/build.sh b/build.sh index fb8c0e947..70d5c488c 100755 --- a/build.sh +++ b/build.sh @@ -2,14 +2,13 @@ #source build-config.cfg source build-config.cfg -#destination=~/devel/wordpress/wp-content/plugins/test-tainacan +#destination=~/devel/wordpress/wp-content/plugins/tainacan sh compile-sass.sh composer install echo "Atualizando arquivos em $destination" -rm -r $destination +rm -rf $destination mkdir $destination cp -R src/ $destination/ -cp -R vendor/ $destination/ rm -rf $destination/scss diff --git a/composer.json b/composer.json index 707192187..e1a34004c 100644 --- a/composer.json +++ b/composer.json @@ -5,5 +5,8 @@ "require": { "respect/validation": "^1.1" }, - "minimum-stability": "dev" + "minimum-stability": "dev", + "config": { + "vendor-dir": "src/vendor" + } } diff --git a/src/api/endpoints/class-tainacan-rest-collections-controller.php b/src/api/endpoints/class-tainacan-rest-collections-controller.php new file mode 100644 index 000000000..3710c290a --- /dev/null +++ b/src/api/endpoints/class-tainacan-rest-collections-controller.php @@ -0,0 +1,78 @@ +namespace = 'tainacan/v2'; + $this->rest_base = 'collections'; + $this->collections_respository = new Repositories\Collections(); + + add_action('rest_api_init', array($this, 'register_routes')); + } + + public function register_routes(){ + register_rest_route($this->namespace, '/' . $this->rest_base, array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array($this, 'get_items'), + //'permission_callback' => array($this, 'get_items_permission_check'), + ), + )); + register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P[\d]+)', array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array($this, 'get_item'), + //'permission_callback' => array($this, 'get_item_permission_check'), + ), + )); + } + + public function get_items($request){ + $collections = $this->collections_respository->fetch(); + + $response = $this->prepare_item_for_response($collections, $request); + + return new WP_REST_Response($response, 200); + } + + public function get_item($request){ + $collection_id = $request['id']; + $collection = $this->collections_respository->fetch($collection_id); + + $response = $this->prepare_item_for_response($collection, $request); + + return new WP_REST_Response($response, 200); + } + + public function prepare_item_for_response($item, $request){ + if(is_array($item)){ + + $collections_as_json = []; + foreach ($item as $wp_post){ + array_push($posts_as_json, $wp_post->__toJSON()); + } + + return $collections_as_json; + } + else { + return $item->__toJSON(); + } + } + + public function get_item_schema(){ + + } + + public function get_items_permissions_check($request){ + return parent::get_items_permissions_check($request); // TODO: Change the autogenerated stub + } + + public function get_item_permissions_check($request){ + return parent::get_item_permissions_check($request); // TODO: Change the autogenerated stub + } +} + +?> \ No newline at end of file diff --git a/src/classes/entities/class-tainacan-collection.php b/src/classes/entities/class-tainacan-collection.php index b930d9da7..a40c81a0e 100644 --- a/src/classes/entities/class-tainacan-collection.php +++ b/src/classes/entities/class-tainacan-collection.php @@ -31,6 +31,19 @@ class Collection extends Entity { } } + public function __toString(){ + return 'Hello, I\'m the Collection Entity'; + } + + public function __toJSON(){ + return json_encode( + [ + 'name' => $this->get_name(), + 'description' => $this->get_description(), + 'items_per_page' => $this->get_itens_per_page(), + ] + ); + } /** * Register the post type for this collection * diff --git a/src/tainacan.php b/src/tainacan.php index 9755c9b78..27074bf80 100644 --- a/src/tainacan.php +++ b/src/tainacan.php @@ -1,10 +1,11 @@ fetch($col->get_id()); - - + $this->assertEquals($test->get_name(), 'teste'); $this->assertEquals($test->get_description(), 'adasdasdsa'); $this->assertEquals($test->get_itens_per_page(), 23);