Created
August 21, 2014 09:24
-
-
Save gabrielmerovingi/49e70088cbdaed0672c2 to your computer and use it in GitHub Desktop.
Custom hook that allows you to reward your comment authors for getting their comments voted up or down by fellow member using the Comment Popularity plugin. Simply copy the code and paste it into your theme's functions.php file and you can access the new hook on the myCRED > Hooks page. Supports multiple point types and requires myCRED 1.4 or hi…
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
| /** | |
| * Register Custom Hook | |
| * Comment Popularity | |
| * @see https://github.com/humanmade/comment-popularity | |
| */ | |
| add_filter( 'mycred_setup_hooks', 'mycred_pro_register_comment_pop_hook' ); | |
| function mycred_pro_register_comment_pop_hook( $installed ) { | |
| $installed['comment_pop'] = array( | |
| 'title' => __( 'Comment Popularity', 'textdomain' ), | |
| 'description' => __( 'Reward comment authors with %plural% for up or down votes recieved by other members.', 'textdomain' ), | |
| 'callback' => array( 'myCRED_Hook_Comment_Pop' ) | |
| ); | |
| return $installed; | |
| } | |
| /** | |
| * The Custom Hook | |
| * Comment Popularity | |
| */ | |
| if ( !class_exists( 'myCRED_Hook_Comment_Pop' ) && class_exists( 'myCRED_Hook' ) ) : | |
| class myCRED_Hook_Comment_Pop extends myCRED_Hook { | |
| /** | |
| * Construct | |
| */ | |
| function __construct( $hook_prefs, $type = 'mycred_default' ) { | |
| parent::__construct( array( | |
| 'id' => 'comment_pop', | |
| 'defaults' => array( | |
| 'upvote' => array( | |
| 'creds' => 0, | |
| 'log' => '%plural% for comment upvote' | |
| ), | |
| 'downvote' => array( | |
| 'creds' => 0, | |
| 'log' => '%plural% for comment upvote' | |
| ), | |
| 'daily_max' => 0 | |
| ) | |
| ), $hook_prefs, $type ); | |
| } | |
| /** | |
| * Run | |
| * @since 1.0 | |
| * @version 1.0 | |
| */ | |
| public function run() { | |
| add_action( 'hmn_cp_comment_vote', array( $this, 'vote' ), 10, 3 ); | |
| } | |
| public function vote( $user_id, $comment_id, $vote ) { | |
| // First we check that the user voting is not excluded | |
| if ( $this->core->exclude_user( $user_id ) ) return; | |
| // Get the comment object | |
| $comment = get_comment( $comment_id ); | |
| // Next we make sure the comment author is not excluded | |
| if ( $this->core->exclude_user( $comment->user_id ) ) return; | |
| // Check if we use a limit | |
| if ( $this->reached_limit( $user_id ) ) return; | |
| // Execute | |
| $this->core->add_creds( | |
| 'comment_vote', | |
| $comment->user_id, | |
| $this->prefs[ $vote ]['creds'], | |
| $this->prefs[ $vote ]['log'], | |
| $comment_id, | |
| array( 'ref_type' => 'comment' ), | |
| $this->mycred_type | |
| ); | |
| } | |
| public function reached_limit( $user_id ) { | |
| // If max is set to zero, we do not enforce a max | |
| if ( $this->prefs['daily_max'] == 0 ) return false; | |
| global $wpdb; | |
| $stat_of_today = strtotime( 'today midnight' ); | |
| $now = time(); | |
| // Count the number of times the user has received points today for voting | |
| $count = $wpdb->get_var( $wpdb->prepare( " | |
| SELECT COUNT( * ) | |
| FROM {$this->core->log_table} | |
| WHERE ref = 'comment_vote' | |
| AND user_id = %d | |
| AND time BETWEEN ( %d AND %d );", $user_id, $start_of_today, $now ) ); | |
| if ( $count === NULL ) | |
| $count = 0; | |
| // Compare count | |
| if ( $count < $this->prefs['daily_max'] ) | |
| return false; | |
| return true; | |
| } | |
| /** | |
| * Preferences | |
| * @since 1.0 | |
| * @version 1.0 | |
| */ | |
| public function preferences() { | |
| $prefs = $this->prefs; ?> | |
| <label class="subheader"><?php _e( 'Comment Up Vote', 'textdomain' ); ?></label> | |
| <ol> | |
| <li> | |
| <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'upvote' => 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'upvote' => 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['upvote']['creds'] ); ?>" size="8" /></div> | |
| </li> | |
| </ol> | |
| <label class="subheader"><?php _e( 'Log template', 'mycred' ); ?></label> | |
| <ol> | |
| <li> | |
| <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'upvote' => 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'upvote' => 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['upvote']['log'] ); ?>" class="long" /></div> | |
| <span class="description"><?php echo $this->available_template_tags( array( 'general', 'comment' ) ); ?></span> | |
| </li> | |
| </ol> | |
| <label class="subheader"><?php _e( 'Comment Down Vote', 'textdomain' ); ?></label> | |
| <ol> | |
| <li> | |
| <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'downvote' => 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'downvote' => 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['downvote']['creds'] ); ?>" size="8" /></div> | |
| </li> | |
| </ol> | |
| <label class="subheader"><?php _e( 'Log template', 'mycred' ); ?></label> | |
| <ol> | |
| <li> | |
| <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'downvote' => 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'downvote' => 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['downvote']['log'] ); ?>" class="long" /></div> | |
| <span class="description"><?php echo $this->available_template_tags( array( 'general', 'comment' ) ); ?></span> | |
| </li> | |
| </ol> | |
| <label class="subheader"><?php _e( 'Daily Max', 'textdomain' ); ?></label> | |
| <ol> | |
| <li> | |
| <div class="h2"><input type="text" name="<?php echo $this->field_name( 'daily_max' ); ?>" id="<?php echo $this->field_id( 'daily_max' ); ?>" value="<?php echo $prefs['daily_max']; ?>" size="8" /></div> | |
| <span class="description"><?php echo $this->core->template_tags_general( __( 'The maximum number of votes each user can make per day that rewards %plural%. Use zero for unlimited.', 'textdomain' ) ); ?></span> | |
| </li> | |
| </ol> | |
| <?php | |
| } | |
| } | |
| endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!
I am looking for Disqus integration, do you think you can help me?