From 3b0c8d0a5fb9428babeaef0060cc048f2174f6ac Mon Sep 17 00:00:00 2001 From: jbardnz Date: Wed, 5 Jun 2019 23:21:06 +1200 Subject: [PATCH 1/7] Update Api.php Fixed issue with domains being passed in the wrong format. --- src/Webflow/Api.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Webflow/Api.php b/src/Webflow/Api.php index aaf4564..7c66194 100644 --- a/src/Webflow/Api.php +++ b/src/Webflow/Api.php @@ -117,7 +117,9 @@ public function domains(string $siteId) public function publishSite(string $siteId, array $domains) { - return $this->post("/sites/${siteId}/publish", $domains); + return $this->post("/sites/${siteId}/publish", [ + 'domains' => $domains + ]); } // Collections From 35ce7ca13b6bbebd5e4769f9778537cb4e750605 Mon Sep 17 00:00:00 2001 From: jbardnz Date: Wed, 5 Jun 2019 23:35:01 +1200 Subject: [PATCH 2/7] Update APi.php Fix issue with body response not being parsed correctly. --- src/Webflow/Api.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Webflow/Api.php b/src/Webflow/Api.php index 7c66194..e2c603f 100644 --- a/src/Webflow/Api.php +++ b/src/Webflow/Api.php @@ -57,8 +57,12 @@ private function request(string $path, string $method, array $data = []) } curl_setopt_array($curl, $options); $response = curl_exec($curl); - curl_close($curl); + list($headers, $body) = explode("\r\n\r\n", $response, 2); + + $body = mb_substr($response, curl_getinfo($curl, CURLINFO_HEADER_SIZE)); + curl_close($curl); + return $this->parse($body); } private function get($path) From 3cd9578eb154eb6b01ea4d664c40c5d4c22c9652 Mon Sep 17 00:00:00 2001 From: jbardnz Date: Thu, 6 Jun 2019 17:15:42 +1200 Subject: [PATCH 3/7] Added new function createOrUpdateItemByName Allows you to create an item, or update it if it already exists. --- src/Webflow/Api.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/Webflow/Api.php b/src/Webflow/Api.php index e2c603f..5ef84de 100644 --- a/src/Webflow/Api.php +++ b/src/Webflow/Api.php @@ -210,6 +210,32 @@ public function findOrCreateItemByName(string $collectionId, array $fields) $this->cacheSet($cacheKey, $items); return $newItem; } + + public function createOrUpdateItemByName(string $collectionId, array $fields) + { + if (!isset($fields['name'])) { + throw new WebflowException('name'); + } + $cacheKey = "collection-{$collectionId}-items"; + $instance = $this; + $items = $this->cache($cacheKey, function () use ($instance, $collectionId) { + return $instance->itemsAll($collectionId); + }); + foreach ($items as $item) { + if (strcasecmp($item->name, $fields['name']) === 0) { + + //Update existing item + $this->updateItem($collectionId, $item->id, $fields); + return $item; + } + } + + //Create a new item + $newItem = $this->createItem($collectionId, $fields); + $items[] = $newItem; + $this->cacheSet($cacheKey, $items); + return $newItem; + } private function cache($key, callable $callback) { From a1c116fa68ba027a52fbdb2d4835f2e009268910 Mon Sep 17 00:00:00 2001 From: jbardnz Date: Thu, 6 Jun 2019 17:58:27 +1200 Subject: [PATCH 4/7] Small fix when updating item Merge field automatically. --- src/Webflow/Api.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Webflow/Api.php b/src/Webflow/Api.php index 5ef84de..0793d5c 100644 --- a/src/Webflow/Api.php +++ b/src/Webflow/Api.php @@ -223,9 +223,14 @@ public function createOrUpdateItemByName(string $collectionId, array $fields) }); foreach ($items as $item) { if (strcasecmp($item->name, $fields['name']) === 0) { - + + //Add required fields + if(!array_key_exists('slug', $fields)){ + $fields['slug'] = $item->slug; + } + //Update existing item - $this->updateItem($collectionId, $item->id, $fields); + $this->updateItem($collectionId, $item->_id, $fields); return $item; } } @@ -237,6 +242,7 @@ public function createOrUpdateItemByName(string $collectionId, array $fields) return $newItem; } + private function cache($key, callable $callback) { if (!isset($this->cache[$key])) { From c71aaabcc688907a19fbfd247919cf354bb8d279 Mon Sep 17 00:00:00 2001 From: jbardnz Date: Thu, 20 Jun 2019 21:21:10 +1200 Subject: [PATCH 5/7] Removed caches when updating items --- src/Webflow/Api.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/Webflow/Api.php b/src/Webflow/Api.php index 0793d5c..e875b68 100644 --- a/src/Webflow/Api.php +++ b/src/Webflow/Api.php @@ -216,11 +216,7 @@ public function createOrUpdateItemByName(string $collectionId, array $fields) if (!isset($fields['name'])) { throw new WebflowException('name'); } - $cacheKey = "collection-{$collectionId}-items"; - $instance = $this; - $items = $this->cache($cacheKey, function () use ($instance, $collectionId) { - return $instance->itemsAll($collectionId); - }); + foreach ($items as $item) { if (strcasecmp($item->name, $fields['name']) === 0) { @@ -237,8 +233,7 @@ public function createOrUpdateItemByName(string $collectionId, array $fields) //Create a new item $newItem = $this->createItem($collectionId, $fields); - $items[] = $newItem; - $this->cacheSet($cacheKey, $items); + return $newItem; } From bc102450bd47f2bbd4214e18cbe67f360d199832 Mon Sep 17 00:00:00 2001 From: jbardnz Date: Thu, 20 Jun 2019 21:28:20 +1200 Subject: [PATCH 6/7] Bug fix. --- src/Webflow/Api.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Webflow/Api.php b/src/Webflow/Api.php index e875b68..3b28dd5 100644 --- a/src/Webflow/Api.php +++ b/src/Webflow/Api.php @@ -216,7 +216,9 @@ public function createOrUpdateItemByName(string $collectionId, array $fields) if (!isset($fields['name'])) { throw new WebflowException('name'); } - + + $items = $this->itemsAll($collectionId); + foreach ($items as $item) { if (strcasecmp($item->name, $fields['name']) === 0) { From c954f66bfd6e936d97e47b093c8c73cbb8cb948d Mon Sep 17 00:00:00 2001 From: jbardnz Date: Thu, 20 Jun 2019 21:38:32 +1200 Subject: [PATCH 7/7] Added error if no items found. --- src/Webflow/Api.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Webflow/Api.php b/src/Webflow/Api.php index 3b28dd5..635e487 100644 --- a/src/Webflow/Api.php +++ b/src/Webflow/Api.php @@ -219,6 +219,10 @@ public function createOrUpdateItemByName(string $collectionId, array $fields) $items = $this->itemsAll($collectionId); + if(count($items) == 0){ + throw new WebflowException('No items found'); + } + foreach ($items as $item) { if (strcasecmp($item->name, $fields['name']) === 0) {