Skip to content

About

Propagate fixes in unsynced block patterns to every copy, keeping editors' content

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

HM Pattern Sync

hm-pattern-sync-demo-before-after.mp4

Unsynced block patterns get copied into every page that uses them. When a pattern has to change, fixing the pattern file doesn't fix the copies.

This plugin finds every copy of a pattern and propagates the fix, keeping the content editors wrote (text, images, links, formatting).

wp pattern-sync prepare          # locally, after editing a pattern, before committing
wp pattern-sync update           # on the server, after the deploy: what would change
wp pattern-sync update --apply   # write it; copies that need a decision go to wp-admin

When to use it (and when not)

Prevention first. If a pattern's design lives in the theme, copies never need propagating: use presets (var:preset|…) and, for a section's look, a section style (block style variation in theme.json, WordPress 6.6+). Copies then only carry a class like is-style-hero, and changing the style in the theme changes every page through CSS. No plugin needed.

Use Pattern Sync for what that can't cover:

  • Structure changes: blocks added, removed or replaced (a button becomes an image, an embed becomes an image).
  • Legacy content: copies already saved with inline values (colours, padding, font sizes) before the pattern used presets or section styles.
  • Design changes that don't fit a style variation.

Synced patterns with overrides aren't an alternative for existing content: they only apply to patterns built as synced from the start, and only to the blocks and attributes that support overrides.

Installation

Via Composer (recommended)

{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/humanmade/hm-pattern-sync"
        }
    ],
    "require": {
        "humanmade/hm-pattern-sync": "^1.0"
    }
}

Manual

Download a release (or the main branch) and drop it into wp-content/plugins/. There is no build step: the plugin is PHP only.

Activation creates nothing (no tables, no options). It adds the wp pattern-sync command and the Tools → Pattern Sync screen.

How it works

Finding copies. When a single-root pattern is inserted, WordPress stamps the root block with metadata.patternName. That's the index: no extra table, so it never goes stale. For older content without the stamp, --fingerprint also matches blocks with the same block tree shape (review those before applying).

Knowing what changed. To separate "the fix changed this" from "an editor changed this", the merge needs the pattern as it was before the fix. You wp pattern-sync prepare saves it from git, before the fix is committed, to <theme>/pattern-sync/hero.previous.php, and it ships with the fix. On the server, update finds it there. Only the patterns you fix get a base, and the plugin keeps no history of its own: no database, nothing on activation (ADR 0001).

Merging. For every block attribute, HTML attribute, CSS class and inline style declaration, a three-way merge decides:

  • the fix didn't touch it: keep the page value
  • the page didn't touch it: take the fixed value
  • both changed it differently: conflict. That copy is skipped and reported.

Text always comes from the page. The plugin edits the saved HTML directly (PHP's serialize_blocks() can't regenerate static block markup), and the results open as valid blocks in the editor.

Reviewing copies in wp-admin

Some copies can't be updated unattended: the page customised the same thing the fix changes, editors added blocks, or the fix changes the pattern's structure. With --apply they go to Tools → Pattern Sync (add --no-review to only list them).

The content team decides there (the screen is open to editors: edit_others_pages, plus edit_post for each page on apply). Developers can decide simple cases from the terminal with wp pattern-sync review.

Each card shows the copy before (today) and after (the proposal) side by side, rendered with the theme, and what changes, using the theme's own names ("Background: Base → Accent 2"). Copies that get the same change are grouped: the change is shown once, with two samples and a list of the rest, and one click accepts or rejects the whole group. Decisions are saved as they're made, and Apply decisions writes in batches of 25 with a progress bar.

How the proposal is built: the copy takes the new pattern's structure and keeps the page's content in every block that exists in both versions. If the page and the fix changed the same setting, the fix wins in the proposal (marked ⚠; rejecting keeps the page's). Blocks the fix adds come with placeholder content; blocks it removes are listed with the content that would be lost; blocks the page added are kept. Applying saves a revision first, and a copy whose page changed after the review was created is skipped as stale. See ADR 0003.

Workflow

Once per project

Install and activate the plugin. Activation creates nothing: no tables, no options, no copies of patterns. It only adds the wp pattern-sync command and the Tools → Pattern Sync screen.

For every pattern fix

1. Edit the pattern, then prepare, then commit (locally).

# edit patterns/hero.php
wp pattern-sync prepare
git commit -am "Fix Hero spacing"

prepare looks at which pattern files changed since the last commit and saves the committed version of each (the one before your fix) to <theme>/pattern-sync/<name>.previous.php, already staged. That file is the base the merge needs. It's how the server, which has no git history, knows what the fix changed. It lives outside patterns/ so WordPress doesn't register it as a second pattern.

Committed the fix already? wp pattern-sync prepare --ref=HEAD~1.

2. Try it locally (optional, with a copy of the content).

wp pattern-sync update

3. Deploy. In the PR, hero.previous.php next to hero.php shows exactly what the fix changes.

4. Run it on the server.

vip wp pattern-sync update            # dry run: what would change, page by page
vip wp pattern-sync update --apply    # write it

Without a pattern name, update handles every pattern that has a base in the theme (update mytheme/hero for one). Pantheon: terminus wp <site>.<env> -- pattern-sync update.

With --apply, copies where only the design changed are updated at once, keeping the editors' content. The rest (the page customised the same thing, editors added blocks, or the fix changes the structure) is listed with links and sent to Tools → Pattern Sync.

5. Review. The content team (editors) checks before/after and accepts or rejects, by group or by copy. Developers can decide simple cases from the terminal: wp pattern-sync review list, review decide approve <id>…, review apply.

6. Done. Leave hero.previous.php in the theme or delete it. The next fix of the Hero overwrites it.

Rules of thumb

  • Run it once per fix, as part of shipping that fix. If a deploy went out without running update, run it before the next fix of the same pattern: the next prepare overwrites the base, and copies still on the old version would then look customised (kept or flagged as conflicts, never overwritten).
  • Two fixes of the same pattern in one deploy: keep the base from the first one, which covers both. prepare warns when it's about to replace a base and tells you how to keep the old one.
  • Always look at the dry run first. It writes nothing.
  • Undo: every page gets a revision before it's written; restore it from the editor's Revisions screen.
  • PR checklist: "Changed something in patterns/? Ran wp pattern-sync prepare?"
  • Locally you can skip the base file and compare with git directly: wp pattern-sync update mytheme/hero --from-ref=HEAD~1. --from-file=<path> takes any file.

Scale

Measured on a test site with 300 pages (~12 KB each) and 285 copies of a pattern:

Time Notes
find / dry run 0.5 s +10 MB over WordPress itself
update --apply, 270 pages 6.8 s ~25 ms per page, linear
Review screen, 285 copies 1.3 s 2 groups, 8 previews
Apply 285 decisions from the screen 7.5 s 12 batches
Public pages no change same queries with the plugin on or off

Storage: the index is metadata.patternName, ~30–45 bytes per copy and mostly written by WordPress itself. The real cost is revisions: each applied copy adds two revisions of its page (~6 MB for 270 pages of 12 KB). Bound it with WP_POST_REVISIONS. Review items take ~3 KB each until wp pattern-sync review clear.

Commands

Command
prepare [<pattern>…] [--ref=<ref>] Locally, before committing a fix: saves the version before the fix of each changed pattern to <theme>/pattern-sync/, staged.
update [<pattern>] [--apply] [--no-review] [--fingerprint] [--post=] [--post_type=] [--verbose] [--format=] Propagate fixes: every pattern with a base, or one. Dry run unless --apply; with --apply, copies that need a decision go to review. Copies that need manual review are always listed with their editor and front-end links. With --format=json/csv, every row has pattern, edit and view.
update <pattern> --from-file=<path> | --from-ref=<ref> Same, with the base from a file or a git ref instead of the theme's pattern-sync/ folder.
find <pattern> [--fingerprint] [--post_type=] [--fields=] [--format=] Where a pattern is used. Add edit to --fields for editor links; --format=ids works too.
review list|show|decide|apply|clear The review queue from the terminal (same as the screen).

Statuses: will update / updated, up to date, CONFLICT – skipped.

Searched: every show_ui post type plus wp_template, wp_template_part and wp_block, in publish/draft/pending/private/future.

Safety

  • Dry run by default.
  • Before writing, the current state of each post is saved as a revision, so any page can be restored from the editor's Revisions screen.
  • Any conflict in a copy leaves the whole copy untouched. There are no partial updates.
  • kses is off only while copies are written (the markup comes from the page and the theme's pattern file) and is restored afterwards, also on errors.

Limitations

  • Single-root patterns only. Wrap multi-root patterns in a Group.
  • Unattended updates are design-only. If the fix adds, removes or replaces blocks, or editors added blocks to a copy, it goes to review (--review) instead of being updated.
  • Content in a block the fix removes is lost when the proposal is approved (it's listed before anyone approves). Content isn't moved between different block types (an Embed URL doesn't become an Image).
  • Theme template files (templates/*.html not customised in the Site Editor) aren't touched, only content stored in the database.
  • Fixes are started by a developer (prepare + update); the screen is for decisions (ADR 0003).

Demo

bash tests/e2e/demo.sh     # or: npm run demo

This builds a local WordPress (SQLite, no Docker) with 7 pages that use a Hero pattern, then serves it at http://localhost:8899 (admin / password). Keep that terminal open. It prints the commands to run in a second terminal, from the demo theme's folder (a git repo, like a real project): demo-edit 1|2|3 changes patterns/hero.php the way a developer would, then you run prepare, git commit and update yourself.

Needs PHP 7.4+ with pdo_sqlite (on a Mac: brew install php), git, curl and unzip. Node isn't needed.

Tests

End-to-end, against a real WordPress: SQLite instead of MySQL, no Docker. It takes about a minute locally, and the same scripts run in CI (.github/workflows/test.yml: latest WordPress on PHP 8.3, and WordPress 6.5 on PHP 7.4).

npm install && npx playwright install chromium   # once
npm test                                          # all three steps below
Step What it does
npm run test:setup Builds WordPress in /tmp/hm-pattern-sync-wp (WP_VERSION=6.5 to pick a version), symlinks this plugin, installs the fixture theme tests/fixtures/theme/pstest in its own git repo.
npm run test:cli Creates one page per scenario (tests/fixtures/make-pages.php), commits the fixes in tests/fixtures/fixes/ and checks every wp pattern-sync outcome: statuses, kept content, conflicts left untouched, a second fix, rollback via revisions, catching up two fixes at once, a structure change through the review queue (proposals, approve/reject, stale pages), the dev workflow (prepare, then update with no git on the server) and kses being restored.
npm run test:editor Playwright drives Tools → Pattern Sync (previews, ✔/✖, groups, batches, editor and author access), then opens every page in the block editor and fails if any block is invalid or the edited content is gone.

Needs PHP with pdo_sqlite, Node 20, git and jq. To add a scenario, add a page in make-pages.php, its expected status in run-cli.sh, and (if it's about content) an assertion in editor.spec.js.

Release

See RELEASE.md. In short: update CHANGELOG.md, merge into main, then run Actions → Tag and Release with the version.

Requirements

  • WordPress 6.5+
  • PHP 7.4+
  • WP-CLI, to start a propagation (prepare locally, update on the server)
  • git, locally, for prepare

Decisions

Next steps

  • Match blocks by metadata.name before type, for fixes that reorder blocks.
  • Warn when a pattern hard-codes values instead of using presets or section styles, so it can be fixed before copies spread.

About

Propagate fixes in unsynced block patterns to every copy, keeping editors' content

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages