From f87ed222dab39db5a41f00ecef247b514402ac9f Mon Sep 17 00:00:00 2001 From: Robert Kosek Date: Wed, 24 Mar 2021 09:30:35 -0400 Subject: [PATCH 1/7] Updates for Laravel 8 support --- README.md | 8 ++++---- composer.json | 11 ++++++----- src/Facades/WpApi.php | 15 +++++++++------ src/LaravelWpApiServiceProvider.php | 2 +- src/WpApi.php | 6 +++--- 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index cdb1d06..4e2790a 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\LaravelWpApiServiceProvider::class ) 'aliases' => array( - 'WpApi' => AstritZeqiri\LaravelWpApi\Facades\WpApi::class + 'WpApi' => rk\LaravelWpApi\Facades\WpApi::class ), ``` diff --git a/composer.json b/composer.json index 2597148..f851f85 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", @@ -10,16 +10,17 @@ } ], "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/src/Facades/WpApi.php b/src/Facades/WpApi.php index c738323..7cefd9e 100644 --- a/src/Facades/WpApi.php +++ b/src/Facades/WpApi.php @@ -1,12 +1,15 @@ -auth; } - $response = $this->client->get($this->endpoint . $method, $query, $params); + $response = $this->client->get($this->endpoint . $method . '?' . http_build_query($query), $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') ]; From 3d5448c44b928a6ec7d35783dd01bb6fcca00d01 Mon Sep 17 00:00:00 2001 From: Robert Kosek Date: Wed, 24 Mar 2021 09:54:43 -0400 Subject: [PATCH 2/7] Patch: package auto-discovery clause --- composer.json | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/composer.json b/composer.json index f851f85..e4d5e4c 100755 --- a/composer.json +++ b/composer.json @@ -9,6 +9,16 @@ "email": "joseph.watkin@gmail.com" } ], + "extra": { + "laravel": { + "providers": [ + "rk\\LaravelWpApi\\LaravelWpApiServiceProvider" + ], + "aliases": { + "WpApi": "rk\\LaravelWpApi\\Facades\\WpApi" + } + } + }, "require": { "php": "^7.4|^8.0", "illuminate/support": "^8.12", From c0cec58ee0c2016be9fa3d5136670394db861126 Mon Sep 17 00:00:00 2001 From: Robert Kosek Date: Wed, 24 Mar 2021 10:00:55 -0400 Subject: [PATCH 3/7] Patch: tag the config file so that it can be specifically published --- src/LaravelWpApiServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LaravelWpApiServiceProvider.php b/src/LaravelWpApiServiceProvider.php index 3667c4c..5d3cc32 100644 --- a/src/LaravelWpApiServiceProvider.php +++ b/src/LaravelWpApiServiceProvider.php @@ -23,7 +23,7 @@ public function boot() { $this->publishes([ __DIR__.'/../../config/config.php' => config_path('wp-api.php'), - ]); + ], 'config'); } /** From 271fa2f17d7a6cfb1c93d941cf4104a1b4186eb2 Mon Sep 17 00:00:00 2001 From: Robert Kosek Date: Wed, 24 Mar 2021 10:04:54 -0400 Subject: [PATCH 4/7] Cleaned up structure; fixed config reference --- {src/config => config}/.gitkeep | 0 {src/config => config}/config.php | 0 src/LaravelWpApiServiceProvider.php | 2 +- src/controllers/.gitkeep | 0 src/lang/.gitkeep | 0 src/migrations/.gitkeep | 0 src/views/.gitkeep | 0 7 files changed, 1 insertion(+), 1 deletion(-) rename {src/config => config}/.gitkeep (100%) rename {src/config => config}/config.php (100%) delete mode 100644 src/controllers/.gitkeep delete mode 100644 src/lang/.gitkeep delete mode 100644 src/migrations/.gitkeep delete mode 100644 src/views/.gitkeep diff --git a/src/config/.gitkeep b/config/.gitkeep similarity index 100% rename from src/config/.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/LaravelWpApiServiceProvider.php b/src/LaravelWpApiServiceProvider.php index 5d3cc32..bcf275c 100644 --- a/src/LaravelWpApiServiceProvider.php +++ b/src/LaravelWpApiServiceProvider.php @@ -22,7 +22,7 @@ class LaravelWpApiServiceProvider extends ServiceProvider public function boot() { $this->publishes([ - __DIR__.'/../../config/config.php' => config_path('wp-api.php'), + __DIR__ . '/../config/config.php' => config_path('wp-api.php'), ], 'config'); } 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 From 02a1b94b4df6cdb23b093ee9068cbb5398b5a0e2 Mon Sep 17 00:00:00 2001 From: Robert Kosek Date: Wed, 24 Mar 2021 10:21:00 -0400 Subject: [PATCH 5/7] Patch: facade fix, clean up For some reason PHPStorm isn't seeing the Illuminate\Support\Facades\Facade class. I removed it, but wasn't supposed to --- README.md | 4 ++-- composer.json | 4 ++-- public/.gitkeep | 0 src/{Facades/WpApi.php => Facade.php} | 4 ++-- src/{LaravelWpApiServiceProvider.php => ServiceProvider.php} | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) delete mode 100644 public/.gitkeep rename src/{Facades/WpApi.php => Facade.php} (65%) rename src/{LaravelWpApiServiceProvider.php => ServiceProvider.php} (91%) diff --git a/README.md b/README.md index 4e2790a..798f5fc 100755 --- a/README.md +++ b/README.md @@ -16,11 +16,11 @@ You will need to add the service provider and optionally the facade alias to you ```php 'providers' => array( - rk\LaravelWpApi\LaravelWpApiServiceProvider::class + rk\LaravelWpApi\ServiceProvider::class ) 'aliases' => array( - 'WpApi' => rk\LaravelWpApi\Facades\WpApi::class + 'WpApi' => rk\LaravelWpApi\Facade::class ), ``` diff --git a/composer.json b/composer.json index e4d5e4c..d9b46cf 100755 --- a/composer.json +++ b/composer.json @@ -12,10 +12,10 @@ "extra": { "laravel": { "providers": [ - "rk\\LaravelWpApi\\LaravelWpApiServiceProvider" + "rk\\LaravelWpApi\\ServiceProvider" ], "aliases": { - "WpApi": "rk\\LaravelWpApi\\Facades\\WpApi" + "WpApi": "rk\\LaravelWpApi\\Facade" } } }, diff --git a/public/.gitkeep b/public/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/Facades/WpApi.php b/src/Facade.php similarity index 65% rename from src/Facades/WpApi.php rename to src/Facade.php index 7cefd9e..fb32f9e 100644 --- a/src/Facades/WpApi.php +++ b/src/Facade.php @@ -1,10 +1,10 @@ Date: Wed, 24 Mar 2021 10:27:33 -0400 Subject: [PATCH 6/7] Patch: again, fixing the class reference --- src/Facade.php | 2 +- src/WpApi.php | 64 ++++++++++++++++++++++++++------------------------ 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/src/Facade.php b/src/Facade.php index fb32f9e..5c54951 100644 --- a/src/Facade.php +++ b/src/Facade.php @@ -4,7 +4,7 @@ use rk\LaravelWpApi\WpApi as WordpressApi; -class Facade extends Illuminate\Support\Facades\Facade +class Facade extends \Illuminate\Support\Facades\Facade { protected static function getFacadeAccessor() diff --git a/src/WpApi.php b/src/WpApi.php index 19c6806..eeaed3d 100644 --- a/src/WpApi.php +++ b/src/WpApi.php @@ -1,4 +1,4 @@ -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) @@ -79,7 +84,7 @@ public function postId($id) /** * Get post by slug * - * @param string $slug + * @param string $slug * @return array */ public function post($slug) @@ -90,7 +95,7 @@ public function post($slug) /** * Get page by slug * - * @param string $slug + * @param string $slug * @return array */ public function page($slug) @@ -121,8 +126,8 @@ 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) @@ -133,8 +138,8 @@ 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) @@ -145,8 +150,8 @@ 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) @@ -157,8 +162,8 @@ 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) @@ -169,9 +174,9 @@ 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) @@ -182,12 +187,12 @@ 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($method, array $query = [], array $params = []) { try { @@ -201,11 +206,10 @@ public function get($method, array $query = array(), array $params = []) $response = $this->client->get($this->endpoint . $method . '?' . http_build_query($query), $params); $return = [ - 'results' => json_decode((string) $response->getBody(), true, JSON_THROW_ON_ERROR), + '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(); @@ -218,12 +222,10 @@ public function get($method, array $query = array(), array $params = []) 'error' => $error, 'results' => [], 'total' => 0, - 'pages' => 0 + 'pages' => 0, ]; - } return $return; - } } From 5bf76517ec42e5f922eebe07694d4ea3fdcf8880 Mon Sep 17 00:00:00 2001 From: Robert Kosek Date: Wed, 24 Mar 2021 11:01:05 -0400 Subject: [PATCH 7/7] Tweak auth and update from guzzle use to v7 --- src/WpApi.php | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/WpApi.php b/src/WpApi.php index eeaed3d..cf4e4f2 100644 --- a/src/WpApi.php +++ b/src/WpApi.php @@ -3,6 +3,7 @@ namespace rk\LaravelWpApi; use GuzzleHttp\Client; +use Illuminate\Support\Str; use Illuminate\Support\Traits\Macroable; use GuzzleHttp\Exception\RequestException; @@ -28,7 +29,7 @@ class WpApi /** * Auth headers * - * @var string + * @var mixed */ protected $auth; @@ -37,10 +38,16 @@ class WpApi * * @param string $endpoint * @param Client $client - * @param string $auth + * @param mixed $auth */ - public function __construct($endpoint, Client $client, $auth = null) + public function __construct(string $endpoint, Client $client, $auth = null) { + // Ensure there's a trailing slash to the endpoint as there will be + // a path appended to it. Prevents user error for a tiny cost. + if (!Str::endsWith($endpoint, '/')) { + $endpoint .= '/'; + } + $this->endpoint = $endpoint; $this->client = $client; $this->auth = $auth; @@ -53,7 +60,7 @@ public function __construct($endpoint, Client $client, $auth = null) * @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); } @@ -65,7 +72,7 @@ public function posts($page = null, $params = []) * @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); } @@ -76,7 +83,7 @@ public function pages($page = null, array $params = []) * @param int $id * @return array */ - public function postId($id) + public function postId(int $id): array { return $this->get("posts/$id"); } @@ -87,7 +94,7 @@ public function postId($id) * @param string $slug * @return array */ - public function post($slug) + public function post(string $slug): array { return $this->get('posts', ['filter' => ['name' => $slug]]); } @@ -98,7 +105,7 @@ public function post($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]]); } @@ -108,7 +115,7 @@ public function page($slug) * * @return array */ - public function categories() + public function categories(): array { return $this->get('taxonomies/category/terms'); } @@ -118,7 +125,7 @@ public function categories() * * @return array */ - public function tags() + public function tags(): array { return $this->get('taxonomies/post_tag/terms'); } @@ -130,7 +137,7 @@ public function tags() * @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]]); } @@ -142,7 +149,7 @@ public function categoryPosts($slug, $page = null) * @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]]); } @@ -154,7 +161,7 @@ public function authorPosts($name, $page = null) * @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]]); } @@ -166,7 +173,7 @@ public function tagPosts($tags, $page = null) * @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]]); } @@ -179,7 +186,7 @@ public function search($query, $page = null) * @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]]); } @@ -192,18 +199,16 @@ public function archive($year, $month, $page = null) * @param array $params * @return array */ - public function get($method, array $query = [], 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 . '?' . http_build_query($query), $params); + $response = $this->client->get($this->endpoint . $method, $params); $return = [ 'results' => json_decode((string)$response->getBody(), true, JSON_THROW_ON_ERROR), @@ -211,7 +216,6 @@ public function get($method, array $query = [], array $params = []) 'pages' => $response->getHeaderLine('X-WP-TotalPages'), ]; } catch (RequestException $e) { - $error['message'] = $e->getMessage(); if ($e->getResponse()) {