Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
12/11/2021 12:00:16 PM (3 years ago)
Author:
imath
Message:

Introduce a new method to the BP Attachment API to add file revisions

The BP_Attachment::add_revision() method is primarily used by the BP_Attachment_Avatar class to keep the history of previously uploaded user avatars (and potentially other objects avatar).

When a user uploads a new avatar, the previously uploaded one is moved inside an history subdirectory of the user's avatar directory. This avatars history is then available for the new_avatar activities to display the avatar the user had at the time these were published.

Props vapvarun, oztaser

See #8581

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/classes/class-bp-attachment-avatar.php

    r12588 r13175  
    200200     *
    201201     * @param array $args Array of arguments for the cropping.
    202      * @return array The cropped avatars (full and thumb).
     202     * @return array The cropped avatars (full, thumb and the timestamp).
    203203     */
    204204    public function crop( $args = array() ) {
     
    256256        /**
    257257         * Check that the new avatar doesn't have the same name as the
    258          * old one before deleting
     258         * old one before moving the previous one into history.
    259259         */
    260260        if ( ! empty( $existing_avatar ) && $existing_avatar !== $this->url . $relative_path ) {
    261             bp_core_delete_existing_avatar( array( 'object' => $args['object'], 'item_id' => $args['item_id'], 'avatar_path' => $avatar_folder_dir ) );
     261            // Add a new revision for the existing avatar.
     262            $avatars = bp_attachments_list_directory_files( $avatar_folder_dir );
     263
     264            if ( $avatars ) {
     265                foreach ( $avatars as $avatar_file ) {
     266                    if ( ! isset( $avatar_file->name, $avatar_file->id, $avatar_file->path ) ) {
     267                        continue;
     268                    }
     269
     270                    $is_full  = preg_match( "/-bpfull/", $avatar_file->name );
     271                    $is_thumb = preg_match( "/-bpthumb/", $avatar_file->name );
     272
     273                    if ( $is_full || $is_thumb ) {
     274                        $revision = $this->add_revision(
     275                            'avatar',
     276                            array(
     277                                'file_abspath' => $avatar_file->path,
     278                                'file_id'      => $avatar_file->id,
     279                            )
     280                        );
     281
     282                        if ( is_wp_error( $revision ) ) {
     283                            error_log( $revision->get_error_message() );
     284                        }
     285                    }
     286                }
     287            }
    262288        }
    263289
     
    273299        // Get the file extension.
    274300        $data = @getimagesize( $absolute_path );
    275         $ext  = $data['mime'] == 'image/png' ? 'png' : 'jpg';
     301        $ext  = $data['mime'] === 'image/png' ? 'png' : 'jpg';
    276302
    277303        $args['original_file'] = $absolute_path;
    278304        $args['src_abs']       = false;
    279         $avatar_types = array( 'full' => '', 'thumb' => '' );
     305
     306        $avatar_types = array(
     307            'full'  => '',
     308            'thumb' => '',
     309        );
     310        $timestamp   = bp_core_current_time( true, 'timestamp' );
    280311
    281312        foreach ( $avatar_types as $key_type => $type ) {
     
    288319            }
    289320
    290             $filename         = wp_unique_filename( $avatar_folder_dir, uniqid() . "-bp{$key_type}.{$ext}" );
     321            $filename         = wp_unique_filename( $avatar_folder_dir, $timestamp . "-bp{$key_type}.{$ext}" );
    291322            $args['dst_file'] = $avatar_folder_dir . '/' . $filename;
    292323
     
    297328        @unlink( $absolute_path );
    298329
    299         // Return the full and thumb cropped avatars.
    300         return $avatar_types;
     330        // Return the full, thumb cropped avatars and the timestamp.
     331        return array_merge(
     332            $avatar_types,
     333            array(
     334                'timestamp' => $timestamp,
     335            )
     336        );
    301337    }
    302338
     
    383419                3 => __( 'There was a problem deleting your profile photo. Please try again.', 'buddypress' ),
    384420                4 => __( 'Your profile photo was deleted successfully!', 'buddypress' ),
     421                5 => __( 'Your profile photo was recycled successfully!', 'buddypress' ),
     422                6 => __( 'The profile photo was permanently deleted successfully!', 'buddypress' ),
    385423            );
    386424        } elseif ( ! empty( $group_id ) ) {
     
    401439                3 => __( 'There was a problem deleting the group profile photo. Please try again.', 'buddypress' ),
    402440                4 => __( 'The group profile photo was deleted successfully!', 'buddypress' ),
     441                5 => __( 'The group profile photo was recycled successfully!', 'buddypress' ),
     442                6 => __( 'The group profile photo was permanently deleted successfully!', 'buddypress' ),
    403443            );
    404444        } else {
Note: See TracChangeset for help on using the changeset viewer.