• Resolved jnorell

    (@jnorell)


    Hello,

    What I am trying to accomplish is to have a notification triggered when a custom post type is published (easy), and I run a custom query to build the list of recipients for the notification (based on some settings I added with ACF plugin). It seems like I probably need to define my own Recipient class for this, which is my first approach to solve.

    First thing, when I define a class which extends Abstracts\Recipient in my theme and call notification_register_recipient() with it, I do not see a new recipient type show up when editing my notification. I know the class loads and is registered, because if I reuse an existing recipient class slug like ‘administrator’ I get an error that it’s already in use. And I know the class itself works, because I can put it in the Notification code under class/Defaults/Recipient/ and register it in inc/defaults/recipients.php and it will show up as expected. So my guess is it’s something to do with the timing of when my registration is called (it’s in a theme, so all plugins_loaded hooks have fired before my code can run). Any ideas?

    My next issue/question is what information does the parse_value() function have available? It seems like that is the place I should do my custom query and return an array of recipients. But I need to know the post id which triggered the “post published” notification. Or I need to know a taxonomy value from that post, if there’s an easy way for parse_value() to expand a merge tag.

    And my last question/thought: is this even the right approach to solve this issue? I might try creating a custom merge tag instead, as it looks like I have access to the post id from that. It just seems like a “Recipient class” would be the right way to handle logic related to recipients, so I started there.

    Thanks much!
    Jesse

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter jnorell

    (@jnorell)

    Of course after posting, I immediately come across the answer “by chance” .. for my first issue, it sounds like it’s definitely a problem because I’m integrating in a theme: https://github.com/BracketSpace/Notification/issues/201 I’ll move things to a plugin instead. The two questions/issues remain.

    Thanks!

    Plugin Author Kuba Mikita

    (@kubitomakita)

    Hi Jesse,

    yes, this issue seems to be related!

    This can be really done in two ways:

    1. A custom merge tag

    You have access to the trigger variables, including the post ID etc. You can build your query in there and return comma-separated emails which can be then used in the Email / Merge Tag recipient.

    This would require only one hook and one method call – add_merge_tag. See the guide.

    2. A custom recipient

    This is a very clean approach, but more code to write. Chapeau bas for taking this path and going so far. You are close.

    To clarify some things – the recipient is not connected and it’s not aware of the trigger you are using. Ie. you cannot access any trigger properties from the recipient methods. And this is the part you are missing here to build your query.

    So I suggest doing it like this:
    – create a recipient with a resolvable input value, just like in the email recipient – in this field, you’ll provide the post ID merge tag, ie. {post_ID} or {page_ID} or {custom_post_type_ID}
    – in the recipient parse_value method the $value param will be resolved post ID, so you can get_post_meta( $value ) and build your query.
    – this parse_value method should return an array of emails

    And voila! I hope this helps

    Thread Starter jnorell

    (@jnorell)

    Thanks, that’s the piece I was missing.

    Is there any way for the parse_value method to read the slug of the notification itself? Eg. say I create a Notification with title ‘Test 123’ I’d assume that would get a slug test-123, which I’d like to use in parse_value to apply a filter that varies per notification (in this case ‘notification/recipient/custom/test-123’).

    Thanks,
    Jesse

    Plugin Author Kuba Mikita

    (@kubitomakita)

    This is, unfortunately, not possible.

    What you could do is input into this recipient field something like this:

    dosomethingwith:{post_ID} or unfiltered:{post_ID}

    Then in the parse_value method you can just do explode( ':', $value ); and you get an array with item 0 which is the way you want to parse it and item 1 which is the rendered merge tag.

    Thread Starter jnorell

    (@jnorell)

    Ok, that’s about what I have now. I’m actually creating this as a new ‘Custom’ recipient class that can be filtered by any theme/plugin to supply a list of recipients (merge request coming soon). A couple thoughts:

    – this is almost identical to the current ’email’ class with just a bit of difference in processing the value (parse filter id, and run value through filter to get recipients), would you rather see it right in class/Defaults/Recipient/Email.php or as a separate class/choice ?

    – assuming you might include this in the next release, would it be worth making a notification slug available in order to simplify people using this feature (they would have a consistent/determinate filter name to utilize, rather than having to add their own in the value)? I suppose if doing so it would be necessary to display that slug somewhere, possibly making it editable by the user, and maybe display the final filter name for convenience (“You can use the notification/recipients/your-new-slug filter in your plugin or theme to generate a list of recipients”).

    – for the sake of custom filtering (add_filter) of recipients, doing so from a merge tag would allow more info be passed to the filter function (ie. info from the trigger), would you see that as a better solution? or even have both available?

    Thanks…

    • This reply was modified 5 years, 5 months ago by jnorell.
    Plugin Author Kuba Mikita

    (@kubitomakita)

    Unfortunately adding this filter with the notification slug (hash) or anything won’t be possible. There’s a lack of context in there.

    A custom recipient with input would be the best because it would be configurable per Notification. Or, if you want it to be filterable, you could go with a custom merge tag with a filter and use it within the Email / Merge tag recipient.

    There’s also this filter: https://github.com/BracketSpace/Notification/blob/develop/class/Defaults/Carrier/Email.php#L128 which can give you the Carrier object (Email subject, body etc) and Trigger object (like Post updated).

    Another place where you could hook in is this action: https://github.com/BracketSpace/Notification/blob/develop/class/Abstracts/Trigger.php#L250 where the third parameter is the Notification object. Having this you can setup the recipients field of the Email carrier.

    Thread Starter jnorell

    (@jnorell)

    A custom recipient with input would be the best because it would be configurable per Notification.

    I’ll get a merge request sent shortly, and you can see if it’s anything you’d want to include. Thanks for the links to the other filters, that last one especially would probably have been my solution had I seen it sooner. ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘custom Recipient class’ is closed to new replies.