diff --git a/CHANGELOG.md b/CHANGELOG.md index 745fa7c..b4a3dbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,24 @@ + # Changelog All notable changes to `WP Block Converter` will be documented in this file. +## 1.5.0 + +### Added + +- Add collection of attachments that are created during the conversion process. + Two methods now exist on the converter to help: + - `get_created_attachment_ids()` - Returns the attachment IDs that were created. + - `assign_parent_to_attachments()` - Assigns a parent post to all attachments. + +### Changed + +- Nested images are now properly sideloaded and converted to image blocks where + appropriate. For example, an image inside a figure tag will now be converted + to an image block. An image within a paragraph tag will be sideloaded but + won't be converted to an image block. + ## 1.4.0 - Drops support for PHP 8.0. diff --git a/README.md b/README.md index eaba953..dd28bd4 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ # WP Block Converter -[](https://github.com/alleyinteractive/wp-block-converter/actions/workflows/coding-standards.yml) -[](https://github.com/alleyinteractive/wp-block-converter/actions/workflows/unit-test.yml) +[](https://github.com/alleyinteractive/wp-block-converter/actions/workflows/all-pr-tests.yml) Convert HTML into Gutenberg Blocks with PHP @@ -13,9 +12,9 @@ You can install the package via Composer: composer require alleyinteractive/wp-block-converter ``` -This project is built to be used in a WordPress environment, so it is -recommended to use this package in a WordPress plugin or theme. Using it in -isolation is not supported at this time. +This project is built to be used in a WordPress environment, so it is recommended to use this +package in a WordPress plugin or theme. Using it in isolation is not supported at this time. This +package does not use any NPM library such as `@wordpress/blocks` to convert HTML to blocks. ## Usage @@ -26,7 +25,7 @@ use Alley\WP\Block_Converter\Block_Converter; $converter = new Block_Converter( '
Some HTML
' ); -$blocks = $converter->convert(); +$blocks = $converter->convert(); // Returns a string of converted blocks. ``` ### Filtering the Blocks @@ -61,6 +60,24 @@ add_filter( 'wp_block_converter_document_html', function( string $blocks, \DOMNo }, 10, 2 ); ``` +### Attachment Parents + +When converting HTML to blocks, you may need to attach the images that were +sideloaded to a post parent. After the HTML is converted to blocks, you can get +the attachment IDs that were created or simply attach them to a post. + +```php +$converter = new Block_Converter( 'Some HTML
","thumbnail_width":576,"thumbnail_height":1024,"thumbnail_url":"https://p19-pu-sign-useast8.tiktokcdn-us.com/obj/tos-useast5-p-0068-tx/afac3ae6ea3343c890e12e3cbbca1218_1711003872?lk3s=b59d6b55&nonce=81617&refresh_token=bf81ce66fb4d648cbd499791f37a6354&x-expires=1722110400&x-signature=tpTiBYwvSXjjAEgNRU2F%2BUAz7jo%3D&shp=b59d6b55&shcp=-","provider_url":"https://www.tiktok.com","provider_name":"TikTok","author_unique_id":"atribecalledval","embed_product_id":"7348705314746699054","embed_type":"video"}' ), ] ); + + // Delete all uploaded files between tests. + $dir = wp_upload_dir(); + + shell_exec( "rm -rf {$dir['path']}/*" ); } #[DataProvider( 'converter_data_provider' )] @@ -93,6 +98,51 @@ public static function converter_data_provider() { ]; } + #[DataProvider( 'image_dataprovider' )] + public function test_image( string $html, string $expected ) { + $this->fake_request( 'https://alley.com/wp-content/uploads/2022/01/Screen-Shot-2022-01-19-at-2.51.37-PM.png' ) + ->with_file( __DIR__ . '/../fixtures/image.png' ); + + $converter = new Block_Converter( $html ); + $block = $converter->convert(); + + $this->assertEquals( $expected, $block ); + + $this->assertCount( 1, $converter->get_created_attachment_ids() ); + $this->assertRequestSent( 'https://alley.com/wp-content/uploads/2022/01/Screen-Shot-2022-01-19-at-2.51.37-PM.png', 1 ); + } + + public static function image_dataprovider(): array { + $url = wp_upload_dir()['url']; + + return [ + 'image wrapped with figure/a' => [ + '@atribecalledval Andre 3000 performing at Luna Luna was such an incredible night. I will never forget this night. #losangeles #andre3000 #fyp #foryou #foryoupage
♬ I swear, I Really Wanted To Make A \"Rap\" Album But This Is Literally The Way The Wind Blew Me This Time - André 3000
Content to migrate
Content to migrate