I’m answering my own question. I just found the API docs here for filtering out users prior to collecting them for sending:
https://subscribe2.wordpress.com/support/api/
Thanks for the great plugin!
Cheers.
@linkhousemedia
That’s exactly where I’d have pointed you – glad you found it.
@mattyrob,
Any reason you can see that this would stop sending emails altogether?
function s2member_filter($subscribers, $post_id) {
//Who can see this post?
$whosee = get_field('who_can_view', $post_id);
// $subscribers is an array of subscriber email addresses
foreach ( $subscribers as $subscriber_email ) {
$user_info = get_user_by( 'email', $subscriber_email );
$user_role = $user_info->roles[0];
$user_email = $user_info->data->user_email;
if ( $user_role == 'subscriber' || $user_role == 'author' || $user_role == 'contributor' || $user_role == 'editor' ) $user_role = 'member';
if ( $user_role == $whosee || $user_role == 'leadership_team' || $user_role == 'administrator' ) $allow_post[] = $user_email;
}
$filtered_subscribers = array_intersect($subscribers, $allow_post);
return $filtered_subscribers;
} // end s2member_filter()
@linkhousemedia / Jay
That looks okay to me and on some brief testing of your array collection, comparison and intersecting it worked for me excluding a ‘subscriber’ role and including an ‘administrator’.
Then if I set $whosee to ‘member’ the previously excluded email is now included.
Hmmm. Okay I appreciate that test. Thanks!
Perhaps my get_field() (from the advanced custom fields plugin) isn’t firing. I’ll try using post_meta instead to grab that meta value.
Per my other post, do you have a function I can fire for development to force the digest? Even a simple hack that I can undo when I’m finished?
Thanks for everything!
Jay
@linkhousemedia / Jay
I did wonder about whether the get_field() function would be available but that was beyond my ability to check quickly.
As for firing the digest off, the post meta data is updated with each digest so the only option is to cycle the post to regenerate the correct post meta data and have a short digest cycle.
Okay thanks.
Hopefully last question – I don’t think the filters are firing. I added a piece of code that should kill the entire process and nothing happened. Where do I put these filters? Do they have to go inside an init action or something? Right now, my code is just sitting in the theme’s functions.php file.
I think I know what’s happening here. I could be wrong, but I don’t think your plugin filters users for the digest email – only for the per-post emails. I’ve looked at both the publish() and subscribe2_cron() functions, and only in publish does the filtering come into play.
Am I missing something? If I’m right, would you consider adding this functionality to the digest option? If you have a github repository I’d be happy to fork a change.
Thanks for all of the continued support!
@linkhousemedia / Jay
Which API hook are you using at the moment? Have you tried ‘s2_registered_subscribers’.
Thanks @mattyrob, but my needs ended up being too specific and I built my own little digest email system based on user roles.
I do appreciate all of the help.