Custom Recipient
The Recipient API is meant to provide a unified interface for Carriers. You don't have to use them but it's the best to keep the logic separated.
As an example, default Email Carrier registers a couple of Recipients - User selected from a list, Role, Administrator, Plain text email, etc. They all resolves to a flat array of emails. The Webhook provider registers URL recipients: GET, POST, PUT etc.
Your Custom Recipient may allow to select User from a dropdown and parse list of all selected Users to some kind of UUID, the one which is understood by your Custom Carrier.

Recipient class

1
use BracketSpace\Notification\Abstracts;
2
use BracketSpace\Notification\Defaults\Field;
3
​
4
/**
5
* ExampleRecipient Recipient
6
*/
7
class ExampleRecipient extends Abstracts\Recipient {
8
​
9
/**
10
* Constructor
11
*/
12
public function __construct() {
13
parent::__construct( [
14
'slug' => 'recipient_slug',
15
'name' => __( 'Recipient', 'textdomain' ),
16
'default_value' => 'default',
17
] );
18
}
19
​
20
/**
21
* Parses raw recipient value to something consumable by the Carrier.
22
*
23
* @param string $value Raw value saved by the user.
24
* @return array Array of resolved values
25
*/
26
public function parse_value( $value = '' ) {
27
​
28
if ( empty( $value ) ) {
29
$value = [ $this->get_default_value() ];
30
}
31
​
32
$value = do_something_with_the_value( $value );
33
​
34
// Keep in mind you should return an array here.
35
// This is because you may select a recipient which parses to multiple values.
36
// Example: User Role recipient may parse to multiple emails.
37
return [ $value ];
38
​
39
}
40
​
41
/**
42
* Prints the Recipient field.
43
*
44
* @return Field
45
*/
46
public function input() {
47
​
48
// You should build an array of options here if you are using SelectField field.
49
$opts = [
50
'recipient1' => __( 'Recipient 1', 'textdomain' ),
51
'recipient2' => __( 'Recipient 2', 'textdomain' ),
52
];
53
​
54
// You can use other fields as well.
55
return new Field\SelectField( [
56
'label' => __( 'My Recipient', 'textdomain' ),
57
'name' => 'recipient', // Don't change this.
58
'css_class' => 'recipient-value', // Don't change this.
59
'value' => $this->get_default_value(),
60
'pretty' => true,
61
'options' => $opts,
62
] );
63
​
64
}
65
​
66
}
Copied!

Registering the Recipient

All Recipients must be registered before the Carriers, so you need to use the notification/init action instead of notification/elements.
1
add_action( 'notification/init', function() {
2
// You need to provide the Carrier slug.
3
notification_register_recipient( 'carrier-slug', new ExampleRecipient() );
4
} );
Copied!
Last modified 7mo ago