# Learn From Session - Update Project Rules

Extract project conventions, patterns, and reusable components discovered in this session. Update `CLAUDE.md` so future agents follow established patterns and avoid duplication.

## Goal

Capture project-specific knowledge:
- Naming conventions and prefixes
- Design patterns and architecture
- **Shared components and reusable code**
- **What already exists - don't rewrite it**
- WordPress integration patterns
- What NOT to do and why

## Step 1: Identify Patterns & Conventions

### Naming & Structure
- Class/function naming patterns
- File naming conventions
- CSS class prefixes and BEM patterns
- JavaScript global/module naming
- Database table/column naming
- Hook and action naming

### Reusable Code & Components
- **Shared PHP classes** - What exists, what they do, when to use them
- **Shared JS modules** - Global objects, utility functions
- **Shared CSS components** - Variables, mixins, common classes
- **Helper functions** - Template helpers, utilities
- **Validation methods** - Centralized checks to reuse

### Design Patterns
- Where validation logic lives (don't duplicate)
- Where sanitization happens (don't duplicate)
- How errors are handled (AJAX vs forms)
- How components communicate
- Centralized vs distributed logic

### Code Quality
- **DRY violations found** - Code that was duplicated and shouldn't be
- **Bloat removed** - Unnecessary code that was cleaned up
- **Modular patterns** - How to keep files small and focused
- **Single responsibility** - One class/function = one job

### Anti-Patterns
- Duplicated logic that should call shared code
- Inline implementations that should use existing components
- Patterns that look right but cause issues

## Step 2: Format as Rules

```markdown
### [Convention Name]

[What this convention ensures / why it exists]

```language
// CORRECT - Use existing component
$creator->checkExternalLinks($msg);  // Reuse PostCreator

// WRONG - Duplicating logic (bloat, maintenance burden)
$urlPattern = '#https?://...#';      // NO! Already exists in PostCreator
if (preg_match($urlPattern...))      // This is 30 lines that already exist
```
```

**Include:**
- Reference to existing shared code location
- Why duplication is harmful here
- File paths for reusable components

## Step 3: Update CLAUDE.md

1. Read `CLAUDE.md` in plugin root
2. Add to existing sections or create new ones
3. Update "Common Mistakes to Avoid" table
4. **Update "Reusable PHP Components" section with new discoveries**
5. Preserve existing rules - only add new ones

**Key sections to update:**
- Reusable PHP Components (§8)
- JavaScript Rules (§4) - shared modules
- CSS/SCSS Rules (§3) - shared variables
- Common Mistakes table (§11)

## Step 4: Report

```
## Shared Components Documented

| Component | Location | Use For |
|-----------|----------|---------|
| [class/function] | [file path] | [when to use] |

## DRY Violations Fixed

| Duplicated Code | Should Use Instead |
|-----------------|-------------------|
| [what was duplicated] | [existing component] |

## Rules Added

| Convention/Pattern | Section | Prevents |
|--------------------|---------|----------|
| [pattern] | §[num] | [duplication/bloat/etc] |
```

## Examples of What to Capture

| Discovery | Rule |
|-----------|------|
| URL validation exists in PostCreator | "Use `PostCreator->checkExternalLinks()` - never inline URL regex" |
| Toast component exists in admin | "Use `Toast.js` - never create inline notifications" |
| CSS variables defined in _variables.scss | "Use `var(--presszone-forum-*)` - never hardcode colors" |
| Query class handles all DB with caching | "Use `Query` class - never raw `$wpdb` for forum tables" |
| BBCode parsing is centralized | "Use `PostCreator->parseMessage()` - never parse BBCode inline" |

## Key Principles

1. **Before writing code, search for existing solutions**
2. **If logic exists in 2+ places, centralize it**
3. **Small, focused files > large monolithic files**
4. **One class = one responsibility**
5. **Document shared components so others find them**
