Skip to content

Instantly share code, notes, and snippets.

@gabrielmerovingi
Last active November 24, 2023 02:59
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gabrielmerovingi/403460e2319680fcddd82cb3dac39846 to your computer and use it in GitHub Desktop.
Save gabrielmerovingi/403460e2319680fcddd82cb3dac39846 to your computer and use it in GitHub Desktop.
This is an example how you can add BuddyPress notifications for users when they gain / lose myCRED points.
/**
* Register Custom BP Notifications
* Inform BuddyPress about our custom myCRED related notifications.
* @since 1.0
* @version 1.0
*/
function mycredpro_register_custom_bp_notifications() {
buddypress()->mycred_notifications = new stdClass;
buddypress()->mycred_notifications->notification_callback = 'mycredpro_render_bp_notification';
buddypress()->active_components['mycred_notifications'] = 1;
}
add_action( 'bp_setup_globals', 'mycredpro_register_custom_bp_notifications' );
/**
* Capture myCRED Event
* Whenever we add to the log we add a notification.
* @since 1.0
* @version 1.0
*/
function mycredpro_log_to_bp_notification( $insert_id, $request ) {
if ( $insert_id === false ) return $insert_id;
extract( $request );
if ( function_exists( 'bp_notifications_add_notification' ) )
bp_notifications_add_notification( array(
'user_id' => $user_id,
'item_id' => $insert_id,
'secondary_item_id' => $user_id,
'component_name' => 'mycred_notifications',
'component_action' => 'mycred_points'
) );
return $insert_id;
}
add_filter( 'mycred_new_log_entry_id', 'mycredpro_log_to_bp_notification', 90, 2 );
/**
* Render Notification
* Help BuddyPress out by rendering the log entry into something it can understand.
* @since 1.0
* @version 1.0.1
*/
function mycredpro_render_bp_notification( $action, $item_id, $secondary_item_id, $total_items, $format = 'string', $id = 0 ) {
$return = false;
if ( $action == 'mycred_points' ) {
global $wpdb, $mycred;
$text = $link = '';
$entry = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$mycred->log_table} WHERE id = %d;", $item_id ) );
if ( isset( $entry->id ) ) {
$mycred = mycred( $entry->ctype );
// In this example we link to the ledger in the users profile
// this needs to be changed to the URL you have setup (if you have this enabled)
// or to something specific.
$link = bp_loggedin_user_domain() . 'points-ledger/';
$text = $mycred->parse_template_tags( $entry->entry, $entry );
$title = strip_tags( $text );
}
}
if ( 'string' == $format ) {
$return = '<a href="' . esc_url( $link ) . '" title="' . esc_attr( $title ) . '">' . esc_html( $text ) . '</a>';
}
else {
$return = array(
'text' => $text,
'link' => $link
);
}
return $return;
}
@yward
Copy link

yward commented Feb 8, 2019

This snippet works great, however, the notifications aren't marked as read after you click on them, any tip on how to fix that?

@Ricson18
Copy link

Here:

	$args = array(
		'action'        => 'mod_org_become_org_moderator_request_mark_read',
		'item_id'       => $item_id,
		'secondary_id'  => $secondary_item_id
	);
	
	// Add the args to the URL.
	$custom_link = add_query_arg( $args, $link );

Mark notification as read

function mod_org_become_org_moderator_request_mark_read() {

	   if (isset($_GET['item_id']) && isset($_GET['secondary_id']) && isset($_GET['action']) && $_GET['action'] === 'mod_org_become_org_moderator_request_mark_read') {
 
		  $user_id = get_current_user_id();
		  $item_id = htmlentities($_GET['item_id'], ENT_QUOTES, 'UTF-8');
		  $component_name = 'mod_org_notifications';
		  $component_action = 'mod_org_become_org_moderator_request';
		  $secondary_item_id = htmlentities($_GET['secondary_id']);
		  $is_new = 0;
 
		  bp_notifications_mark_notifications_by_item_id( $user_id, $item_id, $component_name, $component_action, $secondary_item_id, $is_new );
 
	   }
 }
 add_action('wp', 'mod_org_become_org_moderator_request_mark_read');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment