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 EmbedResource $embed */ $embed = Injector::inst()->create(Embeddable::class, $serviceURL); if (!empty($serviceArguments)) { $embed->setOptions(array_merge($serviceArguments, (array) $embed->getOptions())); } // Allow resolver to be mocked $dispatcher = null; if (isset($extra['resolver'])) { $dispatcher = Injector::inst()->create( $extra['resolver']['class'], $serviceURL, $extra['resolver']['config'] ); } elseif (Injector::inst()->has(DispatcherInterface::class)) { $dispatcher = Injector::inst()->get(DispatcherInterface::class); } if ($dispatcher) { $embed->setDispatcher($dispatcher); } // Process embed try { $embed = $embed->getEmbed(); } catch (InvalidUrlException $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 if ($embed && $embed instanceof Adapter) { $result = static::embedForTemplate($embed, $arguments); } // Fallback to link to service if (!$result) { $result = static::linkEmbed($arguments, $serviceURL, $serviceURL); } // Cache result if ($result) { try { $cache->set($key, $result); } catch (InvalidArgumentException $e) { } } return $result; } /** * @param Adapter $embed * @param array $arguments Additional shortcode params * @return string */ public static function embedForTemplate($embed, $arguments) { 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 = "/(