Programmatic Notifications

The notifications don't necessarily have to be saved in WordPress database nor loaded from JSON files. A good example is dynamic trigger testing where all the notifications are created on the fly.

Basic example

You can define the notification with a simple array. Below is a minimal example with all the required parameters.

use BracketSpace\Notification\Register;

Register::notificationFromArray(
    [
        'title'    => 'My programmatic notification', // For internal reference.
        'trigger'  => 'trigger_slug', // Trigger slug (can be a Triggerable object).
        'carriers' => [ // An array with format: carrier_slug => data array
            'email' => [
                'activated'  => true, // Must be true.
                'enabled' => true, // Must be true.
                'subject' => 'Email from ghost notification!', 
                'body' => 'This is nice, {user_first_name}, huh?',
                'recipients' => [
                    [
                        'type' => 'administrator',
                        'recipient' => '',
                    ],
                    [
                        'type' => 'email',
                        'recipient' => '{user_email}',
                    ],
                ],
            ],
        ],
        'enabled'  => true, // Must be true.
    ]
);

You can reference the Carrier fields by checking the source code. You should check the add_form_field method calls which will contain the field slug and other useful info about the type.

You can also register an existing Notification object

use BracketSpace\Notification\Register;
use BracketSpace\Notification\Core\Notification;

// @var Notification $notification
Register::notification($notificaiton);

All arguments

All arguments available:

use BracketSpace\Notification\Register;

Register::notification([
    'hash' => 'my_programmatic_notification', // Unique notification hash, automatically generated.
    'title' => 'My programmatic notification',
    'trigger' => 'trigger_slug',
    'carriers' => [
        'email' => [
            'activated' => true,
            'enabled' => true,
            'subject' => 'Email from ghost notification!', 
            'body' => 'This is nice, {user_first_name}, huh?',
            'recipients' => [
                [
                    'type' => 'administrator',
                    'recipient' => '',
                ],
                [
                    'type' => 'email',
                    'recipient' => '{user_email}',
                ],
            ],
        ],
    ],
    'enabled' => true,
    'extras' => [], // Extra data array, ie. config for add-ons.
    'version' => time(), // Version of the notification, should be a timestamp. Default: current time.
]);

Last updated