Skip to content
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
34 changes: 28 additions & 6 deletions event/formatter_listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,7 @@ public function modify_tag_templates($event)
{
try
{
$tag = $event['configurator']->tags['YOUTUBE'];
$tag->template = str_replace('www.youtube.com', 'www.youtube-nocookie.com', $tag->template);
if (!$this->sites->is_phpbb4())
{
$tag->template = str_replace(' allowfullscreen', ' referrerpolicy="origin" allowfullscreen', $tag->template);
}
$this->configure_youtube($event['configurator']->tags['YOUTUBE']);
}
catch (\RuntimeException $e)
{
Expand Down Expand Up @@ -159,4 +154,31 @@ protected function get_site_ids()

return $site_ids ? json_decode($site_ids, true) : [];
}

/**
* Apply YouTube privacy, compatibility, and Shorts rendering changes.
*
* @param s9e\TextFormatter\Configurator\Items\Tag $tag Tag to configure
* @return void
*/
public function configure_youtube($tag)
{
$template = str_replace('www.youtube.com', 'www.youtube-nocookie.com', (string) $tag->template);
if (!$this->sites->is_phpbb4())
{
$template = str_replace(' allowfullscreen', ' referrerpolicy="origin" allowfullscreen', $template);
}

$shorts_template = str_replace(
['max-width:640px', 'padding-bottom:56.25%'],
['max-width:360px', 'padding-bottom:177.777778%'],
$template
);

$tag->template =
'<xsl:choose>' .
'<xsl:when test="contains(., \'/shorts/\')">' . $shorts_template . '</xsl:when>' .
'<xsl:otherwise>' . $template . '</xsl:otherwise>' .
'</xsl:choose>';
}
}
3 changes: 3 additions & 0 deletions tests/event/listener_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ public function test_youtube_template_uses_phpbb3_compatibility_fixes()
$template = $configurator->tags['YOUTUBE']->template;
$this->assertStringContainsString('www.youtube-nocookie.com', $template);
$this->assertStringContainsString('referrerpolicy="origin"', $template);
$this->assertStringContainsString('contains(., \'/shorts/\')', $template);
$this->assertStringContainsString('max-width:360px', $template);
$this->assertStringContainsString('padding-bottom:177.777778%', $template);
}

public function test_upstream_collection_is_skipped_on_phpbb4()
Expand Down
28 changes: 26 additions & 2 deletions tests/functional/media_embed_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
class media_embed_test extends \phpbb_functional_test_case
{
protected $youtubeId = 'PHzShhtkzEk';
protected $youtubeShortsId = 'xer3HqUWLkw';
protected $ok_ru_Id = '549000643961';

protected static function setup_extensions()
Expand All @@ -36,7 +37,7 @@ public function test_posting_media_bbcode()

$post = $this->create_topic($forum_id, 'Media Embed Test Topic 1', "[media]https://youtu.be/$this->youtubeId[/media]");
$crawler = self::request('GET', "viewtopic.php?t={$post['topic_id']}&sid=$this->sid");
self::assertStringContainsString("//www.youtube-nocookie.com/embed/$this->youtubeId", $crawler->filter("#post_content{$post['topic_id']} iframe")->attr('src'));
self::assertStringContainsString("//www.youtube-nocookie.com/embed/$this->youtubeId", $crawler->filter("#post_content{$post['post_id']} iframe")->attr('src'));
}

public function test_posting_custom_site()
Expand All @@ -50,7 +51,30 @@ public function test_posting_custom_site()

$post = $this->create_topic(2, 'Media Embed Custom Site Test Topic 1', "[media]https://ok.ru/video/$this->ok_ru_Id[/media]");
$crawler = self::request('GET', "viewtopic.php?t={$post['topic_id']}&sid=$this->sid");
self::assertStringContainsString("//ok.ru/videoembed/$this->ok_ru_Id", $crawler->filter("#post_content{$post['topic_id']} iframe")->attr('src'));
self::assertStringContainsString("//ok.ru/videoembed/$this->ok_ru_Id", $crawler->filter("#post_content{$post['post_id']} iframe")->attr('src'));
}

public function test_youtube_shorts_template()
{
$this->login();

$forum_id = 2;

$post = $this->create_topic(
$forum_id,
'YouTube Shorts vertical rendering test',
"https://youtube.com/shorts/$this->youtubeShortsId\nhttps://youtu.be/$this->youtubeId"
);
$crawler = self::request('GET', "viewtopic.php?t={$post['topic_id']}&sid=$this->sid");
$selector = '#post_content' . $post['post_id'] . ' span[data-s9e-mediaembed="youtube"]';
$embeds = $crawler->filter($selector);
$wrappers = $crawler->filter($selector . ' > span');
self::assertCount(2, $embeds);
self::assertCount(2, $wrappers);
self::assertStringContainsString('max-width:360px', $embeds->eq(0)->attr('style'));
self::assertStringContainsString('padding-bottom:177.777778%', $wrappers->eq(0)->attr('style'));
self::assertStringContainsString('max-width:640px', $embeds->eq(1)->attr('style'));
self::assertStringContainsString('padding-bottom:56.25%', $wrappers->eq(1)->attr('style'));
}

public function signatures_data()
Expand Down