Skip to content

Commit

Permalink
Merge pull request #16 from alleyinteractive/feature/makeable
Browse files Browse the repository at this point in the history
Add support for macros
  • Loading branch information
srtfisher authored Feb 28, 2024
2 parents 0356cf2 + 27ca8ae commit 4337f20
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 3 deletions.
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@

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

## 1.3.0

- Adds macro support to the converter, allowing for custom tags to be added to the
converter.
- Adds support for `hr` tags.
- Adds support for embeds.

## 1.2.0

- Enhancement: Adds support for non-standard ports in sanitized URLs (e.g.,
8080).
- Enhancement: Added new filter called `wp_block_converter_sanitized_image_url`
which allows the image URL for converted images to be filtered before being
applied.
- Bugfix: Changed the behavior of the `create_or_get_attachment` function to
throw an exception instead of returning a `WP_Error`, which wasn't being
handled previously and would result in a crash if a non-string value was
returned by `upload_image`.
- Bugfix: Ensure `upload_image` returns the image URL.

## 1.1.0

- Expands PHP version support from just 8.0 to include 8.1 and 8.2

## 1.0.0 - 2022-12-19

- Initial release
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,42 @@ add_filter( 'wp_block_converter_document_html', function( string $blocks, \DOMNo
}, 10, 2 );
```

### Extending the Converter with Macros

You can extend the converter with macros to add custom tags that are not yet
supported by the converter.

```php
use Alley\WP\Block_Converter\Block_Converter;
use Alley\WP\Block_Converter\Block;

Block_Converter::macro( 'special-tag', function ( \DOMNode $node ) {
return new Block( 'core/paragraph', [], $node->textContent );
} );

// You can also use the raw HTML with a helper method from Block Converter:
Block_Converter::macro( 'special-tag', function ( \DOMNode $node ) {
return new Block( 'core/paragraph', [], Block_Converter::get_node_html( $node ) );
} );
```

Macros can also completely override the default behavior of the converter. This
is useful when you need to make one-off changes to the way the converter works
for a specific tag.

```php
use Alley\WP\Block_Converter\Block_Converter;
use Alley\WP\Block_Converter\Block;

Block_Converter::macro( 'p', function ( \DOMNode $node ) {
if (special_condition()) {
return new Block( 'core/paragraph', [ 'attribute' => 123 ], 'This is a paragraph' );
}

return Block_Converter::p( $node );
} );
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
],
"require": {
"php": "^8.0|^8.1|^8.2",
"alleyinteractive/composer-wordpress-autoloader": "^1.0"
"alleyinteractive/composer-wordpress-autoloader": "^1.0",
"mantle-framework/support": "^0.12"
},
"require-dev": {
"alleyinteractive/alley-coding-standards": "^2.0",
"mantle-framework/testkit": "^0.12",
"nunomaduro/collision": "^6.0"
"mantle-framework/testkit": "^0.12"
},
"config": {
"allow-plugins": {
Expand Down
9 changes: 9 additions & 0 deletions src/class-block-converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@
use DOMElement;
use DOMNode;
use Exception;
use Mantle\Support\Traits\Macroable;

/**
* Converts a DOMDocument to Gutenberg block HTML.
*/
class Block_Converter {
use Macroable {
__call as macro_call;
}

/**
* Setup the class.
*
Expand Down Expand Up @@ -89,6 +94,10 @@ public function convert(): string {
* @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] ),
Expand Down
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
* Visit {@see https://mantle.alley.co/testing/test-framework.html} to learn more.
*/
\Mantle\Testing\manager()
->with_sqlite()
->maybe_rsync_plugin()
->install();
17 changes: 17 additions & 0 deletions tests/feature/test-block-converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Alley\WP\Block_Converter\Block;
use Alley\WP\Block_Converter\Block_Converter;
use Alley\WP\Block_Converter\Tests\Test_Case;
use DOMNode;

/**
* Test case for Block Block_Converter Module.
Expand Down Expand Up @@ -157,4 +158,20 @@ public function test_convert_with_filter_override_entire_content() {

$this->assertSame( 'Override', $block );
}

public function test_macroable() {
Block_Converter::macro(
'special-tag',
function (DOMNode $node) {
return new Block( 'paragraph', [ 'attribute' => '123' ], Block_Converter::get_node_html( $node ) );
},
);

$block = ( new Block_Converter( '<special-tag>content here</special-tag>' ) )->convert();

$this->assertEquals(
'<!-- wp:paragraph {"attribute":"123"} --><special-tag>content here</special-tag><!-- /wp:paragraph -->',
$block,
);
}
}

0 comments on commit 4337f20

Please sign in to comment.