Advertisement
cflorence

Show Opposite Gender Base on BUddypress XProfile

Sep 26th, 2022
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.32 KB | Software | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Filters Users based on Gender. Make sure only users of opposite genders are visible.
  5.  */
  6. class BuddyDev_Gender_Based_Member_Filter {
  7.  
  8.     /**
  9.      * Boot
  10.      *
  11.      * @return BuddyDev_Gender_Based_Member_Filter
  12.      */
  13.     public static function boot() {
  14.         $self = new self();
  15.         $self->setup();
  16.         return $self;
  17.     }
  18.  
  19.     /**
  20.      * Setup hooks.
  21.      */
  22.     private function setup() {
  23.         add_filter( 'bp_after_has_members_parse_args', array( $this, 'filter_args' ) );
  24.  
  25.         add_action( 'bp_template_redirect', array( $this, 'check_access' ), 10, 2 );
  26.     }
  27.    
  28.     // Modifies members loop args to filter out users of same gender.
  29.     public function filter_args( $args ) {
  30.         // do not filter if user is not logged in, or is super admin or we are inside dashboard.
  31.         if ( ! is_user_logged_in() || is_super_admin() || ( is_admin() && ! defined( 'DOING_AJAX' ) ) ) {
  32.             return $args;
  33.         }
  34.  
  35.         $opposite_gender = $this->get_opposite_gender( bp_loggedin_user_id() );
  36.         $jlw_gender_field = xprofile_get_field( bp_get_xprofile_gender_type_field_id() );
  37.         if ( $opposite_gender ) {
  38.             $args[$xprofile_query] = $opposite_gender;
  39.         }
  40.         return $args;
  41.  
  42.     }
  43.  
  44.     // Restricts access to user profile.
  45.     // Only allows profile owner, user of opposite gender or site admins to view.
  46.     public function check_access() {
  47.         if ( ! is_user_logged_in() || ! bp_is_user() || bp_is_my_profile() || is_super_admin() ) {
  48.             return;
  49.         }
  50.  
  51.         $user_gender = $this->get_user_gender( bp_loggedin_user_id() );
  52.         $displayed_user_gender = $this->get_user_gender( bp_displayed_user_id() );
  53.  
  54.         // do not allow access for same gender.
  55.         if ( $displayed_user_gender === $user_gender ) {
  56.             bp_core_add_message( __( 'Access denied.', 'yym-helper' ) );
  57.             bp_core_redirect( bp_loggedin_user_domain() );
  58.         }
  59.     }
  60.  
  61.     // Get user gender
  62.     private function get_user_gender( $user_id ) {
  63.         return xprofile_get_field_data( bp_get_xprofile_gender_type_field_id(), $user_id );
  64.     }
  65.  
  66.     // Finds the opposite gender
  67.     private function get_opposite_gender( $user_id ) {
  68.  
  69.         $user_gender = $this->get_user_gender( $user_id );
  70.  
  71.         if ( empty( $user_gender ) ) {
  72.             return null;
  73.         }
  74.  
  75.         if ( $user_gender === 'his_Male' ) {
  76.             return 'her_Female';
  77.         } elseif ( $user_gender === 'her_Female' ) {
  78.             return 'his_Male';
  79.         }
  80.  
  81.         return null;
  82.     }
  83.  
  84. }
  85.  
  86. BuddyDev_Gender_Based_Member_Filter::boot();
  87.  
  88. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement