Skip to content

Instantly share code, notes, and snippets.

@gabrielmerovingi
Last active December 19, 2023 20:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielmerovingi/b44b48fc79b8d09d611e59a33526b5b9 to your computer and use it in GitHub Desktop.
Save gabrielmerovingi/b44b48fc79b8d09d611e59a33526b5b9 to your computer and use it in GitHub Desktop.
This custom hook allows you to reward users viewing content based on the category the post belongs to. Goes into your child theme's functions.php file or custom plugin.
/**
* Register Custom Hook
* @since 1.0
* @version 1.0
*/
add_filter( 'mycred_setup_hooks', 'mycred_pro_register_view_content_category_hook' );
function mycred_pro_register_view_content_category_hook( $installed ) {
$installed['view_category_content'] = array(
'title' => __( '%plural% for Viewing Content (Categories)', 'mycred' ),
'description' => __( 'Award users points for viewing content based on category.', 'mycred' ),
'callback' => array( 'myCRED_View_Content_Category' )
);
return $installed;
}
/**
* Add References
* @since 1.0
* @version 1.0
*/
add_filter( 'mycred_all_references', 'mycred_pro_add_custom_references' );
function mycred_pro_add_custom_references( $references ) {
$categories = get_categories( array( 'hide_empty' => false ) );
if ( ! empty( $categories ) ) {
foreach ( $categories as $cat ) {
$ref = 'view_post_' . $cat->slug;
$ref = sanitize_key( $ref );
if ( ! array_key_exists( $ref, $references ) )
$references[ $ref ] = sprintf( __( 'Viewing content in %s', 'mycred' ), $cat->name );
}
}
return $references;
}
/**
* Load Custom Hook
* @version 1.0.1
*/
add_action( 'mycred_load_hooks', 'mycred_pro_load_view_content_category_hook' );
function mycred_pro_load_view_content_category_hook() {
class myCRED_View_Content_Category extends myCRED_Hook {
public $categories = array();
/**
* Construct
*/
function __construct( $hook_prefs, $type = MYCRED_DEFAULT_TYPE_KEY ) {
$this->categories = get_categories( array( 'hide_empty' => false ) );
$defaults = array();
foreach ( $this->categories as $cat )
$defaults[ $cat->slug ] = array(
'creds' => 0,
'log' => '%plural% for publishing content',
'limit' => '0/x'
);
if ( isset( $hook_prefs['view_category_content'] ) )
$defaults = $hook_prefs['view_category_content'];
parent::__construct( array(
'id' => 'view_category_content',
'defaults' => $defaults
), $hook_prefs, $type );
}
/**
* Run
* @version 1.0
*/
public function run() {
add_action( 'template_redirect', array( $this, 'content_loading' ), 999 );
}
/**
* Content Loaded
* @version 1.0
*/
public function content_loading() {
// Only applicable on single post type view by logged in users
if ( ! is_singular() || ! is_user_logged_in() ) return;
global $post;
$user_id = get_current_user_id();
$data = array( 'ref_type' => 'post' );
// Authors can not get points for viewing their own content
// Excluded users can not get points
if ( $post->post_author == $user_id || $this->core->exclude_user( $user_id ) ) return;
// Get all the categories this post is assigned to
$assigned_categories = get_the_category( $post->ID );
if ( ! empty( $assigned_categories ) ) {
foreach ( $assigned_categories as $category ) {
$reference = 'view_post_' . $category->slug;
$reference = sanitize_key( $reference );
// Make sure we are not over a limit (if set)
if ( $this->over_hook_limit( $category->slug, $reference, $user_id ) ) continue;
// Make sure we only get points once
if ( ! $this->core->has_entry( $reference, $post->ID, $user_id, $data, $this->mycred_type ) ) {
$this->core->add_creds(
$reference,
$user_id,
$this->prefs[ $category->slug ]['creds'],
$this->prefs[ $category->slug ]['log'],
$post->ID,
$data,
$this->mycred_type
);
}
}
}
}
/**
* Preferences
* @version 1.0
*/
public function preferences() {
foreach ( $this->categories as $cat ) {
$prefs = shortcode_atts( array(
'creds' => 0,
'log' => '%plural% for viewing content',
'limit' => '0/x'
), ( ( array_key_exists( $cat->slug, $this->prefs ) ) ? $this->prefs[ $cat->slug ] : array() ) );
?>
<div class="hook-instance">
<h3><?php printf( __( 'Posts in: %s', 'mycred' ), $cat->name ); ?></h3>
<div class="row">
<div class="col-lg-2 col-md-6 col-sm-12 col-xs-12">
<div class="form-group">
<label for="<?php echo $this->field_id( array( $cat->slug => 'creds' ) ); ?>"><?php _e( 'Amount', 'mycred' ); ?></label>
<input type="text" name="<?php echo $this->field_name( array( $cat->slug => 'creds' ) ); ?>" id="<?php echo $this->field_id( array( $cat->slug => 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['creds'] ); ?>" class="form-control" />
</div>
</div>
<div class="col-lg-4 col-md-6 col-sm-12 col-xs-12">
<div class="form-group">
<label for="<?php echo $this->field_id( array( $cat->slug, 'limit' ) ); ?>"><?php _e( 'Limit', 'mycred' ); ?></label>
<?php echo $this->hook_limit_setting( $this->field_name( array( $cat->slug, 'limit' ) ), $this->field_id( array( $cat->slug, 'limit' ) ), $prefs['limit'] ); ?>
</div>
</div>
<div class="col-lg-6 col-md-12 col-sm-12 col-xs-12">
<div class="form-group">
<label for="<?php echo $this->field_id( array( $cat->slug => 'log' ) ); ?>"><?php _e( 'Log Template', 'mycred' ); ?></label>
<input type="text" name="<?php echo $this->field_name( array( $cat->slug => 'log' ) ); ?>" id="<?php echo $this->field_id( array( $cat->slug => 'log' ) ); ?>" placeholder="<?php _e( 'required', 'mycred' ); ?>" value="<?php echo esc_attr( $prefs['log'] ); ?>" class="form-control" />
<span class="description"><?php echo $this->available_template_tags( array( 'general', 'post' ) ); ?></span>
</div>
</div>
</div>
</div>
<?php
}
}
/**
* Sanitise Preferences
* @version 1.1
*/
public function sanitise_preferences( $data ) {
foreach ( $this->categories as $category ) {
if ( isset( $data[ $category->slug ]['limit'] ) && isset( $data[ $category->slug ]['limit_by'] ) ) {
$limit = sanitize_text_field( $data[ $category->slug ]['limit'] );
if ( $limit == '' ) $limit = 0;
$data[ $category->slug ]['limit'] = $limit . '/' . $data[ $category->slug ]['limit_by'];
unset( $data[ $category->slug ]['limit_by'] );
}
}
return $data;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment