has($key)) { return $cache->get($key); } } catch (InvalidArgumentException $e) { } // See https://github.com/oscarotero/Embed#example-with-all-options for service arguments $serviceArguments = []; if (!empty($arguments['width'])) { $serviceArguments['min_image_width'] = $arguments['width']; } if (!empty($arguments['height'])) { $serviceArguments['min_image_height'] = $arguments['height']; } /** @var EmbedContainer $embeddable */ $embeddable = Injector::inst()->create(Embeddable::class, $serviceURL); // Only EmbedContainer is currently supported if (!($embeddable instanceof EmbedContainer)) { throw new \RuntimeException('Emeddable must extend EmbedContainer'); } if (!empty($serviceArguments)) { $embeddable->setOptions(array_merge($serviceArguments, (array) $embeddable->getOptions())); } // Process embed try { // this will trigger a request/response which will then be cached within $embeddable $embeddable->getExtractor(); } catch (NetworkException | RequestException $e) { $message = (Director::isDev()) ? $e->getMessage() : _t(__CLASS__ . '.INVALID_URL', 'There was a problem loading the media.'); $attr = [ 'class' => 'ss-media-exception embed' ]; $result = HTML::createTag( 'div', $attr, HTML::createTag('p', [], $message) ); return $result; } // Convert embed object into HTML $html = static::embeddableToHtml($embeddable, $arguments); // Fallback to link to service if (!$html) { $result = static::linkEmbed($arguments, $serviceURL, $serviceURL); } // Cache result if ($html) { try { $cache->set($key, $html); } catch (InvalidArgumentException $e) { } } return $html; } public static function embeddableToHtml(Embeddable $embeddable, array $arguments): string { // Only EmbedContainer is supported if (!($embeddable instanceof EmbedContainer)) { return ''; } $extractor = $embeddable->getExtractor(); $type = $embeddable->getType(); if ($type === 'video' || $type === 'rich') { // Attempt to inherit width (but leave height auto) if (empty($arguments['width']) && $embeddable->getWidth()) { $arguments['width'] = $embeddable->getWidth(); } return static::videoEmbed($arguments, $extractor->code->html); } if ($type === 'photo') { return static::photoEmbed($arguments, (string) $extractor->url); } if ($type === 'link') { return static::linkEmbed($arguments, (string) $extractor->url, $extractor->title); } return ''; } /** * @param Adapter $embed * @param array $arguments Additional shortcode params * @return string * @deprecated 4.11.0 Use embeddableToHtml() instead */ public static function embedForTemplate($embed, $arguments) { Deprecation::notice('4.11.0', 'Use embeddableToHtml() instead'); switch ($embed->getType()) { case 'video': case 'rich': // Attempt to inherit width (but leave height auto) if (empty($arguments['width']) && $embed->getWidth()) { $arguments['width'] = $embed->getWidth(); } return static::videoEmbed($arguments, $embed->getCode()); case 'link': return static::linkEmbed($arguments, $embed->getUrl(), $embed->getTitle()); case 'photo': return static::photoEmbed($arguments, $embed->getUrl()); default: return null; } } /** * Build video embed tag * * @param array $arguments * @param string $content Raw HTML content * @return string */ protected static function videoEmbed($arguments, $content) { // Ensure outer div has given width (but leave height auto) if (!empty($arguments['width'])) { $arguments['style'] = 'width: ' . intval($arguments['width']) . 'px;'; } // override iframe dimension attributes provided by webservice with ones specified in shortcode arguments foreach (['width', 'height'] as $attr) { if (!($value = $arguments[$attr] ?? false)) { continue; } foreach (['"', "'"] as $quote) { $rx = "/(