Skip to content

Generate the WPML language configuration for Blockstudio blocks - #122

Closed
Contributolo wants to merge 5 commits into
inline0:mainfrom
Contributolo:feat/wpml-config
Closed

Contributolo wants to merge 5 commits into
inline0:mainfrom
Contributolo:feat/wpml-config

Conversation

@Contributolo

Copy link
Copy Markdown
Collaborator

Problem

WPML translates the attributes of a custom block only as far as a wpml-config.xml declares them (Make Custom Gutenberg Blocks Translatable). It reads that file from the root folder of the active theme and of each active plugin (Language Configuration Files); there is no documented PHP hook to add block configuration (hooks reference).

Blockstudio blocks are self-closing and keep every field value in the block comment under blockstudio.attributes. Without an entry, WPML 5.1 falls back to its automatic string detection: for a non-core block with empty inner HTML it registers every non-numeric string attribute for translation (addons/wpml-page-builders/classes/Integrations/Gutenberg/strings-in-block/class-attributes-fallback.php, isEligible(), and class-base.php, has_empty_config()). For a Blockstudio block that means select values, file references, URLs and CSS classes show up in the translation editor next to the real text, and a translated value can break the block. Numbers are never registered (class-attributes.php, ! is_numeric()). A gutenberg-block entry with translate="1" but without keys is dropped when WPML loads the config (class-wpml-gutenberg-config-option.php, update_from_config()), so only translate="0" keeps such a block out of the detection.

Today every Blockstudio project has to write and maintain this configuration by hand, mirroring Blockstudio's storage rules (group ID prefixes, repeater rows, link objects).

What this adds

wp bs wpml config generates the <gutenberg-blocks> configuration for all registered Blockstudio blocks.

wp bs wpml config > wp-content/themes/my-theme/wpml-config.xml
wp bs wpml config --file=wp-content/themes/my-theme/wpml-config.xml
  • Source: Build::blocks() and each block's blockstudio['attributes'], so custom/ fields, block fields, overrides and the blockstudio/blocks/attributes filter are already applied. Fields of extensions that match a Blockstudio block (Extensions::get_matches()) are included, because they are stored in the same blockstudio.attributes object. Extensions of core or third-party blocks are not, so the command never competes with the configuration WPML or another plugin ships for those blocks.
  • Keys follow Container_Field_Handler: everything under blockstudio > attributes; text, textarea, richtext and wysiwyg as keys; link as title plus url with type="link" (WPML points the URL to the translated page when one exists); repeaters as a * level, nested to any depth; groups with an id as key prefix <id>_; groups without an id and tabs add no level.
  • Every other built-in type is not declared, so WPML copies the value unchanged. Hidden fields (for example the reference fields of block fields) are not declared. Fields of custom field types and other unknown types are not declared and the command prints a warning for each. A unit test fails as soon as Field_Type_Config::TYPES gets a type that has no decision.
  • A block without a declared field gets <gutenberg-block type="..." translate="0" />. WPML's own configuration uses the same form for container blocks such as core/group and core/columns (sitepress-multilingual-cms/wpml-config.xml), so inner blocks stay translatable. Lazy Blocks made the same change recently for the same reason (fix(wpml): declare blocks with no translatable control as translate="0" nk-crew/lazy-blocks#412).
  • Each key gets a label (field label, prefixed by the labels of enclosing repeaters and groups with an ID); WPML shows it as the field name in the translation editor. The block title goes into the block label.
  • Output is deterministic (blocks sorted by name, fields in definition order).
  • --file creates a missing file. In an existing file it only replaces the text between <!-- blockstudio:start --> and <!-- blockstudio:end -->, which sit inside <gutenberg-blocks>, so other sections and entries for other blocks stay byte for byte. Before writing, the result is parsed: one gutenberg-blocks element directly in wpml-config, every gutenberg-block inside it; otherwise nothing is written. Block types declared more than once (for example old hand-written entries outside the markers) produce a warning. A file without markers is left unchanged with an error that says where to add them.
  • No runtime cost: Blockstudio\Wpml_Config is not in the eager class list of class-plugin.php; the plugin autoloader loads it when the command runs. No new dependencies (DOM is already used in Block).
  • Docs: new page "WPML" under Dev > Integrations (docs/docs/dev/wpml.md), a section on the CLI page, LLM index rebuilt with npm run build:llm, document count in scripts/check-docs-content.mjs raised to 74.

