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 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

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.
- Fix aspect ratio calculation when height and width are percentages - which fixes TikTok embeds.

## 1.3.1

- Fixes embeds of x.com urls (instead of twitter.com)
Expand Down
12 changes: 9 additions & 3 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 @@ -274,7 +275,7 @@ protected function embed( string $url ): Block {
$data = _wp_oembed_get_object()->get_data( $url, [] );

$aspect_ratio = '';
if ( ! empty( $data->height ) && ! empty( $data->width ) ) {
if ( ! empty( $data->height ) && ! empty( $data->width ) && is_numeric( $data->height ) && is_numeric( $data->width ) ) {
if ( 1.78 === round( $data->width / $data->height, 2 ) ) {
$aspect_ratio = '16-9';
}
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
Loading