diff --git a/src/api/wishes.php b/src/api/wishes.php index e8370c73..709b5599 100644 --- a/src/api/wishes.php +++ b/src/api/wishes.php @@ -28,21 +28,25 @@ switch ($_SERVER['REQUEST_METHOD']) { $response['html'] = $wish->getCard($_GET['wishlist_user']); } } elseif (isset($_GET['wish_url'])) { - $url = trim($_GET['wish_url']); - $cache = new Cache\Embed($url); - $info = $cache->get(true); + if (isset($_GET['isAffiliate'])) { + $response['isAffiliate'] = Wish::hasAffiliateLink($_GET['wish_url']); + } else { + $url = trim($_GET['wish_url']); + $cache = new Cache\Embed($url); + $info = $cache->get(true); - if (isset($info->url) && $info->url) { - $code = URL::getResponseCode($info->url); + if (isset($info->url) && $info->url) { + $code = URL::getResponseCode($info->url); - if ($code < 200 || $code >= 400) { - $info->url = $url; + if ($code < 200 || $code >= 400) { + $info->url = $url; + } } - } - $response = array( - 'info' => $info, - ); + $response = array( + 'info' => $info, + ); + } } elseif (isset($_GET['wishlist_id'], $_GET['wishlist_style'], $_GET['wish_priority'])) { /** * Get wishes by priority diff --git a/src/assets/css/wish.css b/src/assets/css/wish.css index 311012eb..822b6064 100644 --- a/src/assets/css/wish.css +++ b/src/assets/css/wish.css @@ -13,6 +13,11 @@ min-height: 0; } +/** URL */ +i.affiliate.link { + display: none; +} + /** Image */ .ui.modal.wish-details .wish-image { --imageHeight: 40vh; diff --git a/src/assets/js/parts/wish.js b/src/assets/js/parts/wish.js index 82d7a4e5..ab1d82f9 100644 --- a/src/assets/js/parts/wish.js +++ b/src/assets/js/parts/wish.js @@ -250,6 +250,9 @@ $(function () { .modal('show') .addClass(wish_edit_size); + /** Initialise Popups */ + $('[data-content]').popup(); + /** Initialise Tabs */ wish_edit.find('.item[data-tab]') .tab({ @@ -268,6 +271,32 @@ $(function () { $('[name="wish_url"]').val(wish_local.url); $('.ui.selection.dropdown.priority').dropdown('set selected', wish_local.priority); + /** URL */ + $('[name="wish_url"]').on('blur', function(event) { + var input = $(event.target); + + input.attr('disabled', 'disabled'); + + fetch('/index.php?page=api&module=wishes&wish_url=' + input.val() + '&isAffiliate', { + 'method' : 'GET', + 'credentials' : 'include', + }) + .then(handleFetchError) + .then(handleFetchResponse) + .then(function(link) { + if (link.isAffiliate) { + $('[data-content]').css('display', 'inline-block'); + } else { + $('[data-content]').css('display', 'none'); + } + }) + .finally(function() { + input.removeAttr('disabled'); + }); + }); + $('[name="wish_url"]').trigger('blur'); + + /** Purchasable */ if (wish_local.is_purchasable) { checkbox_is_purchasable.checkbox('check'); } else { diff --git a/src/classes/wishthis/Wish.php b/src/classes/wishthis/Wish.php index a789c146..2fad3434 100644 --- a/src/classes/wishthis/Wish.php +++ b/src/classes/wishthis/Wish.php @@ -26,6 +26,9 @@ class Wish public const STATUS_FULFILLED = 'fulfilled'; public static array $priorities; + public static array $affiliates = array( + 'amazon.de' => 'grandel0b-21', + ); public static function initialize() { @@ -79,15 +82,38 @@ class Wish $urlParameters = array(); } - if (\str_contains($urlParts['host'], 'amazon.de')) { - $urlParameters['tag'] = 'grandel0b-21'; - $urlParts['query'] = \http_build_query($urlParameters); - $url = $urlParts['scheme'] . '://' . $urlParts['host'] . $urlParts['path'] . '?' . $urlParts['query']; + foreach (self::$affiliates as $host => $tagId) { + if (\str_contains($urlParts['host'], $host)) { + $urlParameters['tag'] = $tagId; + $urlParts['query'] = \http_build_query($urlParameters); + $url = $urlParts['scheme'] . '://' . $urlParts['host'] . $urlParts['path'] . '?' . $urlParts['query']; + + break; + } } return $url; } + public static function hasAffiliateLink(string $url): bool + { + $urlParts = parse_url($url); + + if (isset($urlParts['query'])) { + \parse_str($urlParts['query'], $urlParameters); + } else { + $urlParameters = array(); + } + + foreach (self::$affiliates as $host => $tagId) { + if (\str_contains($urlParts['host'], $host) && isset($urlParameters['tag'])) { + return true; + } + } + + return false; + } + /** * Non-Static */ diff --git a/src/pages/parts/wish-add.php b/src/pages/parts/wish-add.php index 4ddb35a7..fbf6c5d7 100644 --- a/src/pages/parts/wish-add.php +++ b/src/pages/parts/wish-add.php @@ -42,7 +42,10 @@ namespace wishthis;