Skip to content

Commit

Permalink
[5.x] Update embed_url and trackable_embed_url modifiers to be va…
Browse files Browse the repository at this point in the history
…lid with additional query strings (#11265)
  • Loading branch information
martyf authored Dec 17, 2024
1 parent 527f828 commit 2e57c59
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
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

0 comments on commit 2e57c59

Please sign in to comment.