Skip to content

Serialize the values in pattern block delimiters as JSON - #172

Closed
westonruter wants to merge 18 commits into
fix/phpstan-level-10from
fix/block-attribute-json
Closed

westonruter wants to merge 18 commits into
fix/phpstan-level-10from
fix/block-attribute-json

Conversation

@westonruter

Copy link
Copy Markdown
Member

Description

This is stacked on #171, which needs to be merged first. Its base is that PR's branch, so only the changes below appear here, and GitHub retargets it to trunk once #171 is merged.

The patterns echo translated strings and URLs into the JSON of their block delimiters with esc_html_x(), esc_attr_e(), and esc_url(), which escape for HTML, not for JSON:

  • HTML entities are kept as literal text in the attribute values. With a translation containing an apostrophe, the cover's Alternative text field in the editor shows l'…, and the Search block's placeholder shows ' on the front end, since the block escapes it again.
  • A backslash or a line break in a translation makes the delimiter's JSON invalid, so the block loses all of its attributes, not just the translated one.

A new ipsum_serialize_block_attribute() helper encodes a value with core's serialize_block_attributes(), the PHP counterpart of the block editor's attribute serializer, which also escapes <, >, &, --, and quotes for the HTML comment. The patterns now use it for every value they compute:

  • Plain-text attributes (block names, ARIA labels, alt text, and the search placeholder) get the bare translation.
  • Attributes that hold HTML (the search label and button text, the Read More text, and the Post Terms prefix) get esc_html__().
  • The cover image URLs get esc_url_raw(), since esc_url() would turn & into &#038; inside the URL.

Only the values change. The rest of each delimiter keeps the JSON the editor exports, so patterns can still be copied to and from the editor.

The helper is registered as an escaping function in phpcs.xml.dist, since the escaping sniff would otherwise report each of the 338 calls.

Screenshots

No visual change is intended for the English strings, which contain none of the affected characters.

Testing Instructions

  1. Run npm run lint. It passes.
  2. With a translation of Ipsum whose strings contain an apostrophe, a quote, and a backslash (or a gettext_with_context filter that appends them), insert the CTA Membership pattern. The cover's Alternative text field shows the characters as typed, not as HTML entities, the block is valid, and its other settings are intact.
  3. With the same translation, view a page with the search bar. The search field's placeholder shows the characters as typed.
  4. In the Site Editor, open the templates and check that the List View shows the blocks' names.
  5. Check the front end and the Site Editor with the default colors and at least one other color variation.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Investigating the escaping, reproducing the problems with WP-CLI and in the editor, implementing the helper and converting the patterns under direction, and porting the commits from core to this repository.

🤖 Generated with Claude Code

westonruter and others added 18 commits October 22, 2026 13:27
Thirteen Ipsum patterns append the theme version to image URLs as a
cache busting query string, read with `WP_Theme::get( 'Version' )`. That
returns `string|false`, which `esc_attr()` does not accept, so each of the
60 call sites was a PHPStan level 7 error.

`ipsum_get_version()` narrows the header to a string, falling back to an
empty string when it cannot be read, and the patterns now call it. As
before, the version comes from the parent theme.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`ipsum_enqueue_styles()` loads the parent theme's `style.css` but versioned
it with `wp_get_theme()`, which is the active theme. With a child theme
active, that is the child's version, so updating Ipsum left the stylesheet
URL unchanged, and sites with long static file caches could keep serving
the old file.

Pass `get_template()` so the version is Ipsum's, as the pattern images
already do and as Twenty Twenty-Four does for its parent theme styles.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Replace `ipsum_get_version()` with `ipsum_get_asset_url()`, which takes a
path relative to the theme directory, resolves it with
`get_parent_theme_file_uri()`, and adds the parent theme version as the
`ver` query argument with `add_query_arg()`, skipping it when the version
cannot be read.

The patterns now pass the image path to the helper instead of joining the
template directory URI, the path, and a hand-built query string, which
also stops them emitting an empty `?ver=` when there is no version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`get_parent_theme_file_uri()` returns the theme directory URL rather than
a file URL when the path is empty, which includes `'0'`, so neither is a
valid asset path for `ipsum_get_asset_url()`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The block binding source arguments are an object decoded from the block's
`metadata.bindings` attribute, so they are keyed by string with values of
any type.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The `postId` block context is untyped, and its fallback, `get_the_ID()`,
returns `false` when there is no current post, so neither was a valid
argument for `get_comments_number()` or `get_comments_link()`.

