# Bludit Pro 3.22.0 – Plugin Architect Rulebook

---

## Flash Version: Plugin Boilerplate & Quick Reference

### Plugin Skeleton – Minimal Viable Plugin

Directory: `/bl-plugins/myplugin/plugin.php`
```php
<?php
class myplugin extends Plugin {

    public function init()
    {
        $this->dbFields = array(
            'setting1' => 'default',
            'setting2' => '0'
        );
        // Map hooks to methods (optional)
        $this->customHooks = array(
            'siteHead' => 'injectHead',
            'pageEnd'  => 'appendToPage'
        );
    }

    public function prepare() { }

    // Hook method 1
    public function injectHead()
    {
        echo '<meta name="myplugin-version" content="1.0">';
    }

    // Hook method 2
    public function appendToPage()
    {
        echo '<p>Powered by MyPlugin</p>';
    }

    // Admin settings form
    public function form()
    {
        $html = '<div class="form-group"><label>Setting 1</label>';
        $html .= '<input name="setting1" class="form-control" value="'.$this->getValue('setting1').'">';
        $html .= '</div>';
        return $html;
    }

    public function post()
    {
        $this->setField('setting1', Sanitize::html($_POST['setting1']));
    }
}
```

### Required Language File
`/bl-plugins/myplugin/languages/en.json`:
```json
{
    "plugin-data": {
        "name": "My Plugin",
        "description": "Does something useful."
    }
}
```

### Commonly Used Hooks (for quick reference)

| Hook | Trigger | Typical Use |
|------|---------|-------------|
| `siteHead` | Theme `<head>` | Meta tags, CSS/JS links, analytics |
| `siteBodyBegin` | After `<body>` | Full‑page overlays, alerts |
| `siteBodyEnd` | Before `</body>` | JavaScript, tracking pixels |
| `siteSidebar` | Sidebar area | Widgets, lists, search boxes |
| `pageBegin` | Before article content | Author box, related posts (top) |
| `pageEnd` | After article content | Comments, sharing buttons |
| `paginator` | After paginator built | Custom pagination tweaks |
| `afterPageCreate` | New page published | Indexing, notifications |
| `afterPageModify` | Page edited | Cache clearance |
| `afterPageDelete` | Page deleted | Cleanup |
| `adminHead` | Admin `<head>` | Custom admin CSS/JS |
| `adminSidebar` | Admin sidebar | Add menu links |
| `dashboard` | Dashboard grid | Custom dashboard widgets |
| `editorToolbar` | Content editor toolbar | Add editor buttons |

### Data Persistence Pattern Snippet

```php
// Always declare defaults in init()
public function init() {
    $this->dbFields = array(
        'token' => '',
        'mode'  => 'silent'
    );
}

// Read safely
$token = $this->getValue('token'); // returns null if not defined? actually returns default from dbFields if not set, but because dbFields -> db -> getValue, it's safe.

// Save (after validation)
$this->setField('token', $newToken); // instantly persists
```

### Custom Admin Page via Webhook (Quick Setup)
```php
public function init()
{
    $this->webhook('admin_config', 'adminConfig', true);
}

public function adminConfig()
{
    // Check login and role
    if (!isset($GLOBALS['login']) || !$GLOBALS['login']->isLogged()) {
        Redirect::page('login');
    }
    echo '<h2>My Plugin Config</h2>';
    // ... your interface
    exit;
}
```
The URL will be `/admin/admin_config`.

### Quick Form with CSRF Protection (if needed)
```php
public function myForm()
{
    global $security;
    $html = '<form method="post">';
    $html .= '<input type="hidden" name="tokenCSRF" value="'.$security->getTokenCSRF().'">';
    // ...
    $html .= '</form>';
    return $html;
}
```

### Load Plugin Assets
```php
public function siteHead()
{
    echo '<link rel="stylesheet" href="'.$this->domainPath().'css/style.css">';
    echo $this->includeJS('js/script.js');
}
```
`$this->domainPath()` gives `https://yoursite.com/bl-plugins/myplugin/`.

### Check if Plugin is Installed
```php
if ($this->installed()) {
    // database file exists and loaded
}
```

### Save and Retrieve from Workspace (for custom files)
```php
$ws = $this->workspace(); // /bl-content/workspaces/myplugin/
file_put_contents($ws.'cache.txt', $data);
```

---

*This Rulebook is based on the Bludit Pro 3.22.0 patched codebase and is meant to be used alongside the Pro Developer Reference. For full class method signatures and global constants, refer to that document.*