Guest User

bbPress Post Ratings Hook

a guest
Nov 5th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. // Register Hook
  2. add_filter( 'mycred_setup_hooks', 'register_rate_hook' );
  3. function register_rate_hook( $installed )
  4. {
  5. $installed['complete_profile'] = array(
  6. 'title' => __( '%plural% for Rating a forum post'),
  7. 'description' => __( 'This hook award points to users who rate posts', 'CommentPress Modern Theme' ),
  8. 'callback' => array( 'my_rate_class' )
  9. );
  10. return $installed;
  11. }
  12.  
  13. // myCRED Custom Hook Class
  14. class my_rate_class extends myCRED_Hook {
  15.  
  16. /**
  17. * Construct
  18. */
  19. function __construct( $hook_prefs ) {
  20. parent::__construct( array(
  21. 'id' => 'rate_post',
  22. 'defaults' => array(
  23. 'creds' => 1,
  24. 'log' => '%plural% for rating a post'
  25. )
  26. ), $hook_prefs );
  27. }
  28.  
  29. /**
  30. * Hook into WordPress
  31. */
  32. public function run() {
  33. add_action( 'bbpress_post_rating_UpdateRating', array( $this, 'rate_post' ) );
  34. add_action( 'bp_include', array( $this, 'rate_post' ) );
  35. }
  36.  
  37. /**
  38. * Check if the user qualifies for points
  39. */
  40. public function rate_post( $user_id ) {
  41. // Check if user is excluded (required)
  42. if ( $this->core->exclude_user( $user_id ) ) return;
  43.  
  44. // Execute
  45. $this->core->add_creds(
  46. 'rating_post',
  47. $user_id,
  48. $this->prefs['creds'],
  49. $this->prefs['log']
  50. );
  51. }
  52.  
  53. /**
  54. * Add Settings
  55. */
  56. public function preferences() {
  57. // Our settings are available under $this->prefs
  58. $prefs = $this->prefs; ?>
  59.  
  60. <!-- First we set the amount -->
  61. <label class="subheader"><?php echo $this->core->plural(); ?></label>
  62. <ol>
  63. <li>
  64. <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>
  65. </li>
  66. </ol>
  67. <!-- Then the log template -->
  68. <label class="subheader"><?php _e( 'Log template', 'mycred' ); ?></label>
  69. <ol>
  70. <li>
  71. <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>
  72. </li>
  73. </ol>
  74. <?php
  75. }
  76.  
  77. /**
  78. * Sanitize Preferences
  79. */
  80. public function sanitise_preferences( $data ) {
  81. $new_data = $data;
  82.  
  83. // Apply defaults if any field is left empty
  84. $new_data['creds'] = ( !empty( $data['creds'] ) ) ? $data['creds'] : $this->defaults['creds'];
  85. $new_data['log'] = ( !empty( $data['log'] ) ) ? sanitize_text_field( $data['log'] ) : $this->defaults['log'];
  86.  
  87. return $new_data;
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment