Skip to content

Instantly share code, notes, and snippets.

@gabrielmerovingi
Last active May 3, 2018 21:11
Show Gist options
  • Save gabrielmerovingi/8f6313533956d9575139 to your computer and use it in GitHub Desktop.
Save gabrielmerovingi/8f6313533956d9575139 to your computer and use it in GitHub Desktop.
Custom myCRED hook awarding points for Schreikasten comments.
/**
* Register myCRED Hook
* First we need to tell myCRED that there is this custom hook.
* We need to give a unique ID for our hook and define the Hook class's
* name. The hook class is loaded seperatly.
* @requires myCRED 1.6 or higher
* @version 1.0.2
*/
add_filter( 'mycred_setup_hooks', 'register_schreikasten_mycred_hook' );
function register_schreikasten_mycred_hook( $installed ) {
$installed['schreikasten'] = array(
'title' => __( 'Schreikasten', 'mycred' ),
'description' => __( 'Awards %_plural% for comments.', 'mycred' ),
'callback' => array( 'myCRED_Schreikasten' )
);
return $installed;
}
/**
* Load Schreikasten Hook
* @version 1.0
*/
if ( ! class_exists( 'myCRED_Schreikasten' ) && class_exists( 'myCRED_Hook' ) ) :
/**
* The Custom Hook Class
* Built of the abstract myCRED_Hook class, we only need to define
* a contruct, a run method, hook execution methods and if needed settings
* and setting sanitation.
* @version 1.0
*/
class myCRED_Schreikasten extends myCRED_Hook {
/**
* Construct
*/
function __construct( $hook_prefs, $type = 'mycred_default' ) {
parent::__construct( array(
'id' => 'schreikasten',
'defaults' => array(
'new_comment' => array(
'creds' => 0,
'log' => '%plural% for new comment',
'limit' => '0/x'
),
'remove_comment' => array(
'creds' => 0,
'log' => '%plural% for removed comment',
'limit' => '0/x'
)
)
), $hook_prefs, $type );
}
/**
* Run
* This method runs during "init" if the hook has been enabled.
* If the hook is not enabled, this class will never be constructed.
* This methos should be used to hook into a third party plugin or load
* core features.
* @version 1.0.1
*/
public function run() {
// If we reward affiliate signups
add_action( 'sk_add', array( $this, 'new_comment' ) );
// If we reward visit referrals
add_action( 'sk_delete', array( $this, 'delete_comment' ) );
// If we reward referrals
add_filter( 'sk_points', array( $this, 'show_points' ), 10, 2 );
}
/**
* New Comment
* @version 1.0
*/
public function new_comment( $comment ) {
if ( $comment->user_id == 0 ) return;
// Check for exclusion
if ( $this->core->exclude_user( $comment->user_id ) ) return;
// Limit
if ( $this->over_hook_limit( 'new_comment', 'approved_comment', $comment->user_id ) ) return;
// Execute
$this->core->add_creds(
'approved_comment',
$comment->user_id,
$this->prefs['new_comment']['creds'],
$this->prefs['new_comment']['log'],
$comment->comment_ID,
array( 'ref_id' => 'comment' ),
$this->mycred_type
);
}
/**
* Delete Comment
* @version 1.0
*/
public function delete_comment( $comment ) {
if ( $comment->user_id == 0 ) return;
// Check for exclusion
if ( $this->core->exclude_user( $comment->user_id ) ) return;
// Limit
if ( $this->over_hook_limit( 'remove_comment', 'remove_comment', $comment->user_id ) ) return;
// Execute
$this->core->add_creds(
'remove_comment',
$comment->user_id,
$this->prefs['remove_comment']['creds'],
$this->prefs['remove_comment']['log'],
$comment->comment_ID,
array( 'ref_id' => 'comment' ),
$this->mycred_type
);
}
/**
* Show Points
* @version 1.0.1
*/
public function show_points( $points, $comment ) {
if ( ! isset( $comment->user_id ) || $comment->user_id == 0 ) return $points;
// Check for exclusion
if ( $this->core->exclude_user( $comment->user_id ) ) return $points;
// Get users balance
$balance = $this->core->get_users_balance( $comment->user_id, $this->mycred_type );
return $this->core->format_creds( $balance );
}
/**
* Preferences
* Our hooks settings. This is optional and if this method is not defined
* the hook will show "This hook has no settings" in the admin area.
* In this case the only option available is to enable / disable the hook.
* @version 1.0
*/
public function preferences() {
$prefs = $this->prefs;
?>
<label for="<?php echo $this->field_id( array( 'new_comment', 'creds' ) ); ?>" class="subheader"><?php _e( 'New Comment', 'mycred' ); ?></label>
<ol>
<li>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'new_comment', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'new_comment', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['new_comment']['creds'] ); ?>" size="8" /></div>
</li>
<li>
<label for="<?php echo $this->field_id( array( 'new_comment' => 'limit' ) ); ?>"><?php _e( 'Limit', 'mycred' ); ?></label>
<?php echo $this->hook_limit_setting( $this->field_name( array( 'new_comment' => 'limit' ) ), $this->field_id( array( 'new_comment' => 'limit' ) ), $prefs['new_comment']['limit'] ); ?>
</li>
<li class="empty">&nbsp;</li>
<li>
<label for="<?php echo $this->field_id( array( 'new_comment', 'log' ) ); ?>"><?php _e( 'Log template', 'mycred' ); ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'new_comment', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'new_comment', 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['new_comment']['log'] ); ?>" class="long" /></div>
<span class="description"><?php echo $this->available_template_tags( array( 'general', 'comment' ) ); ?></span>
</li>
</ol>
<label for="<?php echo $this->field_id( array( 'remove_comment', 'creds' ) ); ?>" class="subheader"><?php _e( 'Delete Comment', 'mycred' ); ?></label>
<ol>
<li>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'remove_comment', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'remove_comment', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['remove_comment']['creds'] ); ?>" size="8" /></div>
</li>
<li>
<label for="<?php echo $this->field_id( array( 'remove_comment' => 'limit' ) ); ?>"><?php _e( 'Limit', 'mycred' ); ?></label>
<?php echo $this->hook_limit_setting( $this->field_name( array( 'remove_comment' => 'limit' ) ), $this->field_id( array( 'remove_comment' => 'limit' ) ), $prefs['remove_comment']['limit'] ); ?>
</li>
<li class="empty">&nbsp;</li>
<li>
<label for="<?php echo $this->field_id( array( 'remove_comment', 'log' ) ); ?>"><?php _e( 'Log template', 'mycred' ); ?></label>
<div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'remove_comment', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'remove_comment', 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['remove_comment']['log'] ); ?>" class="long" /></div>
<span class="description"><?php echo $this->available_template_tags( array( 'general', 'comment' ) ); ?></span>
</li>
</ol>
<?php
}
/**
* Sanitise Preferences
* If we are using the hook limit feature we must sanitize our settings.
* This is because limits are stored as a string but submitted in the form
* as two seperate items. These two values "limit" and "limit_by" must be combined
* into limit/limit_by. Example (no limit) 0/x.
* @version 1.0
*/
function sanitise_preferences( $data ) {
if ( isset( $data['new_comment']['limit'] ) && isset( $data['new_comment']['limit_by'] ) ) {
$limit = sanitize_text_field( $data['new_comment']['limit'] );
if ( $limit == '' ) $limit = 0;
$data['new_comment']['limit'] = $limit . '/' . $data['new_comment']['limit_by'];
unset( $data['new_comment']['limit_by'] );
}
if ( isset( $data['remove_comment']['limit'] ) && isset( $data['remove_comment']['limit_by'] ) ) {
$limit = sanitize_text_field( $data['remove_comment']['limit'] );
if ( $limit == '' ) $limit = 0;
$data['remove_comment']['limit'] = $limit . '/' . $data['remove_comment']['limit_by'];
unset( $data['remove_comment']['limit_by'] );
}
return $data;
}
}
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment