From 66c0c2a363dac55d37a2750e0b6799c58432c0ab Mon Sep 17 00:00:00 2001 From: Kurt Trowbridge Date: Tue, 7 Apr 2020 17:17:24 -0400 Subject: [PATCH] Infogram embed auto-conversion from standard embed code to amp-iframe --- src/AMP.php | 1 + src/Pass/InfogramTransformPass.php | 72 ++++++++++++++++++++++++++++++ src/Utility/ActionTakenType.php | 2 + 3 files changed, 75 insertions(+) create mode 100644 src/Pass/InfogramTransformPass.php diff --git a/src/AMP.php b/src/AMP.php index e852ade7..581cdaf1 100644 --- a/src/AMP.php +++ b/src/AMP.php @@ -60,6 +60,7 @@ class AMP 'Lullabot\AMP\Pass\IframeDailymotionTagTransformPass', 'Lullabot\AMP\Pass\IframeYouTubeTagTransformPass', 'Lullabot\AMP\Pass\IframeTagTransformPass', + 'Lullabot\AMP\Pass\InfogramTransformPass', 'Lullabot\AMP\Pass\InstagramTransformPass', 'Lullabot\AMP\Pass\PinterestTagTransformPass', 'Lullabot\AMP\Pass\FacebookNonIframeTransformPass', diff --git a/src/Pass/InfogramTransformPass.php b/src/Pass/InfogramTransformPass.php new file mode 100644 index 00000000..c59cea1f --- /dev/null +++ b/src/Pass/InfogramTransformPass.php @@ -0,0 +1,72 @@ +q->top()->find('div.infogram-embed'); + /** @var DOMQuery $el */ + foreach ($all_infogram as $el) { + /** @var \DOMElement $dom_el */ + $dom_el = $el->get(0); + $lineno = $this->getLineNo($dom_el); + $context_string = $this->getContextString($dom_el); + $script_tag = $this->getScriptTag($el, 'e\.infogram\.com/js\.js&i'); + + $height = isset($this->options['infogram_height']) + ? $this->options['infogram_height'] : 937; + + $width = isset($this->options['infogram_width']) + ? $this->options['infogram_width'] : 1161; + + $src = $el->attr('data-id'); + + if ($src) { + $amp_string =<<<"HTML" + +
Loading...
+
+HTML; + + $el->after($amp_string); + $new_dom_el = $el->get(0); + + if (!empty($script_tag)) { + $script_tag->remove(); + $this->addActionTaken(new ActionTakenLine('a (with associated script tag)', ActionTakenType::INFOGRAM_CONVERTED, $lineno, $context_string)); + } + else { + $this->addActionTaken(new ActionTakenLine('a', ActionTakenType::INFOGRAM_CONVERTED, $lineno, $context_string)); + } + $this->context->addLineAssociation($new_dom_el, $lineno); + } + else { + $this->addActionTaken(new ActionTakenLine('div.infogram-embed', ActionTakenType::INFOGRAM_COULD_NOT_BE_CONVERTED, $lineno, $context_string)); + } + + // Remove the div, its children + $el->removeChildren()->remove(); + + } + + return $this->transformations; + } +} \ No newline at end of file diff --git a/src/Utility/ActionTakenType.php b/src/Utility/ActionTakenType.php index 60b88025..0cc20588 100644 --- a/src/Utility/ActionTakenType.php +++ b/src/Utility/ActionTakenType.php @@ -27,6 +27,8 @@ class ActionTakenType const IMG_PIXEL_CONVERTED = 'tag was converted to the amp-pixel tag.'; const IMG_ANIM_CONVERTED = 'tag was converted to the amp-anim tag.'; const IMG_COULD_NOT_BE_CONVERTED = 'tag could NOT be converted to the amp-img tag as the image is not accessible.'; + const INFOGRAM_CONVERTED = 'infogram tag was converted to the amp-iframe tag.'; + const INFOGRAM_COULD_NOT_BE_CONVERTED = 'infogram tag could NOT be converted to the amp-iframe tag.'; const INSTAGRAM_CONVERTED = 'instagram embed code was converted to the amp-instagram tag.'; const PINTEREST_CONVERTED = 'pinterest embed code was converted to the amp-pinterest tag.'; const VINE_CONVERTED = 'vine embed code was converted to the amp-vine tag.';