Created
November 1, 2016 15:48
-
-
Save gabrielmerovingi/bfe3323225b0df8f149db6024622ffca to your computer and use it in GitHub Desktop.
Custom hook example: Give users points on their birthday (BuddyPress Version). The birthday date must be stored as a custom XProfile field in BuddyPress.
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 Birthday Hook | |
* @version 1.0 | |
*/ | |
add_filter( 'mycred_setup_hooks', 'mycred_pro_register_bp_birthday_hook' ); | |
function mycred_pro_register_bp_birthday_hook( $installed ) { | |
$installed['birthday'] = array( | |
'title' => '%plural% for Birthdays', | |
'description' => 'Reward users with points on their birthday', | |
'callback' => array( 'myCRED_BP_Birthday_Hook' ) | |
); | |
return $installed; | |
} | |
/** | |
* Load Birthday Hook | |
* @version 1.0 | |
*/ | |
add_action( 'mycred_load_hooks', 'mycred_pro_load_bp_birthday_hook' ); | |
function mycred_pro_load_bp_birthday_hook() { | |
// Backwatds comp. pre 1.7 | |
if ( ! defined( 'MYCRED_DEFAULT_TYPE_KEY' ) ) define( 'MYCRED_DEFAULT_TYPE_KEY', 'mycred_default' ); | |
class myCRED_BP_Birthday_Hook extends myCRED_Hook { | |
/** | |
* Construct | |
*/ | |
function __construct( $hook_prefs, $type = MYCRED_DEFAULT_TYPE_KEY ) { | |
parent::__construct( array( | |
'id' => 'birthday', | |
'defaults' => array( | |
'format' => 'Y-m-d', | |
'field_id' => 'birthday', | |
'creds' => 1, | |
'log' => '%plural% for birthday' | |
) | |
), $hook_prefs, $type ); | |
} | |
/** | |
* Run | |
* Runs if the hook is enabled. | |
* @version 1.0 | |
*/ | |
public function run() { | |
add_action( 'mycred_reset_key', array( $this, 'cron' ) ); | |
} | |
/** | |
* Every day, check if anyone has a birthday today. | |
* Users can only get points if they are not excluded and if | |
* they have not recevied points already this year. | |
* @version 1.0 | |
*/ | |
public function cron() { | |
global $wpdb, $bp; | |
$now = current_time( 'timestamp' ); | |
$todays_date = date( $this->prefs['format'], $now ); | |
$this_year = date( 'Y', $now ); | |
$birthdays = $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$bp->profile->table_name_data} WHERE field_id = %s AND value = %s;", $this->prefs['field_id'], $todays_date ) ); | |
if ( ! empty( $birthdays ) ) { | |
foreach ( $birthdays as $user_id ) { | |
if ( $this->core->exclude_user( $user_id ) ) continue; | |
if ( ! $this->has_entry( 'birthday', $this_year, $user_id ) ) | |
$this->core->add_creds( | |
'birthday', | |
$user_id, | |
$this->prefs['creds'], | |
$this->prefs['log'], | |
$this_year, | |
'', | |
$this->mycred_type | |
); | |
} | |
} | |
} | |
/** | |
* Hook Settings | |
*/ | |
public function preferences() { | |
$prefs = $this->prefs; | |
?> | |
<!-- First we set the amount --> | |
<label class="subheader"><?php echo $this->core->plural(); ?></label> | |
<ol> | |
<li> | |
<div class="h2"><input type="text" name="<?php echo $this->field_name( 'creds' ); ?>" id="<?php echo $this->field_id( 'creds' ); ?>" value="<?php echo $this->core->number( $prefs['creds'] ); ?>" size="8" /></div> | |
</li> | |
</ol> | |
<!-- Then the log template --> | |
<label class="subheader">Log Template</label> | |
<ol> | |
<li> | |
<div class="h2"><input type="text" name="<?php echo $this->field_name( 'log' ); ?>" id="<?php echo $this->field_id( 'log' ); ?>" value="<?php echo esc_attr( $prefs['log'] ); ?>" class="long" /></div> | |
</li> | |
</ol> | |
<!-- Field ID --> | |
<label class="subheader">BuddyPress Profile Field ID</label> | |
<ol> | |
<li> | |
<div class="h2"><input type="text" name="<?php echo $this->field_name( 'field_id' ); ?>" id="<?php echo $this->field_id( 'field_id' ); ?>" value="<?php echo esc_attr( $prefs['field_id'] ); ?>" class="long" /></div> | |
<p>The ID of the field where users enter their birthday.</p> | |
</li> | |
</ol> | |
<!-- Date Format --> | |
<label class="subheader">Date Format</label> | |
<ol> | |
<li> | |
<div class="h2"><input type="text" name="<?php echo $this->field_name( 'format' ); ?>" id="<?php echo $this->field_id( 'format' ); ?>" value="<?php echo esc_attr( $prefs['format'] ); ?>" class="long" /></div> | |
<p>The format used when storing the users date of birth. Fields where the date is not formatted correctly will not receive a payout! Recommended either <code>Y-m-d</code> or <code>m/d/Y</code>. For a complete list of acceptable formats, see the <a href="http://php.net/manual/en/function.date.php" target="_blank">PHP documentation.</a></p> | |
</li> | |
</ol> | |
<?php | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment