Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't add programmatic Notification #293

Open
cappelluti opened this issue Oct 1, 2020 · 10 comments
Open

Can't add programmatic Notification #293

cappelluti opened this issue Oct 1, 2020 · 10 comments
Labels

Comments

@cappelluti
Copy link

Hi,
as I see in documentation page (https://docs.bracketspace.com/notification/developer/notifications/programmatic-notifications) I would like to add a notification (with email carrier) by code, not using WP (admin) > Notifications > Add New Notification.

I have already registered in function.php my custom trigger with:

add_action( 'notification/elements', function() {
	require_once ( 'class-my-trigger.php' );
	notification_register_trigger( new MyCustomTrigger() );
});

Now, I would like to associate it to a custom notification declared in function.php. Using the sample in (https://docs.bracketspace.com/notification/developer/notifications/programmatic-notifications) I would expect to see my new notification in the list of WP notification setting page (as it happen adding custom triggers that I can associate to my notifications) but it doesn't.

In particular I tried this code:

add_action( 'notification/trigger/registered', function( $trigger ) {
	if ( 'post/updated' !== $trigger->get_slug() ) {
		return;
	}
	$content = '';
	foreach ( $trigger->get_merge_tags( 'visible' ) as $merge_tag ) {
		$content .= $merge_tag->get_name() . ': {' . $merge_tag->get_slug() . '}' . "\r\n\r\n";
	}
	notification( [
		'title'    => 'My programmatic notification',
		'trigger'  => 'trigger_slug',
		'carriers' => [
			'email' => [
				'activated'  => true,
				'enabled'    => true,
				'subject'    => 'Email from ghost notification!', 
				'body'       => 'This is nice, huh?',
				'recipients' => [
					[
						'type'      => 'role',
						'recipient' => 'administrator',
					],
				],
			],
		],
		'enabled'  => true,
	] );
} );

Thank you.

Greeting,
Enzo

@pewu-dev
Copy link

pewu-dev commented Oct 19, 2020

You should coustomize this example code to your case.
In if statement you have condition that pass only "post/updated" trigger, so this callback not run for your custom trigger. Check your custom trigger slug and place it in condition. Next in notification function you also pass wrong trigger slug, propably this slug doesnt exist so notification is not added. You can put $trigger object or use $trigger->get_slug() (because u have condition below).

@cappelluti
Copy link
Author

cappelluti commented Oct 19, 2020

Hi, thank you for the answer.

This is my Custom Trigger Class. It works perfectly if I associate manually a carrier to it on Wordpress admin plugin page, but I would define it programmaticaly, so that I can change the recipient mail by code:

`
class MyCustomTrigger extends \BracketSpace\Notification\Abstracts\Trigger {

public function __construct() {
    // Add slug and the title.
    parent::__construct(
        'MyCustomTrigger',
        __( 'Invia newsletter a comando', 'MyCustomTrigger' )
    );
    // Hook to the action.
    $this->add_action( 'send_Newsletter_Content', 10, 2 );
	$this->set_description(
		__( 'Descrizione del trigger', 'MyCustomTrigger' )
	);
}

public function action( $t_id , $content) {
		
	// If the message is empty, don't send any notifications.
	if ( empty( $t_id ) ) {
		return false;
	}
	$this->mail_html = $content;
	$this->titolo = "Title";		

}

public function merge_tags() {
	$this->add_merge_tag( new \BracketSpace\Notification\Defaults\MergeTag\StringTag( array(
		'slug'        => 'titolo',
		'name'        => __( 'Titolo dell\'email', 'MyCustomTrigger' ),
		'resolver'    => function( $trigger ) {
			return $trigger->titolo;
			},
	) ) );
	$this->add_merge_tag( new \BracketSpace\Notification\Defaults\MergeTag\HTMLTag( array(
		'slug'        => 'mail_html',
		'name'        => __( 'Email in HTML', 'MyCustomTrigger' ),
		'resolver'    => function( $trigger ) {
			return $trigger->mail_html;
			},
	) ) );
}

}`

I updated the notification with slug "send_Newsletter_Content" but keeps to not work.

`
add_action( 'notification/trigger/registered', function( $trigger ) {
if ( 'send_Newsletter_Content' !== $trigger->get_slug() ) {
return;
}
$content = '';
foreach ( $trigger->get_merge_tags( 'visible' ) as $merge_tag ) {
$content .= $merge_tag->get_name() . ': {' . $merge_tag->get_slug() . '}' . "\r\n\r\n";
}
notification( [
'title' => 'My programmatic notification',
'trigger' => 'send_Newsletter_Content',
'carriers' => [
'email' => [
'activated' => true,
'enabled' => true,
'subject' => 'Email from ghost notification!',
'body' => 'This is nice, huh?',
'recipients' => [
[
'type' => 'email',
'recipient' => '{mymail@gmail.com}',
],
],
],
],
'enabled' => true,
] );
} );

`

Thank you for your help.
Enzo

@pewu-dev
Copy link

Your trigger slug is defined in constructor and is equal to "MyCustomTrigger", not "send_Newsletter_Content" - this is your action.

@jakubmikita
Copy link
Member

@cappelluti If this is only for controlling the recipients, you could use this filter https://github.com/BracketSpace/Notification/blob/develop/src/classes/Defaults/Carrier/Email.php#L132

@pewu-dev I tried to contact you today on Messanger, if you are not using it can we connect in any other way? :)

@cappelluti
Copy link
Author

hi @pewu-dev you can find me on Messanger, my name is Enzo Cappelluti (I tried to contact PeWuDev, but aren't you)

@cappelluti
Copy link
Author

Your trigger slug is defined in constructor and is equal to "MyCustomTrigger", not "send_Newsletter_Content" - this is your action.

Yes, it works. Another question.
As I call my custom trigger with a line like this:

do_action( 'MyCustomTrigger', $t_id , $content);

I would like to pass the same variables $t_id end $content to my custom notification.
When I declare

add_action( 'notification/trigger/registered', function( $trigger ) { 
...
}

how do I get $t_id end $content ?

Thank you

@cappelluti
Copy link
Author

cappelluti commented Oct 22, 2020 via email

@jakubmikita
Copy link
Member

Hi @cappelluti

the Trigger class looks ok. To filter the recipients you can just do this:

add_filter( 'notification/carrier/email/recipients', function( $recipients, $carrier, $trigger ) {
    if ( 'SendNewslettercontent' !== $trigger->get_slug() ) {
        return $recipients;
    }

    // $trigger->mail_html
    // $trigger->title

    return [ 'recipient1@example.com', 'recipient2@example.com' ];
}, 10, 3 );

@cappelluti
Copy link
Author

Hi @Kubitomakita ,
The filter works if I have defined a notification by Wordpress plugin page: a notification associated to my custom Trigger class. Instead I would create my notification by code as with add_action( 'notification/trigger/registered', function($trigger) function.
I don't know how to pass $trigger->mail_html and $trigger->title, created in SendNewslettercontent, in its notification([]), defining different recipients.

Thanks,
Enzo

@gsusI
Copy link

gsusI commented Feb 12, 2023

This might be a bit old; but for others landing on this page.

Notifications registered using notification() do not appear on the Notifications page of the admin dashboard, even if they are being correctly added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants