From 2e57c59a8e0a434f232f1ac4d51a629f16479480 Mon Sep 17 00:00:00 2001 From: Marty Friedel Date: Wed, 18 Dec 2024 02:22:19 +1030 Subject: [PATCH] [5.x] Update `embed_url` and `trackable_embed_url` modifiers to be valid with additional query strings (#11265) --- src/Modifiers/CoreModifiers.php | 8 +++++++ tests/Modifiers/EmbedUrlTest.php | 29 +++++++++++++++++++++++ tests/Modifiers/TrackableEmbedUrlTest.php | 28 ++++++++++++++++++++++ 3 files changed, 65 insertions(+) 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..6061c170f3 100644 --- a/tests/Modifiers/EmbedUrlTest.php +++ b/tests/Modifiers/EmbedUrlTest.php @@ -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(); 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();