Skip to content

Instantly share code, notes, and snippets.

@gabrielmerovingi
Last active September 10, 2020 03:07
Show Gist options
  • Save gabrielmerovingi/ddb11ba0d1c0fdab3d6a to your computer and use it in GitHub Desktop.
Save gabrielmerovingi/ddb11ba0d1c0fdab3d6a to your computer and use it in GitHub Desktop.
Custom myCRED Hook for Post Views Counter. Points can be awarded or deducted from post authors when someone views their post. Requires Post Views Counter version 1.0.5 or higher.
/**
* Post Views Counter Hook
* Requires Post Views Counter 1.0.5 or higher.
* @version 1.0.1
* @author Gabriel S Merovingi
*/
add_filter( 'mycred_setup_hooks', 'register_post_views_counter_hook_in_mycred' );
function register_post_views_counter_hook_in_mycred( $installed ) {
$installed['postviewscounter'] = array(
'title' => __( 'Post Views Counter', 'textdomain' ),
'description' => __( 'This hook awards / deducts points for post views via the Post Views Counter plugin.', 'textdomain' ),
'callback' => array( 'myCRED_Hook_Post_Views_Counter' )
);
return $installed;
}
if ( class_exists( 'myCRED_Hook' ) ) :
class myCRED_Hook_Post_Views_Counter extends myCRED_Hook {
/**
* Construct
*/
function __construct( $hook_prefs, $type = 'mycred_default' ) {
parent::__construct( array(
'id' => 'postviewscounter',
'defaults' => array(
'creds' => 1,
'log' => '%plural% for post view',
'limit' => 0
)
), $hook_prefs, $type );
}
/**
* Hook into Post Views Counter
* @since 1.0
* @version 1.0
*/
public function run() {
add_action( 'pvc_after_count_visit', array( $this, 'process_new_view' ) );
}
/**
* Process View
* @since 1.0
* @version 1.0.1
*/
public function process_new_view( $post_id ) {
$post = get_post( $post_id );
if ( ! isset( $post->post_author ) ) return;
// Check for exclusions
if ( $this->core->exclude_user( $post->post_author ) ) return;
// Payout if not over limit
if ( ! $this->over_hook_limit( '', 'postview', $post->post_author ) )
$this->core->add_creds(
'postview',
$post->post_author,
$this->prefs['creds'],
$this->prefs['log'],
$post_id,
array( 'ref_type' => 'post' ),
$this->mycred_type
);
}
/**
* Preference for this Hook
* @since 1.0
* @version 1.0
*/
public function preferences() {
$prefs = $this->prefs;
?>
<label class="subheader" for="<?php echo $this->field_id( 'creds' ); ?>"><?php _e( 'Post View', 'textdomain' ); ?></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->format_number( $prefs['creds'] ); ?>" size="8" /></div>
</li>
<li>
<label for="<?php echo $this->field_id( 'limit' ); ?>"><?php _e( 'Limit', 'textdomain' ); ?></label>
<?php echo $this->hook_limit_setting( $this->field_name( 'limit' ), $this->field_id( 'limit' ), $prefs['limit'] ); ?>
</li>
<li class="empty">&nbsp;</li>
<li>
<label for="<?php echo $this->field_id( 'log' ); ?>"><?php _e( 'Log template', 'textdomain' ); ?></label>
<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>
<span class="description"><?php echo $this->core->available_template_tags( array( 'general', 'post' ) ); ?></span>
</li>
</ol>
<?php
}
/**
* Sanitise Preferences
* @since 1.0
* @version 1.0
*/
public function sanitise_preferences( $data ) {
if ( isset( $data['limit'] ) && isset( $data['limit_by'] ) ) {
$limit = sanitize_text_field( $data['limit'] );
if ( $limit == '' ) $limit = 0;
$data['limit'] = $limit . '/' . $data['limit_by'];
unset( $data['limit_by'] );
}
return $data;
}
}
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment