LogoLogo
WordPress.orgExtensionsSupport
Version 9
Version 9
  • Notification – Custom Notifications and Alerts for WordPress
  • Updating to v9
  • Known issues
  • πŸ€Έβ€β™€οΈ User guide
    • Update broke my site
    • How Notification plugin works
    • Who can use this plugin
    • How to create Notifications
    • Troubleshooting
    • Advanced
      • How to escape { character
      • Background processing
      • How to setup different FROM address for different Notifications
      • Custom Post Type support
      • Disable upselling
      • How to send HTML Emails
  • πŸ”§Developer
    • General
      • Plugin loading chain
      • Runtime
      • Extension possibilities
      • Creating an extension
      • Customizations
      • Bundling Notification plugin
      • White label mode
    • Notifications
      • Suppressing the Notification
      • JSON synchronization
      • Programmatic Notifications
    • Triggers
      • List of all default Triggers
      • Custom Trigger
      • Enable support for non-public Custom Post Type
      • Adding Merge Tags to existing Triggers
    • Carriers
      • Custom Carrier
      • Adding custom fields to Carrier form
      • Suppressing the Carrier
    • Recipients
      • Custom Recipient
    • Snippets
      • General
        • Automatic Trigger testing
        • Allow other roles to edit Notifications
        • Programmatic Notification with manual Trigger
        • Background Processing filter
      • Triggers
        • Post
        • User
      • Integations
        • WP All Import
        • MemberPress
  • 🧩Extensions
    • Installation
    • Planned extensions
    • 3rd Party Extensions
    • Custom Fields
      • v2.2
      • v1.4
      • v1.3
    • Conditionals
    • Email Attachments
    • Push
    • Scheduled Triggers
    • Slack
    • Twilio
    • Webhooks
Powered by GitBook
On this page
  • Recipient class
  • Registering the Recipient

Was this helpful?

  1. Developer
  2. Recipients

Custom Recipient

PreviousRecipientsNextSnippets

Last updated 6 months ago

Was this helpful?

The API is meant to provide a unified interface for Carriers. You don't have to use them but it's best to keep the logic separated.

As an example, the default Email Carrier registers a couple of Recipients - User selected from a list, Role, Administrator, Plain text email, etc. They all resolve to a flat array of emails. The Webhook provider registers URL recipients: GET, POST, PUT, etc.

Your Custom Recipient may allow you to select a User from a dropdown and parse a list of all selected Users to some kind of UUID, the one which is understood by your .

Recipient class

use BracketSpace\Notification\Repository\Recipient\BaseRecipient;
use BracketSpace\Notification\Repository\Field;

/**
 * ExampleRecipient Recipient
 */
class ExampleRecipient extends BaseRecipient
{
    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct(
            [
                'slug' => 'recipient_slug',
                'name' => __('Recipient', 'textdomain'),
                '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 parseValue($value = '')
    {
        if (empty($value)) {
            $value = [$this->getDefaultValue()];
        }

        $value = doSomethingWithTheUserValue($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 should build an array of options here if you are using SelectField field.
        $opts = [
            'recipient1' => __('Recipient 1', 'textdomain'),
            'recipient2' => __('Recipient 2', 'textdomain'),
        ];

        // You can use other fields as well.
        return new Field\SelectField(
            [
                'label' => __('My Recipient', 'textdomain'),
                'name' => 'recipient', // Don't change this.
                'css_class' => 'recipient-value', // Don't change this.
                'value' => $this->getDefaultValue(),
                'pretty' => true,
                'options' => $opts,
            ] 
        );
    }
}

Registering the Recipient

Since version 8.0.0, recipients can be registered anytime on notification/init action and it doesn't matter if it's before or after the Carrier is registered.

use BracketSpace\Notification\Register;

add_action('notification/init', function() {
    // You need to provide the Carrier slug.
    Register::recipient('carrier-slug', new ExampleRecipient());
});
πŸ”§
Custom Carrier
Recipient