Advertisement
Guest User

Untitled

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