feat: add indicator for affiliate links

This commit is contained in:
grandeljay 2023-12-20 02:27:13 +01:00
parent f0f4bdb2f9
commit 7171b94132
5 changed files with 83 additions and 16 deletions

View file

@ -28,21 +28,25 @@ switch ($_SERVER['REQUEST_METHOD']) {
$response['html'] = $wish->getCard($_GET['wishlist_user']); $response['html'] = $wish->getCard($_GET['wishlist_user']);
} }
} elseif (isset($_GET['wish_url'])) { } elseif (isset($_GET['wish_url'])) {
$url = trim($_GET['wish_url']); if (isset($_GET['isAffiliate'])) {
$cache = new Cache\Embed($url); $response['isAffiliate'] = Wish::hasAffiliateLink($_GET['wish_url']);
$info = $cache->get(true); } else {
$url = trim($_GET['wish_url']);
$cache = new Cache\Embed($url);
$info = $cache->get(true);
if (isset($info->url) && $info->url) { if (isset($info->url) && $info->url) {
$code = URL::getResponseCode($info->url); $code = URL::getResponseCode($info->url);
if ($code < 200 || $code >= 400) { if ($code < 200 || $code >= 400) {
$info->url = $url; $info->url = $url;
}
} }
}
$response = array( $response = array(
'info' => $info, 'info' => $info,
); );
}
} elseif (isset($_GET['wishlist_id'], $_GET['wishlist_style'], $_GET['wish_priority'])) { } elseif (isset($_GET['wishlist_id'], $_GET['wishlist_style'], $_GET['wish_priority'])) {
/** /**
* Get wishes by priority * Get wishes by priority

View file

@ -13,6 +13,11 @@
min-height: 0; min-height: 0;
} }
/** URL */
i.affiliate.link {
display: none;
}
/** Image */ /** Image */
.ui.modal.wish-details .wish-image { .ui.modal.wish-details .wish-image {
--imageHeight: 40vh; --imageHeight: 40vh;

View file

@ -250,6 +250,9 @@ $(function () {
.modal('show') .modal('show')
.addClass(wish_edit_size); .addClass(wish_edit_size);
/** Initialise Popups */
$('[data-content]').popup();
/** Initialise Tabs */ /** Initialise Tabs */
wish_edit.find('.item[data-tab]') wish_edit.find('.item[data-tab]')
.tab({ .tab({
@ -268,6 +271,32 @@ $(function () {
$('[name="wish_url"]').val(wish_local.url); $('[name="wish_url"]').val(wish_local.url);
$('.ui.selection.dropdown.priority').dropdown('set selected', wish_local.priority); $('.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) { if (wish_local.is_purchasable) {
checkbox_is_purchasable.checkbox('check'); checkbox_is_purchasable.checkbox('check');
} else { } else {

View file

@ -26,6 +26,9 @@ class Wish
public const STATUS_FULFILLED = 'fulfilled'; public const STATUS_FULFILLED = 'fulfilled';
public static array $priorities; public static array $priorities;
public static array $affiliates = array(
'amazon.de' => 'grandel0b-21',
);
public static function initialize() public static function initialize()
{ {
@ -79,15 +82,38 @@ class Wish
$urlParameters = array(); $urlParameters = array();
} }
if (\str_contains($urlParts['host'], 'amazon.de')) { foreach (self::$affiliates as $host => $tagId) {
$urlParameters['tag'] = 'grandel0b-21'; if (\str_contains($urlParts['host'], $host)) {
$urlParts['query'] = \http_build_query($urlParameters); $urlParameters['tag'] = $tagId;
$url = $urlParts['scheme'] . '://' . $urlParts['host'] . $urlParts['path'] . '?' . $urlParts['query']; $urlParts['query'] = \http_build_query($urlParameters);
$url = $urlParts['scheme'] . '://' . $urlParts['host'] . $urlParts['path'] . '?' . $urlParts['query'];
break;
}
} }
return $url; 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 * Non-Static
*/ */

View file

@ -42,7 +42,10 @@ namespace wishthis;
<div class="column"> <div class="column">
<div class="field"> <div class="field">
<label><?= __('URL') ?></label> <label>
<?= __('URL') ?>
<i class="orange linkify icon link affiliate" data-content="<?= __('This is an affiliate link.') ?>"></i>
</label>
<input type ="url" <input type ="url"
name ="wish_url" name ="wish_url"