diff --git a/CHANGELOG.md b/CHANGELOG.md index b4a3dbb..f877a0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,12 @@ All notable changes to `WP Block Converter` will be documented in this file. +## 1.5.1 + +### Fixed + +- Fixed bug where macros can be used to modify/replace any tag. + ## 1.5.0 ### Added diff --git a/README.md b/README.md index dd28bd4..dfcbd40 100644 --- a/README.md +++ b/README.md @@ -106,7 +106,7 @@ use Alley\WP\Block_Converter\Block_Converter; use Alley\WP\Block_Converter\Block; Block_Converter::macro( 'p', function ( \DOMNode $node ) { - if (special_condition()) { + if ( special_condition() ) { return new Block( 'core/paragraph', [ 'attribute' => 123 ], 'This is a paragraph' ); } diff --git a/src/class-block-converter.php b/src/class-block-converter.php index bb2ca84..6c4fdec 100644 --- a/src/class-block-converter.php +++ b/src/class-block-converter.php @@ -78,7 +78,7 @@ public function convert(): string { * * @since 1.0.0 * - * @param string $html HTML converted into Gutenberg blocks. + * @param string $html HTML converted into Gutenberg blocks. * @param DOMNodeList $content The original DOMNodeList. */ $html = trim( (string) apply_filters( 'wp_block_converter_document_html', $html, $content ) ); @@ -94,20 +94,37 @@ public function convert(): string { * @param DOMNode $node The node to convert. * @return Block|null */ - protected function convert_node( DOMNode $node ): ?Block { + public function convert_node( DOMNode $node ): ?Block { if ( '#text' === $node->nodeName ) { return null; } + if ( static::has_macro( $node->nodeName ) ) { + $block = static::macro_call( $node->nodeName, [ $node ] ); + } else { + $block = match ( strtolower( $node->nodeName ) ) { + 'ul' => $this->ul( $node ), + 'ol' => $this->ol( $node ), + 'img' => $this->img( $node ), + 'blockquote' => $this->blockquote( $node ), + 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' => $this->h( $node ), + 'p', 'a', 'abbr', 'b', 'code', 'em', 'i', 'strong', 'sub', 'sup', 'span', 'u' => $this->p( $node ), + 'figure' => $this->figure( $node ), + 'br', 'cite', 'source' => null, + 'hr' => $this->separator(), + default => $this->html( $node ), + }; + } + /** * Hook to allow output customizations. * * @since 1.0.0 * * @param Block|null $block The generated block object. - * @param DOMNode $node The node being converted. + * @param DOMNode $node The node being converted. */ - $block = apply_filters( 'wp_block_converter_block', $this->{$node->nodeName}( $node ), $node ); + $block = apply_filters( 'wp_block_converter_block', $block, $node ); if ( ! $block || ! $block instanceof Block ) { return null; @@ -180,32 +197,6 @@ protected function sideload_child_images( DOMNode $node ): void { } } - /** - * Magic function to call parsers for specific HTML tags. - * - * @param string $name The tag name. - * @param array $arguments The DOMNode. - * @return Block|null - */ - public function __call( $name, $arguments ): ?Block { - if ( static::has_macro( $name ) ) { - return static::macro_call( $name, $arguments ); - } - - return match ( $name ) { - 'ul' => $this->ul( $arguments[0] ), - 'ol' => $this->ol( $arguments[0] ), - 'img' => $this->img( $arguments[0] ), - 'blockquote' => $this->blockquote( $arguments[0] ), - 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' => $this->h( $arguments[0] ), - 'p', 'a', 'abbr', 'b', 'code', 'em', 'i', 'strong', 'sub', 'sup', 'span', 'u' => $this->p( $arguments[0] ), - 'figure' => $this->figure( $arguments[0] ), - 'br', 'cite', 'source' => null, - 'hr' => $this->separator(), - default => $this->html( $arguments[0] ), - }; - } - /** * Convert the children of a node to blocks. * diff --git a/tests/feature/BlockConverterTest.php b/tests/feature/BlockConverterTest.php index d3b38e5..85f0a60 100644 --- a/tests/feature/BlockConverterTest.php +++ b/tests/feature/BlockConverterTest.php @@ -16,6 +16,7 @@ use Mantle\Testing\Concerns\Refresh_Database; use PHPUnit\Framework\Attributes\DataProvider; +use function Mantle\Support\Helpers\collect; use function Mantle\Testing\mock_http_response; /** @@ -159,6 +160,7 @@ public function test_convert_with_empty_paragraphs_of_arbitrary_length_to_block( public function test_convert_with_filter_override_single_tag() { $this->expectApplied( 'wp_block_converter_document_html' )->once(); + $this->expectApplied( 'wp_block_converter_block' )->once()->andReturnInstanceOf( Block::class ); $html = '
Content to migrate