|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2016 Google |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | + |
| 19 | +namespace Lullabot\AMP\Pass; |
| 20 | + |
| 21 | +use QueryPath\DOMQuery; |
| 22 | + |
| 23 | +use Lullabot\AMP\Utility\ActionTakenLine; |
| 24 | +use Lullabot\AMP\Utility\ActionTakenType; |
| 25 | + |
| 26 | +/** |
| 27 | + * Class ObjectVimeoTagTransformPass |
| 28 | + * @package Lullabot\AMP\Pass |
| 29 | + * |
| 30 | + * Transform all Vimeo embed code <object> tags which don't have noscript as an ancestor to <amp-vimeo> tags |
| 31 | + * |
| 32 | + * This is what a vimeo embed looks like: |
| 33 | + * <object width="550" height="309"> |
| 34 | + * <param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=12223465&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=00ADEF&fullscreen=1" /> |
| 35 | + * <param name="allowfullscreen" value="true" /> |
| 36 | + * <param name="allowscriptaccess" value="always" /> |
| 37 | + * <embed src="http://vimeo.com/moogaloop.swf?clip_id=12223465&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=00ADEF&fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="550" height="309"></embed> |
| 38 | + * </object> |
| 39 | + * |
| 40 | + * @see https://github.com/ampproject/amphtml/blob/master/extensions/amp-vimeo/amp-vimeo.md |
| 41 | + * |
| 42 | + */ |
| 43 | +class ObjectVimeoTagTransformPass extends BasePass |
| 44 | +{ |
| 45 | + function pass() |
| 46 | + { |
| 47 | + $all_objects = $this->q->find('object:not(noscript object)'); |
| 48 | + /** @var DOMQuery $el */ |
| 49 | + foreach ($all_objects as $el) { |
| 50 | + /** @var \DOMElement $dom_el */ |
| 51 | + $dom_el = $el->get(0); |
| 52 | + $lineno = $this->getLineNo($dom_el); |
| 53 | + $context_string = $this->getContextString($dom_el); |
| 54 | + |
| 55 | + $actionTakenType = ''; |
| 56 | + |
| 57 | + if ($this->isVimeoObject($el)) { |
| 58 | + $vimeo_code = $this->getVimeoCode($el); |
| 59 | + |
| 60 | + // If we couldnt find a vimeo videoid then we abort |
| 61 | + if (empty($vimeo_code)) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + $el->after('<amp-vimeo width="16" height="9" layout="responsive" data-videoid="' . $vimeo_code . '"></amp-vimeo>'); |
| 66 | + $new_dom_el = $el->next()->get(0); |
| 67 | + |
| 68 | + $actionTakenType = ActionTakenType::VIMEO_OBJECT_CONVERTED; |
| 69 | + |
| 70 | + } else { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + // Remove the object and its children |
| 75 | + $el->removeChildren()->remove(); |
| 76 | + $this->addActionTaken(new ActionTakenLine('object', $actionTakenType, $lineno, $context_string)); |
| 77 | + $this->context->addLineAssociation($new_dom_el, $lineno); |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + return $this->transformations; |
| 82 | + } |
| 83 | + |
| 84 | + protected function isVimeoObject(DOMQuery $el) |
| 85 | + { |
| 86 | + $params = $el->find('param'); |
| 87 | + foreach ($params as $param) { |
| 88 | + if ($param->attr('name') == 'movie') { |
| 89 | + $param_value = $param->attr('value'); |
| 90 | + if (preg_match('&(*UTF8)(vimeo\.com)&i', $param_value)) { |
| 91 | + return true; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * |
| 100 | + * Get the Vimeo Code |
| 101 | + * |
| 102 | + * @param DOMQuery $el |
| 103 | + * @return string |
| 104 | + */ |
| 105 | + protected function getVimeoCode(DOMQuery $el) |
| 106 | + { |
| 107 | + $matches = []; |
| 108 | + $vimeo_code = ''; |
| 109 | + $params = $el->find('param'); |
| 110 | + |
| 111 | + foreach ($params as $param) { |
| 112 | + if ($param->attr('name') == 'movie') { |
| 113 | + $param_value = $param->attr('value'); |
| 114 | + |
| 115 | + $pattern = '#http://(?:\w+.)?vimeo.com/(?:video/|moogaloop\.swf\?clip_id=)(\w+)#i'; |
| 116 | + if (preg_match($pattern, $param_value, $matches)) { |
| 117 | + if (!empty($matches[1])) { |
| 118 | + $vimeo_code = $matches[1]; |
| 119 | + return $vimeo_code; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return $vimeo_code; |
| 126 | + } |
| 127 | +} |
0 commit comments