Please or Register to create posts and topics.

Getting notified on Ultimate Member's Real-Time Notifications add-on

Page 1 of 2Next

Hi @asgaros, I know that you’re not supporting other plugins, but I’m trying to get the notifications redirected to the Ultimate Member’s Real-Time Notifications rather than being emailed. This way when they are online they will see when someone responds immediately and their email box won’t get hit with all sorts of emails. This is specific to an online event, so it would be very handy.

So in trying to get this to work, I’m looking at your hooks to see if you have something that I can trigger the notifications upon subscribing to a topic. I noticed that the subscription link updates the user’s meta key “asgarosforum_subscription_topic” with the post ID, which will help me identify and link back from the notifications screen; however, triggering the notification and preventing the emails is where I’m at right now.

I found on your list of hooks ?:

  • asgarosforum_filter_notify_global_topic_subscribers_message
  • asgarosforum_filter_notify_topic_subscribers_message
  • asgarosforum_filter_notify_mentioned_user_message
  • asgarosforum_subscriber_mails_new_post
  • asgarosforum_subscriber_mails_new_topic

Any help would be greatly appreciated. Thanks in advance.

Hey @gogrw,

if you want a trigger to create the real time notifications I would advice you to check out the ‘asgarosforum_user_replacements’ filter. This will give you one array with the replacements for the email and and the WP_USER object for the receiver. This filter will fire separately for every mail/user that will be sent.

If you want to remove the online users from the mailing list and only show the instant notification you will have the problem, that WordPress doesn’t give you the information if a user is online or not. But to stop Asgaros Forum from sending the email would be quite simple. Just use the ‘asgarosforum_subscriber_mails_new_post’ and ‘asgarosforum_subscriber_mails_new_topic’ filter and remove the recipients from the list.

I’m quite busy at the moment but I will try to write the documentation for these filters next. I will let you know when it’s done.

How does your code look right now?

Need professional help with Asgaros Forum? Book an appointment with us at domra Web Solutions for setup and customization services. Learn more about our Toolbox for Asgaros Forum plugin to enhance your forum experience.

Thank you, @qualmy91!

I’m a little confused about the asgarosforum_user_replacements filter. Maybe you can elaborate on that?

EDIT: Okay, `asgarosforum_after_add_post_submit` and `asgarosforum_after_add_topic_submit` work for triggering the notifications, and now I’m able to pass the post and topic info. I am stuck at trying to get the subscribed users. Tried to use the same logic from the notify_about_new_post() function, but it’s not working.

Here is what I have now. See STEP 3.

/*
 *   Asgaros Forum -> UM Notifications API
 */
// STEP 1: Extend the filter: um_notifications_core_log_types with your new notification type
add_filter('um_notifications_core_log_types', 'add_asgaros_notification_type', 200 );
function add_asgaros_notification_type( $array ) {
    
  $array['asgaros_subscription'] = array(
    'title' => 'Forum Notification', // title for reference in backend settings
    'template' => 'Discussions: {message}', // the template, {member} is a tag, this is how the notification will appear in your notifications
    'account_desc' => 'When there is activity on my discussions', // title for account page (notification settings)
  );
    
  return $array;
}

// STEP 2: Add an icon and color to this new notification type
add_filter('um_notifications_get_icon', 'add_asgaros_notification_icon', 10, 2 );
function add_asgaros_notification_icon( $output, $type ) {
  
  if ( $type == 'asgaros_subscription' ) { // note that our new action id is "asgaros_subscription" from previous filter
    $output = '<i class="um-icon-chatbox" style="color: #00b56c"></i>';
  }
  
  return $output;
}

