Skip to content

Fix Save Patterns deleting unrelated template and template-part customizations - #913

Open
KhushalSainS wants to merge 2 commits into
WordPress:trunkfrom
KhushalSainS:fix/save-patterns-data-loss
Open

KhushalSainS wants to merge 2 commits into
WordPress:trunkfrom
KhushalSainS:fix/save-patterns-data-loss

Conversation

@KhushalSainS

Copy link
Copy Markdown

What this fixes

Save Patterns removed every custom template and template part from the database, not just the ones written to the theme. Templates that did not reference a saved pattern were deleted with no warning.

This change clears only the templates and template parts that were actually written to disk during the save, and only once, after all patterns have been processed.

Fixes #912

Root cause

CBT_Theme_Patterns::replace_local_pattern_references() called clear_user_templates_customizations() and clear_user_template_parts_customizations() without arguments from inside the per-pattern loop. Without arguments those helpers clear every custom template and template part, so each pattern iteration re-cleared the whole set. The collected slugs also mixed wp_template and wp_template_part, so a template named page could delete an unrelated part named page.

Changes

includes/create-theme/theme-patterns.php

  • Collect written slugs per post type (wp_template, wp_template_part) instead of one mixed list.
  • Clear customizations once, after all patterns are processed, via clear_saved_pattern_template_customizations().
  • Record a slug only when its .html file exists on disk, so a failed write never leads to a cleared customization.
  • Keep replacing numeric pattern references in every template/pattern file on each iteration, so files written for an earlier pattern also lose later numeric references.
  • Guard glob() results before looping and use basename() in the duplicate-pattern check so it cannot match a partial path.

includes/create-theme/theme-templates.php

  • get_theme_templates() and add_templates_to_local() accept an optional $post_type.
  • add_templates_to_local() returns early for an empty slug list, instead of exporting every template (WordPress treats an empty slug__in as no filter).
  • clear_user_templates_customizations() and clear_user_template_parts_customizations() accept an optional $slugs list and skip entries without a wp_id.

tests/test-theme-patterns.php

  • Regression tests for:
    • unrelated template part survives when a template referencing a pattern is saved;
    • written templates are cleared before a duplicate-pattern error returns;
    • unrelated part with the same slug as a written template survives;
    • a shared template and part with the same slug both get their references replaced.

Manual before/after

Same WordPress site, same seeded data, Save Patterns on / Save Template Changes off:

Before (trunk) After
Template referencing the pattern cleared cleared
Unrelated custom template deleted kept
Pattern post deleted deleted
Theme files written yes yes
Saved template content <!-- wp:pattern {"slug":"theme/pattern"} /--> same

Also verified with one template referencing two patterns: the saved file contains two local pattern references, zero wp:block {"ref":...} references, and both pattern files are created.

@KhushalSainS
KhushalSainS marked this pull request as ready for review November 16, 2026 15:09
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.

Save Patterns silently deletes unrelated template and template-part customizations

1 participant