From a031f00e94df55c7c5d61e35237ec53d29ff1f44 Mon Sep 17 00:00:00 2001 From: Eduardo Trujillo Date: Wed, 26 Oct 2016 14:52:23 -0700 Subject: [PATCH] feat(core): Improve query parsing and rendering for affiliate tags --- src/core/utils.php | 35 +++++++++++++++++++++++++ src/views/snagshout-widget-campaign.php | 11 ++++---- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/core/utils.php b/src/core/utils.php index ad921f4..465df75 100644 --- a/src/core/utils.php +++ b/src/core/utils.php @@ -19,3 +19,38 @@ function snagshout_ellipsis($input, $max) { return mb_strimwidth($input, 0, $max, "..."); } + +function snagshout_parse_query($qs) { + $result = []; + + parse_str($qs, $result); + + return $result; +} + +function snagshout_mutate_query($url, $mutations) { + $components = parse_url($url); + + $query = http_build_query(array_merge( + isset($components['query']) + ? snagshout_parse_query($components['query']) + : [], + $mutations + )); + + $fragmented = [ + isset($components['scheme']) ? $components['scheme'] . '://' : null, + isset($components['username']) + ? $components['username'] + . (isset($components['password']) ? ':' . $components['password'] : '') + . '@' + : null, + $components['host'], + isset($components['port']) ? ':' . $components['port'] : null, + $components['path'], + $query ? '?' . $query : null, + isset($components['anchor']) ? '#' . $components['anchor'] : null, + ]; + + return implode('', array_filter($fragmented)); +} diff --git a/src/views/snagshout-widget-campaign.php b/src/views/snagshout-widget-campaign.php index 4c82bc4..2666eb5 100644 --- a/src/views/snagshout-widget-campaign.php +++ b/src/views/snagshout-widget-campaign.php @@ -22,12 +22,11 @@ $percentOff = round((1.0 - ($offerPrice/$price)) * 100.0); -$externalUrl = $campaign->product->externalUrl; - -if (get_option('snagshout_affiliate_id')) { - $externalUrl .= parse_url($externalUrl, PHP_URL_QUERY) ? '&' : '?'; - $externalUrl .= vsprintf('tag=%s', [get_option('snagshout_affiliate_id')]); -} +$externalUrl = snagshout_mutate_query($campaign->product->externalUrl, [ + 'tag' => get_option('snagshout_affiliate_id') + ? get_option('snagshout_affiliate_id') + : null +]); $columnClass = 'ss-col-6';