Advertisement
designbymerovingi

Custom Role Based Badge

Feb 16th, 2015
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.49 KB | None | 0 0
  1. /**
  2.  * Register Custom Reference
  3.  * This will allow us to create a badge for Founders
  4.  * @version 1.0
  5.  */
  6. add_filter( 'mycred_all_references', 'mycred_pro_add_custom_ref' );
  7. function mycred_pro_add_custom_ref( $references ) {
  8.  
  9.     $references['founder'] = 'Founder';
  10.     return $references;
  11.  
  12. }
  13.  
  14. /**
  15.  * Assign Custom Badge
  16.  * Intercepting the Assign Badge AJAX call, we check
  17.  * if this is our custom badge, and if it is, assign it to
  18.  * all subscribers.
  19.  * @version 1.1
  20.  */
  21. add_action( 'wp_ajax_mycred-assign-badge', 'mycred_pro_assign_custom_badge', 1 );
  22. function mycred_pro_assign_custom_badge() {
  23.  
  24.     check_ajax_referer( 'mycred-assign-badge', 'token' );
  25.  
  26.     $badge_id = absint( $_POST['badge_id'] );
  27.     $requirements = mycred_get_badge_requirements( $badge_id );
  28.     if ( ! empty( $requirements ) ) {
  29.         $count = 0;
  30.         foreach ( $requirements as $level => $needs ) {
  31.             if ( isset( $needs['reference'] ) && $needs['reference'] == 'founder' ) {
  32.  
  33.                 // Get all subscribers
  34.                 $users = get_users( array(
  35.                     'role'   => 'subscriber',
  36.                     'fields' => array( 'ID' )
  37.                 ) );
  38.  
  39.                 // If we have users loop through and award this badge to the users
  40.                 if ( ! empty( $users ) ) {
  41.                     foreach ( $users as $user ) {
  42.                         mycred_update_user_meta( $user->ID, 'mycred_badge' . $badge_id, '', 1 );
  43.                         $count ++;
  44.                     }
  45.                 }
  46.  
  47.                 // No need to loop
  48.                 break;
  49.  
  50.             }
  51.         }
  52.  
  53.         if ( $count > 0 )
  54.             wp_send_json_success( sprintf( '%d Users earned the founder badge.', count( $count ) ) );
  55.  
  56.     }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement