Group Mods

  • Profile picture of @mercime
  • Profile picture of r-a-y
  • Profile picture of Hugo

Support: Creating & Extending

Existing and new plugins/components and themes.

How to tell if a person has uploaded a avatar only once? (22 posts)

Started 2 years ago by: Tosh

  • Profile picture of Tosh Tosh said 2 years ago:

    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?

  • Profile picture of Tosh Tosh said 2 years ago:

    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

  • Profile picture of r-a-y r-a-y said 2 years ago:

    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.

  • Profile picture of Tosh Tosh said 2 years ago:

    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.”;
    }

  • Profile picture of r-a-y r-a-y said 2 years ago:

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

    If you need some additional help, let us know.

  • Profile picture of Tosh Tosh said 2 years ago:

    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.

  • Profile picture of r-a-y r-a-y said 2 years ago:

    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.

  • Profile picture of Tosh Tosh said 2 years ago:

    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

  • Profile picture of r-a-y r-a-y said 2 years ago:

    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’;

  • Profile picture of Tosh Tosh said 2 years ago:

    $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.

  • Profile picture of r-a-y r-a-y said 2 years ago:

    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!

  • Profile picture of Tosh Tosh said 2 years ago:

    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

  • Profile picture of Tosh Tosh said 2 years ago:

    @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’);

    }

  • Profile picture of r-a-y r-a-y said 2 years ago:

    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

  • Profile picture of Tosh Tosh said 2 years ago:

    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' );