• Resolved PinkishHue

    (@pinkishhue)


    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!

    • This topic was modified 3 years, 11 months ago by PinkishHue.
    • This topic was modified 3 years, 11 months ago by PinkishHue.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter PinkishHue

    (@pinkishhue)

    Should I instead be creating a custom merge tag that contains an array of email addresses?

    Tried this but the array was empty, clearly I’m not quite grasping this…

    function merge_tags() {
    
        $this->add_merge_tag( new \BracketSpace\Notification\Defaults\MergeTag\EmailTag( array(
            'slug'        => 'users_newsletter_optin',
            'name'        => __( 'Users Newsletter Opt-in'),
            'resolver'    => function( $trigger ) {
    
    $args	=	array(  
            'meta_key'     => 'dwd_newsletter_optin',
            'meta_value'   => 'yes',
    			);
    
    $author = get_users($args);
                return $author->user_email;
            },
        ) ) );
    
    }
    Plugin Author Kuba Mikita

    (@kubitomakita)

    Hi @pinkishhue

    you should be using a Custom Recipient, that’s a good call!

    And it’s easy, you just need to return the array of emails from the parse_value() method. Check this example plugin how we are checking for a particular user meta.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Create custom recipient list based on user custom meta – need pointers!’ is closed to new replies.