Auto-subscribe user groups for notifications

A common request for Edit Flow is to auto-subscribe users and/or user groups for notifications. This would remove the repetition associated with the task, and reduces the likelihood of someone missing a notification when a post changes status or when an editorial comment is left.

Because of the complexity involved, Edit Flow doesn’t offer auto-subscription functionality through the admin. As of v0.7 it’s something you can easily do with a little bit of code.

First, read the introduction to extending Edit Flow. Once you’ve done so, you can modify the code snippet below to work for your needs. Think of this as a mini-framework for doing whatever you want to do at the action. The snippet below shows you how you can follow a user group based on a specific status, and also how you can automatically follow all user groups that the post author is a part of.

[sourcecode lang=”php”]/**
* Auto-subscribe or unsubscribe an Edit Flow user group when a post changes status
*
* @see http://editflow.org/extend/auto-subscribe-user-groups-for-notifications/
*
* @param string $new_status New post status
* @param string $old_status Old post status (empty if the post was just created)
* @param object $post The post being updated
* @return bool $send_notif Return true to send the email notification, return false to not
*/
function efx_auto_subscribe_usergroup( $new_status, $old_status, $post ) {
global $edit_flow;

// When the post is first created, you might want to automatically set
// all of the user’s user groups as following the post
if ( ‘draft’ == $new_status ) {
// Get all of the user groups for this post_author
$usergroup_ids_to_follow = $edit_flow->user_groups->get_usergroups_for_user( $post->post_author );
$usergroup_ids_to_follow = array_map( ‘intval’, $usergroup_ids_to_follow );
$edit_flow->notifications->follow_post_usergroups( $post->ID, $usergroup_ids_to_follow, true );
}

// You could also follow a specific user group based on post_status
if ( ‘copy-edit’ == $new_status ) {
// You’ll need to get term IDs for your user groups and place them as
// comma-separated values
$usergroup_ids_to_follow = array(
// 18,
);
$edit_flow->notifications->follow_post_usergroups( $post->ID, $usergroup_ids_to_follow, true );
}

// Return true to send the email notification
return $new_status;
}
add_filter( ‘ef_notification_status_change’, ‘efx_auto_subscribe_usergroup’, 10, 3 );[/sourcecode]