The key structure (nested keys under blockstudio > attributes, * for repeater rows two levels deep, type="link" on a nested URL, key labels) was verified on a WordPress 7.1.3 site with WPML 5.1.0 and the classic translation editor through complete translation round trips, including internal links switching to the translated page when a job is completed. For the 25 blocks of that site, the generator produces the same key tree as the hand-written configuration that was tested.

How it was tested

  • New tests/unit/wpml/WpmlConfigTest.php (12 tests): exact output for text, textarea, richtext, wysiwyg, link, tabs, groups with and without ID and a repeater nested two levels deep, with select, files, toggle, number, icon and message left out; translate="0" blocks; hidden fields and flattened groups; unknown types reported; every built-in type classified; the declared key paths equal the text and link attributes Attribute_Builder registers for the same fields (including groups inside tabs and repeaters inside groups); escaping; merge, markers and validation; registered_blocks() against the test theme, including the text fields the extensions/text.json extension adds to blockstudio/type-text.
  • Docker is not available here, so the unit tests ran in WordPress Playground (WordPress 7.1.3, PHP 8.3) with the plugin and the test theme active and Build::refresh_blocks() as in bootstrap-wp.php: WpmlConfigTest 12/12 and PageSyncReportTest pass.
  • WP-CLI in the same Playground setup against the test theme: 195 blocks, 87 with text fields, 108 with translate="0", warnings for the four test/* custom field type fields. Writing a new file, running again ("up to date"), merging into a file with custom-types and a foreign block, the duplicate warning, a file without markers (error, file unchanged) and the usage message without subcommand all behave as described.
  • Generated and merged documents validate against WPML's published schema (wpml-config.xsd).
  • composer cs, PHPStan and npm run check pass. No E2E run, nothing in the editor changes.

Questions for you

  1. File instead of runtime. Lazy Blocks feeds its config into WPML at runtime through the wpml_config_array filter. That filter is not in WPML's hooks reference, so this PR uses the documented file and an explicit command, like wp bs pages sync. If you prefer an automatic, opt-in integration (for example a wpml/enabled setting), the same generator could feed that filter.
  2. Opting fields in or out. Every text field is declared, also ones that hold technical values (the test theme's textClass extension fields, the code textarea of the code element). A field property such as "translate": false, and a way for custom field types to declare which parts of their value are text, would need a schema change, so I left both out. Do you want either?
  3. Hidden fields are skipped because editors do not write them. Fine with you?
  4. Link IDs. link.id is not declared as type="post-ids", because external links store a string there ("id":"google.com").
  5. Naming and placement: command wp bs wpml config, markers <!-- blockstudio:start -->/<!-- blockstudio:end -->, the page under Dev > Integrations, @since 7.7.3. Possible changelog line: New: wp bs wpml config generates the WPML language configuration for all Blockstudio blocks.
  6. Overlap with Add bs_block_extension_fields() for the fields extensions add to a block #121. Once bs_block_extension_fields() lands, registered_blocks() can use it together with bs_block_fields(). Both PRs rebuild the LLM files; a conflict there resolves with npm run build:llm. Duplicate extension matches are harmless here, keys are deduplicated.

@dnnsjsk dnnsjsk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reproduced a correctness issue in Wpml_Config::section(): duplicate field IDs do not follow the runtime override rules. Block::extension_attributes() and Extensions::render_blocks() use the later definition, but keys()/unique() keeps the first translatable node. For example, two matching extensions defining tone as text followed by select still generate a translatable tone key; link followed by text still generates nested title/url keys for a scalar field, and text followed by link generates a scalar key instead of the link object. This can expose technical values for translation or prevent valid text from being translated.

Please resolve effective field definitions/storage keys before classifying them, including a later non-text definition removing an earlier text declaration. Merely keeping the last generated XML node will not fix text -> select, since select generates no node. Add regression cases for block-owned fields overridden by extensions, overlapping extensions, and text/select/link transitions (including nested/container paths). This needs fixing before merge, so I am leaving this PR open rather than including the current version in 7.7.2.

WPML translates the attributes of a custom block only when a
wpml-config.xml declares them. Without an entry, its automatic string
detection offers every string attribute of a Blockstudio block for
translation, including select values, file references and URLs.

Add `wp bs wpml config`, which declares the text, textarea, richtext and
wysiwyg fields of every registered block under blockstudio > attributes,
with link titles as text and link URLs as type="link", repeater rows as a
"*" level and group IDs as key prefixes. Blocks without text fields are
declared with translate="0". Fields of unknown types are reported and not
declared.

The command prints the configuration or, with --file, writes it. In an
existing file only the section between the Blockstudio markers is
replaced, and the result is checked before it is written. The generator
class is loaded by the autoloader only when the command runs.
Add a WPML page under Dev > Integrations that explains why Blockstudio
blocks need a wpml-config.xml, what the generated configuration declares
for each field type, and how to keep the file up to date. List the
command on the CLI page and rebuild the LLM documentation index.
…aring it

A storage key can be defined more than once: twice in one field list, by a
block field and an extension field, or by two extensions. The field handlers
write each attribute by storage key and later extensions replace earlier ones,
but keys() and unique() kept the first translatable node, so a select after a
text field was still declared, a link after a text field stayed scalar, and a
text field after a link kept the nested keys.

effective() now resolves the definition in effect for each storage key (later
replaces earlier as a whole, a hidden field included) through groups, tabs and
repeater rows, and keys() classifies only those. Extension fields come from
bs_block_extension_fields(). Regression tests cover block fields overridden by
extensions, overlapping extensions, and text, select and link transitions in
rows, groups with an ID and tabs.
@Contributolo

Copy link
Copy Markdown
Collaborator Author

Thanks for the reproduction, you're right. Fixed in 3854f20, rebased on main so the extension fields come from bs_block_extension_fields().

  • effective() resolves the definition in effect for each storage key before anything is classified. A later definition replaces an earlier one as a whole, whatever its type, a later hidden field included. It walks groups with an ID (prefixed keys), groups without an ID and tabs (no level of their own), and resolves repeater rows on their own level. keys() only classifies these resolved definitions; unique() is gone.
  • Order: the block's own fields, then the fields of each matching extension in registration order, so an extension overrides a block field and a later extension an earlier one.
  • Regression tests: text → select, select → text, link → text, text → link and text → hidden; a block field overridden by an extension plus overlapping extensions (select → text, text → link, link → textarea); inside repeater rows (in one row, and a later repeater replacing the rows), a group with an ID against a field with the same storage key in both orders, a later hidden group, and a tab field against a later field in both orders. The three new tests fail against the previous version.
  • The docs page states the rule in "What is declared"; includes/llm is regenerated.

One thing to double-check: for a block field and an extension field with the same key, Block::render() combines the definitions as $blocks[ $name ]->attributes + $extension_attributes, so the block's own definition wins for the template there, while Extensions::render_blocks() uses the extension's. I followed your description (extension after block). If the block's own definition should win instead, it is the order in registered_blocks() and the expectation in test_extensions_override_block_fields_and_earlier_extensions.

Checked locally: the WpmlConfig unit tests outside wp-env (PHPUnit 11; the two tests that need a running WordPress were not run), composer cs, PHPStan level 3 on the changed classes, and scripts/check-docs-content.mjs.

@dnnsjsk

dnnsjsk commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Thanks for addressing the extension/extension collisions and adding the regression cases. Your note about the block-own-field case is correct: I verified Block::render() builds $definitions with the block attributes on the left of a PHP array union, so the block-owned definition wins in that template path. Extensions::render_blocks() separately uses the extension definitions. My earlier assumption that an extension always overrides an own field was too broad.

Please do not change that existing renderer precedence to accommodate the generator. The generator needs an explicit storage/translation policy for conflicting own-field and extension types, backed by tests against the registered block and actual render paths; ambiguous conflicts should be reported instead of silently guessing a text/link/value schema. The updated documentation currently still claims a universal later-definition-wins rule that the template path does not implement. This remains a follow-up outside the tested 7.7.2 release cut while that policy is resolved.

…essing

A block field and an extension field with the same storage key both apply:
Block::render() gives the template the block's definition (the block
attributes on the left of the array union), Extensions::render_blocks()
uses the extension's, and the editor shows a control for each that writes
the same value. Declaring the key from either definition guesses what is
stored there.

The generator now keeps the block's fields and the extension fields apart:

- Within the block's fields, outside repeater rows, a later definition
  replaces an earlier one, as the attribute builder does. Within the fields
  of the matching extensions, a later field or a later extension wins, as
  Block::extension_attributes() and Extensions::render_blocks() do.
- A block field and an extension field on one key are declared once when
  both classify the same: text (text, textarea, richtext, wysiwyg), link,
  or a value that is not declared. Repeaters are compared row field by row
  field. The label comes from the block's field.
- Two fields with the same ID in the rows of one repeater are compared the
  same way: the builder keeps both row definitions and Block::render()
  transforms a row value with the first.
- Differing types are not declared. section() lists them under conflicts,
  and wp bs wpml config prints a warning for each.

A message field no longer replaces a field with the same ID, because the
builder registers nothing for it. No renderer precedence changes.

The documentation replaces the universal later-definition rule with this
policy. New tests register a block and two extensions through Build::init()
and check the generated keys and conflicts against what the template and
the extension renderer receive for the same stored values.
The editor shows a control for every field with an ID, also for two fields of one block, so it does not tell a block and extension conflict apart. What does is that Block::render() and Extensions::render_blocks() read the key with different definitions.
@Contributolo

Copy link
Copy Markdown
Collaborator Author

Thanks, that makes sense. I kept every renderer as it is and gave the generator an explicit policy instead (a9a7049, docs wording in 26c764f):

  • Within the block's own fields, outside repeater rows, a later definition replaces an earlier one, because the field handlers write $attributes[ $id ]. Within the fields of the matching extensions the same holds, in registration order, as in Block::extension_attributes() and Extensions::render_blocks(). In both cases every render path reads the key with that one definition.
  • A block field and an extension field on one key both stay in effect: Block::render() gives the template the block's definition, Extensions::render_blocks() applies the extension's. The key is declared only when both classify the same: text (text, textarea, richtext and wysiwyg all store a string and get the same declaration), link, or not declared. Repeaters are compared row field by row field, and the label comes from the block's field.
  • Duplicate IDs in the rows of one repeater are treated the same way: the builder keeps both row definitions, Block::attribute_definition() transforms a row value with the first, and the editor fills a new row with the default of the last. The previous docs claimed later-wins there, which was wrong.
  • Anything that classifies differently is not declared and is listed in the new section()['conflicts'], e.g. acme/card: tone (text in the block, select in an extension). wp bs wpml config prints a warning for each.
  • WpmlConfigRenderPathsTest registers a block and two extensions through Build::init(), checks the generated keys and conflicts, and renders through do_blocks() to assert what the template and the extension renderer receive for the same stored values: text vs select (each path reads it with its own definition), text vs textarea (same text), and a later extension (both paths use it).
  • The WPML docs page replaces the universal rule with this policy.

Checked: the WPML and related unit tests in WordPress Playground (70/70), PHPCS, PHPStan level 3, npm run check. On the test theme the generated file is unchanged from the previous version.

Understood that this stays outside 7.7.2.

@dnnsjsk

dnnsjsk commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Included in Blockstudio 7.7.4.

The WPML configuration generator has shipped, including nested-field handling, conflict diagnostics and CLI/documentation coverage.

Your changes were incorporated into the consolidated release branch with integration tests and release adjustments, so I am closing this PR as shipped rather than merging the original branch again. Full CI (14/14) and Distribution (5/5) passed on release commit e1d38df. Thank you for the contribution.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants