简体   繁体   English

WordPress 在点击时将 BuddyPress 自定义通知标记为已读

[英]WordPress mark BuddyPress custom notifications as read on clicking

I have created a custom notification in Buddypress.我在 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)-通知看起来像这样(单击它会转到主持人可以批准\/拒绝帖子的主题)-

自定义通知<\/a>

When I click on the notification, it isn't marked as read.当我点击通知时,它没有被标记为已读。 I understand that the notification link should have a 'bbp_mark_read' action for it to be marked as read.我了解通知链接应该有一个“bbp_mark_read”操作,以便将其标记为已读。 But I am not able to find proper way to add the action to the notification url.但我无法找到将操作添加到通知 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 .它可以在bp-notifications-functions.phpbuddyboss 函数参考中找到。

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.不要在这里使用随机数,因为它只持续 24 小时,如果用户在那之后检查通知,你的删除它的功能将不起作用。

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.您不需要为此功能获取$post对象。 You can get post author name by id which is $secondary_item_id , like this:您可以通过 id 获取帖子作者姓名$secondary_item_id ,如下所示:

$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 .但是您从不使用它,而是使用bp_core_get_user_displayname( $post->post_author ) ,您可以再次获得这样的bp_core_get_user_displayname( $secondary_item_id ) ,而不需要$post Your $custom_title and $custom_text are the same.你的$custom_title$custom_text是一样的。 You should just use one variable instead of repeating yourself.您应该只使用一个变量而不是重复自己。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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