| 1 |
# Imagify – AI Coding & Architecture Guidelines |
| 2 |
|
| 3 |
This file defines NON-NEGOTIABLE rules for any AI-assisted work |
| 4 |
(Claude Code, ChatGPT, JetBrains AI Assistant, Cursor, etc.) |
| 5 |
in this repository. |
| 6 |
|
| 7 |
Skills define behavioral guidance. |
| 8 |
AGENTS.md defines mandatory guardrails. |
| 9 |
If a conflict exists, AGENTS.md prevails. |
| 10 |
|
| 11 |
The objective is to keep Imagify: |
| 12 |
|
| 13 |
- WordPress.org compliant |
| 14 |
- Architecturally consistent |
| 15 |
- Secure |
| 16 |
- Maintainable |
| 17 |
- Review-friendly |
| 18 |
|
| 19 |
This document applies to ALL automated or AI-generated changes. |
| 20 |
|
| 21 |
--- |
| 22 |
|
| 23 |
# 1. Project Overview |
| 24 |
|
| 25 |
Imagify is a single-edition WordPress plugin for image optimization. |
| 26 |
|
| 27 |
- **Repo:** `wp-media/imagify-plugin` |
| 28 |
- **Plugin slug:** `imagify` |
| 29 |
- **PHP namespace root:** `Imagify\` |
| 30 |
- **PSR-4 root:** `classes/` |
| 31 |
|
| 32 |
There is no FREE/PRO split. The codebase has two layers: |
| 33 |
|
| 34 |
- `classes/` — modern PSR-4 code, namespace `Imagify\`, `declare(strict_types=1)` required. **New features go here.** |
| 35 |
- `inc/classes/` — legacy classmap code, `Imagify_` prefix. **Do not add new classes here; migrate out instead.** |
| 36 |
|
| 37 |
When modifying architecture: |
| 38 |
- Prefer the modern `classes/` layer for all new work. |
| 39 |
- Follow service provider + subscriber pattern for wiring. |
| 40 |
|
| 41 |
--- |
| 42 |
|
| 43 |
# 2. Technology Stack |
| 44 |
|
| 45 |
- PHP 7.3+ (strict types, PSR-4 autoloading via Composer) |
| 46 |
- WordPress plugin APIs (hooks, options, WP-CLI, AJAX) |
| 47 |
- League Container (DI container + service providers + event subscribers) |
| 48 |
- ActionScheduler (async background jobs) |
| 49 |
- Strauss (Composer dependency namespace prefixing → `Imagify\Dependencies\`) |
| 50 |
- JavaScript / Grunt (`_dev/` pipeline → `assets/`) |
| 51 |
- Playwright + TypeScript (E2E testing under `Tests/e2e/`) |
| 52 |
|
| 53 |
--- |
| 54 |
|
| 55 |
# 3. Code Structure |
| 56 |
|
| 57 |
``` |
| 58 |
classes/ New PHP code (PSR-4, Imagify\ namespace) |
| 59 |
inc/ Legacy PHP includes (procedural, no namespace) |
| 60 |
inc/classes/ Legacy class files migrating toward classes/ |
| 61 |
assets/ Compiled frontend assets (do not edit directly) |
| 62 |
_dev/ Frontend source (JS, SCSS, Grunt config) |
| 63 |
views/ PHP view templates |
| 64 |
Tests/ PHPUnit tests |
| 65 |
Tests/e2e/ Playwright E2E tests (TypeScript) |
| 66 |
bin/ CLI scripts (dev-up, dev-down, dev-seed, test-e2e, build-knowledge-graph) |
| 67 |
docs/ Documentation (E2E_TESTING.md, etc.) |
| 68 |
.aiassistant/ Skill files for AI assistants |
| 69 |
.claude/agents/ Claude Code sub-agents (qa-engineer, e2e-qa-tester) |
| 70 |
``` |
| 71 |
|
| 72 |
--- |
| 73 |
|
| 74 |
# 4. Coding Standards & Static Analysis |
| 75 |
|
| 76 |
Source of truth: |
| 77 |
|
| 78 |
- Composer scripts (`composer.json`) |
| 79 |
- PHPCS ruleset (`phpcs.xml`) |
| 80 |
- PHPStan config (`phpstan.neon.dist`) |
| 81 |
- WordPress Plugin Check: https://github.com/WordPress/plugin-check/ |
| 82 |
- CI pipeline rules |
| 83 |
|
| 84 |
Imagify must remain compatible with WordPress.org validation rules. |
| 85 |
|
| 86 |
Any change affecting public APIs, output, security, metadata, or |
| 87 |
plugin bootstrap behavior must be evaluated against WordPress Plugin Check expectations. |
| 88 |
|
| 89 |
AI MUST: |
| 90 |
|
| 91 |
- Read `composer.json` first and use the defined scripts (e.g. `phpcs`, `phpcbf`, `run-stan`, `test-unit`, `test-integration`) instead of inventing commands. |
| 92 |
- Auto-discover PHPCS configuration and follow it as the single source of truth. |
| 93 |
|
| 94 |
## 4.1 Tooling Auto-Discovery (MANDATORY) |
| 95 |
|
| 96 |
Before making changes that affect standards or formatting, the agent MUST locate and respect the repository configuration files. |
| 97 |
|
| 98 |
### Required reads (in this order) |
| 99 |
1. `composer.json` — use scripts defined in `"scripts"` whenever possible; prefer the exact commands used by CI; do not invent lint/test commands. |
| 100 |
2. PHPCS ruleset (first match wins): `phpcs.xml`, `phpcs.xml.dist` |
| 101 |
3. Static analysis configs (if present): `phpstan.neon.dist` |
| 102 |
|
| 103 |
### Execution rules |
| 104 |
- Do NOT hardcode PHPCS standards. |
| 105 |
- Do NOT assume WordPress-Core or WordPress-Extra unless defined in the ruleset. |
| 106 |
|
| 107 |
If no PHPCS configuration exists, stop and ask. |
| 108 |
|
| 109 |
--- |
| 110 |
|
| 111 |
# 5. Architectural Integrity |
| 112 |
|
| 113 |
AI must NOT: |
| 114 |
|
| 115 |
- Introduce global state. |
| 116 |
- Add new singletons or `InstanceGetterTrait` usage in `classes/`. |
| 117 |
- Bypass dependency injection patterns used in the project. |
| 118 |
- Couple UI logic to infrastructure logic. |
| 119 |
- Add new classes to `inc/classes/`. |
| 120 |
|
| 121 |
Follow existing patterns: |
| 122 |
|
| 123 |
- Service providers (`classes/*/ServiceProvider.php`) |
| 124 |
- Subscribers (`classes/*/Subscriber.php` implementing `SubscriberInterface`) |
| 125 |
- Container-based wiring (`config/providers.php`) |
| 126 |
- Strict types in all new `classes/` files |
| 127 |
|
| 128 |
--- |
| 129 |
|
| 130 |
# 6. Testing & Validation |
| 131 |
|
| 132 |
For every change: |
| 133 |
|
| 134 |
1. Ensure no new PHPCS violations. |
| 135 |
2. Ensure static analysis still passes. |
| 136 |
3. Avoid altering unrelated test behavior. |
| 137 |
4. Do not delete tests unless clearly obsolete. |
| 138 |
|
| 139 |
If modifying templates: |
| 140 |
- Validate escaping correctness. |
| 141 |
- Ensure no functional regressions. |
| 142 |
|
| 143 |
--- |
| 144 |
|
| 145 |
# 7. E2E Testing |
| 146 |
|
| 147 |
Two Claude Code sub-agents in `.claude/agents/` support QA workflows: |
| 148 |
|
| 149 |
| Agent | Use when | |
| 150 |
|-------|----------| |
| 151 |
| `qa-engineer` | Validating a PR against its ticket spec (strategy selection, test report) | |
| 152 |
| `e2e-qa-tester` | Driving the browser via Playwright, converting flows to spec files | |
| 153 |
|
| 154 |
Full E2E testing documentation: [](docs/E2E_TESTING.md`docs/E2E_TESTING.md`](docs/E2E_TESTING.md](docs/E2E_TESTING.md) |
| 155 |
|
| 156 |
The test directory is `Tests/e2e/` (capital T, consistent with the existing `Tests/` PHPUnit directory). |
| 157 |
|
| 158 |
The E2E suite runs in CI via `.github/workflows/e2e.yml`. The `IMAGIFY_TESTS_API_KEY` GitHub secret must be configured for optimization tests to run. |
| 159 |
|
| 160 |
--- |
| 161 |
|
| 162 |
# 8. Local Development |
| 163 |
|
| 164 |
```bash |
| 165 |
# Start the local WordPress environment (Docker via wp-env) |
| 166 |
bash bin/dev-up.sh |
| 167 |
|
| 168 |
# Stop (preserves data) / full wipe |
| 169 |
bash bin/dev-down.sh |
| 170 |
bash bin/dev-down.sh --clean |
| 171 |
|
| 172 |
# Seed test data (idempotent) |
| 173 |
bash bin/dev-seed.sh |
| 174 |
|
| 175 |
# Run E2E tests locally (sources .env.local for API key automatically) |
| 176 |
bash bin/test-e2e.sh |
| 177 |
bash bin/test-e2e.sh --headed # watch the browser |
| 178 |
bash bin/test-e2e.sh --ui # Playwright interactive UI |
| 179 |
bash bin/test-e2e.sh specs/smoke # single spec |
| 180 |
``` |
| 181 |
|
| 182 |
Create `.env.local` at the repo root (gitignored) with: |
| 183 |
``` |
| 184 |
IMAGIFY_TESTS_API_KEY=your-key-here |
| 185 |
``` |
| 186 |
|
| 187 |
- Site: `http://localhost:8888` |
| 188 |
- Admin: `http://localhost:8888/wp-admin` — `admin` / `password` |
| 189 |
|
| 190 |
--- |
| 191 |
|
| 192 |
# 9. AI Working Protocol |
| 193 |
|
| 194 |
AI must work in small, incremental changes. |
| 195 |
|
| 196 |
After each logical change set: |
| 197 |
- explain what changed |
| 198 |
- explain why |
| 199 |
- list potential edge cases |
| 200 |
|
| 201 |
AI must NOT: |
| 202 |
|
| 203 |
- Perform massive automated refactors without approval. |
| 204 |
- Reorganize files without explicit instruction. |
| 205 |
- Rewrite entire classes when a minimal fix is sufficient. |
| 206 |
|
| 207 |
## 9.1 Git Commit & Push Policy |
| 208 |
|
| 209 |
By default, AI may only **suggest** commit messages and must not run `git commit` or `git push`. |
| 210 |
|
| 211 |
**Exception — Issue Workflow:** When operating under the issue-workflow skill (triggered by `/task <number>`, `issue <number>`, or `#<number>`), the agent MAY: |
| 212 |
|
| 213 |
1. Run atomic `git commit` calls — one commit per logical, self-contained change set. |
| 214 |
2. Run `git push` exactly once after all commits are ready, to publish the branch. |
| 215 |
3. Create a GitHub Pull Request using the prepared PR draft. |
| 216 |
4. Monitor PR CI status checks until all pass or a failure is detected. |
| 217 |
|
| 218 |
Atomic commit rules: |
| 219 |
- Each commit must pass PHPCS and static analysis before being committed. |
| 220 |
- Commit message format: `type(scope): short description` (Conventional Commits). |
| 221 |
- No `Co-Authored-By` lines in commits. |
| 222 |
- Do not squash unrelated changes into a single commit. |
| 223 |
- Do not amend commits that have already been pushed. |
| 224 |
|
| 225 |
--- |
| 226 |
|
| 227 |
# 10. PR Hygiene |
| 228 |
|
| 229 |
Changes must: |
| 230 |
|
| 231 |
- Be minimal and scoped. |
| 232 |
- Have clear intent. |
| 233 |
- Avoid noise in diff. |
| 234 |
- Avoid unrelated formatting changes. |
| 235 |
|
| 236 |
--- |
| 237 |
|
| 238 |
# 11. Security First |
| 239 |
|
| 240 |
Always assume: |
| 241 |
|
| 242 |
- User input is untrusted. |
| 243 |
- Remote API responses are untrusted. |
| 244 |
- Stored values may be tampered with. |
| 245 |
|
| 246 |
Never: |
| 247 |
|
| 248 |
- Store sensitive values in plain text without review. |
| 249 |
- Introduce unsafe serialization. |
| 250 |
- Echo unescaped dynamic data. |
| 251 |
|
| 252 |
--- |
| 253 |
|
| 254 |
# 12. When in Doubt |
| 255 |
|
| 256 |
Stop. |
| 257 |
Explain the ambiguity. |
| 258 |
Ask for clarification. |
| 259 |
|
| 260 |
Architectural integrity is more important than speed. |
| 261 |
|
| 262 |
--- |
| 263 |
|
| 264 |
# 13. Sub-Agents |
| 265 |
|
| 266 |
Reusable specialist agents live in `.aiassistant/agents/`. Claude Code discovers them via the `.claude/agents` symlink; other tools can read them directly from `.aiassistant/agents/`. |
| 267 |
|
| 268 |
| Agent | File | Invoke when | |
| 269 |
|-------|------|-------------| |
| 270 |
| `qa-engineer` | `.aiassistant/agents/qa-engineer.md` | Validating a PR against its ticket spec — reads acceptance criteria, runs functional/browser/analysis strategies, produces a structured test report | |
| 271 |
| `e2e-qa-tester` | `.aiassistant/agents/e2e-qa-tester.md` | Driving the browser via Playwright, walking through "How to test" steps, converting validated flows into Playwright spec files under `Tests/e2e/` | |
| 272 |
|
| 273 |
The `qa-engineer` agent delegates browser flows to `e2e-qa-tester` automatically when the change involves admin UI. |
| 274 |
|
| 275 |
--- |
| 276 |
|
| 277 |
# 14. Skills Activation |
| 278 |
|
| 279 |
The repository defines AI Skills under `.aiassistant/skills/`. |
| 280 |
|
| 281 |
Agents MUST activate the relevant skill depending on the task: |
| 282 |
|
| 283 |
| Task | Skill | |
| 284 |
|------|-------| |
| 285 |
| Template or UI changes | WordPress Compliance | |
| 286 |
| Structural or architectural changes | Imagify Architecture | |
| 287 |
| Service modifications | Both skills | |
| 288 |
| Codebase exploration / dependency tracing | Knowledge Graph | |
| 289 |
| Working on a GitHub issue | Issue Workflow | |
| 290 |
|
| 291 |
## 13.1 Knowledge Graph |
| 292 |
|
| 293 |
A pre-built dependency graph is available at `.aiassistant/graph/dependency-graph.json`. |
| 294 |
|
| 295 |
Before exploring the codebase structure (finding a class, tracing dependencies, exploring namespaces), **read this file first**. It contains: |
| 296 |
- `nodes`: per-file namespace, declared symbols, and imports. |
| 297 |
- `symbol_index`: maps every fully-qualified PHP class/interface/trait/enum to its file. |
| 298 |
|
| 299 |
Run `node bin/build-knowledge-graph.js` to refresh after structural changes (`--full` to force rebuild). |
| 300 |
|
| 301 |
--- |
| 302 |
|
| 303 |
# 15. Repository Specs |
| 304 |
|
| 305 |
The repository may define task-specific implementation specs under `.aiassistant/specs/`. |
| 306 |
|
| 307 |
Specs provide detailed guidance for recurring technical problems |
| 308 |
(e.g. PHPCS warnings, architecture migrations, WordPress compliance patterns). |
| 309 |
|
| 310 |
When a relevant spec exists, agents must follow it in addition to AGENTS.md and the applicable skills. |
| 311 |
|
| 312 |
--- |
| 313 |
|
| 314 |
# AI Task Priority |
| 315 |
|
| 316 |
When executing tasks, agents must prioritize: |
| 317 |
|
| 318 |
1. Security |
| 319 |
2. WordPress.org compliance |
| 320 |
3. Architectural integrity |
| 321 |
4. Backward compatibility |
| 322 |
5. Minimal diffs |
| 323 |
6. Performance |
| 324 |
|
| 325 |
AGENTS.md remains the final authority. |
| 326 |
|