Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom hook that allows you to set a specific amount of points to reward users when they publish posts in a pre-defined sets of categories.
/**
* Register Custom Hook
* @since 1.0
* @version 1.0
*/
add_filter( 'mycred_setup_hooks', 'mycred_pro_register_publish_content_category_hook' );
function mycred_pro_register_publish_content_category_hook( $installed ) {
$installed['publish_category_content'] = array(
'title' => __( '%plural% for Publishing Content (Categories)', 'mycred' ),
'description' => __( 'Award users points based on the category a post is published.', 'mycred' ),
'callback' => array( 'myCRED_Publish_Content_Category' )
);
return $installed;
}
/**
* Load Custom Hook
* @version 1.0
*/
add_action( 'mycred_load_hooks', 'mycred_pro_load_publish_content_category_hook' );
function mycred_pro_load_publish_content_category_hook() {
class myCRED_Publish_Content_Category extends myCRED_Hook {
public $categories = array();
/**
* Construct
*/
function __construct( $hook_prefs, $type = MYCRED_DEFAULT_TYPE_KEY ) {
$this->categories = array( 'funny', 'lifeatwork', 'lifestyle', 'nsfw', 'tips-insight', 'traveling', 'trending-figure', 'wtf' );
$defaults = array();
foreach ( $this->categories as $cat )
$defaults[ $cat ] = array(
'creds' => 0,
'log' => '%plural% for publishing content',
'limit' => '0/x'
);
if ( isset( $hook_prefs['publish_category_content'] ) )
$defaults = $hook_prefs['publish_category_content'];
parent::__construct( array(
'id' => 'publish_category_content',
'defaults' => $defaults
), $hook_prefs, $type );
}
/**
* Run
* @version 1.0
*/
public function run() {
add_action( 'transition_post_status', array( $this, 'maybe_publish' ), 10, 3 );
add_filter( 'mycred_all_references', array( $this, 'add_badge_support' ) );
}
/**
* Add Badge Support
* @version 1.0
*/
public function add_badge_support( $references ) {
foreach ( $this->categories as $category ) {
$cat = get_term_by( 'slug', $category );
if ( ! isset( $cat->term_id ) ) continue;
$ref = 'published_' . $cat->slug;
if ( ! array_key_exists( $ref, $references ) )
$references[ $ref ] = sprintf( 'Published content in %s', $cat->name );
}
return $references;
}
/**
* Maybe Publish
* @version 1.0
*/
public function maybe_publish( $new_status, $old_status, $post ) {
// Check for exclusions
if ( $this->core->exclude_user( $post->post_author ) === true ) return;
// We want to fire when content get published or when it gets privatly published
$status = apply_filters( 'mycred_publish_hook_old', array( 'new', 'auto-draft', 'draft', 'private', 'pending' ) );
$publish_status = apply_filters( 'mycred_publish_hook_new', array( 'publish', 'private' ) );
// Make sure this is the right transition
if ( in_array( $old_status, $status ) && in_array( $new_status, $publish_status ) ) {
// Check if the post has any of our nominated categories
if ( ! has_term( $this->categories, 'category', $post ) ) return;
// Get all the categories the post belongs to in case we have more than one
$categories_assigned = get_the_category( $post->ID );
// Loop through all assigned categories
if ( ! empty( $categories_assigned ) ) {
foreach ( $categories_assigned as $category ) {
// If we set a vale of zero, skip
if ( $this->prefs[ $category->slug ]['creds'] == 0 ) continue;
// Add this under a custom reference
$reference = 'published_' . $category->slug;
$data = array( 'ref_type' => 'post' );
if ( ! $this->over_hook_limit( $category->slug, $reference, $post->post_author ) )
$this->core->add_creds(
$reference,
$post->post_author,
$this->prefs[ $category->slug ]['creds'],
$this->prefs[ $category->slug ]['log'],
$post->ID,
$data,
$this->mycred_type
);
}
}
}
}
/**
* Preference for Viewing Content Hook
* @version 1.0
*/
public function preferences() {
foreach ( $this->categories as $category ) {
$cat = get_term_by( 'slug', $category, 'category' );
if ( ! isset( $cat->term_id ) ) continue;
$prefs = shortcode_atts( array(
'creds' => 0,
'log' => '%plural% for publishing content',
'limit' => '0/x'
), ( ( array_key_exists( $category, $this->prefs ) ) ? $this->prefs[ $category ] : array() ) );
?>
<div class="hook-instance">
<h3><?php printf( 'Published in: %s', $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( $category => 'creds' ) ); ?>"><?php _e( 'Amount', 'mycred' ); ?></label>
<input type="text" name="<?php echo $this->field_name( array( $category => 'creds' ) ); ?>" id="<?php echo $this->field_id( array( $category => '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( $category, 'limit' ) ); ?>"><?php _e( 'Limit', 'mycred' ); ?></label>
<?php echo $this->hook_limit_setting( $this->field_name( array( $category, 'limit' ) ), $this->field_id( array( $category, '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( $category => 'log' ) ); ?>"><?php _e( 'Log Template', 'mycred' ); ?></label>
<input type="text" name="<?php echo $this->field_name( array( $category => 'log' ) ); ?>" id="<?php echo $this->field_id( array( $category => '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.0
*/
public function sanitise_preferences( $data ) {
foreach ( $this->categories as $category ) {
if ( isset( $data[ $category ]['limit'] ) && isset( $data[ $category ]['limit_by'] ) ) {
$limit = sanitize_text_field( $data[ $category ]['limit'] );
if ( $limit == '' ) $limit = 0;
$data[ $category ]['limit'] = $limit . '/' . $data[ $category ]['limit_by'];
unset( $data[ $category ]['limit_by'] );
}
}
return $data;
}
}
}
@pije76

This comment has been minimized.

Copy link

@pije76 pije76 commented Jul 20, 2019

How about to set a custom amount of points to reward for users when they publish each product woocommerce for any product categories? And add new metabox under a product page in admin?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment