Skip to content

Commit

Permalink
feat(core): Improve query parsing and rendering for affiliate tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardo Trujillo committed Oct 26, 2016
1 parent 5caf44f commit a031f00
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
35 changes: 35 additions & 0 deletions src/core/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
11 changes: 5 additions & 6 deletions src/views/snagshout-widget-campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down

0 comments on commit a031f00

Please sign in to comment.