Created
August 18, 2014 14:54
-
-
Save timersys/e5ea7ff8c36431572ac8 to your computer and use it in GitHub Desktop.
My Creed Wsi integration
This file contains hidden or 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
<?php | |
/** | |
* MyCred Hook | |
* @since v2 | |
* @version 1.1 | |
*/ | |
class Wsi_MyCreed extends myCRED_Hook { | |
/** | |
* Construct | |
*/ | |
function __construct( $hook_prefs ) { | |
parent::__construct( array( | |
'id' => 'wsi', | |
'defaults' => array( | |
'send_invite' => array( | |
'creds' => 1, | |
'log' => '%plural% for sending an invitation', | |
'limit' => 0 | |
), | |
'accept_invite' => array( | |
'creds' => 5, | |
'log' => '%plural% for accepted invitation', | |
'limit' => 0 | |
) | |
) | |
), $hook_prefs ); | |
} | |
/** | |
* Run | |
* @since v2 | |
* @version 1.0 | |
*/ | |
public function run() { | |
if ( $this->prefs['send_invite']['creds'] != 0 ) { | |
add_action( 'wsi_invitation_sent', array( $this, 'send_invite' ), 10, 2 ); | |
} | |
if ( $this->prefs['accept_invite']['creds'] != 0 ) { | |
add_action( 'wsi_invitation_accepted', array( $this, 'accept_invite' ), 10, 3 ); | |
} | |
} | |
/** | |
* Sending Invites | |
* @since v2 | |
* @version 1.0 | |
*/ | |
public function send_invite( $user_id, $wsi_obj_id ) { | |
//this is not a registered user | |
if( empty($user_id)) return; | |
// Check if user is excluded (required) | |
if ( $this->core->exclude_user( $user_id ) ) return; | |
// Limit Check | |
if ( $this->prefs['send_invite']['limit'] != 0 ) { | |
$user_log = get_user_meta( $user_id, 'mycred_wsi', true ); | |
if ( empty( $user_log['sent'] ) ) $user_log['sent'] = 0; | |
// Return if limit is reached | |
if ( $user_log['sent'] >= $this->prefs['send_invite']['limit'] ) return; | |
} | |
// Award Points | |
$this->core->add_creds( | |
'wsi_sending_an_invite', | |
$user_id, | |
$this->prefs['send_invite']['creds'], | |
$this->prefs['send_invite']['log'], | |
'', | |
'', | |
apply_filters( 'wsi_cred_type' ,$cred_type, $wsi_obj_id ) | |
); | |
// Update limit | |
if ( $this->prefs['send_invite']['limit'] != 0 ) { | |
$user_log['sent'] = $user_log['sent']+1; | |
update_user_meta( $user_id, 'mycred_wsi', $user_log ); | |
} | |
} | |
/** | |
* Accepting Invites | |
* @since v2 | |
* @version 1.0 | |
*/ | |
public function accept_invite( $user_id, $stats ) { | |
//this is not a registered user | |
if( empty($user_id)) return; | |
// Check if user is excluded (required) | |
if ( $this->core->exclude_user( $user_id ) ) return; | |
// Limit Check | |
if ( $this->prefs['send_invite']['limit'] != 0 ) { | |
$user_log = get_user_meta( $user_id, 'mycred_wsi', true ); | |
if ( empty( $user_log['sent'] ) ) $user_log['sent'] = 0; | |
// Return if limit is reached | |
if ( $user_log['sent'] >= $this->prefs['send_invite']['limit'] ) return; | |
} | |
// Award Points | |
$this->core->add_creds( | |
'wsi_accepting_an_invite', | |
$user_id, | |
$this->prefs['accept_invite']['creds'], | |
$this->prefs['accept_invite']['log'], | |
'', | |
'', | |
apply_filters( 'wsi_cred_type' ,$cred_type, $stats->wsi_obj_id ) | |
); | |
// Update Limit | |
if ( $this->prefs['accept_invite']['limit'] != 0 ) { | |
$user_log['accepted'] = $user_log['accepted']+1; | |
update_user_meta( $user_id, 'mycred_wsi', $user_log ); | |
} | |
} | |
/** | |
* Preferences | |
* @since v2 | |
* @version 1 | |
*/ | |
public function preferences() { | |
$prefs = $this->prefs; | |
?> | |
<!-- Creds for Sending Invites --> | |
<label for="<?php echo $this->field_id( array( 'send_invite', 'creds' ) ); ?>" class="subheader"><?php echo $this->core->template_tags_general( __( '%plural% for Sending An Invite', 'mycred' ) ); ?></label> | |
<ol> | |
<li> | |
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'send_invite', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'send_invite', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['send_invite']['creds'] ); ?>" size="8" /></div> | |
</li> | |
<li class="empty"> </li> | |
<li> | |
<label for="<?php echo $this->field_id( array( 'send_invite', 'log' ) ); ?>"><?php _e( 'Log template', 'mycred' ); ?></label> | |
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'send_invite', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'send_invite', 'log' ) ); ?>" value="<?php echo $prefs['send_invite']['log']; ?>" class="long" /></div> | |
<span class="description"><?php _e( 'Available template tags: General', 'mycred' ); ?></span> | |
</li> | |
</ol> | |
<label for="<?php echo $this->field_id( array( 'send_invite', 'limit' ) ); ?>" class="subheader"><?php _e( 'Limit', 'mycred' ); ?></label> | |
<ol> | |
<li> | |
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'send_invite', 'limit' ) ); ?>" id="<?php echo $this->field_id( array( 'send_invite', 'limit' ) ); ?>" value="<?php echo $prefs['send_invite']['limit']; ?>" size="8" /></div> | |
<span class="description"><?php echo $this->core->template_tags_general( __( 'Maximum number of invites that grants %_plural%. Use zero for unlimited.', 'mycred' ) ); ?></span> | |
</li> | |
</ol> | |
<!-- Creds for Accepting Invites --> | |
<label for="<?php echo $this->field_id( array( 'accept_invite', 'creds' ) ); ?>" class="subheader"><?php echo $this->core->template_tags_general( __( '%plural% for Accepting An Invite', 'mycred' ) ); ?></label> | |
<ol> | |
<li> | |
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'accept_invite', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'accept_invite', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['accept_invite']['creds'] ); ?>" size="8" /></div> | |
<span class="description"><?php echo $this->core->template_tags_general( __( '%plural% for each invited user that accepts an invitation.', 'mycred' ) ); ?></span> | |
</li> | |
<li class="empty"> </li> | |
<li> | |
<label for="<?php echo $this->field_id( array( 'accept_invite', 'log' ) ); ?>"><?php _e( 'Log template', 'mycred' ); ?></label> | |
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'accept_invite', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'accept_invite', 'log' ) ); ?>" value="<?php echo $prefs['accept_invite']['log']; ?>" class="long" /></div> | |
<span class="description"><?php _e( 'Available template tags: General', 'mycred' ); ?></span> | |
</li> | |
</ol> | |
<label for="<?php echo $this->field_id( array( 'accept_invite', 'limit' ) ); ?>" class="subheader"><?php _e( 'Limit', 'mycred' ); ?></label> | |
<ol> | |
<li> | |
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'accept_invite', 'limit' ) ); ?>" id="<?php echo $this->field_id( array( 'accept_invite', 'limit' ) ); ?>" value="<?php echo $prefs['accept_invite']['limit']; ?>" size="8" /></div> | |
<span class="description"><?php echo $this->core->template_tags_general( __( 'Maximum number of accepted invitations that grants %_plural%. Use zero for unlimited.', 'mycred' ) ); ?></span> | |
</li> | |
</ol> | |
<?php unset( $this ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment