Examples
Real-world code samples for WPZylos Events.
User Registration Flow
Events
namespace App\Events;
class UserRegistered
{
public function __construct(
public readonly User $user,
public readonly string $registrationSource
) {}
}
class WelcomeEmailSent
{
public function __construct(
public readonly User $user,
public readonly string $messageId
) {}
}
Listeners
namespace App\Listeners;
class SendWelcomeEmail
{
public function handle(UserRegistered $event): void
{
$messageId = wp_mail(
$event->user->email,
'Welcome!',
'Thanks for registering.'
);
// Dispatch follow-up event
$this->dispatcher->dispatch(new WelcomeEmailSent($event->user, $messageId));
}
}
class CreateUserProfile
{
public function handle(UserRegistered $event): void
{
update_user_meta($event->user->ID, 'profile_created', true);
update_user_meta($event->user->ID, 'registration_source', $event->registrationSource);
}
}
Registration
$provider->addListener(UserRegistered::class, [SendWelcomeEmail::class, 'handle']);
$provider->addListener(UserRegistered::class, [CreateUserProfile::class, 'handle']);
Order Processing
Subscriber
namespace App\Subscribers;
class OrderEventSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
OrderPlaced::class => 'onOrderPlaced',
OrderPaid::class => 'onOrderPaid',
OrderShipped::class => 'onOrderShipped',
OrderCompleted::class => 'onOrderCompleted',
];
}
public function onOrderPlaced(OrderPlaced $event): void
{
// Send confirmation email
$this->sendOrderConfirmation($event->order);
// Reserve inventory
$this->inventoryService->reserve($event->order->items);
// Log order
$this->logger->info('Order placed', ['order_id' => $event->order->id]);
}
public function onOrderPaid(OrderPaid $event): void
{
// Record payment
$this->paymentService->recordPayment($event->order, $event->payment);
// Send receipt
$this->sendPaymentReceipt($event->order, $event->payment);
}
public function onOrderShipped(OrderShipped $event): void
{
// Send tracking email
$this->sendTrackingInfo($event->order, $event->trackingNumber);
}
public function onOrderCompleted(OrderCompleted $event): void
{
// Request review
$this->requestReview($event->order);
// Update stats
$this->statsService->recordCompletedOrder($event->order);
}
}
Plugin Integration
Service Provider
class MyPluginEventServiceProvider extends ServiceProvider
{
public function boot(): void
{
$provider = $this->app->make(ListenerProvider::class);
// Register listeners
$provider->addListener(UserRegistered::class, [SendWelcomeEmail::class, 'handle']);
// Register subscribers
$eventProvider = $this->app->make(EventServiceProvider::class);
$eventProvider->subscribe(new OrderEventSubscriber());
$eventProvider->subscribe(new NotificationSubscriber());
}
}
Controller Usage
class RegistrationController
{
public function __construct(
private EventDispatcher $dispatcher
) {}
public function register(Request $request): Response
{
$user = $this->createUser($request->all());
$this->dispatcher->dispatch(new UserRegistered($user, 'web'));
return Response::json(['success' => true]);
}
}
Validation Events
class FormSubmitted extends StoppableEvent
{
public array $errors = [];
public function __construct(
public readonly array $data
) {}
public function addError(string $field, string $message): void
{
$this->errors[$field] = $message;
}
public function hasErrors(): bool
{
return count($this->errors) > 0;
}
}
// Validation listener
$provider->addListener(FormSubmitted::class, function (FormSubmitted $event) {
if (empty($event->data['email'])) {
$event->addError('email', 'Email is required');
}
if ($event->hasErrors()) {
$event->stopPropagation();
}
}, 1); // Low priority value - runs first
// Processing listener (only runs if no errors)
$provider->addListener(FormSubmitted::class, function (FormSubmitted $event) {
// Process form - only reached if validation passed
}, 100);