// STEP 3: Add the notification trigger when a user posts in a subscribed forum/topic/post
add_action('asgarosforum_after_add_post_submit', 'trigger_asgaros_notification', 20, 6);
add_action('asgarosforum_after_add_topic_submit', 'trigger_asgaros_notification', 20, 6);
function trigger_asgaros_notification( $post_id ) {
  global $um_notifications;
    global $asgarosforum;
    
    $post = $asgarosforum->content->get_post($post_id);
    $topic = $asgarosforum->content->get_topic($post->parent_id);
//    $forum = $asgarosforum->asgarosforum->content->get_forum($topic->parent_id);

    // Get more data.
    $post_link = $asgarosforum->rewrite->get_post_link($post_id, $topic->id);
    $topic_name = esc_html(stripslashes($topic->name));
    $author_name = $asgarosforum->getUsername($post->author_id);
    
    // Get the user's avatar
    $photo = um_get_avatar_url( get_avatar( $post->author_id, 40 ) ); 

    // Write the notification message
    $message = $author_name.' posted on discussion: '.$topic_name; 
      
    // Set the UM notification variables
    $vars['photo'] = $photo;
    $vars['member'] = $author_name;
    $vars['message'] = $message;
    $vars['notification_uri'] = $post_link;
    
    // Prepare mailing-list.
    $topic_subscribers = array();

    // Get topic subscribers.
    $topic_subscribers_query = array(
        'fields'        => array('id', 'user_email'),
        'exclude'       => array(get_current_user_id()),
        'meta_key'      => 'asgarosforum_subscription_topic',
        'meta_value'    => $topic->id,
        'meta_compare'  => '='
    );

    $get_users_result = get_users($topic_subscribers_query);

    if (!empty($get_users_result)) {
        $topic_subscribers = array_merge($topic_subscribers, $get_users_result);
    }

    // Get global post subscribers.
    $topic_subscribers_query = array(
        'fields'        => array('id', 'user_email'),
        'exclude'       => array(get_current_user_id()),
        'meta_key'      => 'asgarosforum_subscription_global_posts',
        'meta_compare'  => 'EXISTS'
    );

    $get_users_result = get_users($topic_subscribers_query);

    if (!empty($get_users_result)) {
        $topic_subscribers = array_merge($topic_subscribers, $get_users_result);
    }

    // Remove banned users from mailing list.
    foreach ($topic_subscribers as $key => $subscriber) {
        if ($asgarosforum->permissions->isBanned($subscriber->id)) {
            unset($topic_subscribers[$key]);
        }
    }

    // Remove non-moderators from mailing list.
    if ($asgarosforum->category_access_level == 'moderator') {
        foreach ($topic_subscribers as $key => $subscriber) {
            if (!$asgarosforum->permissions->isModerator($subscriber->id)) {
                unset($topic_subscribers[$key]);
            }
        }
    }

    // Send notifications
    foreach($topic_subscribers as $subscriber) {
        UM()->Notifications_API()->api()->store_notification( $subscriber->ID, 'asgaros_subscription', $vars );
    }
    
    // For testing to a direct user
//    UM()->Notifications_API()->api()->store_notification( 18, 'asgaros_subscription', $vars );
}

 

Thanks for your help!!

Hey @gogrw,

what exactly is your problem with your code. I tried this code on my local machine and it seems to work fine. Maybe the problem is line 62 where you exclude the current user? Make sure use another user as subscriber than yourself.

The ‘asgarosforum_user_replacements’ filter works a bit different. This filter gives you the possibility to replace parameters before the email is sent. So you will get an array with the parameters for the notification:

