From 23648ffe9d1216a961bd96ed4227fa06ddbf76bd Mon Sep 17 00:00:00 2001 From: elfeffe Date: Mon, 15 May 2017 19:10:53 +0200 Subject: [PATCH] Some improvements, get post by id and post by slug --- src/Threesquared/LaravelWpApi/WpApi.php | 46 ++++++++----------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/src/Threesquared/LaravelWpApi/WpApi.php b/src/Threesquared/LaravelWpApi/WpApi.php index 74971dc..fc3c86c 100755 --- a/src/Threesquared/LaravelWpApi/WpApi.php +++ b/src/Threesquared/LaravelWpApi/WpApi.php @@ -1,29 +1,23 @@ endpoint = $endpoint; - $this->client = $client; + $this->client = ($client) ? $client : new Client; $this->auth = $auth; } - /** * Get all posts * @@ -48,7 +41,6 @@ public function posts($page = null) { return $this->get('posts', ['page' => $page]); } - /** * Get all pages * @@ -59,7 +51,6 @@ public function pages($page = null) { return $this->get('posts', ['type' => 'page', 'page' => $page]); } - /** * Get post by id * @@ -70,7 +61,6 @@ public function postId($id) { return $this->get("posts/$id"); } - /** * Get post by slug * @@ -79,7 +69,18 @@ public function postId($id) */ public function post($slug) { - return $this->get('posts', ['filter' => ['name' => $slug]]); + return $this->get('posts', ['slug' => $slug]); + } + + /** + * Get media by id + * + * @param string $slug + * @return array + */ + public function mediaId($id) + { + return $this->get("media/$id"); } /** @@ -92,7 +93,6 @@ public function page($slug) { return $this->get('posts', ['type' => 'page', 'filter' => ['name' => $slug]]); } - /** * Get all categories * @@ -102,7 +102,6 @@ public function categories() { return $this->get('taxonomies/category/terms'); } - /** * Get all tags * @@ -112,7 +111,6 @@ public function tags() { return $this->get('taxonomies/post_tag/terms'); } - /** * Get posts from category * @@ -124,7 +122,6 @@ public function categoryPosts($slug, $page = null) { return $this->get('posts', ['page' => $page, 'filter' => ['category_name' => $slug]]); } - /** * Get posts by author * @@ -136,7 +133,6 @@ public function authorPosts($name, $page = null) { return $this->get('posts', ['page' => $page, 'filter' => ['author_name' => $name]]); } - /** * Get posts tagged with tag * @@ -148,7 +144,6 @@ public function tagPosts($tags, $page = null) { return $this->get('posts', ['page' => $page, 'filter' => ['tag' => $tags]]); } - /** * Search posts * @@ -160,7 +155,6 @@ public function search($query, $page = null) { return $this->get('posts', ['page' => $page, 'filter' => ['s' => $query]]); } - /** * Get posts by date * @@ -173,7 +167,6 @@ public function archive($year, $month, $page = null) { return $this->get('posts', ['page' => $page, 'filter' => ['year' => $year, 'monthnum' => $month]]); } - /** * Get data from the API * @@ -185,39 +178,28 @@ public function get($method, array $query = array()) { try { - $query = ['query' => $query]; - if ($this->auth) { $query['auth'] = $this->auth; } - $response = $this->client->get($this->endpoint . $method, $query); - $return = [ 'results' => json_decode((string) $response->getBody(), true), 'total' => $response->getHeaderLine('X-WP-Total'), 'pages' => $response->getHeaderLine('X-WP-TotalPages') ]; - } catch (RequestException $e) { - $error['message'] = $e->getMessage(); - if ($e->getResponse()) { $error['code'] = $e->getResponse()->getStatusCode(); } - $return = [ 'error' => $error, 'results' => [], 'total' => 0, 'pages' => 0 ]; - } - return $return; - } }