Create custom recipient list based on user custom meta – need pointers!
-
Hello, thanks for the incredible plugin, I can see the possibilities are huge with this!
I am trying to build a list of custom recipients based on a user meta field. I have the meta field working and can display it in a list like so:
$args = array( 'meta_key' => 'dwd_newsletter_optin', 'meta_value' => 'yes', ); $all_users = get_users($args); echo '<ol>'; foreach ($all_users as $user) { echo '<li><span>' . esc_html($user->ID) . ' : ' . esc_html($user->user_email) . ' : ' . esc_html($user->display_name) . '</span></li>'; } echo '</ol>';
But I am unsure how to incorporate this in to the custom recipient code as provided in the docs here – https://docs.bracketspace.com/notification/developer/recipients/custom-recipient
use BracketSpace\Notification\Abstracts; use BracketSpace\Notification\Defaults\Field; /** * ExampleRecipient Recipient */ class ExampleRecipient extends Abstracts\Recipient { /** * 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 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 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->get_default_value(), 'pretty' => true, 'options' => $opts, ] ); } }
Can anyone give me a pointer on this? I will try some things, but I’m unsure which part of the above code should be the email address, is it ‘recipient1’ etc?
Many thanks for any clues!
- The topic ‘Create custom recipient list based on user custom meta – need pointers!’ is closed to new replies.