Custom Trigger
New, custom triggers can be easily registered in your plugin or theme. All you need is a simple class declaration and a function call.
The Trigger is really only a wrapper for WordPress' action(s).

Trigger class

Have a look at the example Trigger class.
1
/**
2
* Custom trigger class
3
*/
4
class CustomTrigger extends \BracketSpace\Notification\Abstracts\Trigger {
5
6
/**
7
* Constructor
8
*/
9
public function __construct() {
10
11
// 1. Slug, can be prefixed with your plugin name.
12
// 2. Title, should be translatable.
13
parent::__construct(
14
'myplugin/custom_trigger',
15
__( 'Custom Trigger title', 'textdomain' )
16
);
17
18
// 1. Action hook.
19
// 2. (optional) Action priority, default: 10.
20
// 3. (optional) Action args, default: 1.
21
// It's the same as add_action( 'any_action_hook', 'callback', 10, 2 ) with
22
// only difference - the callback is always action() method (see below).
23
$this->add_action( 'any_action_hook', 10, 2 );
24
25
// 1. Trigger group, should be translatable.
26
// This is optional, Group is displayed in the Trigger select.
27
$this->set_group( __( 'My Triggers', 'textdomain' ) );
28
29
// 1. Trigger description, should be translatable.
30
// This is optional, Description is displayed in the Trigger select.
31
$this->set_description(
32
__( 'Fires when a page with "myparam" parameter is visited', 'textdomain' )
33
);
34
35
}
36
37
/**
38
* Assigns action callback args to object
39
* Return `false` if you want to abort the trigger execution
40
*
41
* You can use the action method arguments as usually.
42
*
43
* @return mixed void or false if no notifications should be sent
44
*/
45
public function action( $param_one, $param_two ) {
46
47
/**
48
* This is a method callback hooked to the action you've added in the Constructor.
49
*
50
* Two important things which are happening here:
51
* - $this->callback_args is a numeric array containing all the callback parameters
52
* if you want to treat them as an array
53
* - if you want to abort Trigger execution, you must return false here
54
*/
55
56
// If the second parameter ($process in our action) is false then abort, no carriers will be processed.
57
if ( false === $param_two ) {
58
return false;
59
}
60
61
// We can assign any property here, whole object will be accessible in Merge Tag resolver.
62
$this->param_value = $param_one;
63
64
}
65
66
/**
67
* Registers attached merge tags
68
*
69
* @return void
70
*/
71
public function merge_tags() {
72
73
/**
74
* In this method you can assign any Merge Tags to the trigger.
75
*
76
* To see what Merge Tags are available go to Notification plugin's core
77
* and look in class/Defaults/MergeTag directory.
78
*/
79
80
$this->add_merge_tag( new \BracketSpace\Notification\Defaults\MergeTag\StringTag( [
81
// Slug (required), this will be used as {parametrized_url} value.
82
// Don't translate this.
83
'slug' => 'parametrized_url',
84
// Name (required), should be translatable.
85
'name' => __( 'Parametrized URL', 'textdomain' ),
86
// Resolver (required), this can be a closure like below or function name
87
// like: 'parametrized_url_resolver' or array( $this, 'parametrized_url_resolver' ).
88
'resolver' => function( $trigger ) {
89
// Trigger object is available here,
90
// with all the properties you set in action() method.
91
return add_query_arg( 'source', $trigger->param_value, site_url() );
92
},
93
// Description (optional), should be translatable, default: ''.
94
'description' => __( 'Homepage URL with ?source= param', 'textdomain' ),
95
// Example indicator (optional)
96
// if true, then description will have "Example" label, default: false.
97
'example' => true,
98
] ) );
99
100
}
101
102
}
Copied!

Registering the Trigger

Having this class is only a first step. To get the trigger registered you must create an instance of this class and pass it to the Notification plugin:
1
notification_register_trigger( new CustomTrigger() );
Copied!
If you are experiencing issues like Fatal error: Class ‘BracketSpace\Notification\Abstracts\Trigger’ not found please wrap your class definition and function call in the notification/boot/initial action.
Last modified 2yr ago