Advertisement
designbymerovingi

myCRED Log Entry to BuddyPress Notification

Oct 21st, 2016
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.33 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
  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;
  56.  
  57.         $entry = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$mycred->log_table} WHERE id = %d;", $item_id ) );
  58.         if ( isset( $entry->id ) ) {
  59.  
  60.             $mycred = mycred( $entry->ctype );
  61.  
  62.             // In this example we link to the ledger in the users profile
  63.             // this needs to be changed to the URL you have setup (if you have this enabled)
  64.             // or to something specific.
  65.             $link   = bp_loggedin_user_domain() . 'points-ledger/';
  66.  
  67.             $text   = $mycred->parse_template_tags( $entry->entry, $entry );
  68.             $title  = strip_tags( $text );
  69.  
  70.         }
  71.  
  72.     }
  73.  
  74.     if ( 'string' == $format ) {
  75.         $return = '<a href="' . esc_url( $link ) . '" title="' . esc_attr( $title ) . '">' . esc_html( $text ) . '</a>';
  76.     }
  77.     else {
  78.         $return = array(
  79.             'text' => $text,
  80.             'link' => $link
  81.         );
  82.     }
  83.  
  84.     return $return;
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement