/** * Return the total of all high-fives given to a particular user * * The most straightforward way to get a post count is to run a WP_Query. In your own plugin * you might consider storing data like this with update_option(), incrementing each time * a new item is published. * * @package BuddyPress_Skeleton_Component * @since 1.6 * * @return int */ function bp_example_get_total_high_five_count_for_user($user_id = false) { // If no explicit user id is passed, fall back on the loggedin user if (!$user_id) { $user_id = bp_loggedin_user_id(); } if (!$user_id) { return 0; } $high_fives = new BP_Example_Highfive(); $high_fives->get(array('recipient_id' => $user_id)); return apply_filters('bp_example_get_total_high_five_count', $high_fives->query->found_posts, $high_fives); }
/** * bp_example_send_high_five() * * Sends a high five message to a user. Registers an notification to the user * via their notifications menu, as well as sends an email to the user. * * Also records an activity stream item saying "User 1 high-fived User 2". */ function bp_example_send_highfive($to_user_id, $from_user_id) { global $bp; check_admin_referer('bp_example_send_high_five'); /** * We'll store high-fives as usermeta, so we don't actually need * to do any database querying. If we did, and we were storing them * in a custom DB table, we'd want to reference a function in * bp-example-classes.php that would run the SQL query. */ delete_user_meta($to_user_id, 'high-fives'); /* Get existing fives */ $existing_fives = maybe_unserialize(get_user_meta($to_user_id, 'high-fives', true)); /* Check to see if the user has already high-fived. That's okay, but lets not * store duplicate high-fives in the database. What's the point, right? */ if (!in_array($from_user_id, (array) $existing_fives)) { $existing_fives[] = (int) $from_user_id; /* Now wrap it up and fire it back to the database overlords. */ update_user_meta($to_user_id, 'high-fives', serialize($existing_fives)); // Let's also record it in our custom database tables $db_args = array('recipient_id' => (int) $to_user_id, 'high_fiver_id' => (int) $from_user_id); $high_five = new BP_Example_Highfive($db_args); $high_five->save(); } /*** * Now we've registered the new high-five, lets work on some notification and activity * stream magic. */ /*** * Post a screen notification to the user's notifications menu. * Remember, like activity streams we need to tell the activity stream component how to format * this notification in bp_example_format_notifications() using the 'new_high_five' action. */ bp_core_add_notification($from_user_id, $to_user_id, $bp->example->slug, 'new_high_five'); /* Now record the new 'new_high_five' activity item */ $to_user_link = bp_core_get_userlink($to_user_id); $from_user_link = bp_core_get_userlink($from_user_id); bp_example_record_activity(array('type' => 'rejected_terms', 'action' => apply_filters('bp_example_new_high_five_activity_action', sprintf(__('%s high-fived %s!', 'bp-example'), $from_user_link, $to_user_link), $from_user_link, $to_user_link), 'item_id' => $to_user_id)); /* We'll use this do_action call to send the email notification. See bp-example-notifications.php */ do_action('bp_example_send_high_five', $to_user_id, $from_user_id); return true; }