Skip to content

Commit

Permalink
Merge pull request #20 from alleyinteractive/fix/issue-19/embeds-requ…
Browse files Browse the repository at this point in the history
…ire-line-breaks-around-url

Issue-19: embeds must have a line break before and after the url
  • Loading branch information
mogmarsh authored Mar 28, 2024
2 parents c643d2c + 385ba6d commit 2145f4b
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 145 deletions.
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' ) ) {
$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

0 comments on commit 2145f4b

Please sign in to comment.