Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
12/11/2021 06:52:01 PM (5 years ago)
Author:
imath
Message:

Add a new "Recycle" tab to the Avatar UI

Thanks to this tab users can manage their avatars history. They can choose to:

  • reuse a previously uploaded avatar as their current profile photo,
  • permanently delete an avatar from their avatars history.

Props vapvarun, oztaser

See #8581

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-attachments.php

    r13174 r13179  
    410410        // It's an avatar, we need to crop it.
    411411        if ( 'avatar' === $type ) {
    412                 $created = bp_core_avatar_handle_crop( array(
    413                         'object'        => $r['object'],
    414                         'avatar_dir'    => trim( dirname( $attachment_data['subdir'] ), '/' ),
    415                         'item_id'       => (int) $r['item_id'],
    416                         'original_file' => trailingslashit( $attachment_data['subdir'] ) . $image_file_name,
    417                         'crop_w'        => $r['crop_w'],
    418                         'crop_h'        => $r['crop_h'],
    419                         'crop_x'        => $r['crop_x'],
    420                         'crop_y'        => $r['crop_y']
    421                 ) );
     412                $created = bp_core_avatar_handle_crop(
     413                        array(
     414                                'object'        => $r['object'],
     415                                'avatar_dir'    => trim( dirname( $attachment_data['subdir'] ), '/' ),
     416                                'item_id'       => (int) $r['item_id'],
     417                                'original_file' => trailingslashit( $attachment_data['subdir'] ) . $image_file_name,
     418                                'crop_w'        => $r['crop_w'],
     419                                'crop_h'        => $r['crop_h'],
     420                                'crop_x'        => $r['crop_x'],
     421                                'crop_y'        => $r['crop_y']
     422                        )
     423                );
    422424
    423425        // It's a cover image we need to fit it to feature's dimensions.
    424426        } elseif ( 'cover_image' === $type ) {
    425                 $cover_image = bp_attachments_cover_image_generate_file( array(
    426                         'file'            => $image_file_path,
    427                         'component'       => $r['component'],
    428                         'cover_image_dir' => $attachment_data['path']
    429                 ) );
     427                $cover_image = bp_attachments_cover_image_generate_file(
     428                        array(
     429                                'file'            => $image_file_path,
     430                                'component'       => $r['component'],
     431                                'cover_image_dir' => $attachment_data['path']
     432                        )
     433                );
    430434
    431435                $created = ! empty( $cover_image['cover_file'] );
    … …  
    788792                $defaults['multi_selection'] = false;
    789793
    790                 // Does the object already has an avatar set.
     794                // Does the object already has an avatar set?
    791795                $has_avatar = $defaults['multipart_params']['bp_params']['has_avatar'];
    792796
    793                 // What is the object the avatar belongs to.
     797                // What is the object the avatar belongs to?
    794798                $object = $defaults['multipart_params']['bp_params']['object'];
     799
     800                // Get The item id.
     801                $item_id = $defaults['multipart_params']['bp_params']['item_id'];
    795802
    796803                // Init the Avatar nav.
    797804                $avatar_nav = array(
    798                         'upload' => array( 'id' => 'upload', 'caption' => __( 'Upload', 'buddypress' ), 'order' => 0  ),
    799 
    800                         // The delete view will only show if the object has an avatar.
    801                         'delete' => array( 'id' => 'delete', 'caption' => __( 'Delete', 'buddypress' ), 'order' => 100, 'hide' => (int) ! $has_avatar ),
     805                        'upload'  => array(
     806                                'id'      => 'upload',
     807                                'caption' => __( 'Upload', 'buddypress' ),
     808                                'order'   => 0
     809                        ),
     810                        'delete'  => array(
     811                                'id'      => 'delete',
     812                                'caption' => __( 'Delete', 'buddypress' ),
     813                                'order'   => 100,
     814                                'hide'    => (int) ! $has_avatar
     815                        ),
    802816                );
     817
     818                if ( 'user' === $object ) {
     819                        // Look inside history to see if the user previously uploaded avatars.
     820                        $avatars_history = bp_avatar_get_avatars_history( $item_id, $object );
     821
     822                        if ( $avatars_history ) {
     823                                ksort( $avatars_history );
     824                                $settings['history']       = array_values( $avatars_history );
     825                                $settings['historyNonces'] = array(
     826                                        'recylePrevious' => wp_create_nonce( 'bp_avatar_recycle_previous' ),
     827                                        'deletePrevious' => wp_create_nonce( 'bp_avatar_delete_previous' ),
     828                                );
     829
     830                                $avatar_nav['recycle'] = array(
     831                                        'id'      => 'recycle',
     832                                        'caption' => __( 'Recycle', 'buddypress' ),
     833                                        'order'   => 20,
     834                                        'hide'    => (int) empty( $avatars_history ),
     835                                );
     836                        }
     837                }
    803838
    804839                // Create the Camera Nav if the WebCam capture feature is enabled.
    805840                if ( bp_avatar_use_webcam() && 'user' === $object ) {
    806                         $avatar_nav['camera'] = array( 'id' => 'camera', 'caption' => __( 'Take Photo', 'buddypress' ), 'order' => 10 );
     841                        $avatar_nav['camera'] = array(
     842                                'id'      => 'camera',
     843                                'caption' => __( 'Take Photo', 'buddypress' ),
     844                                'order'   => 10
     845                        );
    807846
    808847                        // Set warning messages.
Note: See TracChangeset for help on using the changeset viewer.