This repository has been archived by the owner on Jun 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from BurdaMagazinOrg/feature/INREL-6531-amazo…
…n-tag-non-productdb INREL-6531: Replace Amazon tag for non-ProductDB products
- Loading branch information
Showing
2 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
modules/infinite_advertising_products/src/ReplaceAmazonTags.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
namespace Drupal\infinite_advertising_products; | ||
|
||
use Drupal\Component\Utility\UrlHelper; | ||
use Drupal\Core\Render\Markup; | ||
use Drupal\Core\Url; | ||
use QueryPath\DOMQuery; | ||
use Symfony\Component\DomCrawler\Crawler; | ||
|
||
/** | ||
* Defines a Replace Amazon Tags object. | ||
* | ||
* @package Drupal\infinite_advertising_products | ||
*/ | ||
class ReplaceAmazonTags { | ||
|
||
/** | ||
* Overridden Amazon Tag. | ||
* | ||
* @var string | ||
*/ | ||
protected $amazonTag; | ||
|
||
/** | ||
* The Symfony DOM crawler. | ||
* | ||
* @var Crawler | ||
*/ | ||
protected $crawler; | ||
|
||
/** | ||
* The QueryPath DOM query. | ||
* | ||
* @var DOMQuery | ||
*/ | ||
protected $queryPath; | ||
|
||
/** | ||
* The attribute we want to replace. | ||
* | ||
* @var string | ||
*/ | ||
protected $replaceAttr; | ||
|
||
/** | ||
* Array of product selectors for non-AMP. | ||
* | ||
* @var array | ||
*/ | ||
protected $selectors = [ | ||
'a.item-product[data-vars-product-provider="amazon"]', | ||
'.item-ecommerce[data-provider="amazon"]', | ||
]; | ||
|
||
/** | ||
* Constructs a ReplaceAmazonTags object. | ||
* | ||
* @param $amazonTag | ||
* The Amazon tag to override. | ||
* @param string $replaceAttr | ||
* The attribute we want to replace. | ||
*/ | ||
public function __construct($amazonTag, $replaceAttr = 'data-external-url') { | ||
$this->amazonTag = $amazonTag; | ||
$this->crawler = new Crawler(); | ||
$this->replaceAttr = $replaceAttr; | ||
} | ||
|
||
/** | ||
* Replace Amazon tag for all non-ProductDB products in given HTML markup. | ||
* | ||
* @param \Drupal\Core\Render\Markup $markup | ||
* The HTML markup to replace Amazon tag of Amazon products. | ||
*/ | ||
public function replaceAmazonTags(Markup $markup) { | ||
|
||
// Loading the html here instead of passing it into the constructor | ||
// prevents problems with the charset. | ||
$this->crawler->addHtmlContent($markup); | ||
|
||
foreach ($this->selectors as $selector) { | ||
$itemProducts = $this->crawler->filter($selector); | ||
foreach ($itemProducts as $itemProduct) { | ||
$amazonUrl = $itemProduct->getAttribute($this->replaceAttr); | ||
if (parse_url($amazonUrl) !== FALSE) { | ||
$itemProduct->setAttribute($this->replaceAttr, $this->getChangedAmazonUrl($amazonUrl)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Get Amazon URL with replaced Amazon tag. | ||
* | ||
* @param $amazonUrl | ||
* The Amazon URL of a Amazon product. | ||
* | ||
* @return \Drupal\Core\GeneratedUrl|string | ||
* The Amazon URL with replaced Amazon tag. | ||
*/ | ||
private function getChangedAmazonUrl($amazonUrl) { | ||
$amazonUrl = UrlHelper::parse($amazonUrl); | ||
$amazonUrl['query']['tag'] = $this->amazonTag; | ||
$options = [ | ||
'query' => $amazonUrl['query'], | ||
'fragment' => $amazonUrl['fragment'], | ||
]; | ||
return Url::fromUri($amazonUrl['path'], $options)->toString(); | ||
} | ||
|
||
/** | ||
* Get markup from given DOM crawler. | ||
* | ||
* @return \Drupal\Component\Render\MarkupInterface|string | ||
*/ | ||
public function getMarkup() { | ||
libxml_use_internal_errors(TRUE); | ||
$this->queryPath = html5qp($this->crawler); | ||
libxml_clear_errors(); | ||
|
||
$html = $this->queryPath->find('body')->innerHtml5(); | ||
return Markup::create($html); | ||
} | ||
|
||
} |