Custom Carrier
Carrier is an element which connects the WordPress action with a service. Default Carriers are Email and Webhook.
If you want to connect with API or other delivery service, you should create a custom Carrier.
Carriers very often needs the Recipients, so you may want to add a custom one as well.

Carrier class

1
use BracketSpace\Notification\Interfaces\Triggerable;
2
use BracketSpace\Notification\Abstracts;
3
use BracketSpace\Notification\Defaults\Field;
4
​
5
/**
6
* ExampleCarrier Carrier
7
*/
8
class ExampleCarrier extends Abstracts\Carrier {
9
​
10
/**
11
* Carrier icon, optional
12
*
13
* @var string SVG
14
*/
15
public $icon = '<svg>...</svg>';
16
​
17
/**
18
* Carrier constructor
19
*/
20
public function __construct() {
21
// Provide the slug and translatable name.
22
parent::__construct( 'example-carrier', __( 'Example Carrier', 'textdomain' ) );
23
}
24
​
25
/**
26
* Used to register Carrier form fields
27
* Uses $this->add_form_field();
28
*
29
* @return void
30
*/
31
public function form_fields() {
32
​
33
$this->add_form_field( new Field\InputField( [
34
'label' => __( 'Example field', 'notification' ),
35
'name' => 'fieldslug',
36
] ) );
37
​
38
// Special field which renders all Carrier's recipients.
39
// You may override name, slug and description here.
40
$this->add_recipients_field( [
41
'name' => 'Items',
42
'slug' => 'items',
43
] );
44
​
45
}
46
​
47
/**
48
* Sends the notification
49
*
50
* @param Triggerable $trigger trigger object.
51
* @return void
52
*/
53
public function send( Triggerable $trigger ) {
54
// Data contains the user data with rendered Merge Tags.
55
$data = $this->data;
56
​
57
// Parsed recipients are also available.
58
$data['parsed_recipients'];
59
​
60
// This is where you should connect with your service to send out the Notifiation.
61
}
62
​
63
}
Copied!

Registering the Carrier

To get the Carrier registered you must create an instance of your class and pass it to the Notification plugin. The best action to do that is notification/init.
1
use BracketSpace\Notification\Register;
2
​
3
add_action( 'notification/init', function() {
4
Register::carrier( new ExampleCarrier() );
5
} );
Copied!
Last modified 4d ago