Advertisement
designbymerovingi

Points for bbPress topic author when topic is favorited

Jun 19th, 2013
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.07 KB | None | 0 0
  1. add_filter( 'mycred_setup_hooks', 'register_my_bbp_fav_hook' );
  2. function register_my_bbp_fav_hook( $installed )
  3. {
  4.     $installed['bbp_fav_topic'] = array(
  5.         'title'       => __( 'bbPress Favorites', 'mytheme' ),
  6.         'description' => __( 'Give points to topic author when topic is favorited.', 'mytheme' ),
  7.         'callback'    => array( 'my_bbp_fav_hook' )
  8.     );
  9.     return $installed;
  10. }
  11.  
  12. if ( !class_exists( 'my_bbp_fav_hook' ) ) {
  13.     class my_bbp_fav_hook extends myCRED_Hook {
  14.  
  15.         function __construct( $hook_prefs ) {
  16.             parent::__construct( array(
  17.                 'id'       => 'bbp_fav_topic',
  18.                 'defaults' => array(
  19.                     'fav_topic' => array(
  20.                         'creds'   => 1,
  21.                         'log'     => '%plural% for someone favorited your forum topic'
  22.                     )
  23.                 )
  24.             ), $hook_prefs );
  25.         }
  26.  
  27.         public function run() {
  28.             add_action( 'bbp_add_user_favorite',  array( $this, 'fav_topic' ), 10, 2 );
  29.         }
  30.  
  31.         /**
  32.          * Check if the user qualifies for points
  33.          */
  34.         public function fav_topic( $user_id, $topic_id ) {
  35.             // $user_id is loggedin_user, not author
  36.  
  37.             // get topic author
  38.             $topic_author = get_post_field( 'post_author', $topic_id );
  39.  
  40.             // Check if user is excluded (required)
  41.             if ( $this->core->exclude_user( $topic_author ) || $topic_author == $user_id  ) return;
  42.  
  43.             // Make sure this is a unique event
  44.             if ( $this->has_entry( 'topic_favorited', $topic_id, $topic_author ) ) return;
  45.  
  46.             // Execute
  47.             $this->core->add_creds(
  48.                 'topic_favorited',
  49.                 $topic_author,
  50.                 $this->prefs['fav_topic']['creds'],
  51.                 $this->prefs['fav_topic']['log'],
  52.                 $topic_id
  53.             );
  54.  
  55.             // Clean up
  56.             //unset( $this );
  57.         }
  58.  
  59.         /**
  60.          * Add Settings
  61.          */
  62.          public function preferences() {
  63.             // Our settings are available under $this->prefs
  64.             $prefs = $this->prefs; ?>
  65.  
  66.     <!-- First we set the amount -->
  67.     <label class="subheader"><?php echo $this->core->plural(); ?></label>
  68.     <ol>
  69.         <li>
  70.             <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>
  71.         </li>
  72.     </ol>
  73.     <!-- Then the log template -->
  74.     <label class="subheader"><?php _e( 'Log template', 'mycred' ); ?></label>
  75.     <ol>
  76.         <li>
  77.             <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>
  78.         </li>
  79.     </ol>
  80.     <?php
  81.         }
  82.  
  83.         /**
  84.          * Sanitize Preferences
  85.          */
  86.         public function sanitise_preferences( $data ) {
  87.             $new_data = $data;
  88.  
  89.             // Apply defaults if any field is left empty
  90.             $new_data['fav_topic']['creds'] = ( !empty( $data['fav_topic']['creds'] ) ) ? $data['fav_topic']['creds'] : $this->defaults['fav_topic']['creds'];
  91.             $new_data['fav_topic']['log'] = ( !empty( $data['fav_topic']['log'] ) ) ? sanitize_text_field( $data['fav_topic']['log'] ) : $this->defaults['fav_topic']['log'];
  92.  
  93.             return $new_data;
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement