Created
June 16, 2016 14:15
-
-
Save gabrielmerovingi/916bb49035b35f8e4a1a4032891ce520 to your computer and use it in GitHub Desktop.
Custom myCRED hook for BP Follow.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Register Custom myCRED Hook | |
* @since 1.0 | |
* @version 1.0 | |
*/ | |
add_filter( 'mycred_setup_hooks', 'BP_Follow_myCRED_Hook' ); | |
function BP_Follow_myCRED_Hook( $installed ) { | |
$installed['hook_bp_follow'] = array( | |
'title' => __( 'BP Follow', 'mycred' ), | |
'description' => __( 'Awards %_plural% for following other users.', 'mycred' ), | |
'callback' => array( 'myCRED_Hook_BP_Follow' ) | |
); | |
return $installed; | |
} | |
/** | |
* Hook for Following | |
* @since 1.0 | |
* @version 1.0 | |
*/ | |
add_action( 'mycred_load_hooks', 'mycred_load_bp_follow_hook' ); | |
function mycred_load_bp_follow_hook() { | |
class myCRED_Hook_BP_Follow extends myCRED_Hook { | |
/** | |
* Construct | |
*/ | |
function __construct( $hook_prefs, $type = 'mycred_default' ) { | |
parent::__construct( array( | |
'id' => 'hook_bp_follow', | |
'defaults' => array( | |
'add_follow' => array( | |
'creds' => 0, | |
'log' => '%plural% for Following' | |
), | |
'new_follower' => array( | |
'creds' => 0, | |
'log' => '%plural% for a New Follower' | |
) | |
) | |
), $hook_prefs, $type ); | |
} | |
/** | |
* Run | |
* @since 1.0 | |
* @version 1.0 | |
*/ | |
public function run() { | |
// Follow/Follower | |
if ( $this->prefs['add_follow']['creds'] != 0 || $this->prefs['new_follower']['creds'] != 0 ) | |
add_action( 'bp_follow_start_following', array( $this, 'add_follow' ), 20, 1 ); | |
} | |
/** | |
* Add Follow | |
* @since 1.0 | |
* @version 1.0 | |
*/ | |
public function add_follow( $follow ) { | |
$follower_id = $follow->follower_id; | |
$leader_id = $follow->leader_id; | |
// Check if follow completed | |
if ( $this->core->exclude_user( $follower_id ) ) return; | |
// Check if friend is excluded | |
if ( $this->core->exclude_user( $leader_id ) ) return; | |
// Make sure this is unique event | |
if ( $this->core->has_entry( 'add_follow', $leader_id, $follower_id ) ) return; | |
// Points to initiator | |
$this->core->add_creds( | |
'add_follow', | |
$follower_id, | |
$this->prefs['add_follow']['creds'], | |
$this->prefs['add_follow']['log'], | |
$leader_id, | |
array( 'ref_type' => 'user' ), | |
$this->mycred_type | |
); | |
// Points to friend (ignored if we are deducting points for new friendships) | |
if ( $this->prefs['new_follower']['creds'] > 0 ) | |
$this->core->add_creds( | |
'new_follower', | |
$leader_id, | |
$this->prefs['new_follower']['creds'], | |
$this->prefs['new_follower']['log'], | |
$follower_id, | |
array( 'ref_type' => 'user' ), | |
$this->mycred_type | |
); | |
} | |
/** | |
* Preferences | |
* @since 1.0 | |
* @version 1.0 | |
*/ | |
public function preferences() { | |
$prefs = $this->prefs; | |
?> | |
<label class="subheader" for="<?php echo $this->field_id( array( 'add_follow' => 'creds' ) ); ?>"> | |
<ol> | |
<li> | |
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'add_follow' => 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'add_follow' => 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['add_follow']['creds'] ); ?>" size="8" /></div> | |
</li> | |
</ol> | |
<label class="subheader" for="<?php echo $this->field_id( array( 'add_follow' => 'log' ) ); ?>"> | |
<ol> | |
<li> | |
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'add_follow' => 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'add_follow' => 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['add_follow']['log'] ); ?>" class="long" /></div> | |
<span class="description"><?php echo $this->available_template_tags( array( 'general', 'post' ) ); ?></span> | |
</li> | |
</ol> | |
<label class="subheader" for="<?php echo $this->field_id( array( 'new_follower' => 'creds' ) ); ?>"> | |
<ol> | |
<li> | |
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'new_follower' => 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'new_follower' => 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['new_follower']['creds'] ); ?>" size="8" /></div> | |
</li> | |
</ol> | |
<label class="subheader" for="<?php echo $this->field_id( array( 'new_follower' => 'log' ) ); ?>"> | |
<ol> | |
<li> | |
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'new_follower' => 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'new_follower' => 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['add_follow']['log'] ); ?>" class="long" /></div> | |
<span class="description"><?php echo $this->available_template_tags( array( 'general', 'post' ) ); ?></span> | |
</li> | |
</ol> | |
<?php | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment