diff --git a/README.md b/README.md index cdb1d06..798f5fc 100755 --- a/README.md +++ b/README.md @@ -2,13 +2,13 @@ This is a fork of threesquared/laravel-wp-api # laravel-wp-api -Laravel 5 package for the [Wordpress JSON REST API](https://github.com/WP-API/WP-API) +Laravel 8 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: - "astritzeqiri/laravel-wp-api": "~2.1" + "rk/laravel-wp-api": "^4.0" ## Configuration @@ -16,11 +16,11 @@ You will need to add the service provider and optionally the facade alias to you ```php 'providers' => array( - AstritZeqiri\LaravelWpApi\LaravelWpApiServiceProvider::class + rk\LaravelWpApi\ServiceProvider::class ) 'aliases' => array( - 'WpApi' => AstritZeqiri\LaravelWpApi\Facades\WpApi::class + 'WpApi' => rk\LaravelWpApi\Facade::class ), ``` diff --git a/composer.json b/composer.json index 2597148..d9b46cf 100755 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "jwatkin423/laravel-wp-api", + "name": "rk/laravel-wp-api", "description": "Laravel package for the Wordpress JSON REST API", "keywords": ["laravel", "package", "wordpress", "API", "REST"], "license": "MIT", @@ -9,17 +9,28 @@ "email": "joseph.watkin@gmail.com" } ], + "extra": { + "laravel": { + "providers": [ + "rk\\LaravelWpApi\\ServiceProvider" + ], + "aliases": { + "WpApi": "rk\\LaravelWpApi\\Facade" + } + } + }, "require": { - "php": ">=5.5.0", - "illuminate/support": "~5.2", - "guzzlehttp/guzzle": "~6.0" + "php": "^7.4|^8.0", + "illuminate/support": "^8.12", + "guzzlehttp/guzzle": "^7.3", + "ext-json": "*" }, "require-dev": { "phpunit/phpunit": "4.8.*" }, "autoload": { "psr-4": { - "Jwatkin423\\LaravelWpApi\\": "src/" + "rk\\LaravelWpApi\\": "src/" } }, "minimum-stability": "dev" diff --git a/public/.gitkeep b/config/.gitkeep similarity index 100% rename from public/.gitkeep rename to config/.gitkeep diff --git a/src/config/config.php b/config/config.php similarity index 100% rename from src/config/config.php rename to config/config.php diff --git a/src/Facade.php b/src/Facade.php new file mode 100644 index 0000000..5c54951 --- /dev/null +++ b/src/Facade.php @@ -0,0 +1,15 @@ +publishes([ - __DIR__.'/../../config/config.php' => config_path('wp-api.php'), - ]); + __DIR__ . '/../config/config.php' => config_path('wp-api.php'), + ], 'config'); } /** diff --git a/src/WpApi.php b/src/WpApi.php index 79f8759..cf4e4f2 100644 --- a/src/WpApi.php +++ b/src/WpApi.php @@ -1,30 +1,35 @@ -endpoint = $endpoint; $this->client = $client; $this->auth = $auth; @@ -45,11 +56,11 @@ public function __construct($endpoint, Client $client, $auth = null) /** * Get all posts * - * @param int $page - * @param array $params + * @param int $page + * @param array $params * @return array */ - public function posts($page = null, $params = []) + public function posts(int $page = null, array $params = []): array { return $this->get('posts', ['page' => $page], $params); } @@ -57,21 +68,22 @@ public function posts($page = null, $params = []) /** * Get all pages * - * @param int $page - * @param array $params + * @param int $page + * @param array $params * @return array */ - public function pages($page = null, array $params = []) + public function pages(int $page = null, array $params = []): array { - return $this->get('posts', ['type' => 'page', 'page' => $page], $params); } + return $this->get('posts', ['type' => 'page', 'page' => $page], $params); + } /** * Get post by id * - * @param int $id + * @param int $id * @return array */ - public function postId($id) + public function postId(int $id): array { return $this->get("posts/$id"); } @@ -79,10 +91,10 @@ public function postId($id) /** * Get post by slug * - * @param string $slug + * @param string $slug * @return array */ - public function post($slug) + public function post(string $slug): array { return $this->get('posts', ['filter' => ['name' => $slug]]); } @@ -90,10 +102,10 @@ public function post($slug) /** * Get page by slug * - * @param string $slug + * @param string $slug * @return array */ - public function page($slug) + public function page(string $slug): array { return $this->get('posts', ['type' => 'page', 'filter' => ['name' => $slug]]); } @@ -103,7 +115,7 @@ public function page($slug) * * @return array */ - public function categories() + public function categories(): array { return $this->get('taxonomies/category/terms'); } @@ -113,7 +125,7 @@ public function categories() * * @return array */ - public function tags() + public function tags(): array { return $this->get('taxonomies/post_tag/terms'); } @@ -121,11 +133,11 @@ public function tags() /** * Get posts from category * - * @param string $slug - * @param int $page + * @param string $slug + * @param int $page * @return array */ - public function categoryPosts($slug, $page = null) + public function categoryPosts(string $slug, int $page = null) { return $this->get('posts', ['page' => $page, 'filter' => ['category_name' => $slug]]); } @@ -133,11 +145,11 @@ public function categoryPosts($slug, $page = null) /** * Get posts by author * - * @param string $name - * @param int $page + * @param string $name + * @param int $page * @return array */ - public function authorPosts($name, $page = null) + public function authorPosts(string $name, int $page = null) { return $this->get('posts', ['page' => $page, 'filter' => ['author_name' => $name]]); } @@ -145,11 +157,11 @@ public function authorPosts($name, $page = null) /** * Get posts tagged with tag * - * @param string $tags - * @param int $page + * @param string $tags + * @param int $page * @return array */ - public function tagPosts($tags, $page = null) + public function tagPosts(string $tags, int $page = null) { return $this->get('posts', ['page' => $page, 'filter' => ['tag' => $tags]]); } @@ -157,11 +169,11 @@ public function tagPosts($tags, $page = null) /** * Search posts * - * @param string $query - * @param int $page + * @param string $query + * @param int $page * @return array */ - public function search($query, $page = null) + public function search(string $query, int $page = null) { return $this->get('posts', ['page' => $page, 'filter' => ['s' => $query]]); } @@ -169,12 +181,12 @@ public function search($query, $page = null) /** * Get posts by date * - * @param int $year - * @param int $month - * @param int $page + * @param int $year + * @param int $month + * @param int $page * @return array */ - public function archive($year, $month, $page = null) + public function archive(int $year, int $month, int $page = null) { return $this->get('posts', ['page' => $page, 'filter' => ['year' => $year, 'monthnum' => $month]]); } @@ -182,32 +194,28 @@ public function archive($year, $month, $page = null) /** * Get data from the API * - * @param string $method - * @param array $query - * @param array $params + * @param string $method + * @param array $query + * @param array $params * @return array */ - public function get($method, array $query = array(), array $params = []) + public function get(string $method, array $query = [], array $params = []): array { - try { - - $query = ['query' => $query]; + $params['query'] = $query; if ($this->auth) { - $query['auth'] = $this->auth; + $params['auth'] = $this->auth; } - $response = $this->client->get($this->endpoint . $method, $query, $params); + $response = $this->client->get($this->endpoint . $method, $params); $return = [ - 'results' => json_decode((string) $response->getBody(), true), + 'results' => json_decode((string)$response->getBody(), true, JSON_THROW_ON_ERROR), 'total' => $response->getHeaderLine('X-WP-Total'), - 'pages' => $response->getHeaderLine('X-WP-TotalPages') + 'pages' => $response->getHeaderLine('X-WP-TotalPages'), ]; - } catch (RequestException $e) { - $error['message'] = $e->getMessage(); if ($e->getResponse()) { @@ -218,12 +226,10 @@ public function get($method, array $query = array(), array $params = []) 'error' => $error, 'results' => [], 'total' => 0, - 'pages' => 0 + 'pages' => 0, ]; - } return $return; - } } diff --git a/src/config/.gitkeep b/src/config/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/controllers/.gitkeep b/src/controllers/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/lang/.gitkeep b/src/lang/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/migrations/.gitkeep b/src/migrations/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/views/.gitkeep b/src/views/.gitkeep deleted file mode 100644 index e69de29..0000000