Resolve the post with `get_post()` instead, which falls back to the
current post just as `get_the_ID()` did, and return null when there is
none, so the block keeps its original content rather than linking to
comments on no post.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The `default_template_types` filter passes template types keyed by slug,
each an array with a string title and description.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The `block_type_metadata` filter passes the decoded `block.json`, keyed by
string with values of any type.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The block metadata values are untyped, so writing `ariaLabel` straight
into `$metadata['supports']` wrote an offset on a mixed value. Read the
supports into a local array first, starting from an empty one when it is
missing or not an array, then add the ARIA label support and assign it
back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Block bindings pass the source arguments before the block instance, so
`ipsum_comments_cta_binding()` must accept them to reach the block, even
though it does not use them. Unset them to mark them as deliberately
unused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Ipsum's patterns put translated strings and URLs into block delimiters by
echoing `esc_html_x()`, `esc_attr_e()` and `esc_url()` inside the JSON.
Escaping for HTML is the wrong encoding there: the entities are kept as
literal text in the attribute value, so a translated alt text shows as
`l&#39;été &amp; …` in the editor, and JSON escapes are left alone, so a
backslash or line break in a translation makes the JSON invalid and the
block loses all its attributes.

`ipsum_serialize_block_attribute()` encodes a single value with
`serialize_block_attributes()`, the PHP counterpart of the block editor's
attribute serializer, which also escapes `--`, `<`, `>`, `&` and quotes as
unicode escapes for the HTML comment. The value is wrapped under a key
that is then stripped, so the call satisfies the function's attributes
object parameter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The `metadata.name` of each block in Ipsum's patterns is plain text, but
was echoed with `esc_html_x()`, so a translation's HTML entities were kept
as literal text in the name, and a backslash or line break in it made the
delimiter's JSON invalid, dropping all the block's attributes.

Translate the names with `_x()` and encode them with
`ipsum_serialize_block_attribute()` instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The `ariaLabel` and cover `alt` attributes in Ipsum's patterns are plain
text, but were echoed with `esc_attr_e()` and `esc_attr_x()`, so the
editor showed a translation's HTML entities literally, for example in the
cover's Alternative text field and in the image's alt in the canvas.

Translate them with `__()` and `_x()` and encode them with
`ipsum_serialize_block_attribute()` instead. The cover's `<img>` markup
keeps `esc_attr_x()`, since that is an HTML attribute.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Search block `label`, Read More `content` and Post Terms `prefix`
attributes hold HTML, which their render callbacks output through
`wp_kses_post()` or as is, so escaping the translations for HTML is right.
But echoing `esc_attr_e()` straight into the delimiter's JSON left JSON
escapes alone, so a backslash or line break in a translation made the JSON
invalid, dropping all the block's attributes.

Escape the translations with `esc_html__()`, then encode them with
`ipsum_serialize_block_attribute()`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The cover block `url` attribute is a URL, not HTML, but was echoed into
the delimiter's JSON with `esc_url()`, which encodes `&` as `&#38;` for
HTML. With more than one query argument, the entity would become part of
the URL in the attribute.

Sanitize the URLs with `esc_url_raw()` and encode them with
`ipsum_serialize_block_attribute()` instead. The cover's `<img>` markup
keeps `esc_url()`, since that is an HTML attribute.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Search block in Ipsum's patterns still echoed `esc_attr_e()` into its
`placeholder` and `buttonText` attributes, so a backslash or line break in
a translation made the delimiter's JSON invalid, dropping all the block's
attributes.

The placeholder is plain text that the block sets with the HTML API, which
escapes it again, so a translated apostrophe or ampersand also showed on
the front end as a literal `&#39;` or `&amp;`. Translate it with `__()`.
The button text holds HTML, output through `wp_kses_post()`, so escape it
with `esc_html__()`. Encode both with `ipsum_serialize_block_attribute()`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…PCS.

The patterns echo `ipsum_serialize_block_attribute()` into their block
delimiters, which the escaping sniff reported as unescaped output in 338
places. The helper does escape the value for that context: it encodes it
with `serialize_block_attributes()`, which escapes `<`, `>`, `&`, `--`,
and quotes for JSON in an HTML comment. Register it as a custom escaping
function rather than ignoring each call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@carolinan

Copy link
Copy Markdown
Contributor

Is this something that could be done in core?

I'm struggling to see why this should be the responsibility of the theme, since it sounds like this is something ervery single block theme would need to do? Not a one-off bug for an individual theme to solve?

@westonruter

Copy link
Copy Markdown
Member Author

Yes, I think a helper in core would be warranted which the block themes can re-use in their patterns. But I wanted to raise this to show what it could look like, and it could land first here in the theme and then be put in core as part of the core merge.

@MaggieCabrera

Copy link
Copy Markdown
Contributor

Is this something that could be done in core?

I'm struggling to see why this should be the responsibility of the theme, since it sounds like this is something ervery single block theme would need to do? Not a one-off bug for an individual theme to solve?

same thinking here. It seems like this shouldn't be the theme's responsibility

@westonruter

Copy link
Copy Markdown
Member Author

Agreed. I'll propose this for core directly.

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

Labels

[Area] Patterns Issues or PRs that touch the theme's block patterns. [Area] Theme Functions Issues or PRs that touch functions.php.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants