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']);
}
} 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

View file

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

View file

@ -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 {

View file

@ -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
*/

View file

@ -42,7 +42,10 @@ namespace wishthis;
<div class="column">
<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"
name ="wish_url"