Thanks Kuba.
I followed the instructions (I think!), but wrapping the notification_register_carrier()
inside a add_action( 'notification/elements', function() {})
seems to cause the carrier not to be available.
If I leave it outside of that add_action hook, I can set up the carrier, and assign it to a notification as expected, but unfortunately it doesn’t seem to actually be triggered like the email one does.
Feeling a little bit dim, I have to admit.
Here’s the file I’ve created in my plugin folder:
<?php
/**
* Email Carrier
*
* @package notification
*/
namespace BracketSpace\Notification\Defaults\Carrier;
use BracketSpace\Notification\Interfaces\Triggerable;
use BracketSpace\Notification\Abstracts;
use BracketSpace\Notification\Defaults\Field;
/**
* Email Carrier
*/
class wpnzcfcn_DB extends Abstracts\Carrier {
/**
* Carrier icon
*
* @var string SVG
*/
public $icon = '<svg xmlns="https://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M22 18.055v2.458c0 1.925-4.655 3.487-10 3.487-5.344 0-10-1.562-10-3.487v-2.458c2.418 1.738 7.005 2.256 10 2.256 3.006 0 7.588-.523 10-2.256zm-10-3.409c-3.006 0-7.588-.523-10-2.256v2.434c0 1.926 4.656 3.487 10 3.487 5.345 0 10-1.562 10-3.487v-2.434c-2.418 1.738-7.005 2.256-10 2.256zm0-14.646c-5.344 0-10 1.562-10 3.488s4.656 3.487 10 3.487c5.345 0 10-1.562 10-3.487 0-1.926-4.655-3.488-10-3.488zm0 8.975c-3.006 0-7.588-.523-10-2.256v2.44c0 1.926 4.656 3.487 10 3.487 5.345 0 10-1.562 10-3.487v-2.44c-2.418 1.738-7.005 2.256-10 2.256z"/></svg>';
/**
* Carrier constructor
*
* @since 5.0.0
*/
public function __construct() {
parent::__construct( 'wpnzcfcn-database', __( 'Internal Notification', 'wpnzcfcn' ) );
}
/**
* Used to register Carrier form fields
* Uses $this->add_form_field();
*
* @return void
*/
public function form_fields() {
$this->add_form_field( new Field\InputField( [
'label' => __( 'Subject', 'wpnzcfcn' ),
'name' => 'subject',
] ) );
$body_field = new Field\TextareaField( [
'label' => __( 'Body', 'wpnzcfcn' ),
'name' => 'body',
'allowed_unfiltered' => true,
] );
$this->add_form_field( $body_field );
$this->add_form_field( new Field\RecipientsField( [
'carrier' => $this->get_slug(),
] ) );
}
/**
* Sends the notification
*
* @param Triggerable $trigger trigger object.
* @return void
*/
public function send( Triggerable $trigger ) {
$data = $this->data;
// Neither the transients, or the log files, get written...
set_transient(
'wpnzcfcn_transient_notification_test_data',
$data,
1 * DAY_IN_SECONDS
);
set_transient(
'wpnzcfcn_transient_notification_test_trigger',
$trigger,
1 * DAY_IN_SECONDS
);
wpnzcfcn_log_error( 'notification',json_encode($data) );
wpnzcfcn_log_error( 'notification',json_encode($trigger) );
}
}
/**
* ExampleRecipient Recipient
*/
class UserID extends Abstracts\Recipient {
/**
* Constructor
*/
public function __construct() {
parent::__construct( [
'slug' => 'recipient_user_id',
'name' => __( 'User ID', 'wpnzcfcn' ),
'default_value' => 'default',
] );
}
/**
* Parses raw recipient value to something consumable by the Carrier.
*
* @param string $value Raw value saved by the user.
* @return array Array of resolved values
*/
public function parse_value( $value = '' ) {
if ( empty( $value ) ) {
$value = [ $this->get_default_value() ];
}
//$value = do_something_with_the_value( $value );
// Keep in mind you should return an array here.
// This is because you may select a recipient which parses to multiple values.
// Example: User Role recipient may parse to multiple emails.
return [ $value ];
}
/**
* Prints the Recipient field.
*
* @return Field
*/
public function input() {
// You can use other fields as well.
return new Field\InputField( [
'label' => __( 'Notification Recipients', 'wpnzcfcn' ),
'name' => 'recipient', // Don't change this.
'css_class' => 'recipient-value', // Don't change this.
'value' => $this->get_default_value(),
'pretty' => true,
'description' => __( 'You can use any valid merge tag.', 'wpnzcfcn' ),
'resolvable' => true,
] );
}
}
// This doesn't work if wrapped.
//add_action( 'notification/elements', function() {
notification_register_recipient( 'wpnzcfcn-database', new UserID() );
notification_register_carrier( new wpnzcfcn_DB() );
//} );