<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][13175] trunk: Introduce a new method to the BP Attachment API to add file revisions</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://buddypress.trac.wordpress.org/changeset/13175">13175</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://buddypress.trac.wordpress.org/changeset/13175","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>imath</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-12-11 12:00:16 +0000 (Sat, 11 Dec 2021)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="http://buddypress.trac.wordpress.org/ticket/8581">#8581</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpcoreclassesclassbpattachmentavatarphp">trunk/src/bp-core/classes/class-bp-attachment-avatar.php</a></li>
<li><a href="#trunksrcbpcoreclassesclassbpattachmentphp">trunk/src/bp-core/classes/class-bp-attachment.php</a></li>
<li><a href="#trunktestsphpunittestcasescoreclassbpattachmentphp">trunk/tests/phpunit/testcases/core/class-bp-attachment.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpcoreclassesclassbpattachmentavatarphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/classes/class-bp-attachment-avatar.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/classes/class-bp-attachment-avatar.php  2021-12-11 09:40:29 UTC (rev 13174)
+++ trunk/src/bp-core/classes/class-bp-attachment-avatar.php    2021-12-11 12:00:16 UTC (rev 13175)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -199,7 +199,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @see  BP_Attachment::crop for the list of parameters
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param array $args Array of arguments for the cropping.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @return array The cropped avatars (full and thumb).
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @return array The cropped avatars (full, thumb and the timestamp).
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function crop( $args = array() ) {
</span><span class="cx" style="display: block; padding: 0 10px">                // Bail if the original file is missing.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -255,10 +255,36 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                /**
</span><span class="cx" style="display: block; padding: 0 10px">                 * Check that the new avatar doesn't have the same name as the
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                 * old one before deleting
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * old one before moving the previous one into history.
</ins><span class="cx" style="display: block; padding: 0 10px">                  */
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! empty( $existing_avatar ) && $existing_avatar !== $this->url . $relative_path ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        bp_core_delete_existing_avatar( array( 'object' => $args['object'], 'item_id' => $args['item_id'], 'avatar_path' => $avatar_folder_dir ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Add a new revision for the existing avatar.
+                       $avatars = bp_attachments_list_directory_files( $avatar_folder_dir );
+
+                       if ( $avatars ) {
+                               foreach ( $avatars as $avatar_file ) {
+                                       if ( ! isset( $avatar_file->name, $avatar_file->id, $avatar_file->path ) ) {
+                                               continue;
+                                       }
+
+                                       $is_full  = preg_match( "/-bpfull/", $avatar_file->name );
+                                       $is_thumb = preg_match( "/-bpthumb/", $avatar_file->name );
+
+                                       if ( $is_full || $is_thumb ) {
+                                               $revision = $this->add_revision(
+                                                       'avatar',
+                                                       array(
+                                                               'file_abspath' => $avatar_file->path,
+                                                               'file_id'      => $avatar_file->id,
+                                                       )
+                                               );
+
+                                               if ( is_wp_error( $revision ) ) {
+                                                       error_log( $revision->get_error_message() );
+                                               }
+                                       }
+                               }
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Make sure we at least have minimal data for cropping.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -272,12 +298,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Get the file extension.
</span><span class="cx" style="display: block; padding: 0 10px">                $data = @getimagesize( $absolute_path );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $ext  = $data['mime'] == 'image/png' ? 'png' : 'jpg';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $ext  = $data['mime'] === 'image/png' ? 'png' : 'jpg';
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $args['original_file'] = $absolute_path;
</span><span class="cx" style="display: block; padding: 0 10px">                $args['src_abs']       = false;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $avatar_types = array( 'full' => '', 'thumb' => '' );
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $avatar_types = array(
+                       'full'  => '',
+                       'thumb' => '',
+               );
+               $timestamp   = bp_core_current_time( true, 'timestamp' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 foreach ( $avatar_types as $key_type => $type ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( 'thumb' === $key_type ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $args['dst_w'] = bp_core_avatar_thumb_width();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -287,7 +318,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $args['dst_h'] = bp_core_avatar_full_height();
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $filename         = wp_unique_filename( $avatar_folder_dir, uniqid() . "-bp{$key_type}.{$ext}" );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $filename         = wp_unique_filename( $avatar_folder_dir, $timestamp . "-bp{$key_type}.{$ext}" );
</ins><span class="cx" style="display: block; padding: 0 10px">                         $args['dst_file'] = $avatar_folder_dir . '/' . $filename;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        $avatar_types[ $key_type ] = parent::crop( $args );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -296,8 +327,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Remove the original.
</span><span class="cx" style="display: block; padding: 0 10px">                @unlink( $absolute_path );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Return the full and thumb cropped avatars.
-               return $avatar_types;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Return the full, thumb cropped avatars and the timestamp.
+               return array_merge(
+                       $avatar_types,
+                       array(
+                               'timestamp' => $timestamp,
+                       )
+               );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -382,6 +418,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                2 => __( 'Your new profile photo was uploaded successfully.', 'buddypress' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                3 => __( 'There was a problem deleting your profile photo. Please try again.', 'buddypress' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                4 => __( 'Your profile photo was deleted successfully!', 'buddypress' ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                5 => __( 'Your profile photo was recycled successfully!', 'buddypress' ),
+                               6 => __( 'The profile photo was permanently deleted successfully!', 'buddypress' ),
</ins><span class="cx" style="display: block; padding: 0 10px">                         );
</span><span class="cx" style="display: block; padding: 0 10px">                } elseif ( ! empty( $group_id ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $script_data['bp_params'] = array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -400,6 +438,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                2 => __( 'The group profile photo was uploaded successfully.', 'buddypress' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                3 => __( 'There was a problem deleting the group profile photo. Please try again.', 'buddypress' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                4 => __( 'The group profile photo was deleted successfully!', 'buddypress' ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                5 => __( 'The group profile photo was recycled successfully!', 'buddypress' ),
+                               6 => __( 'The group profile photo was permanently deleted successfully!', 'buddypress' ),
</ins><span class="cx" style="display: block; padding: 0 10px">                         );
</span><span class="cx" style="display: block; padding: 0 10px">                } else {
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcbpcoreclassesclassbpattachmentphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/classes/class-bp-attachment.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/classes/class-bp-attachment.php 2021-12-11 09:40:29 UTC (rev 13174)
+++ trunk/src/bp-core/classes/class-bp-attachment.php   2021-12-11 12:00:16 UTC (rev 13175)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -560,6 +560,75 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Adds a new revision of a file.
+        *
+        * @since 10.0.0
+        *
+        * @param string $attachment_type The attachement type (eg: avatar).
+        * @param array $args {
+        *     @type string $file_abspath The source file (absolute path) for the attachment.
+        *     @type string $file_id      Optional. The file ID to use as a suffix for the revision directory.
+        * }
+        * @return object|WP_Error An object informing about the URL an Path to a revision file, a WP_Error object on failure.
+        */
+       public function add_revision( $attachment_type, $args = array() ) {
+               $r = bp_parse_args(
+                       $args,
+                       array(
+                               'file_abspath' => '',
+                               'file_id'      => '',
+                       ),
+                       'attachment_' . $attachment_type . '_add_revision'
+               );
+
+               if ( ! $r['file_abspath'] ) {
+                       return new WP_Error( 'missing_parameter', __( 'The absolute path to your file is missing.', 'buddypress' ) );
+
+                       // Make sure it's coming from an uploaded file.
+               } elseif ( false === strpos( $r['file_abspath'], $this->upload_path ) ) {
+                       return new WP_Error( 'forbidden_path', __( 'The absolute path to your file is not allowed.', 'buddypress' ) );
+
+               } else {
+                       $filepath = $r['file_abspath'];
+               }
+
+               $dirname  = trailingslashit( dirname( $filepath ) );
+               $filename = sanitize_file_name( wp_basename( $filepath ) );
+
+               if ( ! $r['file_id'] ) {
+                       $r['file_id'] = $filename;
+               }
+
+               $file_id = wp_hash( $r['file_id'] );
+
+               // Set the revision name & dir.
+               $revision_name = '';
+               $revision_dir  = $dirname . '._revisions_' . $file_id;
+
+               // Avatars and Cover Images are specific attachments.
+               if ( 'avatar' === $attachment_type || 'cover_image' === $attachment_type ) {
+                       $revision_dir  = $dirname . 'history';
+               }
+
+               // Create the revision directory if it doesn't exist yet.
+               if ( ! is_dir( $revision_dir ) ) {
+                       mkdir( $revision_dir );
+               }
+
+               $revision_name = wp_unique_filename( $revision_dir, $filename );
+               $revision_path = trailingslashit( $revision_dir ) . $revision_name;
+
+               if ( ! rename( $filepath, $revision_path ) ) {
+                       return new WP_Error( 'adding_revision_failed', __( 'An unexpected error occured while adding the revision.', 'buddypress' ) );
+               }
+
+               return (object) array(
+                       'url'  => str_replace( trailingslashit( $this->upload_path ), trailingslashit( $this->url ), $revision_path ),
+                       'path' => $revision_path,
+               );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Get full data for an image
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 2.4.0
</span></span></pre></div>
<a id="trunktestsphpunittestcasescoreclassbpattachmentphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/testcases/core/class-bp-attachment.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/testcases/core/class-bp-attachment.php        2021-12-11 09:40:29 UTC (rev 13174)
+++ trunk/tests/phpunit/testcases/core/class-bp-attachment.php  2021-12-11 12:00:16 UTC (rev 13175)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -406,6 +406,84 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->clean_files( 'shrink' );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * @group add_revision
+        */
+       public function test_bp_attachment_add_revision() {
+               if ( false === _wp_image_editor_choose() || version_compare( phpversion(), '7.0' , '<' ) ) {
+                       $this->markTestSkipped( 'This test requires PHP >= 7.0 and to have a valid image editor that is compatible with WordPress.' );
+               }
+
+               $image = BP_TESTS_DIR . 'assets/upside-down.jpg';
+
+               $attachment = new BPTest_Attachment_Extension(
+                       array(
+                               'base_dir'   => 'add_revision',
+                               'action'     => 'attachment_action',
+                               'file_input' => 'attachment_file_input',
+                       )
+               );
+
+               $abs_path_copy = $attachment->upload_path . '/upside-down.jpg';
+               copy( $image, $abs_path_copy );
+
+               $revision = $attachment->add_revision(
+                       'media',
+                       array(
+                               'file_abspath' => $abs_path_copy,
+                               'file_id'      => 'media',
+                       )
+               );
+
+               $this->assertFalse( file_exists( $abs_path_copy ) );
+               $this->assertTrue( file_exists( $revision->path ) );
+
+               // Cleanup
+               @unlink( $revision->path );
+               @rmdir( dirname( $revision->path ) );
+               $this->clean_files( 'add_revision' );
+       }
+
+       /**
+        * @group add_revision
+        * @group avatars
+        */
+       public function test_bp_attachment_add_avatar_history() {
+               if ( false === _wp_image_editor_choose() || version_compare( phpversion(), '7.0' , '<' ) ) {
+                       $this->markTestSkipped( 'This test requires PHP >= 7.0 and to have a valid image editor that is compatible with WordPress.' );
+               }
+
+               $image = BP_TESTS_DIR . 'assets/upside-down.jpg';
+
+               $attachment = new BPTest_Attachment_Extension(
+                       array(
+                               'base_dir'   => 'add_history',
+                               'action'     => 'attachment_action',
+                               'file_input' => 'attachment_file_input',
+                       )
+               );
+
+               $abs_path_copy = $attachment->upload_path . '/upside-down.jpg';
+               copy( $image, $abs_path_copy );
+
+               $revision = $attachment->add_revision(
+                       'avatar',
+                       array(
+                               'file_abspath' => $abs_path_copy,
+                               'file_id'      => 'avatar',
+                       )
+               );
+
+               $this->assertFalse( file_exists( $abs_path_copy ) );
+               $this->assertTrue( file_exists( $revision->path ) );
+               $this->assertSame( $attachment->url . '/history/upside-down.jpg', $revision->url );
+
+               // Cleanup
+               @unlink( $revision->path );
+               @rmdir( dirname( $revision->path ) );
+               $this->clean_files( 'add_history' );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public function limit_to_50px( $max_width ) {
</span><span class="cx" style="display: block; padding: 0 10px">                return 50;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span></span></pre>
</div>
</div>

</body>
</html>