Skip to content

Keep backslashes in values synced to post meta - #202

Merged
dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:fix/post-meta-backslashes
Nov 7, 2026
Merged

dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:fix/post-meta-backslashes

Conversation

@Contributolo

Copy link
Copy Markdown
Collaborator

Fixes #200.

A field with postMeta storage lost every backslash on save: a\b was stored as ab, a\\b as a\b. The block attribute in post_content kept the value, only the meta copy changed.

Cause

Storage_Sync::sync_field_value() passes the value parsed from the block straight to update_post_meta(). update_metadata() expects a slashed value ("For historical reasons both the meta key and the meta value are expected to be slashed", reference) and runs wp_unslash() on it first (meta.php on the 7.1 tag). A value that was never slashed loses its backslashes.

Fix

wp_slash( $value ) before update_post_meta(), the same way Content_Sync already slashes for add_metadata(). wp_slash() walks arrays, so repeater rows are covered. The update_option() branch stays as it is: update_option() does not unslash.

I also looked at the other update_post_meta() calls in includes/. Page_Sync stores paths through Page_Paths::encode(), which runs wp_normalize_path(), plus UIDs, fingerprints and flags, so no backslashes there. The one free-form value is _blockstudio_page_meta (the meta of a page file), which is only read back as a fallback when the registry has no entry for the post. I left it out to keep this change to the reported bug.

Tests

tests/unit/storage/PostMetaSaveTest.php saves a post through wp_insert_post( wp_slash( ... ) ), the way the REST posts controller does, with a\b, a\\b, q"x\y"z and C:\Users\me, checks that post_content keeps the value and that the meta matches on the first and on a second save, and does the same for a repeater field. All five cases fail before the change (the meta reads ab, a\b, q"xy"z, C:Usersme, and ['ab', 'c\d']) and pass after.

tests/unit/storage/ runs green locally (81 tests) against WordPress Playground with the test theme. composer cs and PHPStan on storage-sync.php are clean. The full suites are left to CI as per AGENTS.md.

update_post_meta() runs wp_unslash() on its value, so field values parsed
from block attributes lost every backslash on their way into post meta.
Slash them first, as content sync already does for add_metadata().

Fixes inline0#200
@Contributolo Contributolo added the bug Something isn't working label Nov 6, 2026
@dnnsjsk
dnnsjsk merged commit 0169f90 into inline0:main Nov 7, 2026
@dnnsjsk

dnnsjsk commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Merged and released in 7.7.13, included in 12440a6. Your WordPress save-hook regressions are included with the post-meta slashing fix for #200, alongside editor save/reload/resave coverage and related database storage regressions. Full CI and distribution checks passed. Thanks for the contribution.

@WebUppAdmin

Copy link
Copy Markdown

Thanks for merging this and for releasing it in 7.7.13. We tested 7.7.13 and backslashes in postMeta storage now survive a save and a resave.

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

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Backslashes are stripped from postMeta fields on save (storage-sync.php passes an unslashed value to update_post_meta)

3 participants