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

Use fixed-height layout for amp-tiktok and add height estimation #6586

Merged
merged 2 commits into from
Aug 31, 2021
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
22 changes: 19 additions & 3 deletions includes/embeds/class-amp-tiktok-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,29 @@ protected function make_embed_amp_compatible( Element $blockquote ) {

$this->remove_embed_script( $blockquote );

// Initial height of video (most of them anyway).
$height = 575;

// Add the height of the metadata card with the CTA, username, and audio source.
$height += 118;

// Estimate the lines of text in the paragraph description (150-character limit).
$p = $blockquote->getElementsByTagName( Tag::P )->item( 0 );
if ( $p instanceof Element ) {
$height += 8; // Top margin.

// Add height for the lines of text, where there are approx. 39 chars fit on
// a line, and a line's height is 18px.
$height += ceil( strlen( trim( $p->textContent ) ) / 39 ) * 18;
}
Comment on lines +67 to +81
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite a clever way of determining the height!


$amp_tiktok = AMP_DOM_Utils::create_node(
Document::fromNode( $dom ),
Extension::TIKTOK,
[
Attribute::LAYOUT => Layout::RESPONSIVE,
Attribute::HEIGHT => 575,
Attribute::WIDTH => 325,
Attribute::LAYOUT => Layout::FIXED_HEIGHT,
Attribute::HEIGHT => $height,
Attribute::WIDTH => 'auto',
Attribute::DATA_SRC => $video_url,
]
);
Expand Down
6 changes: 3 additions & 3 deletions tests/php/test-class-amp-tiktok-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function get_conversion_data() {
'https://www.tiktok.com/@scout2015/video/6718335390845095173' . PHP_EOL,

'
<amp-tiktok layout="responsive" height="575" width="325" data-src="https://www.tiktok.com/@scout2015/video/6718335390845095173">
<amp-tiktok layout="fixed-height" height="755" width="auto" data-src="https://www.tiktok.com/@scout2015/video/6718335390845095173">
<blockquote class="tiktok-embed" cite="https://www.tiktok.com/@scout2015/video/6718335390845095173" data-video-id="6718335390845095173" placeholder>
<section> <a target="_blank" title="@scout2015" href="https://www.tiktok.com/@scout2015">@scout2015</a>
<p>Scramble up ur name &amp; I’ll try to guess it😍❤️ <a title="foryoupage" target="_blank" href="https://www.tiktok.com/tag/foryoupage">#foryoupage</a> <a title="PetsOfTikTok" target="_blank" href="https://www.tiktok.com/tag/PetsOfTikTok">#petsoftiktok</a> <a title="aesthetic" target="_blank" href="https://www.tiktok.com/tag/aesthetic">#aesthetic</a></p>
Expand All @@ -97,7 +97,7 @@ public function get_conversion_data() {
<blockquote class="tiktok-embed" cite="https://www.tiktok.com/@countingprimes/video/6988237085899574533" data-video-id="6988237085899574533" style="max-width: 605px;min-width: 325px;" > <section> <a target="_blank" title="@countingprimes" href="https://www.tiktok.com/@countingprimes">@countingprimes</a> <p>You can now embed TikTok\'s in AMP</p> <a target="_blank" title="♬ original sound - countingprimes" href="https://www.tiktok.com/music/original-sound-6988236987325057798">♬ original sound - countingprimes</a> </section> </blockquote> <script async src="https://www.tiktok.com/embed.js"></script>
',
'
<amp-tiktok layout="responsive" height="575" width="325" data-src="https://www.tiktok.com/@countingprimes/video/6988237085899574533">
<amp-tiktok layout="fixed-height" height="719" width="auto" data-src="https://www.tiktok.com/@countingprimes/video/6988237085899574533">
<blockquote class="tiktok-embed" cite="https://www.tiktok.com/@countingprimes/video/6988237085899574533" data-video-id="6988237085899574533" placeholder>
<section>
<a target="_blank" title="@countingprimes" href="https://www.tiktok.com/@countingprimes">@countingprimes</a>
Expand All @@ -116,7 +116,7 @@ public function get_conversion_data() {
<!-- /wp:html -->
',
'
<amp-tiktok layout="responsive" height="575" width="325" data-src="https://www.tiktok.com/@countingprimes/video/6988237085899574533">
<amp-tiktok layout="fixed-height" height="719" width="auto" data-src="https://www.tiktok.com/@countingprimes/video/6988237085899574533">
<blockquote class="tiktok-embed" cite="https://www.tiktok.com/@countingprimes/video/6988237085899574533" data-video-id="6988237085899574533" placeholder>
<section>
<a target="_blank" title="@countingprimes" href="https://www.tiktok.com/@countingprimes">@countingprimes</a>
Expand Down