Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to tell if a person has uploaded a avatar only once?

  • Avatar of Tosh
    Tosh
    Participant

    @xberserker

    Weird question I know. Here is what I’m trying to do.

    If a user has never uploaded a avatar before, run some code.
    If they have uploaded a avatar before run this code.

    Anyway to do this?

Viewing 23 replies - 1 through 23 (of 23 total)
  • Avatar of Tosh
    Tosh
    Participant

    @xberserker

    What about adding a table to a database that says this user has uploaded at least 1 avatar? And based on if that’s true.

    So something like this?

    If avatar is uploaded

    run this and tag them in the database that they uploaded a avatar

    else

    run this

    Avatar of r-a-y
    r-a-y
    Moderator

    @r-a-y

    There’s currently no way that BP can tell if you’ve uploaded an avatar for the first time.

    Actually, there is one way… BP never deletes any avatars! So you could potentially check the avatars folder and see if a user_id folder exists. If it doesn’t exist the user has never uploaded an avatar before.

    Avatar of Tosh
    Tosh
    Participant

    @xberserker

    Interesting idea, how would I got about checking if a user_id folder is there?

    The avatars folder for each person user id is here:
    /wp-content/uploads/avatars/678/

    found this code online.. trying to think how to modify it to find the “wp-content” folder to start out on.

    WP_CONTENT_DIR
    http://codex.wordpress.org/Determining_Plugin_and_Content_Directories

    $dirname = $_POST["DirectoryName"];
    $filename = “/folder/{$dirname}/”;

    if (file_exists($filename)) {
    echo “The directory {$dirname} exists”;
    } else {
    mkdir(“folder/{$dirname}”, 0777);
    echo “The directory {$dirname} was successfully created.”;
    }

    Avatar of r-a-y
    r-a-y
    Moderator

    @r-a-y

    Check out how BP checks for avatar folders in /bp-core/bp-core-avatars.php.

    If you need some additional help, let us know.

    Avatar of Tosh
    Tosh
    Participant

    @xberserker

    I tried this:

    if ( file_exists( $avatar_folder_dir ) ) {

    // Run code if user hasn’t uploaded a avatar

    } else {

    // Run code if they have a avatar uploaded already

    }

    It thinks they uploaded a avatar either way. I created a new user and uploaded a avatar and it ran the else.

    Avatar of r-a-y
    r-a-y
    Moderator

    @r-a-y

    Do you have $avatar_folder_dir defined?
    You’ll also have to make modifications on the code you copied over from bp-core-avatars.php.

    Avatar of Tosh
    Tosh
    Participant

    @xberserker

    I just copied over this stuff:

    $avatar_folder_url = apply_filters( ‘bp_core_avatar_folder_url’, str_replace( WP_CONTENT_DIR, BP_AVATAR_URL, BP_AVATAR_UPLOAD_PATH ) . ‘/’ . $avatar_dir . ‘/’ . $item_id, $item_id, $object, $avatar_dir );
    $avatar_folder_dir = apply_filters( ‘bp_core_avatar_folder_dir’, BP_AVATAR_UPLOAD_PATH . ‘/’ . $avatar_dir . ‘/’ . $item_id, $item_id, $object, $avatar_dir );

    I’ll tinker later.. feels like it’s getting closer at least lol

    Avatar of r-a-y
    r-a-y
    Moderator

    @r-a-y

    If you check the code that you just posted by itself, you’re missing a bunch of variables like $avatar_dir, $item_id, $object, which is why your code isn’t working.

    Just declare them manually:
    $avatar_dir = ‘avatars';
    $item_id = THE USER ID YOU’RE CHECKING
    $object = ‘user';

    Avatar of Tosh
    Tosh
    Participant

    @xberserker

    $avatar_dir = ‘avatars';
    $item_id = $bp->displayed_user->id;
    $object = ‘user';

    $avatar_folder_url = apply_filters( ‘bp_core_avatar_folder_url’, str_replace( WP_CONTENT_DIR, BP_AVATAR_URL, BP_AVATAR_UPLOAD_PATH ) . ‘/’ . $avatar_dir . ‘/’ . $item_id, $item_id, $object, $avatar_dir );
    $avatar_folder_dir = apply_filters( ‘bp_core_avatar_folder_dir’, BP_AVATAR_UPLOAD_PATH . ‘/’ . $avatar_dir . ‘/’ . $item_id, $item_id, $object, $avatar_dir );

    f ( file_exists( $avatar_folder_dir ) ) {

    // Run code if user hasn’t uploaded a avatar

    } else {

    // Run code if they have a avatar uploaded already

    }

    Correct? Thanks for your help Ray.

    Avatar of r-a-y
    r-a-y
    Moderator

    @r-a-y

    You need a “global $bp;” call if you’re going to be checking for the displayed user’s ID.

    Also you might want to add the filter on $avatar_dir just in case anyone is overriding it!

    $avatar_dir = 'avatars';
    $avatar_dir = apply_filters( 'bp_core_avatar_dir', $avatar_dir, $object );

    Give it a shot!

    Avatar of Tosh
    Tosh
    Participant

    @xberserker

    I had that a little higher in my code, but I added another.

    Edit: Tried that code you just added. Same :/
    I’ll try again next week. Going out of town for the weekend tomorrow :D

    Avatar of Tosh
    Tosh
    Participant

    @xberserker

    @r-a-y

    I tried this as well. Not the desired outcome.

    if ( !$no_grav ) {

    Went back to this, because I’m sure I am getting closer .. When I test this with an account has an existing avatar, it still gives me points.

    // Avatar Anti-Spam
    global $bp;
    $avatar_dir = ‘avatars';
    $avatar_dir = apply_filters( ‘bp_core_avatar_dir’, $avatar_dir, $object );
    $item_id = $bp->displayed_user->id;
    $object = ‘user';

    $avatar_folder_url = apply_filters( ‘bp_core_avatar_folder_url’, str_replace( WP_CONTENT_DIR, BP_AVATAR_URL, BP_AVATAR_UPLOAD_PATH ) . ‘/’ . $avatar_dir . ‘/’ . $item_id, $item_id, $object, $avatar_dir );
    $avatar_folder_dir = apply_filters( ‘bp_core_avatar_folder_dir’, BP_AVATAR_UPLOAD_PATH . ‘/’ . $avatar_dir . ‘/’ . $item_id, $item_id, $object, $avatar_dir );

    if ( file_exists( $avatar_folder_dir ) ) {
    // Avatar has been upload so zero points

    // ZERO Points for Avatar Upload if they already uploaded at least 1
    function my_bp_avatar_add_cppoints() {
    if( function_exists(‘cp_alterPoints’) && is_user_logged_in() ){
    cp_alterPoints(cp_currentUser(), 0 );
    cp_log(‘Avatar Uploaded’, cp_currentUser(), 0, BuddyPress);
    echo ‘zero points';
    }
    }
    add_action(‘xprofile_avatar_uploaded’,’my_bp_avatar_add_cppoints’);

    } else {

    // Add Points Avatar Upload
    function my_bp_avatar_add_cppoints() {
    if( function_exists(‘cp_alterPoints’) && is_user_logged_in() ){
    //cp_alterPoints(cp_currentUser(), get_option(‘bp_avatar_spam_add_cp_bp’) );
    cp_alterPoints(cp_currentUser(), get_option(‘bp_avatar_add_cp_bp’) );
    cp_log(‘Avatar Uploaded’, cp_currentUser(), get_option(‘bp_avatar_add_cp_bp’), BuddyPress);
    echo ‘add points';
    }
    }
    add_action(‘xprofile_avatar_uploaded’,’my_bp_avatar_add_cppoints’);

    }

    Avatar of r-a-y
    r-a-y
    Moderator

    @r-a-y

    That’s a pretty messy conditional – it’s not great to have functions within functions.

    I took a little deeper look at the BP code for avatars and the following should work, though I haven’t tested it:

    http://pastebin.com/KvsL9svw

    Avatar of Tosh
    Tosh
    Participant

    @xberserker

    Cool thank. I tried to upload 2 avatars. Still gives points. It gave an error with your original code. I just changed it slightly to this.

    function my_bp_avatar_add_cppoints() {
    global $bp;

    $path = BP_AVATAR_UPLOAD_PATH . ‘/avatars/’ . $bp->loggedin_user->id;

    $filecount = count(glob(“” . $path . “*.jpg”));

    // this means that the user only has one avatar
    if($filecount <= 2) {
    if( function_exists(‘cp_alterPoints’) && is_user_logged_in() ){
    cp_alterPoints(cp_currentUser(), get_option(‘bp_avatar_add_cp_bp’) );
    cp_log(‘Avatar Uploaded’, cp_currentUser(), get_option(‘bp_avatar_add_cp_bp’), BuddyPress);
    }
    }
    }
    add_action(‘xprofile_avatar_uploaded’, ‘my_bp_avatar_add_cppoints’ );

    Avatar of r-a-y
    r-a-y
    Moderator

    @r-a-y

    Try echoing $filecount see if anything outputs.
    Also echo the $path to see if it correctly outputs the upload path.

    Avatar of Tosh
    Tosh
    Participant

    @xberserker

    Here are the results:

    File Count: 0
    Path: /home/tosh/public_html/3blogtest/wp-content/uploads/avatars/59

    Avatar of r-a-y
    r-a-y
    Moderator

    @r-a-y

    Okay file count isn’t working, but we’re getting somewhere ;)
    I’ll reply back with a better filecount function.

    Avatar of Tosh
    Tosh
    Participant

    @xberserker

    @r-a-y I got it to echo the file count correctly. But when you upload a avatar it will always be 2 files. There is a bpfull.jpg & -bpthumb.jpg file. I assume the avatars/59/ is created when a new user uploads for the first time?

    Avatar of r-a-y
    r-a-y
    Moderator

    @r-a-y

    @xberserker – yup, when you upload an avatar, it outputs two files, that’s why I check if the file count is less than two. If it’s less than two, you’ll know that an avatar has just been uploaded. You’re right that the /avatars/USER_ID/ folder gets created on the first upload.

    Avatar of Tosh
    Tosh
    Participant

    @xberserker

    @r-a-y When the do_action “xprofile_avatar_uploaded” is called the 2 files are already created, where it be a new member or another avatar upload. The hunt continues, lol.

    I had to use something similar, in my case I just need to know if a user has an avatar or not:

    `
    function bp_has_avatar($user_id) {

    global $bp;

    $path = BP_AVATAR_UPLOAD_PATH . ‘/avatars/’ . $user_id . ‘/';

    return count(glob(” . $path . ‘*.jpg’));

    }
    `

    Avatar of studiohyperset
    studiohyperset
    Participant

    @studiohyperset

    I struggled with this too, but here’s the best solution I came up with:

    :: http://studiohyperset.com/checking-for-avatars-and-gravatars-in-buddypress/4892

    Avatar of meg@info
    meg@info
    Participant

    @megainfo

    Hi,
    Check this :)


    /**
    * Check if the current user has an uploaded avatar
    * @return boolean
    */
    function current_user_has_avatar() {
    global $bp;
    if ( bp_core_fetch_avatar( array( 'item_id' => $bp->loggedin_user->id, 'no_grav' => true,'html'=> false ) ) != bp_core_avatar_default() )
    return true;
    return false;
    }
Viewing 23 replies - 1 through 23 (of 23 total)

You must be logged in to reply to this topic.