-
-
Notifications
You must be signed in to change notification settings - Fork 641
[6.x] Augment video fields to a value object carrying the provider #15458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 6.x
Are you sure you want to change the base?
Changes from all commits
aed6a73
c44253e
862f032
f17b781
3745c70
9240993
83df089
dc360f7
56acf47
cdbcc04
81c399a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,222 @@ | ||
| <?php | ||
|
|
||
| namespace Statamic\Fieldtypes\Video; | ||
|
|
||
| use ArrayAccess; | ||
| use Illuminate\Contracts\Support\Arrayable; | ||
| use Illuminate\Support\Str; | ||
| use JsonSerializable; | ||
| use Statamic\Contracts\Support\Boolable; | ||
| use Statamic\Support\FileTypes; | ||
|
|
||
| class Embed implements Arrayable, ArrayAccess, Boolable, JsonSerializable | ||
| { | ||
| const CLOUDFLARE = 'cloudflare'; | ||
| const CLOUDFLARE_EMBED_URL = 'https://iframe.cloudflarestream.com/'; | ||
| const CLOUDFLARE_ID_PATTERN = '/^[a-zA-Z0-9]+$/'; | ||
| const CLOUDFLARE_PREFIX = 'cloudflare:'; | ||
| const FILE = 'file'; | ||
| const UNSUPPORTED = 'unsupported'; | ||
| const VIMEO = 'vimeo'; | ||
| const YOUTUBE = 'youtube'; | ||
|
|
||
| public static function fromValue(?string $value): self | ||
| { | ||
| if (blank($value)) { | ||
| return static::unsupported($value); | ||
| } | ||
|
|
||
| if (Str::startsWith($value, self::CLOUDFLARE_PREFIX)) { | ||
| $id = Str::after($value, self::CLOUDFLARE_PREFIX); | ||
|
|
||
| return preg_match(self::CLOUDFLARE_ID_PATTERN, $id) | ||
| ? new self(self::CLOUDFLARE, $value, self::CLOUDFLARE_EMBED_URL.$id, $id) | ||
| : static::unsupported($value); | ||
| } | ||
|
|
||
| if ($provider = static::oembedProvider($value)) { | ||
| return new self($provider, $value, static::embedUrl($value)); | ||
| } | ||
|
|
||
| if (static::isVideoFile($value)) { | ||
| return new self(self::FILE, $value, $value); | ||
| } | ||
|
|
||
| return static::unsupported($value); | ||
| } | ||
|
|
||
| public static function unsupported(?string $value = null): self | ||
| { | ||
| return new self(self::UNSUPPORTED, $value); | ||
| } | ||
|
|
||
| public function __construct( | ||
| public readonly string $provider, | ||
| public readonly ?string $url = null, | ||
| public readonly ?string $embedUrl = null, | ||
| public readonly ?string $id = null, | ||
| ) { | ||
| } | ||
|
|
||
| public function isEmbeddable(): bool | ||
| { | ||
| return ! in_array($this->provider, [self::FILE, self::UNSUPPORTED]); | ||
| } | ||
|
|
||
| public function isSupported(): bool | ||
| { | ||
| return $this->provider !== self::UNSUPPORTED; | ||
| } | ||
|
|
||
| public function toArray(): array | ||
| { | ||
| return [ | ||
| 'embed_url' => $this->embedUrl, | ||
| 'id' => $this->id, | ||
| 'provider' => $this->provider, | ||
| 'url' => $this->url, | ||
| ]; | ||
| } | ||
|
|
||
| public function toBool(): bool | ||
| { | ||
| return (bool) $this->url; | ||
| } | ||
|
|
||
| public function __toString(): string | ||
| { | ||
| return (string) $this->url; | ||
| } | ||
|
|
||
| #[\ReturnTypeWillChange] | ||
| public function jsonSerialize() | ||
| { | ||
| return (string) $this; | ||
| } | ||
|
|
||
| #[\ReturnTypeWillChange] | ||
| public function offsetExists(mixed $offset) | ||
| { | ||
| return array_key_exists($offset, $this->toArray()); | ||
| } | ||
|
|
||
| #[\ReturnTypeWillChange] | ||
| public function offsetGet(mixed $offset) | ||
| { | ||
| return $this->toArray()[$offset] ?? null; | ||
| } | ||
|
|
||
| #[\ReturnTypeWillChange] | ||
| public function offsetSet(mixed $offset, mixed $value) | ||
| { | ||
| } | ||
|
|
||
| #[\ReturnTypeWillChange] | ||
| public function offsetUnset(mixed $offset) | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * Turn a link that's direct to a video's page into its embeddable equivalent. | ||
| */ | ||
| public static function embedUrl(?string $url): ?string | ||
| { | ||
| if (blank($url)) { | ||
| return $url; | ||
| } | ||
|
|
||
| if (Str::contains($url, self::VIMEO)) { | ||
| return static::vimeoEmbedUrl($url); | ||
| } | ||
|
|
||
| if (Str::contains($url, 'youtu.be')) { | ||
| $url = str_replace('youtu.be', 'www.youtube.com/embed', $url); | ||
|
|
||
| // Check for start at point and replace it with correct parameter. | ||
| if (Str::contains($url, '?t=')) { | ||
| $url = str_replace('?t=', '?start=', $url); | ||
| } | ||
| } | ||
|
|
||
| if (Str::contains($url, 'youtube.com/watch?v=')) { | ||
| $url = str_replace('watch?v=', 'embed/', $url); | ||
|
|
||
| if (Str::contains($url, '&t=')) { | ||
| $url = str_replace('&t=', '?start=', $url); | ||
| } | ||
| } | ||
|
|
||
| if (Str::contains($url, 'youtube.com/shorts/')) { | ||
| $url = str_replace('shorts/', 'embed/', $url); | ||
| } | ||
|
|
||
| if (Str::contains($url, 'youtube.com')) { | ||
| $url = str_replace('youtube.com', 'youtube-nocookie.com', $url); | ||
| } | ||
|
|
||
| // This avoids SSL issues when using the non-www version | ||
| if (Str::contains($url, '//youtube-nocookie.com')) { | ||
| $url = str_replace('//youtube-nocookie.com', '//www.youtube-nocookie.com', $url); | ||
| } | ||
|
|
||
| if (Str::contains($url, '&') && ! Str::contains($url, '?')) { | ||
| $url = Str::replaceFirst('&', '?', $url); | ||
| } | ||
|
|
||
| return $url; | ||
| } | ||
|
|
||
| public static function isEmbeddableUrl(?string $url): bool | ||
| { | ||
| return filled($url) && Str::contains($url, ['youtu.be', 'youtube', self::VIMEO]); | ||
| } | ||
|
|
||
| protected static function isVideoFile(string $url): bool | ||
| { | ||
| if (blank($path = parse_url($url, PHP_URL_PATH))) { | ||
| return false; | ||
| } | ||
|
|
||
| return in_array(strtolower(pathinfo($path, PATHINFO_EXTENSION)), FileTypes::video()); | ||
| } | ||
|
|
||
| protected static function oembedProvider(string $url): ?string | ||
| { | ||
| if (Str::contains($url, self::VIMEO)) { | ||
| return self::VIMEO; | ||
| } | ||
|
|
||
| if (Str::contains($url, ['youtu.be', 'youtube'])) { | ||
| return self::YOUTUBE; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| // Unlisted vimeo urls are in the form vimeo.com/id/hash, but embeds pass the hash as a get param. | ||
| protected static function vimeoEmbedUrl(string $url): string | ||
| { | ||
| $url = str_replace('/vimeo.com', '/player.vimeo.com/video', $url); | ||
| $hash = ''; | ||
|
|
||
| if (! Str::contains($url, 'progressive_redirect') && Str::substrCount($url, '/') > 4) { | ||
| $hash = Str::afterLast($url, '/'); | ||
| $url = Str::beforeLast($url, '/'); | ||
|
|
||
| if (Str::contains($hash, '?')) { | ||
| $url .= '?'.Str::after($hash, '?'); | ||
| $hash = Str::before($hash, '?'); | ||
| } | ||
| } | ||
|
|
||
| $paramsToAdd = '?dnt=1'; | ||
|
|
||
| if ($hash) { | ||
| $paramsToAdd .= '&h='.$hash; | ||
| } | ||
|
|
||
| return Str::contains($url, '?') | ||
| ? str_replace('?', $paramsToAdd.'&', $url) | ||
| : $url.$paramsToAdd; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -28,13 +28,15 @@ | |||||||||||||||||||||||||
| use Statamic\Fieldtypes\Bard; | ||||||||||||||||||||||||||
| use Statamic\Fieldtypes\Bard\Augmentor; | ||||||||||||||||||||||||||
| use Statamic\Fieldtypes\Link\ArrayableLink; | ||||||||||||||||||||||||||
| use Statamic\Fieldtypes\Video\Embed; | ||||||||||||||||||||||||||
| use Statamic\Statamic; | ||||||||||||||||||||||||||
| use Statamic\Support\Arr; | ||||||||||||||||||||||||||
| use Statamic\Support\Dumper; | ||||||||||||||||||||||||||
| use Statamic\Support\Html; | ||||||||||||||||||||||||||
| use Statamic\Support\Str; | ||||||||||||||||||||||||||
| use Statamic\Support\Traits\ChecksDumpability; | ||||||||||||||||||||||||||
| use Statamic\View\Antlers\Language\Runtime\GlobalRuntimeState; | ||||||||||||||||||||||||||
| use Stringable; | ||||||||||||||||||||||||||
| use Stringy\StaticStringy as Stringy; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| use function Statamic\trans; | ||||||||||||||||||||||||||
|
|
@@ -1560,7 +1562,9 @@ public function length($value) | |||||||||||||||||||||||||
| return $value->count(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if ($value instanceof Arrayable) { | ||||||||||||||||||||||||||
| // Value objects like ArrayableString are both Arrayable and Stringable. | ||||||||||||||||||||||||||
| // They stand in for a string, so measure the string, not the array. | ||||||||||||||||||||||||||
| if ($value instanceof Arrayable && ! $value instanceof Stringable) { | ||||||||||||||||||||||||||
|
Comment on lines
+1565
to
+1567
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment scopes this to value objects, but In Measured on the last of those:
For the value objects the change is right — Narrowing the guard to the value-object family fixes it:
Suggested change
Though that second |
||||||||||||||||||||||||||
| $value = $value->toArray(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
@@ -3199,60 +3203,11 @@ public function yearsAgo($value, $params) | |||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| public function embedUrl($url) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| if (Str::contains($url, 'vimeo')) { | ||||||||||||||||||||||||||
| $url = str_replace('/vimeo.com', '/player.vimeo.com/video', $url); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| [$url, $hash] = $this->handleUnlistedVimeoUrls($url); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| $paramsToAdd = '?dnt=1'; | ||||||||||||||||||||||||||
| if ($hash) { | ||||||||||||||||||||||||||
| $paramsToAdd .= '&h='.$hash; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (Str::contains($url, '?')) { | ||||||||||||||||||||||||||
| $url = str_replace('?', $paramsToAdd.'&', $url); | ||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||
| $url .= $paramsToAdd; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| return $url; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (Str::contains($url, 'youtu.be')) { | ||||||||||||||||||||||||||
| $url = str_replace('youtu.be', 'www.youtube.com/embed', $url); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Check for start at point and replace it with correct parameter. | ||||||||||||||||||||||||||
| if (Str::contains($url, '?t=')) { | ||||||||||||||||||||||||||
| $url = str_replace('?t=', '?start=', $url); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (Str::contains($url, 'youtube.com/watch?v=')) { | ||||||||||||||||||||||||||
| $url = str_replace('watch?v=', 'embed/', $url); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (Str::contains($url, '&t=')) { | ||||||||||||||||||||||||||
| $url = str_replace('&t=', '?start=', $url); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (Str::contains($url, 'youtube.com/shorts/')) { | ||||||||||||||||||||||||||
| $url = str_replace('shorts/', 'embed/', $url); | ||||||||||||||||||||||||||
| if ($url instanceof Embed) { | ||||||||||||||||||||||||||
| return $url->embedUrl ?? $url->url; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (Str::contains($url, 'youtube.com')) { | ||||||||||||||||||||||||||
| $url = str_replace('youtube.com', 'youtube-nocookie.com', $url); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // This avoids SSL issues when using the non-www version | ||||||||||||||||||||||||||
| if (Str::contains($url, '//youtube-nocookie.com')) { | ||||||||||||||||||||||||||
| $url = str_replace('//youtube-nocookie.com', '//www.youtube-nocookie.com', $url); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (Str::contains($url, '&') && ! Str::contains($url, '?')) { | ||||||||||||||||||||||||||
| $url = Str::replaceFirst('&', '?', $url); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| return $url; | ||||||||||||||||||||||||||
| return Embed::embedUrl($url); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
|
|
@@ -3264,6 +3219,14 @@ public function embedUrl($url) | |||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| public function trackableEmbedUrl($url) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| if ($url instanceof Embed) { | ||||||||||||||||||||||||||
| $url = $url->url; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (blank($url)) { | ||||||||||||||||||||||||||
| return $url; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
edalzell marked this conversation as resolved.
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (Str::contains($url, 'vimeo')) { | ||||||||||||||||||||||||||
| return str_replace('/vimeo.com', '/player.vimeo.com/video', $url); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
@@ -3296,7 +3259,11 @@ public function trackableEmbedUrl($url) | |||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| public function isEmbeddable($url) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| return Str::contains($url, ['youtu.be', 'youtube', 'vimeo']); | ||||||||||||||||||||||||||
| if ($url instanceof Embed) { | ||||||||||||||||||||||||||
| return $url->isEmbeddable(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
edalzell marked this conversation as resolved.
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| return Embed::isEmbeddableUrl($url); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
|
|
@@ -3381,24 +3348,6 @@ private function getFromContext($context, $params, $key = 0) | |||||||||||||||||||||||||
| Arr::get($context, $params[$key], $params[$key]); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // unlisted vimeo urls are in the form vimeo.com/id/hash, but embeds pass the hash as a get param | ||||||||||||||||||||||||||
| private function handleUnlistedVimeoUrls($url) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| $hash = ''; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (! Str::contains($url, 'progressive_redirect') && Str::substrCount($url, '/') > 4) { | ||||||||||||||||||||||||||
| $hash = Str::afterLast($url, '/'); | ||||||||||||||||||||||||||
| $url = Str::beforeLast($url, '/'); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (Str::contains($hash, '?')) { | ||||||||||||||||||||||||||
| $url .= '?'.Str::after($hash, '?'); | ||||||||||||||||||||||||||
| $hash = Str::before($hash, '?'); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| return [$url, $hash]; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| private function dumpingAllowed(array $params): bool | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| return $this->traitDumpingAllowed() || (Arr::get($params, 0) === 'force'); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.