Points for bbPress topic author when topic is favorited
-
Hello Gabriel,
thanks for your wonderful plugin – it’s really fun!
I tried to create a custom hook for Favorites in bbPress 2.0 forums: if somebody adds the topic to his favorites, the topic author should get points.
I followed your tutorial for custom hooks, but didn’t get it working. The hook settings show up in wp admin, but when somebody favorites a topic, no points are added.
What do you think: would you be interested to add this functionality to the core of your plugin?
Otherwise – would you mind taking a look at my code if I did something wrong?add_filter( 'mycred_setup_hooks', 'register_my_bbp_fav_hook' ); function register_my_bbp_fav_hook( $installed ) { $installed['bbp_fav_topic'] = array( 'title' => __( 'bbPress Favorites', 'mytheme' ), 'description' => __( 'Give points to topic author when topic is favorited.', 'mytheme' ), 'callback' => array( 'my_bbp_fav_hook' ) ); return $installed; } if ( !class_exists( 'my_bbp_fav_hook' ) ) { class my_bbp_fav_hook extends myCRED_Hook { function __construct( $hook_prefs ) { parent::__construct( array( 'id' => 'bbp_fav_topic', 'defaults' => array( 'fav_topic' => array( 'creds' => 1, 'log' => '%plural% for someone favorited your forum topic' ) ) ), $hook_prefs ); } public function run() { add_action( 'bbp_add_user_favorite', array( $this, 'fav_topic' ) ); } /** * Check if the user qualifies for points */ public function fav_topic( $user_id, $topic_id ) { // $user_id is loggedin_user, not author // get topic author $topic_author = get_post_field( 'post_author', $topic_id ); // Check if user is excluded (required) if ( $this->core->exclude_user( $topic_author ) ) return; // Make sure this is a unique event if ( $this->has_entry( 'topic_favorited', $topic_id, $topic_author ) ) return; // Execute $this->core->add_creds( 'topic_favorited', $topic_author, $this->prefs['fav_topic']['creds'], $this->prefs['fav_topic']['log'], $topic_id ); // Clean up //unset( $this ); } /** * Add Settings */ public function preferences() { // Our settings are available under $this->prefs $prefs = $this->prefs; ?> <!-- First we set the amount --> <label class="subheader"><?php echo $this->core->plural(); ?></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> </ol> <!-- Then the log template --> <label class="subheader"><?php _e( 'Log template', 'mycred' ); ?></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 $prefs['log']; ?>" class="long" /></div> </li> </ol> <?php } /** * Sanitize Preferences */ public function sanitise_preferences( $data ) { $new_data = $data; // Apply defaults if any field is left empty $new_data['creds'] = ( !empty( $data['creds'] ) ) ? $data['creds'] : $this->defaults['creds']; $new_data['log'] = ( !empty( $data['log'] ) ) ? sanitize_text_field( $data['log'] ) : $this->defaults['log']; return $new_data; } } }
- The topic ‘Points for bbPress topic author when topic is favorited’ is closed to new replies.