Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
12/11/2021 12:00:16 PM (2 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.php

    r13108 r13175  
    561561
    562562    /**
     563     * Adds a new revision of a file.
     564     *
     565     * @since 10.0.0
     566     *
     567     * @param string $attachment_type The attachement type (eg: avatar).
     568     * @param array $args {
     569     *     @type string $file_abspath The source file (absolute path) for the attachment.
     570     *     @type string $file_id      Optional. The file ID to use as a suffix for the revision directory.
     571     * }
     572     * @return object|WP_Error An object informing about the URL an Path to a revision file, a WP_Error object on failure.
     573     */
     574    public function add_revision( $attachment_type, $args = array() ) {
     575        $r = bp_parse_args(
     576            $args,
     577            array(
     578                'file_abspath' => '',
     579                'file_id'      => '',
     580            ),
     581            'attachment_' . $attachment_type . '_add_revision'
     582        );
     583
     584        if ( ! $r['file_abspath'] ) {
     585            return new WP_Error( 'missing_parameter', __( 'The absolute path to your file is missing.', 'buddypress' ) );
     586
     587            // Make sure it's coming from an uploaded file.
     588        } elseif ( false === strpos( $r['file_abspath'], $this->upload_path ) ) {
     589            return new WP_Error( 'forbidden_path', __( 'The absolute path to your file is not allowed.', 'buddypress' ) );
     590
     591        } else {
     592            $filepath = $r['file_abspath'];
     593        }
     594
     595        $dirname  = trailingslashit( dirname( $filepath ) );
     596        $filename = sanitize_file_name( wp_basename( $filepath ) );
     597
     598        if ( ! $r['file_id'] ) {
     599            $r['file_id'] = $filename;
     600        }
     601
     602        $file_id = wp_hash( $r['file_id'] );
     603
     604        // Set the revision name & dir.
     605        $revision_name = '';
     606        $revision_dir  = $dirname . '._revisions_' . $file_id;
     607
     608        // Avatars and Cover Images are specific attachments.
     609        if ( 'avatar' === $attachment_type || 'cover_image' === $attachment_type ) {
     610            $revision_dir  = $dirname . 'history';
     611        }
     612
     613        // Create the revision directory if it doesn't exist yet.
     614        if ( ! is_dir( $revision_dir ) ) {
     615            mkdir( $revision_dir );
     616        }
     617
     618        $revision_name = wp_unique_filename( $revision_dir, $filename );
     619        $revision_path = trailingslashit( $revision_dir ) . $revision_name;
     620
     621        if ( ! rename( $filepath, $revision_path ) ) {
     622            return new WP_Error( 'adding_revision_failed', __( 'An unexpected error occured while adding the revision.', 'buddypress' ) );
     623        }
     624
     625        return (object) array(
     626            'url'  => str_replace( trailingslashit( $this->upload_path ), trailingslashit( $this->url ), $revision_path ),
     627            'path' => $revision_path,
     628        );
     629    }
     630
     631    /**
    563632     * Get full data for an image
    564633     *
Note: See TracChangeset for help on using the changeset viewer.