Guest User

Untitled

a guest
Jul 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. /**
  2. * Register Custom BP Notifications
  3. * Inform BuddyPress about our custom myCRED related notifications.
  4. * @since 1.0
  5. * @version 1.0
  6. */
  7. function mycredpro_register_custom_bp_notifications() {
  8.  
  9. buddypress()->mycred_notifications = new stdClass;
  10. buddypress()->mycred_notifications->notification_callback = 'mycredpro_render_bp_notification';
  11.  
  12. buddypress()->active_components['mycred_notifications'] = 1;
  13.  
  14. }
  15. add_action( 'bp_setup_globals', 'mycredpro_register_custom_bp_notifications' );
  16.  
  17. /**
  18. * Capture myCRED Event
  19. * Whenever we add to the log we add a notification.
  20. * @since 1.0
  21. * @version 1.0
  22. */
  23. function mycredpro_log_to_bp_notification( $insert_id, $request ) {
  24.  
  25. if ( $insert_id === false ) return $insert_id;
  26.  
  27. extract( $request );
  28.  
  29. if ( function_exists( 'bp_notifications_add_notification' ) )
  30. bp_notifications_add_notification( array(
  31. 'user_id' => $user_id,
  32. 'item_id' => $insert_id,
  33. 'secondary_item_id' => $user_id,
  34. 'component_name' => 'mycred_notifications',
  35. 'component_action' => 'mycred_points'
  36. ) );
  37.  
  38. return $insert_id;
  39.  
  40. }
  41. add_filter( 'mycred_new_log_entry_id', 'mycredpro_log_to_bp_notification', 90, 2 );
  42.  
  43. /**
  44. * Render Notification
  45. * Help BuddyPress out by rendering the log entry into something it can understand.
  46. * @since 1.0
  47. * @version 1.0.1
  48. */
  49. function mycredpro_render_bp_notification( $action, $item_id, $secondary_item_id, $total_items, $format = 'string', $id = 0 ) {
  50.  
  51. $return = false;
  52.  
  53. if ( $action == 'mycred_points' ) {
  54.  
  55. global $wpdb, $mycred;
  56.  
  57. $text = $link = '';
  58. $entry = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$mycred->log_table} WHERE id = %d;", $item_id ) );
  59. if ( isset( $entry->id ) ) {
  60.  
  61. $mycred = mycred( $entry->ctype );
  62.  
  63. // In this example we link to the ledger in the users profile
  64. // this needs to be changed to the URL you have setup (if you have this enabled)
  65. // or to something specific.
  66. $link = bp_loggedin_user_domain() . 'points-ledger/';
  67.  
  68. $text = $mycred->parse_template_tags( $entry->entry, $entry );
  69. $title = strip_tags( $text );
  70.  
  71. }
  72.  
  73. }
  74.  
  75. if ( 'string' == $format ) {
  76. $return = '<a href="' . esc_url( $link ) . '" title="' . esc_attr( $title ) . '">' . esc_html( $text ) . '</a>';
  77. }
  78. else {
  79. $return = array(
  80. 'text' => $text,
  81. 'link' => $link
  82. );
  83. }
  84.  
  85. return $return;
  86.  
  87. }
Add Comment
Please, Sign In to add comment