Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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
- $component_names[] = 'kleo_likes';
- // 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( $content, $post_id, $secondary_item_id, $total_items, $format = 'string', $action ) {
- // New custom notifications
- if ( 'kleo_likes_action' === $action ) {
- $post = get_post( $post_id );
- if($secondary_item_id > 0) {
- $user_data_info = get_userdata( $secondary_item_id );
- $liker_name = $user_data_info->user_nicename;
- }else {
- $liker_name = 'Guest';
- }
- $custom_title = $post->post_title;
- $custom_link = get_permalink($post->ID);
- $custom_text = $liker_name . __(' liked your post ', 'kleo_framework') . get_the_title( $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;
- }
- return $content;
- }
- bp_notifications_get_notifications_for_user();
- add_filter( 'bp_notifications_get_notifications_for_user', 'custom_format_buddypress_notifications', 10, 6 );
- // this hooks to comment creation and saves the comment id
- function bp_custom_add_notification( $post_id ) {
- $post = get_post( $post_id );
- $author_id = $post->post_author;
- bp_notifications_add_notification( array(
- 'user_id' => $author_id,
- 'item_id' => $post_id,
- 'secondary_item_id' => get_current_user_id(),
- 'component_name' => 'kleo_likes',
- 'component_action' => 'kleo_likes_action',
- 'date_notified' => bp_core_current_time(),
- 'is_new' => 1,
- ) );
- }
- add_action( 'after_kleo_posts_like', 'bp_custom_add_notification', 99 );
Advertisement
Add Comment
Please, Sign In to add comment