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

Issue-19: embeds must have a line break before and after the url #20

Merged
merged 4 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `WP Block Converter` will be documented in this file.

## 1.3.2

- Preserve new lines in embed blocks. They are required for proper front end rendering.

## 1.3.1

- Fixes embeds of x.com urls (instead of twitter.com)
Expand Down
10 changes: 8 additions & 2 deletions src/class-block-converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function convert(): string {
}

// Merge the block into the HTML collection.

$html[] = $this->minify_block( (string) $tag_block );
}

Expand Down Expand Up @@ -446,8 +447,13 @@ public static function get_node_tag_from_html( $html, $tag = 'body' ) {
* @return string
*/
protected function minify_block( $block ) {
if ( preg_match( '/(\s){2,}/s', $block ) === 1 ) {
return preg_replace( '/(\s){2,}/s', '', $block );
if ( \str_contains( $block, 'wp-block-embed' ) ) {
attackant marked this conversation as resolved.
Show resolved Hide resolved
$pattern = '/(\h){2,}/s';
} else {
$pattern = '/(\s){2,}/s';
}
if ( preg_match( $pattern, $block ) === 1 ) {
return preg_replace( $pattern, '', $block );
}

return $block;
Expand Down
24 changes: 18 additions & 6 deletions tests/feature/test-block-converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ public function test_youtube_url_to_embed() {

$this->assertNotEmpty( $block );
$this->assertSame(
'<!-- wp:embed {"url":"https://www.youtube.com/watch?v=dQw4w9WgXcQ","type":"video","providerNameSlug":"youtube","responsive":true,"className":"wp-embed-aspect-16-9 wp-has-aspect-ratio"} --><figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">https://www.youtube.com/watch?v=dQw4w9WgXcQ</div></figure><!-- /wp:embed -->',
'<!-- wp:embed {"url":"https://www.youtube.com/watch?v=dQw4w9WgXcQ","type":"video","providerNameSlug":"youtube","responsive":true,"className":"wp-embed-aspect-16-9 wp-has-aspect-ratio"} --><figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
https://www.youtube.com/watch?v=dQw4w9WgXcQ
</div></figure><!-- /wp:embed -->',
$block,
);
}
Expand All @@ -184,7 +186,9 @@ public function test_twitter_url_to_embed() {

$this->assertNotEmpty( $block );
$this->assertSame(
'<!-- wp:embed {"url":"https://twitter.com/alleyco/status/1679189879086018562","type":"rich","providerNameSlug":"twitter","responsive":true} --><figure class="wp-block-embed is-type-rich is-provider-twitter wp-block-embed-twitter"><div class="wp-block-embed__wrapper">https://twitter.com/alleyco/status/1679189879086018562</div></figure><!-- /wp:embed -->',
'<!-- wp:embed {"url":"https://twitter.com/alleyco/status/1679189879086018562","type":"rich","providerNameSlug":"twitter","responsive":true} --><figure class="wp-block-embed is-type-rich is-provider-twitter wp-block-embed-twitter"><div class="wp-block-embed__wrapper">
https://twitter.com/alleyco/status/1679189879086018562
</div></figure><!-- /wp:embed -->',
$block,
);
}
Expand All @@ -199,7 +203,9 @@ public function test_x_url_to_embed() {

$this->assertNotEmpty( $block );
$this->assertSame(
'<!-- wp:embed {"url":"https://twitter.com/alleyco/status/1679189879086018562","type":"rich","providerNameSlug":"twitter","responsive":true} --><figure class="wp-block-embed is-type-rich is-provider-twitter wp-block-embed-twitter"><div class="wp-block-embed__wrapper">https://twitter.com/alleyco/status/1679189879086018562</div></figure><!-- /wp:embed -->',
'<!-- wp:embed {"url":"https://twitter.com/alleyco/status/1679189879086018562","type":"rich","providerNameSlug":"twitter","responsive":true} --><figure class="wp-block-embed is-type-rich is-provider-twitter wp-block-embed-twitter"><div class="wp-block-embed__wrapper">
https://twitter.com/alleyco/status/1679189879086018562
</div></figure><!-- /wp:embed -->',
$block,
);
}
Expand All @@ -214,7 +220,9 @@ public function test_linked_x_url_to_embed() {

$this->assertNotEmpty( $block );
$this->assertSame(
'<!-- wp:embed {"url":"https://twitter.com/alleyco/status/1679189879086018562","type":"rich","providerNameSlug":"twitter","responsive":true} --><figure class="wp-block-embed is-type-rich is-provider-twitter wp-block-embed-twitter"><div class="wp-block-embed__wrapper">https://twitter.com/alleyco/status/1679189879086018562</div></figure><!-- /wp:embed -->',
'<!-- wp:embed {"url":"https://twitter.com/alleyco/status/1679189879086018562","type":"rich","providerNameSlug":"twitter","responsive":true} --><figure class="wp-block-embed is-type-rich is-provider-twitter wp-block-embed-twitter"><div class="wp-block-embed__wrapper">
https://twitter.com/alleyco/status/1679189879086018562
</div></figure><!-- /wp:embed -->',
$block,
);
}
Expand All @@ -225,7 +233,9 @@ public function test_instagram_url_to_embed() {

$this->assertNotEmpty( $block );
$this->assertSame(
'<!-- wp:embed {"url":"https://www.instagram.com/p/CSpmSvAphdf/","type":"rich","providerNameSlug":"instagram","responsive":true} --><figure class="wp-block-embed is-type-rich is-provider-instagram wp-block-embed-instagram"><div class="wp-block-embed__wrapper">https://www.instagram.com/p/CSpmSvAphdf/</div></figure><!-- /wp:embed -->',
'<!-- wp:embed {"url":"https://www.instagram.com/p/CSpmSvAphdf/","type":"rich","providerNameSlug":"instagram","responsive":true} --><figure class="wp-block-embed is-type-rich is-provider-instagram wp-block-embed-instagram"><div class="wp-block-embed__wrapper">
https://www.instagram.com/p/CSpmSvAphdf/
</div></figure><!-- /wp:embed -->',
$block,
);
}
Expand All @@ -236,7 +246,9 @@ public function test_facebook_url_to_embed() {

$this->assertNotEmpty( $block );
$this->assertSame(
'<!-- wp:embed {"url":"https://www.facebook.com/sesametheopossum/posts/1329405240877426","type":"rich","providerNameSlug":"embed-handler","responsive":true,"previewable":false} --><figure class="wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler"><div class="wp-block-embed__wrapper">https://www.facebook.com/sesametheopossum/posts/1329405240877426</div></figure><!-- /wp:embed -->',
'<!-- wp:embed {"url":"https://www.facebook.com/sesametheopossum/posts/1329405240877426","type":"rich","providerNameSlug":"embed-handler","responsive":true,"previewable":false} --><figure class="wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler"><div class="wp-block-embed__wrapper">
https://www.facebook.com/sesametheopossum/posts/1329405240877426
</div></figure><!-- /wp:embed -->',
$block,
);
}
Expand Down