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

Infogram implementation #272

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/AMP.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
72 changes: 72 additions & 0 deletions src/Pass/InfogramTransformPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
namespace Lullabot\AMP\Pass;

use QueryPath\DOMQuery;

use Lullabot\AMP\Utility\ActionTakenLine;
use Lullabot\AMP\Utility\ActionTakenType;

/**
* Class InfogramTransformPass
* @package Lullabot\AMP\Pass
*/
class InfogramTransformPass extends BasePass
{
function pass()
{
$all_infogram = $this->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"
<amp-iframe
height="$height"
width="$width"
layout="responsive"
frameborder="0"
sandbox="allow-scripts allow-same-origin allow-popups"
resizable="resizable"
allowfullscreen="allowfullscreen"
src="https://e.infogram.com/$src">
<div style="visibility: hidden" overflow="overflow" tabindex="0" role="button" aria-label="Loading..." placeholder="placeholder">Loading...</div>
</amp-iframe>
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;
}
}
2 changes: 2 additions & 0 deletions src/Utility/ActionTakenType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
Expand Down