BracketSpace / Notification

WordPress Notification plugin
https://docs.bracketspace.com/notification/
GNU General Public License v3.0
170 stars 30 forks source link

Can't add programmatic Notification #293

Open cappelluti opened 4 years ago

cappelluti commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

jakubmikita commented 4 years ago

@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 commented 4 years ago

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

cappelluti commented 4 years ago

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 commented 4 years ago

Hi, I’m sorry for insistence but I don’t understand how to follow your suggestion. I’m using the following class to send email:

class SendNewslettercontent extends \BracketSpace\Notification\Abstracts\Trigger { public function construct() { parent::construct('SendNewslettercontent', ( 'Send different newsletter (post, video, suppl..)', 'SendNewslettercontent' )); $this->add_action( 'send_Newsletter_content', 10, 2 ); } public function action($t_id , $type_content) { if ( empty( $t_id ) ) { return false; } $this->title = getMyTitle($type_content); $this->mail_html = getMyBody($t_id, $type_content); } public function merge_tags() { $this->add_merge_tag( new \BracketSpace\Notification\Defaults\MergeTag\StringTag( array( 'slug' => 'title', 'name' => __( 'title email', 'SendNewslettercontent' ), 'resolver' => function( $trigger ) {return $trigger->title;}, ))); $this->add_merge_tag( new \BracketSpace\Notification\Defaults\MergeTag\HTMLTag( array( 'slug' => 'mail_html', 'name' => ( 'Email in HTML', 'SendNewslettercontent' ), 'resolver' => function( $trigger ) {return $trigger->mail_html;}, ))); } }

In this way I can trigger my sending email and manage its subject and content:

add_action( 'notification/elements', function() { notification_register_trigger( new SendNewslettercontent() ); } ); do_action( 'send_Newsletter_content', $t_id , $type_content)

I would control the recipient starting by $type_content variable. You suggested me to use this filter https://github.com/BracketSpace/Notification/blob/develop/src/classes/Defaults/Carrier/Email.php#L132 but I don’t understand how in the past code.

Alternatively I tried to add:

add_action( 'notification/trigger/registered', function($trigger) { if ( SendNewslettercontent!== $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' => SendNewslettercontent, 'carriers' => [ 'email' => [ 'activated' => true, 'enabled' => true, 'subject' => 'HOW DO I GET BY CUSTOM SUBJECT?, 'body' => HOW DO I GET BY BODY?, 'recipients' => [ [ 'type' => 'email', 'recipient' => my@email.com, ], ], ], ], 'enabled' => true, ] ); } );

This sends correctly email to recipients that I can define by code but I don’t know how to get title and mail_html

Thank you for your help. Very gratefull.

Greetings, Enzo Cappelluti

Da: Jakub Mikita notifications@github.com Inviato: lunedì 19 ottobre 2020 18:45 A: BracketSpace/Notification Notification@noreply.github.com Cc: Enzo Cappelluti e.cappelluti@seedstm.com; Mention mention@noreply.github.com Oggetto: Re: [BracketSpace/Notification] Can't add programmatic Notification (#293)

@cappellutihttps://github.com/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

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/BracketSpace/Notification/issues/293#issuecomment-712292298, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ABR5ZTFPVVR57H4JWAMVFPDSLRUJDANCNFSM4SARAYTA.

jakubmikita commented 4 years ago

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 commented 4 years ago

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 _addaction( 'notification/trigger/registered', function($trigger) function. I don't know how to pass _$trigger->mailhtml and $trigger->title, created in SendNewslettercontent, in its notification([]), defining different recipients.

Thanks, Enzo

gsusI commented 1 year ago

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.