Skip to content

Commit

Permalink
Merge pull request #182 from obeyer/youtube-ts
Browse files Browse the repository at this point in the history
convert youtube hash into start parameter
  • Loading branch information
Sigurður Guðbrandsson authored Apr 18, 2017
2 parents dc7e99c + 8c69a9d commit f9abb4f
Show file tree
Hide file tree
Showing 3 changed files with 5,304 additions and 4 deletions.
28 changes: 26 additions & 2 deletions src/Pass/IframeYouTubeTagTransformPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,29 @@ function pass()
$lineno = $this->getLineNo($dom_el);
$context_string = $this->getContextString($dom_el);
$youtube_code = $this->getYouTubeCode($el);
$youtube_hash = $this->getYouTubeCodeHash($el);

// If we couldnt find a youtube videoid then we abort
if (empty($youtube_code)) {
continue;
}

$extra_data = NULL;
if (!empty($youtube_hash)) {
// Try to parse this into a start value: t=333
// #t=333 is the old way, and youtube dropped support for it. transform it into start=333
parse_str($youtube_hash, $extras);
if (!empty($extras['t'])) {
$extra_data = "data-param-start=\"{$extras['t']}\"";
}
}

if ($el->hasAttr('class')) {
$class_attr = $el->attr('class');
}

/** @var \DOMElement $new_dom_el */
$el->after("<amp-youtube data-videoid=\"$youtube_code\" layout=\"responsive\"></amp-youtube>");
$el->after("<amp-youtube data-videoid=\"$youtube_code\" layout=\"responsive\" {$extra_data}></amp-youtube>");
$new_el = $el->next();
$new_dom_el = $new_el->get(0);
if (!empty($class_attr)) {
Expand Down Expand Up @@ -127,13 +138,26 @@ protected function getYouTubeCode(DOMQuery $el)
if (!empty($matches[1]))
{
$youtube_code = $matches[1];
return htmlspecialchars($youtube_code);
}
}

return htmlspecialchars($youtube_code);
}

/**
*
* Get the youtube anchor hash (if available)
*
* @param DOMQuery $el
* @return string
*/
protected function getYouTubeCodeHash(DOMQuery $el) {
$href = $el->attr('src');
// Return hash-tag timestamp jump: #t=243
$youtube_code_hash = explode('#', $href);
return !empty($youtube_code_hash[1]) ? $youtube_code_hash[1] : NULL;
}

/**
* @param DOMQuery $el
* @param DOMQuery $new_dom_el
Expand Down
Loading

0 comments on commit f9abb4f

Please sign in to comment.