diff --git a/src/Modifiers/CoreModifiers.php b/src/Modifiers/CoreModifiers.php index 0d3b1a8d0a..dca01d2c57 100644 --- a/src/Modifiers/CoreModifiers.php +++ b/src/Modifiers/CoreModifiers.php @@ -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; } @@ -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; } diff --git a/tests/Modifiers/EmbedUrlTest.php b/tests/Modifiers/EmbedUrlTest.php index c452a54fc6..5fcea67e59 100644 --- a/tests/Modifiers/EmbedUrlTest.php +++ b/tests/Modifiers/EmbedUrlTest.php @@ -84,7 +84,36 @@ public function it_transforms_youtube_urls() ); } - public function embed($url) + #[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(); } diff --git a/tests/Modifiers/TrackableEmbedUrlTest.php b/tests/Modifiers/TrackableEmbedUrlTest.php index e606651b3f..a87171230f 100644 --- a/tests/Modifiers/TrackableEmbedUrlTest.php +++ b/tests/Modifiers/TrackableEmbedUrlTest.php @@ -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();