From f9ed069d94761049f14753e467c8de1488fd8ff8 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 14 Mar 2024 16:44:07 +0100 Subject: [PATCH] wip #6775 @1.5 --- .../Operations/Tools/WebflowOperation.php | 9 ++ app/Models/ToolWebflow.php | 5 +- app/Services/Webflow.php | 104 +++++++++++++++++- 3 files changed, 111 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/Admin/Operations/Tools/WebflowOperation.php b/app/Http/Controllers/Admin/Operations/Tools/WebflowOperation.php index 1942b039e..dce574f41 100644 --- a/app/Http/Controllers/Admin/Operations/Tools/WebflowOperation.php +++ b/app/Http/Controllers/Admin/Operations/Tools/WebflowOperation.php @@ -23,6 +23,15 @@ trait WebflowOperation $wf->save(); return redirect(backpack_url('tool-webflow/' . $id . '/edit')); }); + + Route::match(['get'], 'webflow/{id}/data', function ($id) { + if (!can('webflow:write')) { + abort(401); + } + $wf = ToolWebflow::withoutGlobalScopes()->find($id); + Webflow::setToken($wf->webflow_api_token); + return response()->json(Webflow::getEditableData($wf->webflow)); + }); } } diff --git a/app/Models/ToolWebflow.php b/app/Models/ToolWebflow.php index c7e4f4e9e..60160d473 100644 --- a/app/Models/ToolWebflow.php +++ b/app/Models/ToolWebflow.php @@ -60,8 +60,7 @@ class ToolWebflow extends ToolboxModel protected function _parsePages() { - Webflow::setToken($this->webflow_api_token); - dd(Webflow::getAllData($this->webflow)); + } public function getLocales() @@ -198,6 +197,8 @@ class ToolWebflow extends ToolboxModel $locales = json_decode($locales, true); } Cache::put('webflow_' . $this->id . '_locales', $locales); + Webflow::setToken($this->webflow_api_token); + Webflow::getAllData($this->webflow); return parent::onRetrieved(); } diff --git a/app/Services/Webflow.php b/app/Services/Webflow.php index b73d2e825..148434ab9 100644 --- a/app/Services/Webflow.php +++ b/app/Services/Webflow.php @@ -3,9 +3,12 @@ namespace App\Services; +use App\Services\Webflow\Excel; +use hollodotme\FastCGI\Requests\GetRequest; use Illuminate\Http\Client\Response; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Http; +use function Symfony\Component\String\b; class Webflow { @@ -40,6 +43,34 @@ class Webflow return $response->json()['access_token']; } + public static function paginatedRequest($url, $key, $data = [], $method = 'get', $ttl = 86400, $force = false) + { + $res = []; + + $page = 0; + $limit = 100; + while (true) { + $reqData = $data; + if ($page > 0) { + $reqData['offset'] = $page * $limit; + } + + $response = self::request($url, $reqData, $method, $ttl, $force); + if (!isset($response['pagination'])) { + break; + } + $pagination = $response['pagination']; + $limit = $pagination['limit']; + $res = array_merge($res, $response[$key]); + if ($pagination['total'] <= $pagination['limit'] * ($page + 1)) { + break; + } + + $page++; + } + return self::_arrayKey($res); + } + public static function request($url, $data = [], $method = 'get', $ttl = 86400, $force = false) { $cacheKey = 'webflow_' . static::getToken() . '_' . $method . '_' . $url . '_' . sha1(print_r($data, true)); @@ -119,7 +150,7 @@ class Webflow public static function getAllData($shortname) { - return ['pages' => self::getAllPagesContents($shortname), 'cms' => self::getAllCMSContents($shortname)]; + return ['pages' => self::getAllPagesContents($shortname), 'cms' => self::getAllCMSContents($shortname), 'assets' => self::listAssets($shortname)]; } public static function getCMSCollectionDetails($collectionID) @@ -129,21 +160,84 @@ class Webflow public static function getCMSCollectionContents($collectionID) { - return self::request('collections/' . $collectionID . '/items')['items']; + return self::paginatedRequest('collections/' . $collectionID . '/items', 'items'); + } + + protected static function _arrayKey($array) + { + $res = []; + foreach ($array as $item) { + $res[$item['id']] = $item; + } + return $res; } public static function listCMSCollections($shortname) { - return self::request('sites/' . self::getSiteId($shortname) . '/collections')['collections']; + return self::paginatedRequest('sites/' . self::getSiteId($shortname) . '/collections', 'collections'); } public static function getPageContents($pageID) { - return self::request('pages/' . $pageID . '/dom'); + return self::paginatedRequest('pages/' . $pageID . '/dom', 'nodes'); } public static function listPages($shortname) { - return self::request('sites/' . self::getSiteId($shortname) . '/pages')['pages']; + return self::paginatedRequest('sites/' . self::getSiteId($shortname) . '/pages', 'pages'); + } + + public static function listAssets($shortname) + { + return self::paginatedRequest('sites/' . self::getSiteId($shortname) . '/assets', 'assets'); + } + + public static function getEditableData($shortname) + { + $data = self::getAllData($shortname); + + $res = ['texts' => [], 'images' => [], 'seo' => []]; + foreach ($data['pages'] as $pageID => $page) { + $details = $page['details']; + if ($details['archived']) { + continue; + } + $seo = [ + 'slug' => $details['slug'], + 'draft' => $details['draft'], + 'seo_title' => $details['seo']['title'], + 'seo_description' => $details['seo']['description'], + 'og_title' => $details['openGraph']['title'], + 'og_description' => $details['openGraph']['description'], + 'og_title_copied' => $details['openGraph']['titleCopied'], + 'og_description_copied' => $details['openGraph']['descriptionCopied'], + ]; + + $res['seo'][$pageID] = $seo; + + $texts = []; + foreach ($page['contents'] as $node) { + if ($node['type'] === 'text') { + if (!$node['text']['text']) { + continue; + } + if (!isset($res['texts'][$node['text']['text']])) { + $res['texts'][$node['text']['text']] = []; + } + $res['texts'][$node['text']['text']][] = ['node' => $node['id'], 'page' => $pageID]; + } else if ($node['type'] === 'image') { + if(!isset($node['image']['assetId'])){ + continue; + } + $assetId = $node['image']['assetId']; + if (isset($res['images'][$assetId])) { + continue; + } + $res['images'][$assetId] = ['alt' => $node['image']['alt'], 'url' => $data['assets'][$assetId]['hostedUrl']]; + } + } + + } + return $res; } } -- 2.39.5