在buddypress中添加自定义通知


Add a custom notification in buddypress

当发生特定事件时,我想向我的buddypress"notification"选项卡添加一个自定义通知。如何做到这一点?

我学习了本教程,非常完整为我工作

BuddyPress:添加自定义通知

我要说的是作者写的。但如果你直接去帖子,你会发现更好的解释。我认为这篇帖子是为假人写的,非常完整和解释性,甚至有一个要点

第一次注册您的组件

您需要将您的通知注册为budypress组件。这很容易。注册的组件名称为自定义

// this is to add a fake component to BuddyPress. A registered component is needed to add notifications
function custom_filter_notifications_get_registered_components( $component_names = array() ) {
    // Force $component_names to be an array
    if ( ! is_array( $component_names ) ) {
        $component_names = array();
    }
    // Add 'custom' component to registered components array
    array_push( $component_names, 'custom' );
    // Return component's with 'custom' appended
    return $component_names;
}
add_filter( 'bp_notifications_get_registered_components', 'custom_filter_notifications_get_registered_components' );

第二次呈现通知

// this gets the saved item id, compiles some data and then displays the notification
function custom_format_buddypress_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
    // New custom notifications
    if ( 'custom_action' === $action ) {
        $comment = get_comment( $item_id );
        $custom_title = $comment->comment_author . ' commented on the post ' . get_the_title( $comment->comment_post_ID );
        $custom_link  = get_comment_link( $comment );
        $custom_text = $comment->comment_author . ' commented on your post ' . get_the_title( $comment->comment_post_ID );
        // WordPress Toolbar
        if ( 'string' === $format ) {
            $return = apply_filters( 'custom_filter', '<a href="' . esc_url( $custom_link ) . '" title="' . esc_attr( $custom_title ) . '">' . esc_html( $custom_text ) . '</a>', $custom_text, $custom_link );
        // BuddyBar
        } else {
            $return = apply_filters( 'custom_filter', array(
                'text' => $custom_text,
                'link' => $custom_link
            ), $custom_link, (int) $total_items, $custom_text, $custom_title );
        }
        return $return;
    }
}
add_filter( 'bp_notifications_get_notifications_for_user', 'custom_format_buddypress_notifications', 10, 5 );

3st启动通知

当有人在帖子上给你写信时,你可以在这里添加通知。使用动作挂钩wp_insert_comment捕获该事件。

// this hooks to comment creation and saves the comment id
function bp_custom_add_notification( $comment_id, $comment_object ) {
    $post = get_post( $comment_object->comment_post_ID );
    $author_id = $post->post_author;
    bp_notifications_add_notification( array(
        'user_id'           => $author_id,
        'item_id'           => $comment_id,
        'component_name'    => 'custom',
        'component_action'  => 'custom_action',
        'date_notified'     => bp_core_current_time(),
        'is_new'            => 1,
    ) );
}
add_action( 'wp_insert_comment', 'bp_custom_add_notification', 99, 2 );

全部在一起

<?php
// this is to add a fake component to BuddyPress. A registered component is needed to add notifications
function custom_filter_notifications_get_registered_components( $component_names = array() ) {
    // Force $component_names to be an array
    if ( ! is_array( $component_names ) ) {
        $component_names = array();
    }
    // Add 'custom' component to registered components array
    array_push( $component_names, 'custom' );
    // Return component's with 'custom' appended
    return $component_names;
}
add_filter( 'bp_notifications_get_registered_components', 'custom_filter_notifications_get_registered_components' );

// this gets the saved item id, compiles some data and then displays the notification
function custom_format_buddypress_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
    // New custom notifications
    if ( 'custom_action' === $action ) {
        $comment = get_comment( $item_id );
        $custom_title = $comment->comment_author . ' commented on the post ' . get_the_title( $comment->comment_post_ID );
        $custom_link  = get_comment_link( $comment );
        $custom_text = $comment->comment_author . ' commented on your post ' . get_the_title( $comment->comment_post_ID );
        // WordPress Toolbar
        if ( 'string' === $format ) {
            $return = apply_filters( 'custom_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( 'custom_filter', array(
                'text' => $custom_text,
                'link' => $custom_link
            ), $custom_link, (int) $total_items, $custom_text, $custom_title );
        }
        return $return;
    }
}
add_filter( 'bp_notifications_get_notifications_for_user', 'custom_format_buddypress_notifications', 10, 5 );

// this hooks to comment creation and saves the comment id
function bp_custom_add_notification( $comment_id, $comment_object ) {
    $post = get_post( $comment_object->comment_post_ID );
    $author_id = $post->post_author;
    bp_notifications_add_notification( array(
        'user_id'           => $author_id,
        'item_id'           => $comment_id,
        'component_name'    => 'custom',
        'component_action'  => 'custom_action',
        'date_notified'     => bp_core_current_time(),
        'is_new'            => 1,
    ) );
}
add_action( 'wp_insert_comment', 'bp_custom_add_notification', 99, 2 );

您使用bp_notifications_add_notification()。以下示例函数与bp_activity_sent_mention_email挂钩-因此,当由于某人被@提及而发送电子邮件通知时,将生成核心通知。

function bp_activity_at_mention_add_notification( $activity, $subject, $message, $content, $receiver_user_id ) {
    if ( bp_is_active( 'notifications' ) ) {
        bp_notifications_add_notification( array(
            'user_id'           => $receiver_user_id,
            'item_id'           => $activity->id,
            'secondary_item_id' => $activity->user_id,
            'component_name'    => buddypress()->activity->id,
            'component_action'  => 'new_at_mention',
            'date_notified'     => bp_core_current_time(),
            'is_new'            => 1,
        ) );
    }
}
add_action( 'bp_activity_sent_mention_email', 'bp_activity_at_mention_add_notification', 10, 5 );

参考编号:http://codex.buddypress.org/developer/function-examples/bp_notifications_add_notification/