Skip to content

Add wp block convert and wp block conversion-support for server-side block conversion - #20

Draft
swissspidy wants to merge 1 commit into
mainfrom
feature/block-convert
Draft

swissspidy wants to merge 1 commit into
mainfrom
feature/block-convert

Conversation

@swissspidy

Copy link
Copy Markdown
Member

Summary

Adds two commands that expose the server-side block conversion prototyped in WordPress/gutenberg#82013 (branch try/13163-php-block-conversion).

Depends on an unmerged Gutenberg PR. The functions these commands call (gutenberg_html_to_block_markup() and gutenberg_get_block_conversion_support()) only exist while a Gutenberg build from that branch is active. No released WordPress or Gutenberg version has them yet. Both commands check function_exists() first and fail with:

Error: Server-side block conversion is not available. Activate the Gutenberg plugin from https://github.com/WordPress/gutenberg/pull/82013.

Commands

wp block convert [<html>] [--file=<file>]

Converts HTML to serialized block markup with gutenberg_html_to_block_markup() and prints it to STDOUT. The HTML comes from the positional argument, else --file, else STDIN (so cat page.html | wp block convert works). Passing both the argument and --file, or passing no input at all, is an error. The docs note that the output is not sanitized and that it should go through the usual filters (for example wp post update) before being stored.

wp block conversion-support [--support=<support>] [--field=<field>] [--fields=<fields>] [--format=<format>]

Lists what a conversion can produce from gutenberg_get_block_conversion_support(), one row per block, with columns name and support (converts, conditional or declines), sorted by name. Uses WP_CLI\Formatter with the usual table, csv, json, count and yaml formats plus --field/--fields, the way the other list commands in this package do. I added a small --support=<level> filter on top of what was asked for, because --support=declines --field=name is the obvious scripting use; happy to drop it if unwanted.

Naming and structure

Neither name clashed with an existing command, so both live directly under the existing wp block namespace as requested. They are implemented as two methods on one class (Block_Conversion_Command) and registered individually in block-command.php with the class/method pair form of WP_CLI::add_command(), behind the same WordPress 5.0 gate as wp block synced-pattern.

Other changes

  • phpstan/gutenberg-block-conversion-stubs.php declares the two Gutenberg functions for PHPStan (added to scanFiles), since level 9 otherwise reports them as not found. The directory is excluded from PHPCS and from the distributed package via .distignore.
  • composer.json lists the new commands under extra.commands.
  • README.md regenerated with wp scaffold package-readme.

Tests

features/block-convert.feature:

  • Run in CI today (plain Given a WP install): each command fails with the "not available" error on STDERR, empty STDOUT, exit code 1.
  • Need the plugin build (tagged @broken, so composer behat skips them, following how wp-cli/wp-cli-tests always excludes that tag): converting <h2>Title</h2><p>Text</p> yields <!-- wp:heading --> and <!-- wp:paragraph -->; reading from STDIN and from --file plus the input-related errors; existing block markup is kept and unknown markup becomes a Custom HTML block; conversion-support lists core/paragraph as converts and core/table as declines, plus the --support, --format=json and --format=count variants. A comment in the feature file explains how to run them locally: build the plugin zip from the branch, drop it in the package root as gutenberg.zip (git-ignored), run composer prepare-tests once, then vendor/bin/behat --tags=@broken features/block-convert.feature. Each scenario installs it with wp plugin install gutenberg.zip --activate.

Validation

  • composer lint, composer lint-gherkin, composer phpcs, composer phpstan: all clean.
  • The CI-safe scenarios pass locally. MySQL was not available in my environment and wordpress.org was blocked, so I ran the whole Behat suite with WP_CLI_TEST_DBTYPE=sqlite against a WordPress 7.1 archive from the GitHub mirror via WP_CLI_TEST_CORE_ZIP: 65 scenarios passed, including the two new ones.
  • The @broken scenarios were not run against a real build of the Gutenberg PR (no way to build it in that environment). To check the command plumbing anyway I ran a throwaway scenario with an mu-plugin that fakes the two functions, covering argument/--file/STDIN input, the error paths, and every output format; that scenario is not part of this PR.
  • wp help block convert and wp help block conversion-support render as expected.

Notes

  • The commands never write anything; sanitization stays the caller's job, as the docs say.
  • Draft on purpose: it should not merge before Gutenberg #82013 lands and the function names are final.

🤖 Generated with Claude Code

https://claude.ai/code/session_01YZNpaiVywfzpc8Hpm6dMa8


Generated by Claude Code

Add two commands that expose the server-side block conversion from
WordPress/gutenberg PR #82013:

- `wp block convert [<html>] [--file=<file>]` converts HTML to block
  markup with `gutenberg_html_to_block_markup()`, reading the HTML from
  the argument, a file, or STDIN.
- `wp block conversion-support [--format=<format>]` lists the blocks a
  conversion can produce with `gutenberg_get_block_conversion_support()`,
  one row per block with `converts`, `conditional` or `declines`.

Both commands fail with a clear error when the conversion functions are
not available. PHPStan gets stubs for those functions so static analysis
can type-check the calls.

The feature file has two scenarios that run in CI against a plain
install and four scenarios tagged @broken that need a Gutenberg build
from the pull request, with instructions on how to run them locally.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YZNpaiVywfzpc8Hpm6dMa8
@coderabbitai

coderabbitai Bot commented Sep 14, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added command:block Related to 'block' command enhancement New feature or request labels Sep 14, 2026
@codecov

codecov Bot commented Sep 14, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 14.03509% with 49 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Block_Conversion_Command.php 14.03% 49 Missing ⚠️

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

command:block Related to 'block' command enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant