Compare commits

..

7 commits
2.0 ... master

Author SHA1 Message Date
Ben Speakman ec6e3d7263 Merge pull request #4 from WebSpanner/master
Add postId() function to get posts by id
2016-12-02 09:15:58 +00:00
Andrew Feeney a389bcea7a Add postId function to get posts by id 2016-12-02 11:07:30 +11:00
Ben Speakman 387b30fffc Merge pull request #1 from rafaelcneves/patch-2
README adjusts
2016-04-29 15:29:35 +01:00
Rafael César Neves 7f2b217bc0 README adjusts 2016-04-29 11:13:00 -03:00
Ben Speakman 7297886a26 Alias WpApi in facade 2016-04-05 09:13:04 +01:00
Ben Speakman ad9c74f218 Use singleton method for Laravel 5.2 2016-03-16 17:21:59 +00:00
Ben Speakman d2c2c0599c Rename vendor, update guzzle and add test 2016-01-22 13:38:09 +00:00
6 changed files with 97 additions and 15 deletions

View file

@ -1,7 +1,6 @@
language: php language: php
php: php:
- 5.4
- 5.5 - 5.5
- 5.6 - 5.6
- hhvm - hhvm

View file

@ -1,6 +1,6 @@
# laravel-wp-api # laravel-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) [![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/v/stable)](https://packagist.org/packages/threesquared/laravel-wp-api)
Laravel 5 package for the [Wordpress JSON REST API](https://github.com/WP-API/WP-API) Laravel 5 package for the [Wordpress JSON REST API](https://github.com/WP-API/WP-API)
@ -12,11 +12,7 @@ Simply add the following line to your `composer.json` and run install/update:
## Configuration ## Configuration
Publish the package config files to configure the location of your Wordpress install: You will need to add the service provider and optionally the facade alias to your `config/app.php`:
php artisan vendor:publish
You will also need to add the service provider and optionally the facade alias to your `app/config/app.php`:
```php ```php
'providers' => array( 'providers' => array(
@ -28,6 +24,10 @@ You will also need to add the service provider and optionally the facade alias t
), ),
``` ```
And publish the package config files to configure the location of your Wordpress install:
php artisan vendor:publish
### Usage ### Usage
The package provides a simplified interface to some of the existing api methods documented [here](http://wp-api.org/). The package provides a simplified interface to some of the existing api methods documented [here](http://wp-api.org/).
@ -51,6 +51,11 @@ WpApi::post($slug);
``` ```
```php
WpApi::postId($id);
```
#### Categories #### Categories
```php ```php
WpApi::categories(); WpApi::categories();

View file

@ -10,8 +10,8 @@
} }
], ],
"require": { "require": {
"php": ">=5.4.0", "php": ">=5.5.0",
"illuminate/support": "~5.0", "illuminate/support": "~5.2",
"guzzlehttp/guzzle": "~6.0" "guzzlehttp/guzzle": "~6.0"
}, },
"require-dev": { "require-dev": {

View file

@ -1,9 +1,10 @@
<?php namespace Threesquared\LaravelWpApi\Facades; <?php namespace Threesquared\LaravelWpApi\Facades;
use Illuminate\Support\Facades\Facade; use Illuminate\Support\Facades\Facade;
use Threesquared\LaravelWpApi\WpApi as WordpressApi;
class WpApi extends Facade { class WpApi extends Facade {
protected static function getFacadeAccessor() { return 'wp-api'; } protected static function getFacadeAccessor() { return WordpressApi::class; }
} }

View file

@ -31,7 +31,7 @@ class LaravelWpApiServiceProvider extends ServiceProvider
*/ */
public function register() public function register()
{ {
$this->app->bindShared('wp-api', function ($app) { $this->app->singleton(WpApi::class, function ($app) {
$endpoint = $this->app['config']->get('wp-api.endpoint'); $endpoint = $this->app['config']->get('wp-api.endpoint');
$auth = $this->app['config']->get('wp-api.auth'); $auth = $this->app['config']->get('wp-api.auth');
@ -40,10 +40,6 @@ class LaravelWpApiServiceProvider extends ServiceProvider
return new WpApi($endpoint, $client, $auth); return new WpApi($endpoint, $client, $auth);
}); });
$this->app->bind('Threesquared\LaravelWpApi\WpApi', function ($app) {
return $app['wp-api'];
});
} }
/** /**

View file

@ -38,56 +38,137 @@ class WpApi
$this->auth = $auth; $this->auth = $auth;
} }
/**
* Get all posts
*
* @param int $page
* @return array
*/
public function posts($page = null) public function posts($page = null)
{ {
return $this->get('posts', ['page' => $page]); return $this->get('posts', ['page' => $page]);
} }
/**
* Get all pages
*
* @param int $page
* @return array
*/
public function pages($page = null) public function pages($page = null)
{ {
return $this->get('posts', ['type' => 'page', 'page' => $page]); return $this->get('posts', ['type' => 'page', 'page' => $page]);
} }
/**
* Get post by id
*
* @param int $id
* @return array
*/
public function postId($id)
{
return $this->get("posts/$id");
}
/**
* Get post by slug
*
* @param string $slug
* @return array
*/
public function post($slug) public function post($slug)
{ {
return $this->get('posts', ['filter' => ['name' => $slug]]); return $this->get('posts', ['filter' => ['name' => $slug]]);
} }
/**
* Get page by slug
*
* @param string $slug
* @return array
*/
public function page($slug) public function page($slug)
{ {
return $this->get('posts', ['type' => 'page', 'filter' => ['name' => $slug]]); return $this->get('posts', ['type' => 'page', 'filter' => ['name' => $slug]]);
} }
/**
* Get all categories
*
* @return array
*/
public function categories() public function categories()
{ {
return $this->get('taxonomies/category/terms'); return $this->get('taxonomies/category/terms');
} }
/**
* Get all tags
*
* @return array
*/
public function tags() public function tags()
{ {
return $this->get('taxonomies/post_tag/terms'); return $this->get('taxonomies/post_tag/terms');
} }
/**
* Get posts from category
*
* @param string $slug
* @param int $page
* @return array
*/
public function categoryPosts($slug, $page = null) public function categoryPosts($slug, $page = null)
{ {
return $this->get('posts', ['page' => $page, 'filter' => ['category_name' => $slug]]); return $this->get('posts', ['page' => $page, 'filter' => ['category_name' => $slug]]);
} }
/**
* Get posts by author
*
* @param string $name
* @param int $page
* @return array
*/
public function authorPosts($name, $page = null) public function authorPosts($name, $page = null)
{ {
return $this->get('posts', ['page' => $page, 'filter' => ['author_name' => $name]]); return $this->get('posts', ['page' => $page, 'filter' => ['author_name' => $name]]);
} }
/**
* Get posts tagged with tag
*
* @param string $tags
* @param int $page
* @return array
*/
public function tagPosts($tags, $page = null) public function tagPosts($tags, $page = null)
{ {
return $this->get('posts', ['page' => $page, 'filter' => ['tag' => $tags]]); return $this->get('posts', ['page' => $page, 'filter' => ['tag' => $tags]]);
} }
/**
* Search posts
*
* @param string $query
* @param int $page
* @return array
*/
public function search($query, $page = null) public function search($query, $page = null)
{ {
return $this->get('posts', ['page' => $page, 'filter' => ['s' => $query]]); return $this->get('posts', ['page' => $page, 'filter' => ['s' => $query]]);
} }
/**
* Get posts by date
*
* @param int $year
* @param int $month
* @param int $page
* @return array
*/
public function archive($year, $month, $page = null) public function archive($year, $month, $page = null)
{ {
return $this->get('posts', ['page' => $page, 'filter' => ['year' => $year, 'monthnum' => $month]]); return $this->get('posts', ['page' => $page, 'filter' => ['year' => $year, 'monthnum' => $month]]);