This file provides guidance to Claude Code and other AI coding agents when working with code in this repository.
Pattern Builder is a WordPress plugin developed by Twenty Bellows. It allows WordPress users to create, edit, organize, and manage block patterns directly in the admin interface — unifying theme patterns (PHP files) and user-created patterns (custom post type) in a single, intuitive UI with visual editing, code editing, live preview, and export capabilities.
- Version: 1.0.4
- Repository: https://github.com/twenty-bellows/pattern-builder
- Issue Tracker: GitHub Issues — https://github.com/twenty-bellows/pattern-builder/issues
- Plugin URI: https://www.twentybellows.com/pattern-builder/
- License: GPL-2.0-or-later
- WordPress Requires: 6.6+
- PHP Requires: 7.2+
A full architectural analysis is in docs/architecture.md. Key decisions to understand before working in this codebase:
The core problem: WordPress's block editor can only edit things with a post ID. File-based theme patterns (.php files in /patterns/) have no post ID. The plugin solves this with a DB mirror + REST hijacking strategy.
DB Mirror (tbell_pattern_block CPT): Each theme pattern file gets a corresponding tbell_pattern_block post that gives it a database identity. This post is the source of the post ID the editor needs. The file remains the source of truth; the DB record is kept in sync.
REST Hijacking: The plugin intercepts /wp/v2/blocks requests at three filter points:
rest_request_after_callbacks(GET) — injects theme pattern posts into the blocks response so the editor sees them alongside user patternsrest_pre_dispatch(PUT/DELETE) — intercepts saves and deletes before the real handler runs, writing changes to the PHP file on disk instead of (or in addition to) the DB
Pattern Registration (on init): On every page load, the plugin globs the theme's /patterns/ directory and upserts DB records for any new or changed patterns. This is a known performance issue (TWE-369) — no caching yet.
Editor Integration: Two things happen in the editor:
syncedPatternFilterinterceptscore/patternblocks to enable editing synced theme patterns in contextPatternPanelAdditionsPluginadds sidebar panels (Source, Sync Status, Associations) when editing awp_blockpost
Admin Page: Plain PHP (Appearance → Pattern Builder). Links to documentation. No JS.
Companion Plugin: synced-patterns-for-themes is a read-only subset of this plugin for production use. It uses the same REST hijacking approach but blocks edits. It self-deactivates when Pattern Builder is active.
- Node.js (v18+ recommended)
- PHP 7.2+ with Composer
- Docker (for
wp-envlocal WordPress environment and PHP integration tests)
- Docker is available (host socket shared). All
wp-envcommands work. wp-envbinary is atnode_modules/.bin/wp-env— run via npm scripts from this directory.- First
npm run startwill pull WordPress Docker images (~1-2 min).
- Several PHP lint violations exist in the codebase (Yoda conditions, inline comment formatting). These are pre-existing and not regressions. Fix them if you touch the file; don't feel obligated to fix unrelated files.
npm run build- Production build with minificationnpm run watch- Development build with hot reloadnpm run format- Format JavaScript codenpm run lint:js- Lint JavaScript filesnpm run lint:css- Lint CSS/SCSS filescomposer format- Format PHP code using WordPress coding standardscomposer lint- Lint PHP code
npm run test:unit- Run JavaScript unit tests (no Docker required)npm run test:unit:watch- Run JavaScript tests in watch modenpm run test:php- Run PHP unit tests in wp-env environment (requires Docker)npm run test:php:watch- Run PHP tests in watch mode (requires Docker)composer test- Run PHP tests directly via PHPUnit (requires WP test bootstrap)
npm run start- Start wp-env with xdebug enablednpm run stop- Stop wp-envnpm run clean- Clean wp-envnpm run plugin-test-env- Start WP Playground for testingnpm run plugin-test- Full build, zip, and test workflow
The plugin follows a component-based OOP architecture with clear separation of concerns:
- Main Entry Point:
pattern-builder.phpinitializes the plugin - Core Class:
Pattern_Builder(singleton inincludes/class-pattern-builder.php) bootstraps all plugin components - Component Classes (
includes/):Pattern_Builder_API- REST API endpoints under/pattern-builder/v1/Pattern_Builder_Admin- Admin UI under Appearance → Pattern BuilderPattern_Builder_Editor- Block editor integrationPattern_Builder_Post_Type- Custom post type for pattern storagePattern_Builder_Security- Security/nonce helpersPattern_Builder_Localization- i18n support
- Build System: Webpack via
@wordpress/scriptswith two entry points:src/PatternBuilder_EditorTools.js- Editor-specific functionality (Gutenberg sidebar panels)src/PatternBuilder_Admin.js- Admin interface (Appearance → Pattern Builder page)
- React Components in
src/components/:PatternBrowserPanel- Main pattern browsing interfacePatternCreatePanel- Pattern creation flowPatternPreview- Pattern preview renderingBlockBindingsPanel- Block bindings configuration panelPatternAssociationsPanel,PatternSyncedStatusPanel,PatternPanelAdditions,PatternSourcePanel- Editor sidebar panelsEditorSidePanel- Editor sidebar containerAdminLandingPage- Main admin page componentPatternList- Pattern list/grid viewPatternBuilderConfiguration- Plugin settings UI
- State Management: WordPress data stores via
src/utils/store.js
- Supports both theme patterns (PHP files in
patterns/) and user patterns (custom post typepattern_builder) - Abstract pattern class (
src/objects/AbstractPattern.js) provides unified interface - Pattern syncing capabilities between theme files and database
Endpoints registered under /wp-json/pattern-builder/v1/. Authentication via WordPress nonce system.
- PHP classes follow WordPress coding standards with proper namespace (
TwentyBellows\PatternBuilder) - JavaScript follows WordPress/Gutenberg patterns using
@wordpresspackages - Assets enqueued with proper dependency management using
.asset.phpfiles generated by Webpack - Security: nonce verification on all state-changing operations, capability checks, data sanitization
- PHP: WordPress Coding Standards (WPCS 3.x) via PHPCS. Config:
phpcs.xml.dist - JavaScript: ESLint via
@wordpress/scriptsdefaults - CSS/SCSS: Stylelint via
@wordpress/scriptsdefaults - Formatting: Prettier (wp-prettier) for JS/CSS
Version is tracked in:
pattern-builder.php(plugin header)package.jsonreadme.txt
Use npm run version-bump to bump all at once.