Skip to:
Content
Pages
Categories
Search
Top
Bottom

Forum Replies Created

Viewing 14 replies - 1 through 14 (of 14 total)

  • ouegy
    Participant

    @ouegy

    @rosyteddy The code probably needs hardening to prevent sql injections – so I wouldn’t advise using it on a live site yet. I’m not sure how to do this myself -maybe @henrywright can suggest a way to improve it?


    ouegy
    Participant

    @ouegy

    @rosyteddy Here is the final code which goes in functions.php

    add_action('bp_ajax_querystring','bpdev_exclude_users',20,2);
    
    function bpdev_exclude_users($qs=false,$object=false){
     //list of users to exclude
    
    global $wpdb;
    
    $currentuser_id = get_current_user_id();
    
    $sql= "SELECT friend_user_id  FROM wp_bp_friends WHERE initiator_user_id='".$currentuser_id."' AND is_confirmed = 1";
    $friendsid = $wpdb->get_results($sql);
    $friendsarray = array();
    foreach($friendsid as $oneitem){
        $friendsarray[]=$oneitem->friend_user_id;
    }
    // $friends =implode(", ",$friendsarray);
    // echo $friends;
    
    $ffidarray =array();
     
    foreach ( $friendsid as $row )
    {
    $sql1= "SELECT friend_user_id  FROM wp_bp_friends WHERE initiator_user_id='".$row->friend_user_id."' AND is_confirmed = 1";
    $ffid = $wpdb->get_results($sql1);
    //print_r($ffid);
    foreach($ffid as $oneitem){
        $ffidarray[]=$oneitem->friend_user_id;
    }
    }
    // $fof =implode(", ",$ffidarray);
    // echo $fof;
    
    $sql2= "SELECT user_id FROM wp_bp_xprofile_data";
    $users = $wpdb->get_results($sql2);
    //print_r($users);
    
    $result = array_merge($friendsarray, $ffidarray);
    //echo $result;
    
    $usersarray =array();
    foreach($users as $oneitem){
        $usersarray[]=$oneitem->user_id;
    } 
    
    $resultarray =array();
    foreach($result as $oneitem){
        $resultarray[]=$oneitem->friend_user_id;
    } 
    
    $excluded_user = array_diff($usersarray , $resultarray);
    $excluded_user =implode(", ",$excluded_user); //comma separated ids of users whom you want to exclude
     
     if($object!='members')//hide for members only
     return $qs;
     
     $args=wp_parse_args($qs);
     
     //check if we are searching  or we are listing friends?, do not exclude in this case
     if(!empty($args['user_id'])||!empty($args['search_terms']))
     return $qs;
     
     if(!empty($args['exclude']))
     $args['exclude']=$args['exclude'].','.$excluded_user;
     else
     $args['exclude']=$excluded_user;
     
     $qs=build_query($args);
     
     return $qs;
     
    }

    Seems to be working but feel free to test further!


    ouegy
    Participant

    @ouegy

    Hi @henrywright

    Trying to debug this line by line and I’ve got this far before it spits out an error

    function bpdev_include_users(){
     //list of users to exclude
    
    global $wpdb;
    $frndsof = array();
    $frnds = array();
    $result = array();
    $friendsid = array();
    $excluded_user = array();
    
    $currentuser_id = get_current_user_id();
    echo $currentuser_id;
    
    $sql= "SELECT friend_user_id  FROM wp_bp_friends WHERE initiator_user_id='".$currentuser_id."' AND is_confirmed = 1";
    $friendsid = $wpdb->get_results($sql);
    
    foreach ( $friendsid as $row )
    {
    $sql1= "SELECT friend_user_id  FROM wp_bp_friends WHERE initiator_user_id='".$row->friend_user_id."' AND is_confirmed = 1";
    $ffid = $wpdb->get_results($sql1);
    
    }
    
    $sql2= "SELECT user_id FROM wp_bp_xprofile_data";
    $users = $wpdb->get_results($sql2);
    
    $result = array_merge($friendsid, $ffid);	
    print_r($result);
    $excluded_user = array_diff( $users , $result ); //comma separated ids of users whom you want to exclude
    
    }
    
    bpdev_include_users();

    On the array_diff line I’m getting the error Object of class stdClass could not be converted to string

    Any ideas?


    ouegy
    Participant

    @ouegy

    Here’s my final code and surprise surprise, it’s not working!!

    add_action('bp_ajax_querystring','bpdev_exclude_users',20,2);
    
    function bpdev_exclude_users($qs=false,$object=false){
     //list of users to exclude
    
    global $wpdb;
    $frndsof = array();
    $currentuser_id = get_current_user_id();
    
    $sql= "SELECT friend_user_id  FROM wp_bp_friends WHERE initiator_user_id='".$currentuser_id."' AND is_confirmed = 1";
    $friendsid = $wpdb->get_results($sql);
    if(!empty($friendsid)){
    foreach ( $friendsid as $row ) {
    $frnds[]= $row->friend_user_id;;
    }
    foreach ( $friendsid as $row )
    {
    $sql1= "SELECT friend_user_id  FROM wp_bp_friends WHERE initiator_user_id='".$row->friend_user_id."' AND is_confirmed = 1";
    $ffid = $wpdb->get_results($sql1);
    if(!empty($ffid)){
    foreach ( $ffid as $row ) {
    $frndsof[]= $row->friend_user_id;;
    }
    }
    }
    
    if ( bp_has_members(bp_ajax_querystring( ‘members’ ) ) ) :
    while ( bp_members() ) : bp_the_member();
    /* Get the field value from all members */
    $number_field = xprofile_get_field_data($field_id, bp_get_member_user_id());
    /* Push user id and number value to an array */
    $numberedUsers[$number_field] = bp_get_member_user_id();
    endwhile;
    endif;
    
    $result = array_merge($frnds, $frndsof);	
     
    $excluded_user = array_diff ( array $numberedUsers , array $result ); //comma separated ids of users whom you want to exclude
     
     if($object!='members')//hide for members only
     return $qs;
     
     $args=wp_parse_args($qs);
     
     //check if we are searching  or we are listing friends?, do not exclude in this case
     if(!empty($args['user_id'])||!empty($args['search_terms']))
     return $qs;
     
     if(!empty($args['exclude']))
     $args['exclude']=$args['exclude'].','.$excluded_user;
     else
     $args['exclude']=$excluded_user;
     
     $qs=build_query($args);
     
     return $qs;
     
    }

    ouegy
    Participant

    @ouegy

    Maybe I could do something like this to get an array of member id’s

    if ( bp_has_members(bp_ajax_querystring( ‘members’ ) ) ) :
    while ( bp_members() ) : bp_the_member();
    /* Get the field value from all members */
    $number_field = xprofile_get_field_data($field_id, bp_get_member_user_id());
    /* Push user id and number value to an array */
    $numberedUsers[$number_field] = bp_get_member_user_id();
    endwhile;
    endif; 

    Then combine the arrays of friends and friends of friends

    $result = array_merge($friendsid, $frndsof);

    And finally use array_diff() to pass into $excluded_user

    $excluded_user = array_diff ( array $numberedUsers , array $result )


    @henrywright
    do you think this approach might work?


    ouegy
    Participant

    @ouegy

    Sorry I don’t think I explained myself very well!

    What I’d like is to remove members from appearing in search and the members template unless they are friends or friends of friends.

    I was thinking of using something like this

    add_action('bp_ajax_querystring','bpdev_exclude_users',20,2);
    function bpdev_exclude_users($qs=false,$object=false){
     //list of users to exclude
     
     $excluded_user='1,2,3';//comma separated ids of users whom you want to exclude
     
     if($object!='members')//hide for members only
     return $qs;
     
     $args=wp_parse_args($qs);
     
     //check if we are searching  or we are listing friends?, do not exclude in this case
     if(!empty($args['user_id'])||!empty($args['search_terms']))
     return $qs;
     
     if(!empty($args['exclude']))
     $args['exclude']=$args['exclude'].','.$excluded_user;
     else
     $args['exclude']=$excluded_user;
     
     $qs=build_query($args);
     
     return $qs;
     
    }

    But then I’d need to generate the excluded user id’s programatically – something like this

    global $wpdb;
    $frndsof = array();
    $currentuser_id = get_current_user_id();
    $sql= "SELECT friend_user_id  FROM wp_bp_friends WHERE initiator_user_id='".$currentuser_id."' AND is_confirmed = 1";
    $friendsid = $wpdb->get_results($sql);
    foreach ( $friendsid as $row )
    {
    $sql1= "SELECT friend_user_id  FROM wp_bp_friends WHERE initiator_user_id='".$row->friend_user_id."' AND is_confirmed = 1";
    $ffid = $wpdb->get_results($sql1);
    if(!empty($ffid)){
    foreach ( $ffid as $row ) {
    $frndsof[]= $row->friend_user_id;;
    }
    }
    }

    I think the second code snippet will get the id’s of friends and friends of friends.

    What I’d like to do is include ALL members id’s in $excluded_user from the first snippet APART FROM the id’s from the second snippet.

    I think I’m right with my logic there – just not sure how to combine the 2 snippets of code.

    Any suggestions are appreciated and thanks for your help!


    ouegy
    Participant

    @ouegy

    Thanks Henry that works perfectly!

    The support on here is amazing!


    ouegy
    Participant

    @ouegy

    Thanks – I’m pretty new to hooks

    does this this look right?

    <?php
    
    add_action( 'xprofile_avatar_uploaded', 'update_membership_level' )
    
    function update_membership_level() {
    
    	$u = new WP_User( $user->ID );
            // Remove role
            $u->remove_role( 's2member_level1' );
    
            // Add role
            $u->add_role( 's2member_level2' );
    
    	}
    	
    ?>

    ouegy
    Participant

    @ouegy

    Hi @henrywright

    I’ve taken a slightly different approach as I couldn’t figure out customising the s2member form.

    What now happens is the user registers and is assigned a restricted membership level (level_1) and automatically logged in and redirected to /profile/change-avatar.

    At the point that the user clicks

    <input type="submit" name="avatar-crop-submit" id="avatar-crop-submit" value="<?php esc_attr_e( 'Crop Image', 'buddypress' ); ?>" />

    in change-avatar.php, is there some sort of event hook that could fire on this to change the user’s role eg. make them a full member (level_2)

    It’s a requirement that the user uploads a photo to become a member of the community.

    Thanks again


    ouegy
    Participant

    @ouegy

    Thanks I’ll take a look at that


    ouegy
    Participant

    @ouegy

    Just checking the DB, all meta_values are longtext and I cant change to BLOB for that specific row…

    Is there some way I might be able to use the code behind the form on the /change-avatar/ page and store whatever file is uploaded in the correct table for BuddyPress?


    ouegy
    Participant

    @ouegy

    Hi @henrywright

    It’s currently being saved to wp_usermeta and is associated with the user.

    the meta_key is wp_avatar_upload and the meta_value image.jpg

    Where does buddypress store the avatars?


    ouegy
    Participant

    @ouegy

    Fixed – I needed to place the template at theme/buddypress/members/single/index-component-profile.php


    ouegy
    Participant

    @ouegy

    Thanks henry,

    I looked at that before but must have been half asleep!

    Simply added index-directory.php to themes/mytheme/buddypress/activity/

Viewing 14 replies - 1 through 14 (of 14 total)
Skip to toolbar