Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor Release - account for url breaking characters #404

Merged
merged 3 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion Model/Api/Request/ProductDataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ private function getCategories(ProductInterface $product): string
$names = [];
foreach ($pathIds as $id) {
if (isset($parentCategories[$id]) && $parentCategories[$id]->getName()) {
$names[] = $parentCategories[$id]->getName();
$names[] = $this->sanitizeCategoryName($parentCategories[$id]->getName());
}
}
$categories[] = implode(self::DELIMITER_CATEGORY, $names);
Expand All @@ -276,6 +276,34 @@ private function getCategories(ProductInterface $product): string
return implode(',', $categories);
}

/**
* Returns sanitized value for payload
*
* @param string|null $theString
* @return string|null
*/
private function sanitizeCategoryName(?string $theString): ?string
{
if (!$theString)
return null;

// Use a regular expression to find HTML-encoded sections (e.g., %25)
$encodedSectionRegex = '/%[0-9A-Fa-f]{2}/';

// Decode HTML-encoded values using a callback function
$decodedString = preg_replace_callback($encodedSectionRegex, function($match) {
return urldecode($match[0]);
}, $theString);

// Replace remaining breaking characters
$resultString = str_replace('%', 'pct ', $decodedString);
$resultString = str_replace('?', '.', $resultString);
$resultString = str_replace('#', '.', $resultString);
$resultString = str_replace('&', 'and ', $resultString);

return $resultString;
}

/**
* Get product image url
*
Expand Down
2 changes: 1 addition & 1 deletion etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<default>
<warranty>
<version>
<tag>2.2.8</tag>
<tag>2.2.9</tag>
</version>
<enableExtend>
<enable>0</enable>
Expand Down
31 changes: 27 additions & 4 deletions view/frontend/web/js/warranty-offers-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ define([

Extend.buttons.render(this.element.get(0), {
referenceId: this.options.productSku,
category: this.options.productInfo.category,
category: this.sanitizeValue(this.options.productInfo.category),
price:this.options.productInfo.price
});
},
Expand All @@ -47,7 +47,7 @@ define([

Extend.buttons.renderSimpleOffer(this.element.get(0), {
referenceId: this.options.productSku,
category: this.options.productInfo.category,
category: this.sanitizeValue(this.options.productInfo.category),
price:this.options.productInfo.price,
onAddToCart: function (data) {
var warranty = data.plan;
Expand All @@ -61,7 +61,30 @@ define([
}
});
},

/**
* Returns sanitized value for payload
* @param {String} theString
* @return {string|null}
*/
sanitizeValue: function (theString ) {
if (!theString)
return;

// Use a regular expression to find HTML-encoded sections (e.g., %25)
var encodedSectionRegex = /%[0-9A-Fa-f]{2}/g;

// Replace each HTML-encoded section with its decoded equivalent
var decodedString = theString.replace(encodedSectionRegex, function(match) {
return decodeURIComponent(match);
});

// replace breaking characters
var theSanitizedString = decodedString.replace(/%/g, "pct ").replace(/\?/g, ".").replace(/#/g, ".").replace(/&/g, "and");

return theSanitizedString;
},

/**
* Returns current warranty offers block instance
*
Expand All @@ -86,7 +109,7 @@ define([
let activeProduct = {
referenceId:productSku,
price: product.price,
category: product.category
category: this.sanitizeValue(product.category)
};
component.setActiveProduct(activeProduct);
}
Expand All @@ -108,7 +131,7 @@ define([
Extend.modal.open({
referenceId: productSku,
price:productInfo.price,
category:productInfo.category,
category: this.sanitizeValue(productInfo.category),
onClose: closeCallback.bind(this)
});
},
Expand Down
Loading