简体   繁体   中英

WordPress mark BuddyPress custom notifications as read on clicking

I have created a custom notification in Buddypress. Whenever a new Topic is started, it is sent for moderation and all the moderators are sent the notification and email to approve the post. The notification looks like this (clicking on it takes to the topic where a moderator can approve\/reject the post)-

I understand that the notification link should have a 'bbp_mark_read' action for it to be marked as read. But I am not able to find proper way to add the action to the notification url. Here is my code-

// Display notification.
add_filter( 'bp_notifications_get_notifications_for_user', 'bbms_moderation_alert_notifications', 10, 5 );

function bbms_moderation_alert_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {

    // New custom notifications
    if ( 'bbms_moderation_alert_action' === $action ) {
        
        $post = get_post( $item_id ); //$item_id represents ID of the Topic
        $author_name = get_the_author_meta('display_name', $post->post_author);
        $custom_title = '[Moderate] ' .bp_core_get_user_displayname( $post->post_author ) . ' created a Topic "' . get_the_title( $item_id ) . '"';
        $custom_link  = get_permalink( $post ); //Need to create a wp_nonce_url with bbp_mark_topic action here
        $custom_text = '[Moderate] ' .bp_core_get_user_displayname( $post->post_author ) . ' created a Topic "' . get_the_title( $item_id ) . '"';

        // WordPress Toolbar
        if ( 'string' === $format ) {
            $return = apply_filters( 'bbms_moderation_alert_filter', '<a href="' . esc_url( $custom_link ) . '" title="' . esc_attr( $custom_title ) . '">' . esc_html( $custom_text ) . '</a>', $custom_text, $custom_link );
        // Deprecated BuddyBar
        } else {
            $return = apply_filters( 'bbms_moderation_alert_filter', array(
                'text' => $custom_text,
                'link' => $custom_link
            ), $custom_link, (int) $total_items, $custom_text, $custom_title );
        }

        return $return;
    }

    return $action;

}

I actually made this for invitations to the event (custom post type), in my case I can get multiple invitations about the same event from different users, but it will work in your case too.

Function which we will use:

bp_notifications_mark_notifications_by_item_id();

It can be found in bp-notifications-functions.php or buddyboss functions reference .

First you need to prepare the url.

$args = array(
    'action'        => 'custom_action',
    'item_id'       => $item_id,
    'secondary_id'  => $secondary_item_id
);

// Add the args to the URL.
$custom_link = add_query_arg( $args, get_the_permalink($item_id) );

'action' can be anything.

You don't use nonce here, because it lasts only for 24 hours, if the user will check notification after that, your function to remove it, won't work.

The second function will handle the unmark of the notification:

function my_post_mark_read() {

   if (get_post_type() === 'my_post_slug') {
  
      if (isset($_GET['item_id']) && isset($_GET['secondary_id']) && isset($_GET['action']) && $_GET['action'] === 'custom_action') {

         $user_id = get_current_user_id();
         $item_id = htmlentities($_GET['item_id'], ENT_QUOTES, 'UTF-8');
         $component_name = 'my_component_name';
         $component_action = 'my_component_action';
         $secondary_item_id = htmlentities($_GET['secondary_id']);
         $is_new = 0;

         bp_notifications_mark_notifications_by_item_id( $user_id, $item_id, $component_name, $component_action, $secondary_item_id, $is_new );

      }
   }
}
add_action('wp', 'my_post_mark_read');

You don't need to get $post object for this function. You can get post author name by id which is $secondary_item_id , like this:

$author_name = get_user_by('id', $secondary_item_id)->display_name;

But you never use it, instead you use bp_core_get_user_displayname( $post->post_author ) , which again you can just get like this bp_core_get_user_displayname( $secondary_item_id ) , without the need of $post . Your $custom_title and $custom_text are the same. You should just use one variable instead of repeating yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM