Last active
October 19, 2021 04:35
-
-
Save andrewandante/af92b29429d4ffc2b61e8442fc542ae7 to your computer and use it in GitHub Desktop.
Vimeo Embed Fix (for "Verify to Continue" error)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SilverStripe\View\Parsers\ShortcodeParser::get('default') | |
->register('embed', [App\Shortcode\CustomVimeoEmbedShortcodeProvider::class, 'handle_shortcode']); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Shortcode; | |
use Psr\Log\LoggerInterface; | |
use Psr\SimpleCache\CacheInterface; | |
use SilverStripe\Core\Injector\Injector; | |
use SilverStripe\View\Shortcodes\EmbedShortcodeProvider; | |
use Throwable; | |
class CustomVimeoEmbedShortcodeProvider extends EmbedShortcodeProvider | |
{ | |
/** | |
* @inheritDoc | |
*/ | |
public static function handle_shortcode($arguments, $content, $parser, $shortcode, $extra = []) | |
{ | |
$cache = Injector::inst()->get(CacheInterface::class . '.EmbedShortcodeProvider'); | |
$cacheKey = 'embed-shortcode-' . preg_replace('/[^a-zA-Z0-9\-]/', '', $arguments['url']); | |
// To prevent being rate limited by vimeo, we manually parse vimeo links to an iframe | |
if (self::is_vimeo_link($arguments)) { | |
$accountID = null; | |
$videoID = ltrim(parse_url($arguments['url'])['path'], '/'); | |
if (strpos($videoID, '/') !== false) { | |
[$videoID, $accountID] = explode('/', $videoID); | |
} | |
$width = $arguments['width'] ?? 480; | |
$height = $arguments['height'] ?? 270; | |
// Add a title attribute to the iframe if one is defined | |
$title = $arguments['title'] ?? 'Embedded Vimeo video'; | |
$caption = $arguments['caption'] ?? ''; | |
$srcString = sprintf('https://player.vimeo.com/video/%s?feature=oembed&rel=0', $videoID); | |
if ($accountID !== null) { | |
$srcString .= sprintf('&h=%s', $accountID); | |
} | |
return self::videoEmbed($arguments, sprintf( | |
'<iframe width="%s" height="%s" src="%s" ' . | |
'frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" ' . | |
'title="%s" allowfullscreen></iframe><p class="caption">%s</p>', | |
$width, | |
$height, | |
$srcString, | |
$title, | |
$caption | |
)); | |
} | |
if ($cache->has($cacheKey)) { | |
return $cache->get($cacheKey); | |
} | |
try { | |
$output = parent::handle_shortcode($arguments, $content, $parser, $shortcode, $extra); | |
$cache->set($cacheKey, $output); | |
return $output; | |
} catch (Throwable $exception) { | |
$msg = 'Handle shortcode failed for shortcode: ' . $shortcode . ' ' . $exception; | |
Injector::inst()->get(LoggerInterface::class)->warning($msg); | |
} | |
return ''; | |
} | |
/** | |
* Check if link is from vimeo | |
* | |
* @param string[] $arguments - shortcode arguments | |
* @return bool | |
*/ | |
public static function is_vimeo_link(array $arguments) | |
{ | |
if (!isset($arguments['url'])) { | |
return false; | |
} | |
return strpos($arguments['url'], 'vimeo.com') !== false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
Name: vimeo-embed | |
After: coreoembed | |
--- | |
SilverStripe\Core\Injector\Injector: | |
SilverStripe\View\Embed\Embeddable: | |
class: App\Extensions\VimeoEmbedResource |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Extensions; | |
use Psr\Log\LoggerInterface; | |
use SilverStripe\Core\Injector\Injector; | |
use SilverStripe\View\Embed\EmbedResource; | |
class VimeoEmbedResource extends EmbedResource | |
{ | |
public function getEmbed() | |
{ | |
try { | |
$embed = parent::getEmbed(); | |
if (!$embed->getCode() && strpos($embed->url, 'vimeo.com') !== false) { | |
$arguments = $this->getOptions(); | |
$accountID = null; | |
$videoID = ltrim(parse_url($embed->url)['path'], '/'); | |
if (strpos($videoID, '/') !== false) { | |
[$videoID, $accountID] = explode('/', $videoID); | |
} | |
$width = $arguments['min_image_width'] ?? 480; | |
$height = $arguments['min_image_height'] ?? 270; | |
// Add a title attribute to the iframe if one is defined | |
$title = 'Embedded Vimeo video'; | |
$caption = $embed->caption ?? ''; | |
$srcString = sprintf('https://player.vimeo.com/video/%s?feature=oembed&rel=0', $videoID); | |
if ($accountID !== null) { | |
$srcString .= sprintf('&h=%s', $accountID); | |
} | |
$embed->code = sprintf( | |
'<iframe width="%s" height="%s" src="%s" ' . | |
'frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" ' . | |
'title="%s" allowfullscreen></iframe><p class="caption">%s</p>', | |
$width, | |
$height, | |
$srcString, | |
$title, | |
$caption | |
); | |
} | |
return $embed; | |
} catch (\Throwable $e) { | |
Injector::inst()->get(LoggerInterface::class)->error('Something wrong with Embed!', $e); | |
Injector::inst()->get(LoggerInterface::class)->error($e->getMessage()); | |
Injector::inst()->get(LoggerInterface::class)->error($e->getTraceAsString()); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment