Advertisement
designbymerovingi

myCRED: BuddyBoss Theme Hook

Jan 1st, 2014
1,356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.54 KB | None | 0 0
  1. /**
  2.  * BuddyBoss Theme Hook
  3.  * Allows you to award myCRED points to users for Wall updates, Wall likes and Photo Uploads.
  4.  * Paste me into your BuddyBoss child theme functions.php file.
  5.  * @see http://mycred.me/about/supported-plugins/buddyboss-theme/
  6.  * @version 1.0
  7.  */
  8. if ( class_exists( 'myCRED_Hook' ) ) :
  9.  
  10.     /**
  11.      * Register myCRED Hook
  12.      */
  13.     add_filter( 'mycred_setup_hooks', 'mycred_register_buddyboss_hook' );
  14.     function mycred_register_buddyboss_hook( $installed ) {
  15.         $installed['buddyboss'] = array(
  16.             'title'       => __( 'BuddyBoss Theme Actions', 'mycred' ),
  17.             'description' => __( 'Award %_plural% for BuddyBoss theme actions.', 'mycred' ),
  18.             'callback'    => array( 'myCRED_Hook_BuddyBoss' )
  19.         );
  20.         return $installed;
  21.     }
  22.  
  23.     /**
  24.      * myCRED Hook
  25.      */
  26.     if ( ! class_exists( 'myCRED_Hook_BuddyBoss' ) ) {
  27.         class myCRED_Hook_BuddyBoss extends myCRED_Hook {
  28.        
  29.             /**
  30.              * Construct
  31.              */
  32.             function __construct( $hook_prefs ) {
  33.                 parent::__construct( array(
  34.                     'id'       => 'buddyboss',
  35.                     'defaults' => array(
  36.                         'image_upload' => array(
  37.                             'creds'   => 0,
  38.                             'log'     => '%plural% for uploading an image.'
  39.                         ),
  40.                         'like_wall'    => array(
  41.                             'creds'   => 0,
  42.                             'log'     => '%plural% for wall like',
  43.                             'limit'   => 1
  44.                         )
  45.                     )
  46.                 ), $hook_prefs );
  47.             }
  48.  
  49.             /**
  50.              * Run
  51.              * @since 1.0
  52.              * @version 1.0
  53.              */
  54.             public function run() {
  55.                 if ( $this->prefs['image_upload']['creds'] != 0 && get_option( 'buddyboss_pics_on', 0 ) )
  56.                     add_action( 'buddyboss_add_attachment', array( $this, 'add_attachment' ) );
  57.                
  58.                 if ( $this->prefs['like_wall']['creds'] != 0 && get_option( 'buddyboss_wall_on', 0 ) )
  59.                     add_action( 'bp_activity_add_user_favorite', array( $this, 'activity_fav' ), 10, 2 );
  60.             }
  61.  
  62.             /**
  63.              * Image Upload Hook
  64.              * @since 1.0
  65.              * @version 1.0
  66.              */
  67.             public function add_attachment( $attachment_id ) {
  68.                 if ( ! is_user_logged_in() ) return;
  69.                
  70.                 $user_id = get_current_user_id();
  71.  
  72.                 // Make sure user is not excluded
  73.                 if ( $this->core->exclude_user( $user_id ) === true ) return;
  74.  
  75.                 // Make sure this is unique
  76.                 if ( $this->has_entry( 'photo_upload', $attachment_id, $user_id ) ) return;
  77.  
  78.                 // Execute
  79.                 $this->core->add_creds(
  80.                     'photo_upload',
  81.                     $user_id,
  82.                     $this->prefs['image_upload']['creds'],
  83.                     $this->prefs['image_upload']['log'],
  84.                     $attachment_id,
  85.                     array( 'ref_type' => 'post' )
  86.                 );
  87.             }
  88.  
  89.             /**
  90.              * Activity "Like"
  91.              * @since 1.0
  92.              * @version 1.0
  93.              */
  94.             public function activity_fav( $activity_id, $user_id ) {
  95.                 // Make sure user is not excluded
  96.                 if ( $this->core->exclude_user( $user_id ) === true ) return;
  97.                
  98.                 // No likes of our own
  99.                 $activity_obj = new BP_Activity_Activity( $activity_id );
  100.                 if ( $activity_obj->user_id == $user_id ) return;
  101.                
  102.                 // Make sure we have not reached limit
  103.                 if ( $this->reached_limit( $user_id ) ) return;
  104.                
  105.                 // Make sure this is unique
  106.                 if ( $this->has_entry( 'wall_like', $activity_id, $user_id ) ) return;
  107.                
  108.                 // Execute
  109.                 $this->core->add_creds(
  110.                     'wall_like',
  111.                     $user_id,
  112.                     $this->prefs['like_wall']['creds'],
  113.                     $this->prefs['like_wall']['log'],
  114.                     $activity_id
  115.                 );
  116.                
  117.                 $this->increment_limit( $user_id );
  118.             }
  119.  
  120.             /**
  121.              * Reached Limit Check
  122.              * @since 1.0
  123.              * @version 1.0
  124.              */
  125.             public function reached_limit( $user_id = NULL ) {
  126.                 if ( $user_id === NULL || empty( $this->prefs['like_wall']['limit'] ) || $this->prefs['like_wall']['limit'] == 0 ) return true;
  127.                
  128.                 $today = date_i18n( 'Y-m-d' );
  129.                 $limit = get_user_meta( $user_id, 'activity_fav_limit', true );
  130.                
  131.                 if ( empty( $limit ) || ! isset( $limit[ $today ] ) ) {
  132.                     $limit = array( $today => 0 );
  133.                     update_user_meta( $user_id, 'activity_fav_limit', $limit );
  134.                 }
  135.                
  136.                 if ( $limit[ $today ] < $this->prefs['like_wall']['limit'] )
  137.                     return false;
  138.                
  139.                 return true;
  140.             }
  141.  
  142.             /**
  143.              * Incrememnt Limit
  144.              * @since 1.0
  145.              * @version 1.0
  146.              */
  147.             public function increment_limit( $user_id = NULL ) {
  148.                 if ( $user_id === NULL ) return true;
  149.                
  150.                 $today = date_i18n( 'Y-m-d' );
  151.                 $limit = get_user_meta( $user_id, 'activity_fav_limit', true );
  152.                
  153.                 if ( empty( $limit ) )
  154.                     $limit = array( $today => 0 );
  155.            
  156.                 $limit[ $today ]++;
  157.  
  158.                 update_user_meta( $user_id, 'activity_fav_limit', $limit );
  159.             }
  160.  
  161.             /**
  162.              * Preference for BuddyBoss Hook
  163.              * @since 1.0
  164.              * @version 1.0
  165.              */
  166.             public function preferences() {
  167.                 $prefs = $this->prefs;
  168.                 $pic_upload = get_option( 'buddyboss_pics_on', 0 );
  169.                 $wall = get_option( 'buddyboss_wall_on', 0 ); ?>
  170.  
  171. <?php if ( $pic_upload ) : ?>
  172. <label class="subheader"><?php _e( 'User Photo Uploading', 'mycred' ); ?></label>
  173. <ol>
  174.     <li>
  175.         <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'image_upload' => 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'image_upload' => 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['image_upload']['creds'] ); ?>" size="8" /></div>
  176.     </li>
  177. </ol>
  178. <label class="subheader"><?php _e( 'Log template', 'mycred' ); ?></label>
  179. <ol>
  180.     <li>
  181.         <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'image_upload' => 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'image_upload' => 'log' ) ); ?>" value="<?php echo $prefs['image_upload']['log']; ?>" class="long" /></div>
  182.         <span class="description"><?php _e( 'Available template tags: General and Post related.', 'mycred' ); ?></span>
  183.     </li>
  184. </ol>
  185. <?php else : ?>
  186.  
  187. <label class="subheader"><?php _e( 'Image Upload', 'mycred' ); ?></label>
  188. <ol>
  189.     <li>
  190.         <p><?php _e( 'User Photo Uploading is not enabled.', 'mycred' ); ?></p>
  191.     </li>
  192. </ol>
  193. <?php endif; ?>
  194.  
  195. <?php if ( $wall ) : ?>
  196.  
  197. <label class="subheader"><?php _e( 'Wall Update', 'mycred' ); ?></label>
  198. <ol>
  199.     <li><?php echo $this->core->template_tags_general( __( 'To award points for wall updates, use the BuddyPress: Members "%plural% for Profile Updates" hook.', 'mycred' ) ); ?></li>
  200. </ol>
  201. <label class="subheader"><?php _e( 'Wall Like', 'mycred' ); ?></label>
  202. <ol>
  203.     <li>
  204.         <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'like_wall' => 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'like_wall' => 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['like_wall']['creds'] ); ?>" size="8" /></div>
  205.     </li>
  206. </ol>
  207. <label class="subheader"><?php _e( 'Log template', 'mycred' ); ?></label>
  208. <ol>
  209.     <li>
  210.         <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'like_wall' => 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'like_wall' => 'log' ) ); ?>" value="<?php echo $prefs['like_wall']['log']; ?>" class="long" /></div>
  211.         <span class="description"><?php _e( 'Available template tags: General and Post related.', 'mycred' ); ?></span>
  212.     </li>
  213.     <li class="empty">&nbsp;</li>
  214.     <li>
  215.         <label><?php _e( 'Daily Limit', 'mycred' ); ?></label>
  216.         <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'like_wall' => 'limit' ) ); ?>" id="<?php echo $this->field_id( array( 'like_wall' => 'limit' ) ); ?>" value="<?php echo $prefs['like_wall']['limit']; ?>" class="short" /> <?php _e( '/ day', 'mycred' ); ?></div>
  217.         <span class="description"><?php _e( 'Optional daily limit for Wall Likes. Use zero for unlimited. Note that authors liking their own updates are not awarded!', 'mycred' ); ?></span>
  218.     </li>
  219. </ol>
  220. <?php endif; ?>
  221.  
  222. <?php           unset( $this );
  223.             }
  224.         }
  225.     }
  226.  
  227. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement