Last active
July 20, 2019 04:20
-
-
Save gabrielmerovingi/5536bdf423ca6c5c79e3 to your computer and use it in GitHub Desktop.
Custom myCRED hook example where points are awarded / deducted for newly published posts, based solely on amounts set via a custom taxonomies description field.
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
/** | |
* Custom Hook: Points based on taxonomy | |
* Register the hook so myCRED can load it. | |
* @since 1.0 | |
* @version 1.1 | |
*/ | |
add_action( 'mycred_setup_hooks', 'mycredpro_register_custom_taxonomy_hook' ); | |
function mycredpro_register_custom_taxonomy_hook( $installed ) { | |
$installed['publishing_ct_content'] = array( | |
'title' => __( '%plural% for Publishing Custom Content', 'mycredcu' ), | |
'description' => __( 'Award %_plural% to users for a selected post type where the amount of points is solely based on the custom taxonomies associated with the post.', 'mycredcu' ), | |
'callback' => array( 'myCRED_Hook_Custom_Taxonomy' ) | |
); | |
return $installed; | |
} | |
/** | |
* Custom Hook: Load custom hook | |
* Since 1.6, this would be the proper way to add in the hook class from a theme file | |
* or from a plugin file. | |
* @since 1.0 | |
* @version 1.0 | |
*/ | |
add_action( 'mycred_load_hooks', 'mycredpro_load_custom_taxonomy_hook', 10 ); | |
function mycredpro_load_custom_taxonomy_hook() { | |
class myCRED_Hook_Custom_Taxonomy extends myCRED_Hook { | |
/** | |
* Construct | |
*/ | |
function __construct( $hook_prefs, $type = 'mycred_default' ) { | |
parent::__construct( array( | |
'id' => 'publishing_ct_content', | |
'defaults' => array( | |
'post_type' => 'post', | |
'taxonomy' => 'category', | |
'log' => '%plural% for publishing content' | |
) | |
), $hook_prefs, $type ); | |
} | |
/** | |
* Run | |
* @since 1.0 | |
* @version 1.0 | |
*/ | |
public function run() { | |
add_action( 'transition_post_status', array( $this, 'publishing_content' ), 10, 3 ); | |
} | |
/** | |
* Publish Content Hook | |
* @since 1.0 | |
* @version 1.0 | |
*/ | |
public function publishing_content( $new_status, $old_status, $post ) { | |
// Make sure the post being published is our type | |
if ( ! isset( $this->prefs['post_type'] ) || $this->prefs['post_type'] != $post->post_type ) return; | |
// 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' ) ); | |
if ( in_array( $old_status, $status ) && in_array( $new_status, $publish_status ) ) { | |
// Get custom taxonomies | |
$terms = wp_get_object_terms( $post->ID, $this->prefs['taxonomy'] ); | |
if ( is_wp_error( $terms ) ) return; | |
$total = 0; | |
if ( ! empty( $terms ) ) { | |
foreach ( $terms as $term ) { | |
// Amount should be held under the terms "description" | |
$amount = sanitize_text_field( $term->description ); | |
// If nothing is set then we continue to the next one | |
if ( $amount == '' ) continue; | |
// Add to total | |
$total = $this->core->number( $total + $amount ); | |
} | |
} | |
// If total is higher then zero award | |
if ( $total > 0 ) { | |
// Prep | |
$data = array( 'ref_type' => 'post' ); | |
// Make sure this is unique | |
if ( $this->core->has_entry( 'publishing_content', $post->ID, $post->post_author, $data, $this->mycred_type ) ) return; | |
// Add Creds | |
$this->core->add_creds( | |
'publishing_content', | |
$post->post_author, | |
$total, | |
$this->prefs['log'], | |
$post->ID, | |
$data, | |
$this->mycred_type | |
); | |
} | |
} | |
} | |
/** | |
* Preference for Publish Content Hook | |
* @since 1.0 | |
* @version 1.0 | |
*/ | |
public function preferences() { | |
$prefs = $this->prefs; | |
?> | |
<label class="subheader"><?php _e( 'Post Type', 'mycredcu' ); ?></label> | |
<ol> | |
<li> | |
<div class="h2"><input type="text" name="<?php echo $this->field_name( 'post_type' ); ?>" id="<?php echo $this->field_id( 'post_type' ); ?>" value="<?php echo esc_attr( $prefs['post_type'] ); ?>" class="long" /></div> | |
</li> | |
</ol> | |
<label class="subheader"><?php _e( 'Taxonomy', 'mycredcu' ); ?></label> | |
<ol> | |
<li> | |
<div class="h2"><input type="text" name="<?php echo $this->field_name( 'taxonomy' ); ?>" id="<?php echo $this->field_id( 'taxonomy' ); ?>" value="<?php echo esc_attr( $prefs['taxonomy'] ); ?>" class="long" /></div> | |
</li> | |
</ol> | |
<label class="subheader"><?php _e( 'Amount', 'mycredcu' ); ?></label> | |
<ol> | |
<li> | |
<p class="description"><?php echo $this->core->template_tags_general( __( 'Use your selected custom taxonomie\'s "Description" field to set the amount of %_plural% you want to award. Leave empty for no %_plural%.', 'mycredcu' ) ); ?></p> | |
</li> | |
</ol> | |
<label class="subheader"><?php _e( 'Log template', 'mycredcu' ); ?></label> | |
<ol> | |
<li> | |
<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->available_template_tags( array( 'general', 'post' ) ); ?></span> | |
</li> | |
</ol> | |
<?php | |
} | |
} | |
} |
Copy paste this script to functions.php , to use this Hook . Points - Hook - Publish content.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi. This looks like a feature I want to use. How does it work? Which is to say, how do I add it?