Skip to content
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

[5.x] Update embed_url and trackable_embed_url modifiers to be valid with additional query strings #11265

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Modifiers/CoreModifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3106,6 +3106,10 @@ public function embedUrl($url)
$url = str_replace('//youtube-nocookie.com', '//www.youtube-nocookie.com', $url);
}

if (Str::contains($url, '&') && ! Str::contains($url, '?')) {
$url = Str::replaceFirst('&', '?', $url);
}

return $url;
}

Expand Down Expand Up @@ -3135,6 +3139,10 @@ public function trackableEmbedUrl($url)
$url = str_replace('watch?v=', 'embed/', $url);
}

if (Str::contains($url, '&') && ! Str::contains($url, '?')) {
$url = Str::replaceFirst('&', '?', $url);
}

return $url;
}

Expand Down
29 changes: 29 additions & 0 deletions tests/Modifiers/EmbedUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,35 @@ public function it_transforms_youtube_urls()
);
}

#[Test]
public function it_ensures_url_with_query_parameters_are_valid()
{
$embedUrl = 'https://www.youtube-nocookie.com/embed/s72r_wu_NVY?pp=player_params';

$this->assertEquals(
$embedUrl,
$this->embed('https://www.youtube.com/watch?v=s72r_wu_NVY&pp=player_params'),
'It transforms the youtube video link with additional query string params'
);
$this->assertEquals(
$embedUrl,
$this->embed('https://youtu.be/s72r_wu_NVY?pp=player_params'),
'It transforms shortened youtube video sharing links with additional query string params'
);

$this->assertEquals(
'https://www.youtube-nocookie.com/embed/s72r_wu_NVY?start=559&pp=player_params',
$this->embed('https://youtu.be/s72r_wu_NVY?t=559&pp=player_params'),
'It transforms the start time parameter of shortened sharing links with additional query string params'
);

$this->assertEquals(
'https://www.youtube-nocookie.com/embed/hyJ7CBs_2RQ?start=2&pp=player_params',
$this->embed('https://www.youtube.com/watch?v=hyJ7CBs_2RQ&t=2&pp=player_params'),
'It transforms the start time parameter of full youtube links with additional query string params'
);
}

public function embed($url)
{
return Modify::value($url)->embedUrl()->fetch();
Expand Down
28 changes: 28 additions & 0 deletions tests/Modifiers/TrackableEmbedUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@ public function it_transforms_youtube_urls()
);
}

public function it_ensures_url_with_query_parameters_are_valid()
{
$embedUrl = 'https://www.youtube-nocookie.com/embed/s72r_wu_NVY?pp=player_params';

$this->assertEquals(
$embedUrl,
$this->embed('https://www.youtube.com/watch?v=s72r_wu_NVY&pp=player_params'),
'It transforms the youtube video link with additional query string params'
);
$this->assertEquals(
$embedUrl,
$this->embed('https://youtu.be/s72r_wu_NVY?pp=player_params'),
'It transforms shortened youtube video sharing links with additional query string params'
);

$this->assertEquals(
'https://www.youtube-nocookie.com/embed/s72r_wu_NVY?start=559&pp=player_params',
$this->embed('https://youtu.be/s72r_wu_NVY?t=559&pp=player_params'),
'It transforms the start time parameter of shortened sharing links with additional query string params'
);

$this->assertEquals(
'https://www.youtube-nocookie.com/embed/hyJ7CBs_2RQ?start=2&pp=player_params',
$this->embed('https://www.youtube.com/watch?v=hyJ7CBs_2RQ&t=2&pp=player_params'),
'It transforms the start time parameter of full youtube links with additional query string params'
);
}

public function embed($url)
{
return Modify::value($url)->trackableEmbedUrl()->fetch();
Expand Down
Loading