From 5794f84759fdd306100bbe45446fe47c5c5b1d6c Mon Sep 17 00:00:00 2001 From: Ben Speakman Date: Wed, 28 Jan 2015 19:11:56 +0000 Subject: [PATCH] First commit --- .gitignore | 4 + .travis.yml | 13 +++ LICENSE | 22 +++++ README.md | 67 +++++++++++++++ composer.json | 26 ++++++ phpunit.xml | 18 ++++ public/.gitkeep | 0 .../LaravelWpApiServiceProvider.php | 57 +++++++++++++ src/Cyberduck/LaravelWpApi/WpApi.php | 82 +++++++++++++++++++ src/config/.gitkeep | 0 src/config/config.php | 7 ++ src/controllers/.gitkeep | 0 src/lang/.gitkeep | 0 src/migrations/.gitkeep | 0 src/views/.gitkeep | 0 tests/.gitkeep | 0 16 files changed, 296 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 phpunit.xml create mode 100644 public/.gitkeep create mode 100644 src/Cyberduck/LaravelWpApi/LaravelWpApiServiceProvider.php create mode 100644 src/Cyberduck/LaravelWpApi/WpApi.php create mode 100644 src/config/.gitkeep create mode 100644 src/config/config.php create mode 100644 src/controllers/.gitkeep create mode 100644 src/lang/.gitkeep create mode 100644 src/migrations/.gitkeep create mode 100644 src/views/.gitkeep create mode 100644 tests/.gitkeep diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5826402 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/vendor +composer.phar +composer.lock +.DS_Store diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f60bbe0 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,13 @@ +language: php + +php: + - 5.4 + - 5.5 + - 5.6 + - hhvm + +before_script: + - travis_retry composer self-update + - travis_retry composer install --prefer-source --no-interaction --dev + +script: phpunit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4246578 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2015 Cyber-Duck Ltd + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..ea5eb8b --- /dev/null +++ b/README.md @@ -0,0 +1,67 @@ +# laravel-ap-api +Laravel 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": "dev-master" + +## Configuration + +Publish the package config files to configure the location of your Wordpress install: + + php artisan config:publish cyberduck/laravel-wp-api + +You will also need to add the service provider and the facade alias to your `app/config/app.php`: + +```php +'providers' => array( + 'Cyberduck\LaravelWpApi\LaravelWpApiServiceProvider' +) + +'aliases' => array( + 'WpApi' => 'Cyberduck\LaravelWpApi\Facades\WpApi' +), +``` + +### 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. + +#### Posts +```php +WpApi::posts($page); + +``` + +#### Post +```php +WpApi::post($slug); + +``` + +#### Categories +```php +WpApi::categories(); + +``` + +#### Category posts +```php +WpApi::category_posts($slug, $page); + +``` + +#### Search +```php +WpApi::search($query, $page); + +``` + +#### Archive +```php +WpApi::archive($year, $month, $page); + +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..c2811c1 --- /dev/null +++ b/composer.json @@ -0,0 +1,26 @@ +{ + "name": "cyberduck/laravel-wp-api", + "description": "", + "keywords": ["laravel", "package", "zoopla"], + "license": "MIT", + "authors": [ + { + "name": "Ben Speakman", + "email": "ben@cyber-duck.co.uk" + } + ], + "require": { + "php": ">=5.4.0", + "illuminate/support": "4.2.*|~5.0", + "guzzlehttp/guzzle": "~4.0" + }, + "autoload": { + "classmap": [ + "src/migrations" + ], + "psr-0": { + "Cyberduck\\LaravelWpApi\\": "src/" + } + }, + "minimum-stability": "stable" +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..3347b75 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,18 @@ + + + + + ./tests/ + + + diff --git a/public/.gitkeep b/public/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/Cyberduck/LaravelWpApi/LaravelWpApiServiceProvider.php b/src/Cyberduck/LaravelWpApi/LaravelWpApiServiceProvider.php new file mode 100644 index 0000000..40ab7ed --- /dev/null +++ b/src/Cyberduck/LaravelWpApi/LaravelWpApiServiceProvider.php @@ -0,0 +1,57 @@ +package('cyberduck/laravel-wp-api'); + } + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + $this->app->bindShared('wp-api', function ($app) { + + $endpoint = $this->app['config']->get('laravel-wp-api::endpoint'); + $client = new Client(); + + return new WpApi($endpoint, $client); + + }); + + $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 new file mode 100644 index 0000000..6cd4d45 --- /dev/null +++ b/src/Cyberduck/LaravelWpApi/WpApi.php @@ -0,0 +1,82 @@ +endpoint = $endpoint; + $this->client = $client; + } + + public function posts($page = null) + { + return $this->_get('posts', ['page' => $page]); + } + + public function post($slug) + { + return $this->_get('posts', ['filter' => ['name' => $slug]]); + } + + public function categories() + { + return $this->_get('taxonomies/category/terms'); + } + + public function category_posts($slug, $page = null) + { + return $this->_get('posts', ['page' => $page, 'filter' => ['category_name' => $slug]]); + } + + 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, 'month' => $month]]); + } + + public function _get($method, array $query = array()) + { + + $client = new Client(); + + try { + + $response = $client->get($this->endpoint . '/wp-json/' . $method, ['query' => $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/config/.gitkeep b/src/config/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/config/config.php b/src/config/config.php new file mode 100644 index 0000000..d71cdca --- /dev/null +++ b/src/config/config.php @@ -0,0 +1,7 @@ + '', + +); diff --git a/src/controllers/.gitkeep b/src/controllers/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/lang/.gitkeep b/src/lang/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/migrations/.gitkeep b/src/migrations/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/views/.gitkeep b/src/views/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/.gitkeep b/tests/.gitkeep new file mode 100644 index 0000000..e69de29