Skip to content

Instantly share code, notes, and snippets.

@gabrielmerovingi
Created January 30, 2014 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielmerovingi/8717806 to your computer and use it in GitHub Desktop.
Save gabrielmerovingi/8717806 to your computer and use it in GitHub Desktop.
This custom hook allows you to award or deduct points from your users who checkin or create new places using the BP Checkin plugin.
/**
* BP Checkins
* @requires myCRED 1.3 +
* @since 0.1
* @version 1.0
*/
if ( class_exists( 'myCRED_Hook' ) && ! class_exists( 'myCRED_BP_Checkins' ) ) {
class myCRED_BP_Checkins extends myCRED_Hook {
/**
* Construct
*/
function __construct( $hook_prefs ) {
parent::__construct( array(
'id' => 'bp_checkins',
'defaults' => array(
'activity_checkin' => array(
'creds' => 0,
'log' => '%plural% for checkin',
'limit' => 0
),
'new_place' => array(
'creds' => 0,
'log' => '%plural% for new place',
'limit' => 0
),
'place_checkin' => array(
'creds' => 0,
'log' => '%plural% for checkin at place',
'limit' => 0
),
'place_comment' => array(
'creds' => 0,
'log' => '%plural% for place comment',
'limit' => 0
),
'place_checkin_comment' => array(
'creds' => 0,
'log' => '%plural% for place checkin comment',
'limit' => 0
),
'group_activity_checkin' => array(
'creds' => 0,
'log' => '%plural% for group checkin',
'limit' => 0
),
'group_new_place' => array(
'creds' => 0,
'log' => '%plural% for new group place',
'limit' => 0
),
'group_place_checkin' => array(
'creds' => 0,
'log' => '%plural% for checkin at group place',
'limit' => 0
),
'group_place_comment' => array(
'creds' => 0,
'log' => '%plural% for group place comment',
'limit' => 0
),
'group_place_checkin_comment' => array(
'creds' => 0,
'log' => '%plural% for group place checkin comment',
'limit' => 0
)
)
), $hook_prefs );
}
/**
* Run
* @since 0.1
* @version 1.0
*/
public function run() {
add_action( 'bp_activity_posted_checkin', array( $this, 'activity_checkin' ), 10, 3 );
add_action( 'bp_groups_posted_checkin', array( $this, 'group_checkin' ), 10, 4 );
}
/**
* Profile Checkin
* @since 1.0
* @version 1.0
*/
public function activity_checkin( $content, $user_id, $activity_id ) {
// Check if user is excluded
if ( $this->core->exclude_user( $user_id ) ) return;
// Get Activity
$activity = bp_activity_get_specific( array( 'activity_ids' => $activity_id ) );
if ( ! isset( $activity['activities'][0]->type ) ) return;
update_option( 'catch_activity', array(
'content' => $content,
'user_id' => $user_id,
'activity_id' => $activity_id,
'activity' => $activity,
'activity_type' => $activity['activities'][0]->type
) );
$activity_type = $activity['activities'][0]->type;
if ( isset( $this->prefs[ $activity_type ]['creds'] ) && $this->prefs[ $activity_type ]['creds'] != 0 ) {
$ref_id = $activity_id;
if ( $activity->item_id !== false )
$ref_id = $activity->item_id;
// Make sure this is unique event
if ( $this->core->has_entry( $activity_type, $user_id, $ref_id ) ) return;
// Check if we are over the daily limit
if ( $this->prefs[ $activity_type ]['limit'] > 0 ) {
$earned = mycred_get_total_by_time( 'today', 'now', $activity_type, $user_id );
$max = $this->core->number( $this->prefs[ $activity_type ]['limit'] * $this->prefs[ $activity_type ]['creds'] );
if ( $earned <= $max ) return;
}
// Execute
$this->core->add_creds(
$activity_type,
$user_id,
$this->prefs[ $activity_type ]['creds'],
$this->prefs[ $activity_type ]['log'],
$ref_id
);
}
}
/**
* Group Checkin
* @since 1.0
* @version 1.0
*/
public function group_checkin( $content, $user_id, $group_id, $activity_id ) {
// Check if user is excluded
if ( $this->core->exclude_user( $user_id ) ) return;
// Get Activity
$activity = bp_activity_get_specific( array( '' => $activity_id ) );
if ( ! isset( $activity['activities'][0]->type ) ) return;
$activity_type = $activity['activities'][0]->type;
if ( isset( $this->prefs[ 'group_' . $activity_type ]['creds'] ) && $this->prefs[ 'group_' . $activity_type ]['creds'] != 0 ) {
$ref_id = $activity_id;
if ( $activity->item_id !== false )
$ref_id = $activity->item_id;
// Make sure this is unique event
if ( $this->core->has_entry( 'group_' . $activity_type, $user_id, $ref_id ) ) return;
// Check if we are over the daily limit
if ( $this->prefs[ 'group_' . $activity_type ]['limit'] > 0 ) {
$earned = mycred_get_total_by_time( 'today', 'now', 'group_' . $activity_type, $user_id );
$max = $this->core->number( $this->prefs[ 'group_' . $activity_type ]['limit'] * $this->prefs[ 'group_' . $activity_type ]['creds'] );
if ( $earned <= $max ) return;
}
// Execute
$this->core->add_creds(
$activity->component,
$user_id,
$this->prefs[ 'group_' . $activity_type ]['creds'],
$this->prefs[ 'group_' . $activity_type ]['log'],
$ref_id
);
}
}
/**
* Preferences for Hook
* @since 1.0
* @version 1.0
*/
public function preferences() {
$prefs = $this->prefs;
$creds_label = $this->core->plural();
$log_label = __( 'Log template', 'mycred' );
$limit_label = __( 'Daily Limit', 'mycred' );
$limit_description = __( 'Optional daily limit to enforce. Use zero for unlimited.', 'mycred' );
$available_tags = __( 'Available template tags: General.', 'mycred' );
$available_tags_post = __( 'Available template tags: General and Post related (places).', 'mycred' ); ?>
<label for="<?php echo $this->field_id( array( 'checkin', 'creds' ) ); ?>" class="subheader"><?php _e( 'Profile Checkin', 'mycred' ); ?></label>
<ol>
<li>
<label for="<?php echo $this->field_id( array( 'activity_checkin', 'creds' ) ); ?>"><?php echo $creds_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'activity_checkin', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'activity_checkin', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['activity_checkin']['creds'] ); ?>" size="8" /></div>
</li>
<li>
<label for="<?php echo $this->field_id( array( 'activity_checkin', 'log' ) ); ?>"><?php echo $log_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'activity_checkin', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'activity_checkin', 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['activity_checkin']['log'] ); ?>" class="long" /></div>
<span class="description"><?php echo $available_tags; ?></span>
</li>
<li>
<label for="<?php echo $this->field_id( array( 'checkin', 'limit' ) ); ?>"><?php echo $limit_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'checkin', 'limit' ) ); ?>" id="<?php echo $this->field_id( array( 'checkin', 'limit' ) ); ?>" value="<?php echo $this->core->number( $prefs['checkin']['limit'] ); ?>" size="8" /> <?php _e( '/ day', 'mycred' ); ?></div>
<span class="description"><?php echo $limit_description; ?></span>
</li>
</ol>
<label for="<?php echo $this->field_id( array( 'new_place', 'creds' ) ); ?>" class="subheader"><?php _e( 'New Place', 'mycred' ); ?></label>
<ol>
<li>
<label for="<?php echo $this->field_id( array( 'new_place', 'creds' ) ); ?>"><?php echo $creds_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'new_place', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'new_place', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['new_place']['creds'] ); ?>" size="8" /></div>
</li>
<li>
<label for="<?php echo $this->field_id( array( 'new_place', 'log' ) ); ?>"><?php echo $log_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'new_place', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'new_place', 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['new_place']['log'] ); ?>" class="long" /></div>
<span class="description"><?php echo $available_tags_post; ?></span>
</li>
<li>
<label for="<?php echo $this->field_id( array( 'new_place', 'limit' ) ); ?>"><?php echo $limit_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'new_place', 'limit' ) ); ?>" id="<?php echo $this->field_id( array( 'new_place', 'limit' ) ); ?>" value="<?php echo $this->core->number( $prefs['new_place']['limit'] ); ?>" size="8" /> <?php _e( '/ day', 'mycred' ); ?></div>
<span class="description"><?php echo $limit_description; ?></span>
</li>
</ol>
<label for="<?php echo $this->field_id( array( 'place_checkin', 'creds' ) ); ?>" class="subheader"><?php _e( 'Place Checkin', 'mycred' ); ?></label>
<ol>
<li>
<label for="<?php echo $this->field_id( array( 'place_checkin', 'creds' ) ); ?>"><?php echo $creds_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'place_checkin', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'place_checkin', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['place_checkin']['creds'] ); ?>" size="8" /></div>
</li>
<li>
<label for="<?php echo $this->field_id( array( 'place_checkin', 'log' ) ); ?>"><?php echo $log_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'place_checkin', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'place_checkin', 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['place_checkin']['log'] ); ?>" class="long" /></div>
<span class="description"><?php echo $available_tags_post; ?></span>
</li>
<li>
<label for="<?php echo $this->field_id( array( 'place_checkin', 'limit' ) ); ?>"><?php echo $limit_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'place_checkin', 'limit' ) ); ?>" id="<?php echo $this->field_id( array( 'place_checkin', 'limit' ) ); ?>" value="<?php echo $this->core->number( $prefs['place_checkin']['limit'] ); ?>" size="8" /> <?php _e( '/ day', 'mycred' ); ?></div>
<span class="description"><?php echo $limit_description; ?></span>
</li>
</ol>
<label for="<?php echo $this->field_id( array( 'place_comment', 'creds' ) ); ?>" class="subheader"><?php _e( 'Place Comment', 'mycred' ); ?></label>
<ol>
<li>
<label for="<?php echo $this->field_id( array( 'place_comment', 'creds' ) ); ?>"><?php echo $creds_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'place_comment', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'place_comment', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['place_comment']['creds'] ); ?>" size="8" /></div>
</li>
<li>
<label for="<?php echo $this->field_id( array( 'place_comment', 'log' ) ); ?>"><?php echo $log_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'place_comment', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'place_comment', 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['place_comment']['log'] ); ?>" class="long" /></div>
<span class="description"><?php echo $available_tags_post; ?></span>
</li>
<li>
<label for="<?php echo $this->field_id( array( 'place_comment', 'limit' ) ); ?>"><?php echo $limit_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'place_comment', 'limit' ) ); ?>" id="<?php echo $this->field_id( array( 'place_comment', 'limit' ) ); ?>" value="<?php echo $this->core->number( $prefs['place_comment']['limit'] ); ?>" size="8" /> <?php _e( '/ day', 'mycred' ); ?></div>
<span class="description"><?php $limit_description; ?></span>
</li>
</ol>
<label for="<?php echo $this->field_id( array( 'place_checkin_comment', 'creds' ) ); ?>" class="subheader"><?php _e( 'Place Checkin Comment', 'mycred' ); ?></label>
<ol>
<li>
<label for="<?php echo $this->field_id( array( 'place_checkin_comment', 'creds' ) ); ?>"><?php echo $creds_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'place_checkin_comment', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'place_checkin_comment', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['place_checkin_comment']['creds'] ); ?>" size="8" /></div>
</li>
<li>
<label for="<?php echo $this->field_id( array( 'place_checkin_comment', 'log' ) ); ?>"><?php echo $log_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'place_checkin_comment', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'place_checkin_comment', 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['place_checkin_comment']['log'] ); ?>" class="long" /></div>
<span class="description"><?php echo $available_tags_post; ?></span>
</li>
<li>
<label for="<?php echo $this->field_id( array( 'place_checkin_comment', 'limit' ) ); ?>"><?php echo $limit_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'place_checkin_comment', 'limit' ) ); ?>" id="<?php echo $this->field_id( array( 'place_checkin_comment', 'limit' ) ); ?>" value="<?php echo $this->core->number( $prefs['place_checkin_comment']['limit'] ); ?>" size="8" /> <?php _e( '/ day', 'mycred' ); ?></div>
<span class="description"><?php echo $limit_description; ?></span>
</li>
</ol>
<label for="<?php echo $this->field_id( array( 'group_activity_checkin', 'creds' ) ); ?>" class="subheader"><?php _e( 'Group Profile Checkin', 'mycred' ); ?></label>
<ol>
<li>
<label for="<?php echo $this->field_id( array( 'group_activity_checkin', 'creds' ) ); ?>"><?php echo $creds_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'group_activity_checkin', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'group_activity_checkin', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['group_activity_checkin']['creds'] ); ?>" size="8" /></div>
</li>
<li>
<label for="<?php echo $this->field_id( array( 'group_activity_checkin', 'log' ) ); ?>"><?php echo $log_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'group_activity_checkin', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'group_activity_checkin', 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['group_activity_checkin']['log'] ); ?>" class="long" /></div>
<span class="description"><?php echo $available_tags; ?></span>
</li>
<li>
<label for="<?php echo $this->field_id( array( 'group_activity_checkin', 'limit' ) ); ?>"><?php echo $limit_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'group_activity_checkin', 'limit' ) ); ?>" id="<?php echo $this->field_id( array( 'group_activity_checkin', 'limit' ) ); ?>" value="<?php echo $this->core->number( $prefs['group_activity_checkin']['limit'] ); ?>" size="8" /> <?php _e( '/ day', 'mycred' ); ?></div>
<span class="description"><?php echo $limit_description; ?></span>
</li>
</ol>
<label for="<?php echo $this->field_id( array( 'group_new_place', 'creds' ) ); ?>" class="subheader"><?php _e( 'Group New Place', 'mycred' ); ?></label>
<ol>
<li>
<label for="<?php echo $this->field_id( array( 'group_new_place', 'creds' ) ); ?>"><?php echo $creds_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'group_new_place', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'group_new_place', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['group_new_place']['creds'] ); ?>" size="8" /></div>
</li>
<li>
<label for="<?php echo $this->field_id( array( 'group_new_place', 'log' ) ); ?>"><?php echo $log_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'group_new_place', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'group_new_place', 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['group_new_place']['log'] ); ?>" class="long" /></div>
<span class="description"><?php echo $available_tags_post; ?></span>
</li>
<li>
<label for="<?php echo $this->field_id( array( 'group_new_place', 'limit' ) ); ?>"><?php echo $limit_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'group_new_place', 'limit' ) ); ?>" id="<?php echo $this->field_id( array( 'group_new_place', 'limit' ) ); ?>" value="<?php echo $this->core->number( $prefs['group_new_place']['limit'] ); ?>" size="8" /> <?php _e( '/ day', 'mycred' ); ?></div>
<span class="description"><?php echo $limit_description; ?></span>
</li>
</ol>
<label for="<?php echo $this->field_id( array( 'group_place_checkin', 'creds' ) ); ?>" class="subheader"><?php _e( 'Group Place Checkin', 'mycred' ); ?></label>
<ol>
<li>
<label for="<?php echo $this->field_id( array( 'group_place_checkin', 'creds' ) ); ?>"><?php echo $creds_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'group_place_checkin', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'group_place_checkin', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['group_place_checkin']['creds'] ); ?>" size="8" /></div>
</li>
<li>
<label for="<?php echo $this->field_id( array( 'group_place_checkin', 'log' ) ); ?>"><?php echo $log_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'group_place_checkin', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'group_place_checkin', 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['group_place_checkin']['log'] ); ?>" class="long" /></div>
<span class="description"><?php echo $available_tags_post; ?></span>
</li>
<li>
<label for="<?php echo $this->field_id( array( 'group_place_checkin', 'limit' ) ); ?>"><?php echo $limit_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'group_place_checkin', 'limit' ) ); ?>" id="<?php echo $this->field_id( array( 'group_place_checkin', 'limit' ) ); ?>" value="<?php echo $this->core->number( $prefs['group_place_checkin']['limit'] ); ?>" size="8" /> <?php _e( '/ day', 'mycred' ); ?></div>
<span class="description"><?php echo $limit_description; ?></span>
</li>
</ol>
<label for="<?php echo $this->field_id( array( 'group_place_comment', 'creds' ) ); ?>" class="subheader"><?php _e( 'Group Place Comment', 'mycred' ); ?></label>
<ol>
<li>
<label for="<?php echo $this->field_id( array( 'group_place_comment', 'creds' ) ); ?>"><?php echo $creds_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'group_place_comment', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'group_place_comment', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['group_place_comment']['creds'] ); ?>" size="8" /></div>
</li>
<li>
<label for="<?php echo $this->field_id( array( 'group_place_comment', 'log' ) ); ?>"><?php echo $log_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'group_place_comment', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'group_place_comment', 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['group_place_comment']['log'] ); ?>" class="long" /></div>
<span class="description"><?php echo $available_tags_post; ?></span>
</li>
<li>
<label for="<?php echo $this->field_id( array( 'group_place_comment', 'limit' ) ); ?>"><?php echo $limit_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'group_place_comment', 'limit' ) ); ?>" id="<?php echo $this->field_id( array( 'group_place_comment', 'limit' ) ); ?>" value="<?php echo $this->core->number( $prefs['group_place_comment']['limit'] ); ?>" size="8" /> <?php _e( '/ day', 'mycred' ); ?></div>
<span class="description"><?php echo $limit_description; ?></span>
</li>
</ol>
<label for="<?php echo $this->field_id( array( 'group_place_checkin_comment', 'creds' ) ); ?>" class="subheader"><?php _e( 'Group Place Checkin Comment', 'mycred' ); ?></label>
<ol>
<li>
<label for="<?php echo $this->field_id( array( 'group_place_checkin_comment', 'creds' ) ); ?>"><?php echo $creds_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'group_place_checkin_comment', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'group_place_checkin_comment', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['group_place_checkin_comment']['creds'] ); ?>" size="8" /></div>
</li>
<li>
<label for="<?php echo $this->field_id( array( 'group_place_checkin_comment', 'log' ) ); ?>"><?php echo $log_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'group_place_checkin_comment', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'group_place_checkin_comment', 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['group_place_checkin_comment']['log'] ); ?>" class="long" /></div>
<span class="description"><?php echo $available_tags_post; ?></span>
</li>
<li>
<label for="<?php echo $this->field_id( array( 'group_place_checkin_comment', 'limit' ) ); ?>"><?php echo $limit_label; ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'group_place_checkin_comment', 'limit' ) ); ?>" id="<?php echo $this->field_id( array( 'group_place_checkin_comment', 'limit' ) ); ?>" value="<?php echo $this->core->number( $prefs['group_place_checkin_comment']['limit'] ); ?>" size="8" /> <?php _e( '/ day', 'mycred' ); ?></div>
<span class="description"><?php echo $limit_description; ?></span>
</li>
</ol>
<?php
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment