From 8abc43b53ceac9626f8ec832e16d6a68800a4f0b Mon Sep 17 00:00:00 2001 From: Claudio-Emmolo <113107618+Claudio-Emmolo@users.noreply.github.com> Date: Mon, 25 Nov 2024 11:50:07 +0100 Subject: [PATCH] Widgetbay, calculate iframe height from /api/widgetbox-count-available-products API --- config/shortcode-plus.php | 2 +- src/Shortcodes/WidgetbayShortcode.php | 29 ++++++++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/config/shortcode-plus.php b/config/shortcode-plus.php index 11d134a..a58cc55 100644 --- a/config/shortcode-plus.php +++ b/config/shortcode-plus.php @@ -40,7 +40,7 @@ 'text' => 'Questo contenuto è ospitato su una piattaforma esterna. Per visualizzarlo, è necessario accettare i cookie', ], 'widgetbay' => [ - 'endpoint' => 'https://widgetbay.3labs.it/widgetbox', + 'endpoint' => 'https://widgetbay.3labs.it', ], 'linksToParse' => [ diff --git a/src/Shortcodes/WidgetbayShortcode.php b/src/Shortcodes/WidgetbayShortcode.php index 4585934..1415d0f 100644 --- a/src/Shortcodes/WidgetbayShortcode.php +++ b/src/Shortcodes/WidgetbayShortcode.php @@ -6,30 +6,30 @@ class WidgetbayShortcode { public function register($shortcode) { - $endpoint = config('shortcode-plus.widgetbay.endpoint'); + $endpoint = config('shortcode-plus.widgetbay.endpoint') . '/widgetbox'; $widgetbayLink = ''; $heightListClass = null; if ($shortcode->id) { - $widgetbayLink = $endpoint.'/'.$shortcode->id; + $widgetbayLink = $endpoint . '/' . $shortcode->id; } if ($shortcode->link) { $shortcode->link = str_replace('&', '%26', $shortcode->link); $heightListClass = $this->calculateIframeHeight($shortcode->link, $shortcode->layout); - $widgetbayLink = $endpoint.'?link='.$shortcode->link; + $widgetbayLink = $endpoint . '?link=' . $shortcode->link; } if ($shortcode->title) { - $widgetbayLink .= '&title='.$shortcode->title; + $widgetbayLink .= '&title=' . $shortcode->title; } if ($shortcode->forcelink) { - $widgetbayLink .= '&forceLink='.$shortcode->forcelink; + $widgetbayLink .= '&forceLink=' . $shortcode->forcelink; } if ($shortcode->layout) { - $widgetbayLink .= '&layout='.$shortcode->layout; + $widgetbayLink .= '&layout=' . $shortcode->layout; } if (empty($widgetbayLink)) { @@ -41,14 +41,25 @@ public function register($shortcode) protected function calculateIframeHeight($products, $layout = null) { - $products = explode(',', $products); - $count = count($products); + $endpoint = config('shortcode-plus.widgetbay.endpoint') . '/api/widgetbox-count-available-products?link=' . $products; + + + //create HTTP request + $request = new \GuzzleHttp\Client(); + $response = $request->get($endpoint); + $count = json_decode($response->getBody()->getContents(), true); + $prefix = $layout === 'hero' ? 'shortcode_widgetbay_list_hero_' : 'shortcode_widgetbay_list_'; if ($count > 1) { - return $prefix.$count; + return $prefix . $count; } return null; } + + protected function checkIfProductIsAvailable($product) + { + return $product->isAvailable(); + } }