diff --git a/README.md b/README.md index d1ad1e2..ecf998d 100755 --- a/README.md +++ b/README.md @@ -1,11 +1,14 @@ # laravel-wp-api -Laravel 5 package for the [Wordpress JSON REST API](https://github.com/WP-API/WP-API) + +[![Build Status](https://travis-ci.org/threesquared/laravel-wp-api.svg?branch=master)](https://travis-ci.org/threesquared/laravel-wp-api) [![Latest Stable Version](https://poser.pugx.org/threesquared/laravel-wp-api/version)](https://packagist.org/packages/threesquared/laravel-wp-api) + +Laravel 5 package for the [Wordpress JSON REST API](https://github.com/WP-API/WP-API) ## Install Simply add the following line to your `composer.json` and run install/update: - "cyberduck/laravel-wp-api": "~1.0" + "threesquared/laravel-wp-api": "~2.0" ## Configuration @@ -17,18 +20,18 @@ You will also need to add the service provider and optionally the facade alias t ```php 'providers' => array( - 'Cyberduck\LaravelWpApi\LaravelWpApiServiceProvider' + Threesquared\LaravelWpApi\LaravelWpApiServiceProvider::class ) 'aliases' => array( - 'WpApi' => 'Cyberduck\LaravelWpApi\Facades\WpApi' + 'WpApi' => Threesquared\LaravelWpApi\Facades\WpApi::class ), ``` ### Usage The package provides a simplified interface to some of the existing api methods documented [here](http://wp-api.org/). -You can either use the Facade provided or inject the WpApi class. +You can either use the Facade provided or inject the `Threesquared\LaravelWpApi\WpApi` class. #### Posts ```php @@ -62,7 +65,19 @@ WpApi::tags(); #### Category posts ```php -WpApi::category_posts($slug, $page); +WpApi::categoryPosts($slug, $page); + +``` + +#### Author posts +```php +WpApi::authorPosts($slug, $page); + +``` + +#### Tag posts +```php +WpApi::tagPosts($slug, $page); ``` diff --git a/composer.json b/composer.json index 38bdda1..218fca8 100755 --- a/composer.json +++ b/composer.json @@ -1,22 +1,25 @@ { - "name": "cyberduck/laravel-wp-api", + "name": "threesquared/laravel-wp-api", "description": "Laravel package for the Wordpress JSON REST API", "keywords": ["laravel", "package", "wordpress", "API", "REST"], "license": "MIT", "authors": [ { "name": "Ben Speakman", - "email": "ben@cyber-duck.co.uk" + "email": "ben@3sq.re" } ], "require": { "php": ">=5.4.0", - "illuminate/support": "~5.0|~5.1", - "guzzlehttp/guzzle": "~5.0" + "illuminate/support": "~5.0", + "guzzlehttp/guzzle": "~6.0" + }, + "require-dev": { + "phpunit/phpunit": "4.8.*" }, "autoload": { "psr-0": { - "Cyberduck\\LaravelWpApi\\": "src/" + "Threesquared\\LaravelWpApi\\": "src/" } }, "minimum-stability": "stable" diff --git a/src/Cyberduck/LaravelWpApi/LaravelWpApiServiceProvider.php b/src/Cyberduck/LaravelWpApi/LaravelWpApiServiceProvider.php deleted file mode 100755 index a01b23f..0000000 --- a/src/Cyberduck/LaravelWpApi/LaravelWpApiServiceProvider.php +++ /dev/null @@ -1,59 +0,0 @@ -publishes([ - __DIR__.'/../../config/config.php' => config_path('wp-api.php'), - ]); - } - - /** - * Register the service provider. - * - * @return void - */ - public function register() - { - $this->app->bindShared('wp-api', function ($app) { - - $endpoint = $this->app['config']->get('wp-api.endpoint'); - $auth = $this->app['config']->get('wp-api.auth'); - $client = $this->app->make('GuzzleHttp\Client'); - - return new WpApi($endpoint, $client, $auth); - - }); - - $this->app->bind('Cyberduck\LaravelWpApi\WpApi', function($app) - { - return $app['wp-api']; - }); - } - - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return ['wp-api']; - } - -} diff --git a/src/Cyberduck/LaravelWpApi/WpApi.php b/src/Cyberduck/LaravelWpApi/WpApi.php deleted file mode 100755 index ad1c239..0000000 --- a/src/Cyberduck/LaravelWpApi/WpApi.php +++ /dev/null @@ -1,112 +0,0 @@ -endpoint = $endpoint; - $this->client = $client; - $this->auth = $auth; - } - - public function posts($page = null) - { - return $this->_get('posts', ['page' => $page]); - } - - public function pages($page = null) - { - return $this->_get('posts', ['type' => 'page', 'page' => $page]); - } - - public function post($slug) - { - return $this->_get('posts', ['filter' => ['name' => $slug]]); - } - - public function page($slug) - { - return $this->_get('posts', ['type' => 'page', 'filter' => ['name' => $slug]]); - } - - public function categories() - { - return $this->_get('taxonomies/category/terms'); - } - - public function tags() - { - return $this->_get('taxonomies/post_tag/terms'); - } - - public function category_posts($slug, $page = null) - { - return $this->_get('posts', ['page' => $page, 'filter' => ['category_name' => $slug]]); - } - - public function author_posts($name, $page = null) - { - return $this->_get('posts', ['page' => $page, 'filter' => ['author_name' => $name]]); - } - - public function tag_posts($tags, $page = null) - { - return $this->_get('posts', ['page' => $page, 'filter' => ['tag' => $tags]]); - } - - public function search($query, $page = null) - { - return $this->_get('posts', ['page' => $page, 'filter' => ['s' => $query]]); - } - - public function archive($year, $month, $page = null) - { - return $this->_get('posts', ['page' => $page, 'filter' => ['year' => $year, 'monthnum' => $month]]); - } - - public function _get($method, array $query = array()) - { - - try { - - $query = ['query' => $query]; - - if($this->auth) { - $query['auth'] = $this->auth; - } - - $response = $this->client->get($this->endpoint . $method, $query); - - $return = [ - 'results' => $response->json(), - 'total' => $response->getHeader('X-WP-Total'), - 'pages' => $response->getHeader('X-WP-TotalPages') - ]; - - } catch (\GuzzleHttp\Exception\TransferException $e) { - - $error['message'] = $e->getMessage(); - - if ($e->getResponse()) { - $error['code'] = $e->getResponse()->getStatusCode(); - } - - $return = [ - 'error' => $error, - 'results' => [], - 'total' => 0, - 'pages' => 0 - ]; - - } - - return $return; - - } - -} diff --git a/src/Cyberduck/LaravelWpApi/Facades/WpApi.php b/src/Threesquared/LaravelWpApi/Facades/WpApi.php similarity index 72% rename from src/Cyberduck/LaravelWpApi/Facades/WpApi.php rename to src/Threesquared/LaravelWpApi/Facades/WpApi.php index ce0e8bb..0b2a97c 100644 --- a/src/Cyberduck/LaravelWpApi/Facades/WpApi.php +++ b/src/Threesquared/LaravelWpApi/Facades/WpApi.php @@ -1,4 +1,4 @@ -publishes([ + __DIR__.'/../../config/config.php' => config_path('wp-api.php'), + ]); + } + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + $this->app->bindShared('wp-api', function ($app) { + + $endpoint = $this->app['config']->get('wp-api.endpoint'); + $auth = $this->app['config']->get('wp-api.auth'); + $client = $this->app->make('GuzzleHttp\Client'); + + return new WpApi($endpoint, $client, $auth); + + }); + + $this->app->bind('Threesquared\LaravelWpApi\WpApi', function ($app) { + return $app['wp-api']; + }); + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return ['wp-api']; + } +} diff --git a/src/Threesquared/LaravelWpApi/WpApi.php b/src/Threesquared/LaravelWpApi/WpApi.php new file mode 100755 index 0000000..c42a469 --- /dev/null +++ b/src/Threesquared/LaravelWpApi/WpApi.php @@ -0,0 +1,142 @@ +endpoint = $endpoint; + $this->client = $client; + $this->auth = $auth; + } + + public function posts($page = null) + { + return $this->get('posts', ['page' => $page]); + } + + public function pages($page = null) + { + return $this->get('posts', ['type' => 'page', 'page' => $page]); + } + + public function post($slug) + { + return $this->get('posts', ['filter' => ['name' => $slug]]); + } + + public function page($slug) + { + return $this->get('posts', ['type' => 'page', 'filter' => ['name' => $slug]]); + } + + public function categories() + { + return $this->get('taxonomies/category/terms'); + } + + public function tags() + { + return $this->get('taxonomies/post_tag/terms'); + } + + public function categoryPosts($slug, $page = null) + { + return $this->get('posts', ['page' => $page, 'filter' => ['category_name' => $slug]]); + } + + public function authorPosts($name, $page = null) + { + return $this->get('posts', ['page' => $page, 'filter' => ['author_name' => $name]]); + } + + public function tagPosts($tags, $page = null) + { + return $this->get('posts', ['page' => $page, 'filter' => ['tag' => $tags]]); + } + + public function search($query, $page = null) + { + return $this->get('posts', ['page' => $page, 'filter' => ['s' => $query]]); + } + + public function archive($year, $month, $page = null) + { + return $this->get('posts', ['page' => $page, 'filter' => ['year' => $year, 'monthnum' => $month]]); + } + + /** + * Get data from the API + * + * @param string $method + * @param array $query + * @return array + */ + public function get($method, array $query = array()) + { + + try { + + $query = ['query' => $query]; + + if ($this->auth) { + $query['auth'] = $this->auth; + } + + $response = $this->client->get($this->endpoint . $method, $query); + + $return = [ + 'results' => json_decode((string) $response->getBody(), true), + 'total' => $response->getHeaderLine('X-WP-Total'), + 'pages' => $response->getHeaderLine('X-WP-TotalPages') + ]; + + } catch (RequestException $e) { + + $error['message'] = $e->getMessage(); + + if ($e->getResponse()) { + $error['code'] = $e->getResponse()->getStatusCode(); + } + + $return = [ + 'error' => $error, + 'results' => [], + 'total' => 0, + 'pages' => 0 + ]; + + } + + return $return; + + } +} diff --git a/tests/GetTest.php b/tests/GetTest.php new file mode 100644 index 0000000..4277a49 --- /dev/null +++ b/tests/GetTest.php @@ -0,0 +1,43 @@ + 2, 'X-WP-TotalPages' => 1], json_encode(['posts' => []])), + new RequestException('Error Communicating with Server', new Request('GET', 'test')) + ]); + + $handler = HandlerStack::create($mock); + $client = new Client(['handler' => $handler]); + + self::$wp = new WpApi('http://test.dev/wp-api', $client); + } + + public function testCanFetchPosts() + { + $response = self::$wp->posts(); + + $this->assertEquals($response['results'], ['posts' => []]); + $this->assertEquals($response['total'], 2); + $this->assertEquals($response['pages'], 1); + } + + public function testCanHandlerError() + { + $response = self::$wp->posts(); + + $this->assertEquals($response['error']['message'], 'Error Communicating with Server'); + } +}