First commit

This commit is contained in:
Ben Speakman 2015-01-28 19:11:56 +00:00
commit 5794f84759
16 changed files with 296 additions and 0 deletions

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
/vendor
composer.phar
composer.lock
.DS_Store

13
.travis.yml Normal file
View file

@ -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

22
LICENSE Normal file
View file

@ -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.

67
README.md Normal file
View file

@ -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);
```

26
composer.json Normal file
View file

@ -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"
}

18
phpunit.xml Normal file
View file

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>

0
public/.gitkeep Normal file
View file

View file

@ -0,0 +1,57 @@
<?php namespace Cyberduck\LaravelWpApi;
use Illuminate\Support\ServiceProvider;
use GuzzleHttp\Client;
class LaravelWpApiServiceProvider extends ServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$this->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'];
}
}

View file

@ -0,0 +1,82 @@
<?php namespace Cyberduck\LaravelWpApi;
use GuzzleHttp\Client;
class WpApi
{
protected $client;
public function __construct($endpoint, Client $client)
{
$this->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;
}
}

0
src/config/.gitkeep Normal file
View file

7
src/config/config.php Normal file
View file

@ -0,0 +1,7 @@
<?php
return array(
'endpoint' => '<WP_LOCATION>',
);

0
src/controllers/.gitkeep Normal file
View file

0
src/lang/.gitkeep Normal file
View file

0
src/migrations/.gitkeep Normal file
View file

0
src/views/.gitkeep Normal file
View file

0
tests/.gitkeep Normal file
View file