Skip to content

Commit

Permalink
Make the children conversion reusable
Browse files Browse the repository at this point in the history
  • Loading branch information
srtfisher committed Jul 25, 2024
1 parent 469f349 commit e0e2b34
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions src/class-block-converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,34 @@ public function __call( $name, $arguments ): ?Block {
};
}

/**
* Convert the children of a node to blocks.
*
* @param DOMNode $node The node.
* @return string The children as blocks.
*/
public function convert_with_children( DOMNode $node ): string {
$children = '';

// Recursively convert the children of the blockquote to blocks.
foreach ( $node->childNodes as $child ) {
$child_block = $this->{$child->nodeName}( $child );

if ( ! empty( $child_block ) ) {
$children .= $this->minify_block( (string) $child_block );
}
}

$node->nodeValue = '__CHILDREN__';

$content = static::get_node_html( $node );

// Replace the placeholder with the children.
$content = str_replace( '__CHILDREN__', $children, $content );

return $content;
}

/**
* Magic function to convert to a string.
*/
Expand Down Expand Up @@ -153,27 +181,7 @@ protected function blockquote( DOMNode $node ): ?Block {
$node->setAttribute( 'class', 'wp-block-quote' );
}

$children = '';

// Recursively convert the children of the blockquote to blocks.
foreach ( $node->childNodes as $child ) {
$child_block = $this->{$child->nodeName}( $child );

if ( ! empty( $child_block ) ) {
$children .= $this->minify_block( (string) $child_block );
}
}

// Replace the children with a placeholder that will be replaced with
// the children after the blockquote is converted to block.
if ( ! empty( $node->nodeValue ) ) {
$node->nodeValue = '__CHILDREN__';
}

$content = static::get_node_html( $node );

// Replace the closing blockquote tag with the children.
$content = str_replace( '__CHILDREN__', $children, $content );
$content = $this->convert_with_children( $node );

if ( empty( $content ) ) {
return null;
Expand Down

0 comments on commit e0e2b34

Please sign in to comment.