Skip to content

Facades and Helpers

Lukman Nakib edited this page May 30, 2026 · 1 revision

Facades & Helpers

Facades

Static entry points to deep services, resolved from the container. The scaffold keeps three — each fronts a service worth a memorable static seam:

Facade Use
Asset Register/enqueue + localize scripts and styles (admin-gated).
Cache Transient-backed get/set/remember/flush.
Config Read app/Config/*.php values: Config::get('app.demo.seed_tasks', 5).
use WPPluginMatrix\Facades\Cache;

$count = Cache::remember('books.count', HOUR_IN_SECONDS, fn () => Book::all() ? count(Book::all()) : 0);

Why only three? See docs/adr/0001-collapse-trivial-facades.md. A Facade exists only where the underlying service is deep enough to earn it — don't add one for a thin wrapper.

Helpers

One-liner builders in app/Helpers/.

SettingsPage

SettingsPage::create('my-settings', 'My Settings')
    ->addField('api_key', 'API Key', 'text')
    ->addField('mode', 'Mode', 'select', ['live' => 'Live', 'test' => 'Test'])
    ->register();

Notice

Admin notices. storeForLater() flashes a notice on the next admin load (shown once, then cleared).

Notice::success('Saved!');
Notice::error('Something went wrong');
Notice::storeForLater('Demo data seeded', 'success'); // survives a redirect; flashes once

CPT (Custom Post Type)

CPT::create('portfolio', 'Portfolio', 'Portfolios')
    ->supports('title', 'editor', 'thumbnail')
    ->archive(true)
    ->restApi(true)
    ->register();

Cron

Cron::scheduleDaily('my_cleanup', fn () => /* … */);
Cron::schedule('my_task', 'hourly', fn () => /* … */);
Cron::unschedule('my_task');

For activation-scheduled cron, prefer contributing to the wppm/lifecycle/cron_events filter (see Lifecycle & Migrations) so deactivation cleans it up automatically.

Clone this wiki locally