array(4) { ["###AUTHOR###"]=> string(5) "Guest" ["###LINK###"]=> string(151) "http://localhost/newWordPress/forum/topic/activity/?part=2#postid-79" ["###TITLE###"]=> string(8) "Activity" ["###CONTENT###"]=> string(74) "
asdf@domitest

asdfasdf

" }

And the WP_User for the User that will be notified.

So in this case you don’t have to find out which users are subscribed to a post or topic. You could use the link to find out if it’s a post or a new topic and send a notification with the UM notification.

This filter has not been created to use for this purpose, but for me it seems to be the better way since you don’t have to do all this queries again to get the subscribed users.

Need professional help with Asgaros Forum? Book an appointment with us at domra Web Solutions for setup and customization services. Learn more about our Toolbox for Asgaros Forum plugin to enhance your forum experience.

Thank you, @qualmy91.

I will have to play around with the other filter as I wouldn’t know where to begin. As for the code I’m using, it just doesn’t do anything. I subscribe using a different user account, and then add a topic and post to the forum the other user is subscribed to, and nothing happens. No UM notification. I know the user is subscribed, though, because that user gets an email notification at the moment.

By the way, how would you suggest I use the ‘asgarosforum_subscriber_mails_new_post’ and ‘asgarosforum_subscriber_mails_new_topic’ filters to remove the recipients from the list? Ultimate Member already tells me who is online, so I don’t need that functionality from Asgaros Forum as well.

Hey @gogrw,

did you also try to var_dump the variables instead of using the notification api from UM? Maybe there are some problems with UM.

It’s also important to use the debug mode to check if there appear some errors.

By the way, how would you suggest I use the ‘asgarosforum_subscriber_mails_new_post’ and ‘asgarosforum_subscriber_mails_new_topic’ filters to remove the recipients from the list? Ultimate Member already tells me who is online, so I don’t need that functionality from Asgaros Forum as well.

You wrote earlier that you only want to send them a notification instead of an email. To do that you have to use this filters and remove the emails from the list.

 

Need professional help with Asgaros Forum? Book an appointment with us at domra Web Solutions for setup and customization services. Learn more about our Toolbox for Asgaros Forum plugin to enhance your forum experience.

@qualmy91 – I haven’t tried var_dumping without the UM notifications. I’ll try that in the morning.

As for removing emails, I was asking if you had any examples of how those filters were to be written. Sorry for not clarifying.

@gogrw

var_dump is very important if you are troubleshooting your php code. To make sure that you will be able to see the output you should follow it by die() which will stop the whole script:

var_dump($variable);
die();

You can use the filters like this:

add_filter('asgarosforum_subscriber_mails_new_post', 'remove_email');
add_filter('asgarosforum_subscriber_mails_new_topic', 'remove_email');

function remove_email($mailinglist){

    $online_users = get_online_users() // Your array with only emails of online users

    $new_mailinglist = array_diff($mailinglist, $online_users);

    return $new_mailinglist;
}

Just replace the function “get_online_users()” with your custom function that returns an array with the email addresses of online users.

Need professional help with Asgaros Forum? Book an appointment with us at domra Web Solutions for setup and customization services. Learn more about our Toolbox for Asgaros Forum plugin to enhance your forum experience.

Thank you, @qualmy91!

I finally got it all to work for what I need. I realized that my code was only sending notifications for posts, not topics, so I changed Step 3 to be 2 separate functions based on post and topic, and made a 3rd function for querying the subscribers. I also don’t need to any emails sent from the Asgaros plugin, so I just changed your “remove_email” function to return an empty array. No more emails, and notifications work as expected.

I understand that you mentioned doing this a different way, but I was closer to figuring it out this way, and the solution is only going to be needed for a week-long event. If it were more long-term, I would probably look more deeply into the method you suggested. I really appreciate your help. Here is my final working code:

/*
 *   Asgaros Forum -> UM Notifications API
 */
// STEP 1: Extend the filter: um_notifications_core_log_types with your new notification type
add_filter('um_notifications_core_log_types', 'add_asgaros_notification_type', 200 );
function add_asgaros_notification_type( $array ) {
    
  $array['asgaros_subscription'] = array(
    'title' => 'Forum Notification', // title for reference in backend settings
    'template' => 'Discussions: {message}', // the template, {member} is a tag, this is how the notification will appear in your notifications
    'account_desc' => 'When there is activity on my discussions', // title for account page (notification settings)
  );
    
  return $array;
}

// STEP 2: Add an icon and color to this new notification type
add_filter('um_notifications_get_icon', 'add_asgaros_notification_icon', 10, 2 );
function add_asgaros_notification_icon( $output, $type ) {
  
  if ( $type == 'asgaros_subscription' ) { // note that our new action id is "asgaros_subscription" from previous filter
    $output = '<i class="um-icon-chatbox" style="color: #00b56c"></i>';
  }
  
  return $output;
}

// STEP 3: Add the notification trigger when a user posts in a subscribed forum/topic/post
add_action('asgarosforum_after_add_post_submit', 'trigger_asgaros_post_notification', 20, 6);
function trigger_asgaros_post_notification( $post_id ) {
  global $um_notifications;
    global $asgarosforum;
    
    $post = $asgarosforum->content->get_post($post_id);
    $topic = $asgarosforum->content->get_topic($post->parent_id);
    $forum = $asgarosforum->content->get_forum($topic->parent_id);

    // Get more data.
    $post_link = $asgarosforum->rewrite->get_post_link($post_id, $topic->id);
    $topic_name = esc_html(stripslashes($topic->name));
    $forum_name = esc_html(stripslashes($forum->name));
    $author_name = $asgarosforum->getUsername($post->author_id);
    
    // Get the user's avatar
    $photo = um_get_avatar_url( get_avatar( $post->author_id, 40 ) ); 

    // Write the notification message
    $message = $author_name.' posted in '.$forum_name.' > '.$topic_name; 
    
    $topic_subscribers = get_asgaros_subscribers('post', $topic->id);

    // Set the UM notification variables
    $vars['photo'] = $photo;
    $vars['member'] = $author_name;
    $vars['message'] = $message;
    $vars['notification_uri'] = $post_link;

    // Send notifications
    foreach($topic_subscribers as $subscriber) {
        UM()->Notifications_API()->api()->store_notification( $subscriber->id, 'asgaros_subscription', $vars );
    }
}

add_action('asgarosforum_after_add_topic_submit', 'trigger_asgaros_topic_notification', 20, 6);
function trigger_asgaros_topic_notification( $post_id ) {
  global $um_notifications;
    global $asgarosforum;
    
    $post = $asgarosforum->content->get_post($post_id);
    $topic = $asgarosforum->content->get_topic($post->parent_id);
    $forum = $asgarosforum->content->get_forum($topic->parent_id);

    // Get more data.
    $topic_link = $asgarosforum->rewrite->get_link('topic', $topic->id);
    $topic_name = esc_html(stripslashes($topic->name));
    $forum_name = esc_html(stripslashes($forum->name));
    $author_name = $asgarosforum->getUsername($post->author_id);
    
    // Get the user's avatar
    $photo = um_get_avatar_url( get_avatar( $post->author_id, 40 ) ); 

    // Write the notification message
    $message = $author_name.' created a new topic in '.$forum_name.' called  <strong>'.$topic_name.'</strong>'; 
    
    $forum_subscribers = get_asgaros_subscribers('topic', $topic->parent_id);

    // Set the UM notification variables
    $vars['photo'] = $photo;
    $vars['member'] = $author_name;
    $vars['message'] = $message;
    $vars['notification_uri'] = $topic_link;

    // Send notifications
    foreach($forum_subscribers as $subscriber) {
        UM()->Notifications_API()->api()->store_notification( $subscriber->id, 'asgaros_subscription', $vars );
    }
}

// Create a function to get subscriber list by ID
add_shortcode( 'get_asgaros_subscribers', 'get_asgaros_subscribers' );
function get_asgaros_subscribers($post_or_topic, $id) {
    global $asgarosforum;
    
    if ($post_or_topic == 'post') {
        $metakey = 'asgarosforum_subscription_topic';
    } elseif ($post_or_topic == 'topic') {
        $metakey = 'asgarosforum_subscription_forum';
    }
    
    // Prepare mailing-list.
    $subscribers = array();

    // Get topic or forum subscribers.
    $subscribers_query = array(
        'fields'        => array('id'),
        'exclude'       => array(get_current_user_id()),
        'meta_key'      => $metakey,
        'meta_value'    => $id,
        'meta_compare'  => '='
    );

    $get_users_result = get_users($subscribers_query);

    if (!empty($get_users_result)) {
        $subscribers = array_merge($subscribers, $get_users_result);
    }

    // Get global post subscribers.
    $subscribers_query = array(
        'fields'        => array('id'),
        'exclude'       => array(get_current_user_id()),
        'meta_key'      => 'asgarosforum_subscription_global_posts',
        'meta_compare'  => 'EXISTS'
    );

    $get_users_result = get_users($subscribers_query);

    if (!empty($get_users_result)) {
        $subscribers = array_merge($subscribers, $get_users_result);
    }
    
    if ($post_or_topic == 'topic') {
        $subscribers_query = array(
            'fields'        => array('id'),
            'exclude'       => array(get_current_user_id()),
            'meta_key'      => 'asgarosforum_subscription_global_topics',
            'meta_compare'  => 'EXISTS'
        );

        $get_users_result = get_users($subscribers_query);

        if (!empty($get_users_result)) {
            $subscribers = array_merge($subscribers, $get_users_result);
        }
    }

    // Remove banned users from mailing list.
    foreach ($subscribers as $key => $subscriber) {
        if ($asgarosforum->permissions->isBanned($subscriber->id)) {
            unset($subscribers[$key]);
        }
    }

    // Remove non-moderators from mailing list.
    if ($asgarosforum->category_access_level == 'moderator') {
        foreach ($subscribers as $key => $subscriber) {
            if (!$asgarosforum->permissions->isModerator($subscriber->id)) {
                unset($subscribers[$key]);
            }
        }
    }
    return $subscribers;
}


/*
 * Remove emails from subscription notifications since we're now using UM notifications
 */
add_filter('asgarosforum_subscriber_mails_new_post', 'peqa_remove_email');
add_filter('asgarosforum_subscriber_mails_new_topic', 'peqa_remove_email');
function peqa_remove_email($mailinglist){
    $new_mailinglist = array();
    return $new_mailinglist;
}

 

Asgaros and qualmy91 have reacted to this post.
Asgarosqualmy91

@gogrw

Hi, I’m trying to implement this same notifications on my site. Where do i have to input this code?

 

Thanks

Page 1 of 2Next