Search by

concept-image / wp-acorn-stubs

concept-image

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

Issues

pkg:composer/concept-image/wp-acorn-stubs

Statistics

Installs: 1 014

Dependents: 0

Suggesters: 0

Stars: 0

2.1.2 2026-12-03 13:27 UTC

This package is auto-updated.

Last update: 2026-12-03 12:28:03 UTC


README

Latest Version on Packagist License

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 in ThemeServiceProvider::$hooks;
  • make:schedule: a WP-Cron scheduled task, an app/Actions class with #[Schedule] that concept-image/wp-cron 3.1 finds by itself;
  • make:ajax: an AJAX action, an app/Actions class 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) and concept-image/wp-cron 3.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

CommandWritesOptions
make:hooks Seoapp/Hooks/Seo.php, added to ThemeServiceProvider::$hooks--action=, --filter= (repeatable), --when-config=, --register / --no-register
make:schedule ImportPodcastsapp/Actions/ImportPodcasts.php with #[Schedule('ci_import_podcasts', Every::Daily)]--hook=, --every=, --interval=, --display=, --at=, --without-overlapping
make:ajax SubscribeToNewsletterapp/Actions/SubscribeToNewsletter.php with #[Ajax] (answers to ci_subscribe_to_newsletter)--name=, --logged, --guest, --capability=, --throttle=, --methods=, --get
make:service Paymentapp/Services/PaymentService.php--singleton
make:action SendWelcomeEmailapp/Actions/SendWelcomeEmailAction.php
make:helper Stringapp/Helpers/StringHelper.php
make:value-object EmailAddressapp/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_card and Billing/ChargeCard are the same class.
  • The suffix (Action, Service, Helper) is added once: make:service Payment and make:service PaymentService both write PaymentService. Hooks classes, scheduled actions, AJAX actions and value objects get no suffix.
  • .php is 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, WhenConfig for hooks; Schedule, Every for scheduled actions; Ajax, Access, AjaxRequest, AjaxResponse for 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=key adds a class-level #[WhenConfig('key')].
  • Registration. The command asks "Register it in ThemeServiceProvider::$hooks?" (yes by default). --register registers without asking, --no-register never does, and -n registers. The class is appended to the $hooks array of app/Providers/ThemeServiceProvider.php, imported (use App\Hooks\Admin\Columns;, in alphabetical order) and written Columns::class as Pint wants it; the file's comments and layout are kept. A class already listed, whatever its spelling (\App\Hooks\Seo::class, or Seo::class with its use), 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
    {
        //
    }
}
OptionDefaultRule
--hook=ci_ + the snakecase class name | `^[a-z0-9]+$`
--every=dailyfifteen-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-overlappingskip 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([
            //
        ]);
    }
}
OptionAttribute argumentRule
--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)
--loggedaccess: Access::Loggedlogged-in users only (a guest gets 401); not with --guest
--guestaccess: Access::Guestguests 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
--getmethods: ['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 PaymentService with a constructor for the injected dependencies. --singleton adds $this->app->singleton(PaymentService::class); to ThemeServiceProvider::register() after parent::register(); (same safe editor as make:hooks, or the line is printed).
  • make:action SendWelcomeEmail: final readonly class SendWelcomeEmailAction with handle(): void.
  • make:helper String: final class StringHelper with a private constructor (static methods only).
  • make:value-object EmailAddress: final readonly class EmailAddress with public string $value and equals().

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.

PlaceholderStubsValue
{{ namespace }}, {{ class }}allthe namespace and class name
{{ imports }}hooks.stub, schedule.stub, ajax.stubthe use lines of the attributes (and of the request and response)
{{ attributes }}hooks.stubthe class-level #[WhenConfig(...)], or nothing
{{ methods }}hooks.stubthe hooked methods, indented
{{ schedule }}schedule.stubthe #[Schedule(...)] line
{{ hook }}schedule.stubthe cron hook name
{{ arguments }}ajax.stubthe arguments of #[Ajax...]: empty, or (...)
{{ action }}ajax.stubthe 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.