簡體   English   中英

WordPress 在點擊時將 BuddyPress 自定義通知標記為已讀

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

我在 Buddypress 中創建了一個自定義通知。 每當開始一個新主題時,它就會被發送以供審核,並且所有版主都會收到通知和電子郵件以批准該帖子。 通知看起來像這樣(單擊它會轉到主持人可以批准\/拒絕帖子的主題)-

當我點擊通知時,它沒有被標記為已讀。 我了解通知鏈接應該有一個“bbp_mark_read”操作,以便將其標記為已讀。 但我無法找到將操作添加到通知 url 的正確方法。 這是我的代碼-

// 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;

}

我實際上是為活動邀請(自定義帖子類型)制作的,在我的情況下,我可以從不同用戶那里獲得關於同一活動的多個邀請,但它也適用於你的情況。

我們將使用的函數:

bp_notifications_mark_notifications_by_item_id();

它可以在bp-notifications-functions.phpbuddyboss 函數參考中找到。

首先,您需要准備網址。

$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) );

“行動”可以是任何東西。

不要在這里使用隨機數,因為它只持續 24 小時,如果用戶在那之后檢查通知,你的刪除它的功能將不起作用。

第二個函數將處理通知的取消標記:

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');

您不需要為此功能獲取$post對象。 您可以通過 id 獲取帖子作者姓名$secondary_item_id ,如下所示:

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

但是您從不使用它,而是使用bp_core_get_user_displayname( $post->post_author ) ,您可以再次獲得這樣的bp_core_get_user_displayname( $secondary_item_id ) ,而不需要$post 你的$custom_title$custom_text是一樣的。 您應該只使用一個變量而不是重復自己。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM