rdusnr

Untitled

Aug 9th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. // this is to add a fake component to BuddyPress. A registered component is needed to add notifications
  2. function custom_filter_notifications_get_registered_components( $component_names = array() ) {
  3. // Force $component_names to be an array
  4. if ( ! is_array( $component_names ) ) {
  5. $component_names = array();
  6. }
  7. // Add 'custom' component to registered components array
  8. $component_names[] = 'kleo_likes';
  9. // Return component's with 'custom' appended
  10. return $component_names;
  11. }
  12. add_filter( 'bp_notifications_get_registered_components', 'custom_filter_notifications_get_registered_components' );
  13.  
  14. // this gets the saved item id, compiles some data and then displays the notification
  15. function custom_format_buddypress_notifications( $content, $post_id, $secondary_item_id, $total_items, $format = 'string', $action ) {
  16. // New custom notifications
  17.  
  18. if ( 'kleo_likes_action' === $action ) {
  19.  
  20. $post = get_post( $post_id );
  21. if($secondary_item_id > 0) {
  22. $user_data_info = get_userdata( $secondary_item_id );
  23. $liker_name = $user_data_info->user_nicename;
  24. }else {
  25. $liker_name = 'Guest';
  26. }
  27. $custom_title = $post->post_title;
  28. $custom_link = get_permalink($post->ID);
  29. $custom_text = $liker_name . __(' liked your post ', 'kleo_framework') . get_the_title( $post->ID );
  30.  
  31. // WordPress Toolbar
  32. if ( 'string' === $format ) {
  33. $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 );
  34. // Deprecated BuddyBar
  35. } else {
  36. $return = apply_filters( 'custom_filter', array(
  37. 'text' => $custom_text,
  38. 'link' => $custom_link
  39. ), $custom_link, (int) $total_items, $custom_text, $custom_title );
  40. }
  41.  
  42. return $return;
  43.  
  44. }
  45.  
  46. return $content;
  47.  
  48. }
  49. bp_notifications_get_notifications_for_user();
  50. add_filter( 'bp_notifications_get_notifications_for_user', 'custom_format_buddypress_notifications', 10, 6 );
  51. // this hooks to comment creation and saves the comment id
  52. function bp_custom_add_notification( $post_id ) {
  53. $post = get_post( $post_id );
  54. $author_id = $post->post_author;
  55.  
  56. bp_notifications_add_notification( array(
  57. 'user_id' => $author_id,
  58. 'item_id' => $post_id,
  59. 'secondary_item_id' => get_current_user_id(),
  60. 'component_name' => 'kleo_likes',
  61. 'component_action' => 'kleo_likes_action',
  62. 'date_notified' => bp_core_current_time(),
  63. 'is_new' => 1,
  64. ) );
  65.  
  66. }
  67. add_action( 'after_kleo_posts_like', 'bp_custom_add_notification', 99 );
Advertisement
Add Comment
Please, Sign In to add comment