# Security, Compliance & Accessibility Rules

> **MANDATORY RULES** - These must be followed in ALL code generation and modifications

---

## 🔒 Security Rules (Zero Tolerance)

### Authentication & Authorization
- **NEVER** use `is_admin()` as a security check - it only checks page location
- **ALWAYS** use `current_user_can()` for capability checks
- **REQUIRED** nonce verification for all POST requests: `wp_verify_nonce()` or `check_admin_referer()`
- **FORBIDDEN** `wp_ajax_nopriv_` hooks for sensitive actions
- **MANDATORY** `permission_callback` for all REST API endpoints (never `__return_true`)

### Input Validation & Sanitization
- **VALIDATE EARLY**: Use `is_email()`, `is_int()`, `absint()`, `validate_file()`
- **SANITIZE INPUT**: `sanitize_text_field()`, `sanitize_key()`, `sanitize_email()`
- **ESCAPE LATE**: Use context-specific escaping at output point:
  - HTML: `esc_html()`, `esc_html__()`
  - Attributes: `esc_attr()`, `esc_attr__()`
  - URLs: `esc_url()`
  - JavaScript: `esc_js()`, `wp_json_encode()`

### Database Security
- **FORBIDDEN**: Direct variable concatenation in SQL queries
- **MANDATORY**: Use `$wpdb->prepare()` with placeholders (`%s`, `%d`, `%f`)
- **ORDER BY**: Use whitelist validation or `sanitize_sql_orderby()`
- **LIKE queries**: Use `$wpdb->esc_like()` before `prepare()`

### File Operations
- **UPLOADS**: Always use `wp_handle_upload()` with restricted MIME types
- **INCLUDES**: Validate file paths with `validate_file()` and whitelist
- **FORBIDDEN**: `unserialize()` on user input - use `json_decode()` instead

### Network Security
- **REMOTE REQUESTS**: Use `wp_safe_remote_get()` instead of `wp_remote_get()`
- **REDIRECTS**: Use `wp_safe_redirect()` instead of `wp_redirect()`

---

## 📋 WordPress.org Compliance Rules

### Naming Conventions
- **PREFIX**: All functions, classes, constants must use `presszone_translate_` prefix (minimum 4 chars)
- **DATABASE OPTIONS**: All option names must start with full prefix
- **FORBIDDEN**: Generic names like `save_data()`, `admin_init()`, `Debug()`

### Code Standards
- **DIRECT ACCESS**: Every PHP file must start with: `if ( ! defined( 'ABSPATH' ) ) exit;`
- **TEXT DOMAIN**: Must match plugin slug exactly: `'translate-press-zone'`
- **ENQUEUING**: Never enqueue globally - check specific admin pages with `$hook` parameter
- **NO CDNs**: All assets must be bundled locally

### Performance
- **AUTOLOADING**: Use WordPress autoloading, not Composer for production
- **CACHING**: Implement proper caching for expensive operations
- **DATABASE**: Minimize queries, use proper indexing

---

## ♿ Accessibility Rules (WCAG 2.1 AA)

### Semantic HTML
- **HEADINGS**: Proper hierarchy (h1 → h2 → h3), no skipping levels
- **LANDMARKS**: Use `<main>`, `<nav>`, `<aside>`, `<section>` appropriately
- **FORMS**: Associate labels with inputs using `for` attribute or wrapping

### Keyboard Navigation
- **FOCUS**: All interactive elements must be keyboard accessible
- **SKIP LINKS**: Provide skip navigation for screen readers
- **TAB ORDER**: Logical tab sequence through interface

### Visual Design
- **CONTRAST**: Minimum 4.5:1 for normal text, 3:1 for large text
- **FOCUS INDICATORS**: Visible focus states for all interactive elements
- **RESPONSIVE**: Support zoom up to 200% without horizontal scrolling

### Screen Reader Support
- **ALT TEXT**: Descriptive alt attributes for images
- **ARIA LABELS**: Use `aria-label`, `aria-describedby` for complex UI
- **LIVE REGIONS**: Use `aria-live` for dynamic content updates
- **FORM ERRORS**: Associate error messages with form fields

### WordPress Specific
- **ADMIN NOTICES**: Use proper WordPress notice classes and ARIA
- **HELP TEXT**: Provide contextual help for complex settings
- **COLOR**: Never rely solely on color to convey information

---

## 🚨 Critical Security Patterns to Avoid

```php
// ❌ FORBIDDEN PATTERNS
if ( is_admin() ) { /* sensitive action */ }
echo $_POST['data'];
$wpdb->query("SELECT * FROM table WHERE id = $id");
unserialize($_POST['data']);
wp_redirect($_GET['url']);

// ✅ SECURE PATTERNS  
if ( current_user_can('manage_options') ) { /* action */ }
echo esc_html($_POST['data']);
$wpdb->query($wpdb->prepare("SELECT * FROM table WHERE id = %d", $id));
json_decode($_POST['data'], true);
wp_safe_redirect($_GET['url']);
```

```javascript
// ❌ FORBIDDEN PATTERNS
element.innerHTML = userInput;
eval(userCode);

// ✅ SECURE PATTERNS
element.textContent = userInput;
// Use proper parsing instead of eval
```

---

## 📝 Implementation Checklist

Before completing any code:
- [ ] All user inputs validated and sanitized
- [ ] All outputs properly escaped for context
- [ ] Capability checks in place for sensitive actions
- [ ] Nonce verification for state-changing operations
- [ ] Database queries use prepared statements
- [ ] File operations properly validated
- [ ] Accessibility attributes added
- [ ] Semantic HTML structure used
- [ ] Keyboard navigation tested
- [ ] Screen reader compatibility verified
