• Resolved fychan66

    (@fychan66)


    Thanks for a brilliant plugin, it’s simplified our needs and development time right down for generating scripted emails to our users on certain actions and dates.

    But, we also have our own plugin’s internal database notification table, to generate an internal messaging/notification system, somewhat like Facebook/Twitter notification labels.

    I’ve tried creating a new Carrier object, but the documentation I could find is lacking. Is there some documentation kicking around similar to:
    https://docs.bracketspace.com/notification/developer/triggers/custom-trigger
    but for registering a new Carrier?

    All I’ll really need for the carrier/trigger/merge tags is to hand in a destination user_id (available in the triggers for emails already) a message, and a subject.

    It feels like I should be able to steal a lot from the email carrier, but I’m missing something….

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Kuba Mikita

    (@kubitomakita)

    Hi, I just wrote some documentation for you ??

    Check it here: https://docs.bracketspace.com/notification/v/7/developer/carriers/custom-carrier

    Note: this is valid for the new Notification version (7) which will be released next week.

    Hope this helps!

    Thread Starter fychan66

    (@fychan66)

    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() );
    //} );
    
    Plugin Author BracketSpace

    (@bracketspace)

    Maybe your plugin loads too late? See the loading stack here: https://docs.bracketspace.com/notification/developer/general/plugin-loading-chain

    All the notification related stuff should be invoked on notification/init and all the elements like carriers on notification/elements

    Thread Starter fychan66

    (@fychan66)

    Thank you – resolved. You’re correct (obviously) – I wasn’t actioning the additions for the right things, at the right times, in the right places.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Creating a new custom Carrier’ is closed to new replies.