concept-image / wp-acorn-stubs
Acorn generators for WordPress themes: hooks classes registered in ThemeServiceProvider (make:hooks), wp-cron scheduled actions (make:schedule), wp-ajax AJAX actions (make:ajax), actions, services, helpers and value objects. Install it as a dev dependency.
Package info
gitlab.com/concept-image-public/wp-acorn-stubs
pkg:composer/concept-image/wp-acorn-stubs
Requires
- php: >=8.4
- concept-image/php-file-editor: ^0.2
- roots/acorn: ^6.4
Requires (Dev)
- concept-image/wp-ajax: ^0.2
- concept-image/wp-cron: ^3.1
- concept-image/wp-hook-attributes: ^0.2
- larastan/larastan: ^3.0
- laravel/pint: ^1.18
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^4.0|^5.0
- pestphp/pest-plugin-arch: ^4.0|^5.0
Suggests
- concept-image/wp-ajax: ^0.1 — discovers the app/Actions classes make:ajax generates
- concept-image/wp-cron: ^3.1 — discovers the app/Actions classes make:schedule generates (required by wp-starter-theme)
- concept-image/wp-hook-attributes: ^0.1 — the #[Action] / #[Filter] / #[WhenConfig] attributes of the classes make:hooks generates (required by wp-starter-theme)
Provides
None
Conflicts
None
Replaces
None
README
Acorn generators for the PHP classes of WordPress themes built on Roots Acorn and concept-image/wp-starter-theme:
make:hooks: a hooks class (#[Action]/#[Filter]methods), registered inThemeServiceProvider::$hooks;make:schedule: a WP-Cron scheduled task, anapp/Actionsclass with#[Schedule]that concept-image/wp-cron 3.1 finds by itself;make:ajax: an AJAX action, anapp/Actionsclass with#[Ajax]that concept-image/wp-ajax finds by itself;make:service(optionally bound as a singleton),make:action,make:helper,make:value-object.
Generated files follow the theme's code style (Pint's Laravel preset, no declare(strict_types=1)).
Requirements
- PHP 8.4+
- Roots Acorn 6.4+
- For the generated code:
concept-image/wp-hook-attributes(hooks classes) andconcept-image/wp-cron3.1+ (scheduled tasks), both required by wp-starter-theme;concept-image/wp-ajax(AJAX actions).
Installation
Install it as a development dependency of the theme:
composer require --dev concept-image/wp-acorn-stubs
Acorn discovers the service provider. Run the commands from the theme directory: wp acorn make:….
Commands
| Command | Writes | Options |
|---|---|---|
make:hooks Seo | app/Hooks/Seo.php, added to ThemeServiceProvider::$hooks | --action=, --filter= (repeatable), --when-config=, --register / --no-register |
make:schedule ImportPodcasts | app/Actions/ImportPodcasts.php with #[Schedule('ci_import_podcasts', Every::Daily)] | --hook=, --every=, --interval=, --display=, --at=, --without-overlapping |
make:ajax SubscribeToNewsletter | app/Actions/SubscribeToNewsletter.php with #[Ajax] (answers to ci_subscribe_to_newsletter) | --name=, --logged, --guest, --capability=, --throttle=, --methods=, --get |
make:service Payment | app/Services/PaymentService.php | --singleton |
make:action SendWelcomeEmail | app/Actions/SendWelcomeEmailAction.php | |
make:helper String | app/Helpers/StringHelper.php | |
make:value-object EmailAddress | app/ValueObjects/EmailAddress.php |
Every command takes --force (-f) to overwrite an existing file, and exits with a non-zero code when it fails
(invalid name or option, existing file). Add -n to never be asked anything.
Enums, traits and interfaces: use Acorn's own make:enum, make:trait and make:interface.
Names
The name is a path of segments separated by / (or .): make:action Billing/ChargeCard writes
App\Actions\Billing\ChargeCardAction in app/Actions/Billing/ChargeCardAction.php.
- Each segment is StudlyCased:
billing/charge_cardandBilling/ChargeCardare the same class. - The suffix (
Action,Service,Helper) is added once:make:service Paymentandmake:service PaymentServiceboth writePaymentService. Hooks classes, scheduled actions, AJAX actions and value objects get no suffix. .phpis dropped. The class always goes under the command's namespace.- Refused: an empty name,
..or an empty segment, a segment starting with a digit or holding other characters than letters and digits, a PHP reserved word (Array,List), and the class names a stub imports (Action,Filter,WhenConfigfor hooks;Schedule,Everyfor scheduled actions;Ajax,Access,AjaxRequest,AjaxResponsefor AJAX actions).
make:hooks
wp acorn make:hooks Seo
wp acorn make:hooks Admin/Columns -n --action=admin_init --filter=manage_edit-event_columns --when-config=theme.admin.columns
namespace App\Hooks\Admin;
use ConceptImage\WpHookAttributes\Attributes\Action;
use ConceptImage\WpHookAttributes\Attributes\Filter;
use ConceptImage\WpHookAttributes\Attributes\WhenConfig;
#[WhenConfig('theme.admin.columns')]
final class Columns
{
/**
* Dependencies only: the class is built on every request, the work happens in the hooked methods.
*/
public function __construct()
{
//
}
#[Action('admin_init')]
public function adminInit(): void
{
//
}
#[Filter('manage_edit-event_columns')]
public function manageEditEventColumns(mixed $value): mixed
{
return $value;
}
}
- One method per
--action/--filter(repeatable), named after the hook in camelCase (acf/save_post→acfSavePost), deduplicated. Without them: one#[Action('init')]example method. --when-config=keyadds a class-level#[WhenConfig('key')].- Registration. The command asks "Register it in ThemeServiceProvider::$hooks?" (yes by default).
--registerregisters without asking,--no-registernever does, and-nregisters. The class is appended to the$hooksarray ofapp/Providers/ThemeServiceProvider.php, imported (use App\Hooks\Admin\Columns;, in alphabetical order) and writtenColumns::classas Pint wants it; the file's comments and layout are kept. A class already listed, whatever its spelling (\App\Hooks\Seo::class, orSeo::classwith itsuse), is left alone. When the file can't be edited safely (missing provider, no$hooks = [...]array,array()syntax…), the command says why and prints the line to add.
make:schedule
wp acorn make:schedule ImportPodcasts --at=03:00 --without-overlapping
namespace App\Actions;
use ConceptImage\WpCron\Attributes\Schedule;
use ConceptImage\WpCron\Enums\Every;
#[Schedule('ci_import_podcasts', Every::Daily, at: '03:00', withoutOverlapping: true)]
final class ImportPodcasts
{
/**
* Run the ci_import_podcasts WP-Cron event. Dependencies can be injected as parameters.
*
* Keep it idempotent: a run can be repeated or skipped. Never echo, die or exit (exit ends the whole WP-Cron
* run): throw an exception instead, wp-cron logs it.
*/
public function handle(): void
{
//
}
}
| Option | Default | Rule |
|---|---|---|
--hook= | ci_ + the snakecase class name | `^[a-z0-9]+$` | |
--every= | daily | fifteen-minutes, hourly, twice-daily, daily, weekly, monthly (the Every cases) |
--interval= | seconds, 60 or more, instead of --every | |
--display= | label of the --interval schedule (requires --interval) | |
--at= | first run at HH:MM, site timezone | |
--without-overlapping | skip a run while the previous one still runs |
Only the arguments you set are written. wp-cron 3.1 discovers app/Actions classes with #[Schedule]: nothing to
register (the command warns when wp-cron isn't installed). Then wp acorn cron:list, wp acorn cron:run {hook},
wp acorn cron:sync.
make:ajax
wp acorn make:ajax Favourites/ToggleFavourite --logged --throttle=10,1
namespace App\Actions\Favourites;
use ConceptImage\WpAjax\Attributes\Ajax;
use ConceptImage\WpAjax\Enums\Access;
use ConceptImage\WpAjax\Http\AjaxRequest;
use ConceptImage\WpAjax\Http\AjaxResponse;
#[Ajax(access: Access::Logged, throttle: '10,1')]
final class ToggleFavourite
{
/**
* Answer the ci_toggle_favourite AJAX action. Dependencies can be injected as parameters.
*
* The nonce, access, capability and throttle are already checked. A failed validation answers 422 with the
* messages per field; return AjaxResponse::error() for an expected failure (an uncaught exception answers 500 and
* is logged).
*/
public function handle(AjaxRequest $request): AjaxResponse
{
$request->validate([
//
]);
return AjaxResponse::data([
//
]);
}
}
| Option | Attribute argument | Rule |
|---|---|---|
--name= | the name (first argument) | ^[a-z][a-z0-9_]{0,63}$, not ci_ajax_nonce; default ci_ + the snake_case class name (ci_toggle_favourite) |
--logged | access: Access::Logged | logged-in users only (a guest gets 401); not with --guest |
--guest | access: Access::Guest | guests only (a logged-in user gets 403) |
--capability= | capability: '…' | checked with current_user_can(), implies a logged-in user; not with --guest |
--throttle= | throttle: '10,1' | attempts,minutes per IP (default: wp-ajax's throttle.default) |
--methods= | methods: ['GET', 'POST'] | comma-separated GET and/or POST, case-insensitive, no duplicates (--methods=GET,POST for a listing called both ways); POST alone is the default and writes nothing |
--get | methods: ['GET'] | accept GET instead of POST: shortcut for --methods=GET, not with --methods |
Only the arguments you set are written (#[Ajax] without any). wp-ajax discovers the app/Actions classes with
#[Ajax] (the folder is shared with wp-cron's #[Schedule] classes): nothing to register (the command warns when
wp-ajax isn't installed). Then wp acorn ajax:list, and call it with <x-ajax::form :action="App\Actions\…::class">
or $ajax('ci_…').
make:service, make:action, make:helper, make:value-object
make:service Payment:final readonly class PaymentServicewith a constructor for the injected dependencies.--singletonadds$this->app->singleton(PaymentService::class);toThemeServiceProvider::register()afterparent::register();(same safe editor asmake:hooks, or the line is printed).make:action SendWelcomeEmail:final readonly class SendWelcomeEmailActionwithhandle(): void.make:helper String:final class StringHelperwith a private constructor (static methods only).make:value-object EmailAddress:final readonly class EmailAddresswithpublic string $valueandequals().
Overriding the stubs
wp acorn vendor:publish --tag=wp-acorn-stubs
copies the stubs to the theme's stubs/wp-acorn-stubs/: action.stub, service.stub, helper.stub,
value-object.stub, hooks.stub, schedule.stub, ajax.stub. A stub there replaces the package's; keep only the ones you
change. Stubs at the root of stubs/ (Acorn's, wp-make-block's) are not read.
| Placeholder | Stubs | Value |
|---|---|---|
{{ namespace }}, {{ class }} | all | the namespace and class name |
{{ imports }} | hooks.stub, schedule.stub, ajax.stub | the use lines of the attributes (and of the request and response) |
{{ attributes }} | hooks.stub | the class-level #[WhenConfig(...)], or nothing |
{{ methods }} | hooks.stub | the hooked methods, indented |
{{ schedule }} | schedule.stub | the #[Schedule(...)] line |
{{ hook }} | schedule.stub | the cron hook name |
{{ arguments }} | ajax.stub | the arguments of #[Ajax...]: empty, or (...) |
{{ action }} | ajax.stub | the AJAX action name |
Claude Code
The package ships a Claude Code skill and rules (resources/claude), published to the Bedrock root's .claude/ by
wp-starter-theme's wp acorn starter:ai, or with wp acorn vendor:publish --tag=wp-acorn-stubs-ai
(wp-acorn-stubs-skill, wp-acorn-stubs-rules).
Development
composer ci # Pint, Larastan (max) on src/, PHPStan level 5 on the generated fixtures, Pest
composer lint
UPDATE_FIXTURES=1 composer test # after an intended change of the generated code
The provider editor is built on concept-image/php-file-editor
(tokenizer-based PHP file edits). Until it is on packagist, composer.json requires it from a path repository
(../php-file-editor, a sibling checkout): remove the path repository once concept-image/php-file-editor is on
packagist. Without PHP 8.4 on the host, mount the parent directory so the path resolves:
docker run --rm -u "$(id -u):$(id -g)" -v "$(dirname "$PWD")":/work -w /work/wp-acorn-stubs composer:2 \
sh -c "composer update concept-image/php-file-editor && composer ci"
Upgrading
See UPGRADE.md and the CHANGELOG.
License
The MIT License (MIT). See LICENSE.md.