-
-
Notifications
You must be signed in to change notification settings - Fork 643
[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
Open
edalzell
wants to merge
17
commits into
statamic:6.x
Choose a base branch
from
edalzell:feature/video-value-object
base: 6.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
aed6a73
Augment video fields to a value object carrying the provider
edalzell c44253e
Accept null in the video embed url helpers
edalzell 862f032
Base video embed truthiness on the value, not the provider
edalzell f17b781
Pass unsupported video urls through the embed url modifier
edalzell 3745c70
Keep trackable embed urls trackable for augmented videos
edalzell 9240993
Treat video files as supported but not embeddable
edalzell 83df089
Detect oembed providers before falling back to video files
edalzell dc360f7
Keep video fields serializing to a string in the api
edalzell 56acf47
Assert augmented video fields behave like the raw value
edalzell cdbcc04
Update video embed tests for the corrected contract
edalzell 81c399a
Measure the string length of stringable value objects
edalzell 41f93c0
Update src/Modifiers/CoreModifiers.php
edalzell e42923c
Import ArrayableString in CoreModifiers
edalzell 204df27
Merge branch '6.x' into feature/video-value-object
edalzell 5821b28
Merge branch '6.x' into feature/video-value-object
edalzell 720ac1b
Make Embed extend ArrayableString
edalzell 25c6864
Drop redundant Embed length guard
edalzell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| <?php | ||
|
|
||
| namespace Statamic\Fieldtypes\Video; | ||
|
|
||
| use Illuminate\Support\Str; | ||
| use Statamic\Fields\ArrayableString; | ||
| use Statamic\Support\FileTypes; | ||
|
|
||
| class Embed extends ArrayableString | ||
| { | ||
| 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, | ||
| ) { | ||
| parent::__construct($url); | ||
| } | ||
|
|
||
| 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 __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; | ||
| } | ||
|
|
||
| /** | ||
| * 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; | ||
| } | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.