Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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' ), 10, 2 );
- }
- /**
- * 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 ) || $topic_author == $user_id ) 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( array( 'fav_topic' => 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'fav_topic' => 'creds' ) ); ?>" value="<?php echo $this->core->format_number( $prefs['fav_topic']['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( array( 'fav_topic' => 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'fav_topic' => 'log' ) ); ?>" value="<?php echo $prefs['fav_topic']['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['fav_topic']['creds'] = ( !empty( $data['fav_topic']['creds'] ) ) ? $data['fav_topic']['creds'] : $this->defaults['fav_topic']['creds'];
- $new_data['fav_topic']['log'] = ( !empty( $data['fav_topic']['log'] ) ) ? sanitize_text_field( $data['fav_topic']['log'] ) : $this->defaults['fav_topic']['log'];
- return $new_data;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement