BuddyDev

Search

Replies

  • Participant
    Level: Initiated
    Posts: 5
    matthew flamm on #413

    The only extensions I have active are akismet, buddypress, and the code above.

    Could there be something else interfering?

  • Participant
    Level: Initiated
    Posts: 5
    matthew flamm on #312

    That’s not a problem. I’m already impressed with how quickly you’re able and willing to help.

  • Participant
    Level: Initiated
    Posts: 5
    matthew flamm on #305

    Thank you for your amazingly quick reply!

    Actually I’m hiding with the code below setup as a plugin. using the code below I get the new visibility option expected, and the field is hidden from the members I expect it to be, but when a user tries to edit the field on their own profile they get the error mentioned and nothing is saved. It only occurs when they try and edit the xprofile group with a field set to one of the new visibilities.

    while trying to figure this out I have tested:
    field > set to “groups” visibility > required > error!
    field > set to “membership2” visibility > required > error!
    field > set to “groups” visibility > not required > error!
    field > set to “membership2” visibility > not required > error!
    field > set to any core visibility > required > works
    field > set to works visibility > required > works

    I also know it is my fields because deleting them or changing them to a core visibility makes the error go away.

    I have tried it with a text box (“test” being entered as the value when editing), radio button, and dropdown.

    I have removed my addition for the membership2 visibility, still the same problem with the group visibility. I have also removed group and confirmed the problem still for membership2

    Here is the code I’m using.

    
    <?php
    
    add_filter( 'bp_xprofile_get_visibility_levels', 'buddydev_customize_profile_visibility' );
    function buddydev_customize_profile_visibility( $allowed_visibilities ) {
    	
    	//add custom visibility 
    	if( bp_is_active( 'groups' ) ) {
    	
    		$allowed_visibilities['groups'] = array(
    			'id'	=> 'groups',
    			'label'	=> _x( 'My Group Members', 'Visibility level setting', 'bp-extended-profile-visibility' )
    		);
    		
    	}
    
    	
    	//this has been tested with this section for membership 2 here and removed
    	
    	include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); //needed to use "is_plugin_active()"
    	
    	//add visibility for fields based on Membership Level, using Membership 2 Pro from WPMUDev
    	if ( is_plugin_active( 'membership/membership2.php' ) ) { //correctly checks for plugin to be active, but only if plugin.php is included above.  otherwise frontend has error of undefined function for is_plugin_active()
    		$allowed_visibilities['membership2'] = array(
    			'id'	=> 'membership2',
    			'label'	=> _x( 'Logged-in Pro Members', 'Visibility level setting', 'bp-extended-profile-visibility' )
    		);
    	}	
    	
    	return $allowed_visibilities;
    }
    
    //now do some magic
    add_filter( 'bp_xprofile_get_hidden_field_types_for_user', 'buddydev_get_hidden_visibility_types_for_user', 10, 3 );
    		
    function buddydev_get_hidden_visibility_types_for_user( $hidden_levels, $displayed_user_id, $current_user_id ) {
    	//if it is not my data and super admin is not viewing and there are no common groups, then hide
    	if( ( $displayed_user_id != $current_user_id ) &&  ! buddydev_user_has_common_group( $displayed_user_id, $current_user_id ) && ! is_super_admin() ) {
    		
    		$hidden_levels[] = 'groups'; //profile field with this privacy level will be hidden for the user
    		
    	}
    	
    	if( ( $displayed_user_id != $current_user_id ) &&  !current_user_on_level(239)  && ! is_super_admin() ) { //&&  !current_user_on_level(239)  this is used to test membership level on the membership plugin. confirmed to work in a different function.  
    		
    		//this gets set or not set correctly based on membership level, so conditional is working.
    		$hidden_levels[] = 'membership2'; //profile field with this privacy level will be hidden for the user
    		
    	}
    	
    	return $hidden_levels;
    	
    }
    
    /**
     * Check if there are any common groups between these two users
     * 
     * @param int $user_id
     * @param int $other_id
     * @return boolean true if ye, else false
     */
    function buddydev_user_has_common_group( $user_id, $other_id ) {
    	
    	$common_groups = buddydev_get_common_groups( $user_id, $other_id );
    	
    	if( ! empty( $common_groups ) )
    		return true;
    	
    	return false;
    	
    }
    /**
     * Get all the common groups between these two users
     * 
     * @global type $wpdb
     * @param int $user_id
     * @param int $other_id
     * @return mixed array of group ids 
     */
    function buddydev_get_common_groups( $user_id, $other_id ) {
    	
    	global $wpdb;
    	
    	//members table
    	$table = buddypress()->groups->table_name_members;
    	
    	$query_user_groups = $wpdb->prepare( "SELECT group_id FROM {$table} WHERE user_id = %d AND is_confirmed = %d ", $user_id, 1 );
    	$query_other_user_groups = $wpdb->prepare( "SELECT group_id FROM {$table} WHERE user_id = %d AND is_confirmed = %d ", $other_id, 1 );
    	
    //should we put a LIMIT clause too, I am not putting as someone may find this function useful for other purposes
    	$commpon_groups = $wpdb->get_col( "{$query_user_groups} AND group_id IN ({$query_other_user_groups})" );
    	
    	
    	return $commpon_groups;
    }