Supporting Cubepoints for Global forums
Recently, I got a lot of request on our forum for supporting global forum with CubePoints BuddyPress Integration plugin by Tosh. I looked into the code of CubePoints buddypress integration plugin and it seemed like an interesting thing to do.
Though the current version of buddypress cubepoints lacked some hook to show the point form and update the settings, I am hopeful that Tosh will add it in next version of BuddyPress cubepoints integration plugin and open a new door for plugin developers to support their own plugin.
Here is what I did to support the global forum with cubepoints plugin.
1. Add a hook to BuddyPress CubePoints Integration plugin admin section to allow showing your form.
So, I put this code there
[sourcecode language="php"]
do_action("bp_cubepoint_main_settings"); //allow other plugin to hook and show their point poption here
[/sourcecode]
That allows any plugin developer to put their own form elements in the CubePoints BuddyPress Integration plugin admin section.
2. Allow plugin developers to update their points/settings when the points table is saved.
Just adding one more hook in the function "cubebp_admin" and everything was setup.
Here is what I added, It may be different when Tosh updates his plugin.
[sourcecode language="php"]
do_action("bp_cubepoint_settings_updated");//allow other plugins to hook here and update their settings on save
[/sourcecode]
Well, the base is ready now. We can easily extend the CubePoints plugin and support our own plugin now.
So, what functionalities we are interested in supporting, Obviously, It narrows down to these two
- Giving/Deducting point on some action performed in our plugin
- Logging the actions
CubePoints core proved a way for that
cp_points($type, $uid, $points, $data)
- $type: your unique action identifier (or let su say It identifies the purpose of the point)
- $uid: the user id to which points should be given
- $points: how many points you want to give
- $data: I am not sure the purpose of it. Perhaps It is used in saving logs
cp_points function adds the point and logs the action.
So, Now you just need to add a function to your action and give points.
Ex.
[sourcecode language="php"]
add_action("something_happened","give_point_on_something_happened");
function give_point_on_something_happened(){
global $bp;
$point=get_option("points_for_my_something_happened_action");//how many points you want to give
cp_points("hello_something_happened",$bp->loggedin_user->id, $point,"");
}
[/sourcecode]
That will add point for your action. But wait you are not done yet. The cp_points already logs your event but to display it, CubePoints needs your help.
Here is what cube point does while displaying it
[sourcecode language="php"]
do_action('cp_logs_description', $result->type, $result->uid, $result->points, $result->data);
[/sourcecode]
Just hook your function to show a meaningful message on the action "cp_logs_description" and you are done.
Here is the continued code snippet.
[sourcecode language="php"]
do_action('cp_logs_description', "my_hellow_something_happened_log_message",10,4);//4 is for no. of args
function my_hellow_something_happened_log_message($type,$uid,$points,$data){
if($type!="hello_something_happened")//remember this is same as we used in cp_points
return;
echo "The wonderful action happened";//well you may make it meaning full by using uid and making something like user x did action alpha beta and received $points point
}
[/sourcecode]
Ok, That was for everyone who want to support cubepoints in their own plugin.
CubePoints For Global Forum
Here is the CubePoints global forum integration plugin.
Download & Install:-
https://buddydev.com/plugins/cubepoints-global-forum-integration/
Here is modified cubepoints-buddypress-integration/includes/bp-cubepoint-admin.php. Replace it in cubepoints-buddypress-integration/includes.
And a screenshot of cubepoints admin settings page
If you are using Global forum cubepoints integration plugin, please note there are four actions defined
- new topic create
- topic delete
- new post(or say reply)
- delete post
There is a bug in global forum, the delete actions are called twice, so points will be deducted twice(as of global-forum-v1.0.2). Please make sure to allocate only half of those point to the delete actions. Say, if you wanted to deduct 10 points for post delete, make sure you specify -5 in the admin option. This is a bug with delete option
(for topic/posts) in current global forum. I will update and fix it when the new release comes.
Looking forward to hear your comments
P.s: Special Thanks to Joey for asking me to do this plugin.
Great work. This will help me write up some documentation on how to tell other plugin authors to tie into my plugin.
Hi Tosh,
Thanks for the comment and your work on the Cubepoint for BuddyPress plugin which made it very easy for me to add the custom code. Sorry I could not follow back on bp.org properly.
Looking forward to your updates 🙂
This has been patched into version 1.9.2 of my plugin now. Thanks!
Pingback:Adding Support For Other BuddyPress Plugins To The CubePoints Buddypress Integration Plugin « SlySpyder Blog
thanks a lot!! man it works for Q&A lite too with few modifications and check out edit.php on Q&A that is where everything is FYI
Glad it helped 🙂
Thanks Brajesh for this guide! It was very helpful allowing us to integrate with cubepoints. I already posted this on SlySpyder's related blog post, but thought I'd post it here as well in case it could help others facing the same issue we had. We wanted to be able to add points dynamically throughout our site without having to add a custom message function for each data type, so I wrote a wrapper function that uses the $data parameter to accomplish this. I borrowed a lot of the logic from the Cubepoints cp_hooks.php file. I've pasted it below, but while it works for me, it may not work on your setup, so use at your own risk.
Put this in your functions.php
if ( ! function_exists( ‘custom_cp_add_points’) ) :
/**
* Custom function to add cubepoints points programmatically and add custom points message to log
*
* Usage: custom_cp_add_points ($type, $user_id, $message, $points, $override);
*
* Paramaters:
* $type = (string)(required) unique string for point value identification
* $user_id = (int)(required) user id of user to receive points
* $message = (string)(optional) message to be displayed in user point list
* $points = (int)(optional) custom point value, will be overriden if an entry for this type exists in db
* $override = (bool)(optional) force override db point total with custom $points
*
*/
function custom_cp_add_points( $type, $user_id, $message = ‘Custom point addition’, $points = null, $override = false ) {
global $bp;
// Check if points value is already stored in database
$point_value = get_option($type);
// If point value in db and override false or points in db and no value passed in, use db
if ( ($point_value && $override == false) || ($point_value && $points == null ) ) {
$points = $point_value;
}
// If no db value and no points passed in, use default
elseif (!$point_value && $points == null) {
$points = 2;
}
// Add cubepoints
cp_points($type, $user_id, $points, $message);
}
endif; //custom_cp_add_points
function custom_cp_add_message($type, $uid, $points, $data) {
echo $data;
}
add_action(‘cp_logs_description’, ‘custom_cp_add_message’, 10, 4);
Then, anywhere in your site you can call something like:
custom_cp_add_points(‘custom_point_type’, bp_loggedin_user_id(), ‘My custom point message’, 10, true);
Just found a bug with my last post in that by echoing data in the second function you also display data that’s stored with other posts, looking for a work around.
Found a workaround, a bit of a hack though.
Add a line in the first function that sets $message = ‘some-unique-tag’ . $message;
Then in the custom_cp_add_message function add something like:
// See if message is from custom add message
if (strpos($data, ‘some-unique-tag’) === 0) {
// Remove ‘some-unique-tag’ from beginning of message
$data = substr($data, 15);
echo $data;
}
// Otherwise don’t output anything
else {
return;
}
Hi Will,
Thank you for posting the code.
I hope it will help other people 🙂
Regards
Brajesh