Skip to content

Commit

Permalink
Widgetbay, calculate iframe height from /api/widgetbox-count-availabl…
Browse files Browse the repository at this point in the history
…e-products API
  • Loading branch information
Claudio-Emmolo committed Nov 25, 2024
1 parent c2cd83f commit 8abc43b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion config/shortcode-plus.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
'text' => 'Questo contenuto è ospitato su una piattaforma esterna. Per visualizzarlo, è necessario <a href="javascript:void(0)" class="iubenda-cs-preferences-link">accettare i cookie</a>',
],
'widgetbay' => [
'endpoint' => 'https://widgetbay.3labs.it/widgetbox',
'endpoint' => 'https://widgetbay.3labs.it',
],

'linksToParse' => [
Expand Down
29 changes: 20 additions & 9 deletions src/Shortcodes/WidgetbayShortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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();
}
}

0 comments on commit 8abc43b

Please sign in to comment.