Repository navigation
Generate the WPML language configuration for Blockstudio blocks - #122
Contributolo wants to merge 5 commits into
Conversation
dnnsjsk
left a comment
There was a problem hiding this comment.
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.
e82a5f9 to
3854f20
Compare
|
Thanks for the reproduction, you're right. Fixed in 3854f20, rebased on main so the extension fields come from
One thing to double-check: for a block field and an extension field with the same key, Checked locally: the WpmlConfig unit tests outside wp-env (PHPUnit 11; the two tests that need a running WordPress were not run), |
|
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.
|
Thanks, that makes sense. I kept every renderer as it is and gave the generator an explicit policy instead (a9a7049, docs wording in 26c764f):
Checked: the WPML and related unit tests in WordPress Playground (70/70), PHPCS, PHPStan level 3, Understood that this stays outside 7.7.2. |
|
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. |
Problem
WPML translates the attributes of a custom block only as far as a
wpml-config.xmldeclares 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(), andclass-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()). Agutenberg-blockentry withtranslate="1"but without keys is dropped when WPML loads the config (class-wpml-gutenberg-config-option.php,update_from_config()), so onlytranslate="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 configgenerates 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.xmlBuild::blocks()and each block'sblockstudio['attributes'], socustom/fields, block fields, overrides and theblockstudio/blocks/attributesfilter are already applied. Fields of extensions that match a Blockstudio block (Extensions::get_matches()) are included, because they are stored in the sameblockstudio.attributesobject. 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.Container_Field_Handler: everything underblockstudio > attributes;text,textarea,richtextandwysiwygas keys;linkastitleplusurlwithtype="link"(WPML points the URL to the translated page when one exists); repeaters as a*level, nested to any depth; groups with anidas key prefix<id>_; groups without anidand tabs add no level.Field_Type_Config::TYPESgets a type that has no decision.<gutenberg-block type="..." translate="0" />. WPML's own configuration uses the same form for container blocks such ascore/groupandcore/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).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 blocklabel.--filecreates 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: onegutenberg-blockselement directly inwpml-config, everygutenberg-blockinside 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.Blockstudio\Wpml_Configis not in the eager class list ofclass-plugin.php; the plugin autoloader loads it when the command runs. No new dependencies (DOM is already used inBlock).docs/docs/dev/wpml.md), a section on the CLI page, LLM index rebuilt withnpm run build:llm, document count inscripts/check-docs-content.mjsraised 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
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 attributesAttribute_Builderregisters 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 theextensions/text.jsonextension adds toblockstudio/type-text.Build::refresh_blocks()as inbootstrap-wp.php:WpmlConfigTest12/12 andPageSyncReportTestpass.translate="0", warnings for the fourtest/*custom field type fields. Writing a new file, running again ("up to date"), merging into a file withcustom-typesand a foreign block, the duplicate warning, a file without markers (error, file unchanged) and the usage message without subcommand all behave as described.composer cs, PHPStan andnpm run checkpass. No E2E run, nothing in the editor changes.Questions for you
wpml_config_arrayfilter. That filter is not in WPML's hooks reference, so this PR uses the documented file and an explicit command, likewp bs pages sync. If you prefer an automatic, opt-in integration (for example awpml/enabledsetting), the same generator could feed that filter.textClassextension fields, thecodetextarea 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?link.idis not declared astype="post-ids", because external links store a string there ("id":"google.com").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.bs_block_extension_fields()lands,registered_blocks()can use it together withbs_block_fields(). Both PRs rebuild the LLM files; a conflict there resolves withnpm run build:llm. Duplicate extension matches are harmless here, keys are deduplicated.