<!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][13178] trunk/src/bp-core/bp-core-avatars.php: Add Ajax handlers for the recycle and delete previous avatar actions</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/13178">13178</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/13178","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 14:13:14 +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'>Add Ajax handlers for the recycle and delete previous avatar actions
These handlers will be used by the avatar UI:
- to reset a previously uploaded user avatar as the current user avatar (`bp_avatar_ajax_recycle_previous_avatar()`),
- to permanently remove a previously uploaded user avatar (`bp_avatar_ajax_delete_previous_avatar()`).
Props vapvarun, oztaser
See <a href="http://buddypress.trac.wordpress.org/ticket/8581">#8581</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpcorebpcoreavatarsphp">trunk/src/bp-core/bp-core-avatars.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpcorebpcoreavatarsphp"></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/bp-core-avatars.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/bp-core-avatars.php 2021-12-11 13:40:24 UTC (rev 13177)
+++ trunk/src/bp-core/bp-core-avatars.php 2021-12-11 14:13:14 UTC (rev 13178)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2240,3 +2240,343 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return wp_filter_object_list( $avatars, array( 'type' => $type ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Recycle a previously uploaded avatar as the current avatar.
+ *
+ * @since 10.0.0
+ */
+function bp_avatar_ajax_recycle_previous_avatar() {
+ if ( ! bp_is_post_request() ) {
+ wp_send_json_error();
+ }
+
+ $avatar_data = bp_parse_args(
+ $_POST,
+ array(
+ 'object' => '',
+ 'item_id' => 0,
+ 'avatar_id' => '',
+ )
+ );
+
+ if ( ! $avatar_data['object'] || ! $avatar_data['item_id'] || ! $avatar_data['avatar_id'] ) {
+ wp_send_json_error();
+ }
+
+ // Check the nonce.
+ check_admin_referer( 'bp_avatar_recycle_previous', 'nonce' );
+
+ // Capability check.
+ if ( ! bp_attachments_current_user_can( 'edit_avatar', $avatar_data ) ) {
+ wp_send_json_error();
+ }
+
+ // Set the Avatar Attachment Instance.
+ $avatar_attachment = new BP_Attachment_Avatar();
+ $object = sanitize_key( $avatar_data['object'] );
+
+ if ( 'user' === $object ) {
+ $avatar_dir = 'avatars';
+ } else {
+ $avatar_dir = $object . '-avatars';
+ }
+
+ $item_id = (int) $avatar_data['item_id'];
+ $avatar_dir_path = $avatar_attachment->upload_path . '/' . $avatar_dir . '/' . $item_id;
+ $current_avatars = bp_attachments_list_directory_files( $avatar_dir_path );
+ $revision_errors = array();
+
+ // This path will be needed to get the avatar revision object.
+ $avatar_full_revision_path = '';
+
+ // Add a revision of the current avatar if it's not a mystery man!
+ if ( $current_avatars ) {
+ foreach( $current_avatars as $current_avatar ) {
+ if ( ! isset( $current_avatar->name, $current_avatar->id, $current_avatar->path ) ) {
+ continue;
+ }
+
+ $is_full = preg_match( "/-bpfull/", $current_avatar->name );
+ $is_thumb = preg_match( "/-bpthumb/", $current_avatar->name );
+
+ if ( $is_full || $is_thumb ) {
+ // Add a revision of the current avatar.
+ $revision = $avatar_attachment->add_revision(
+ 'avatar',
+ array(
+ 'file_abspath' => $current_avatar->path,
+ 'file_id' => $current_avatar->id,
+ )
+ );
+
+ if ( is_wp_error( $revision ) ) {
+ $revision_errors[] = $revision->get_error_message();
+ } elseif ( $is_full ) {
+ $avatar_full_revision_path = $revision->path;
+ }
+ }
+ }
+ }
+
+ // No errors, let's recycle the previous avatar.
+ if ( ! $revision_errors ) {
+ $avatar_id = sanitize_file_name( $avatar_data['avatar_id'] );
+ $suffix = '-bpfull';
+ $history_dir = trailingslashit( bp_core_avatar_upload_path() ) . $avatar_dir . '/' . $item_id . '/history';
+ $avatars = bp_attachments_list_directory_files( $history_dir );
+
+ if ( ! isset( $avatars[ $avatar_id ] ) ) {
+ wp_send_json_error(
+ array(
+ 'message' => __( 'The profile photo you want to recycle cannot be found.', 'buddypress' ),
+ )
+ );
+ }
+
+ // Init recycle vars.
+ $recycle_timestamp = bp_core_current_time( true, 'timestamp' );
+ $recycle_errors = array();
+ $avatar_types = array(
+ 'full' => '',
+ 'thumb' => '',
+ );
+
+ // Use the found previous avatar.
+ $avatar = $avatars[ $avatar_id ];
+ $avatar_types['full'] = $avatar->path;
+ $avatar_types['thumb'] = str_replace( $suffix, '-bpthumb', $avatar->path );
+ $historical_types = $avatar_types;
+
+ // It's a legacy avatar, we need to crop it again and remove the thumb file that is not using a timestamp in its name.
+ if ( ! file_exists( $avatar_types['thumb'] ) ) {
+ $full_avatar_path = $avatar_dir_path . '/' . str_replace( 'bpfull', 'original-file', wp_basename( $avatar->path ) );
+
+ // Move the full version back to avatar dir.
+ rename( $avatar->path, $full_avatar_path );
+
+ $avatar_types = $avatar_attachment->crop(
+ array(
+ 'original_file' => $full_avatar_path,
+ 'avatar_dir' => $avatar_dir,
+ 'object' => $object,
+ 'item_id' => $item_id,
+ )
+ );
+
+ // loop into the history directory to delete the legacy thumb version file.
+ foreach ( $avatars as $avatar_object ) {
+ $timestamp = str_replace( array( '-bpthumb', '-bpfull' ), '', $avatar_object->id );
+
+ if ( ! is_numeric( $timestamp ) && false !== strpos( $avatar_object->id, '-bpthumb' ) ) {
+ @unlink( $avatar_object->path );
+ }
+ }
+ } else {
+ foreach( $avatar_types as $type_key => $avatar_path ) {
+ $filename = wp_basename( $avatar_path );
+ $avatar_id = pathinfo( $filename, PATHINFO_FILENAME );
+ $recycle_path = $avatar_dir_path . '/' . str_replace( $avatar_id, $recycle_timestamp . '-bp' . $type_key, $filename );
+
+ if ( ! rename( $avatar_path, $recycle_path ) ) {
+ $recycle_errors[] = __( 'An unexpected error occured while recycling the previous profile photo.', 'buddypress' );
+ } else {
+ $avatar_types[ $type_key ] = $recycle_path;
+ }
+ }
+
+ $avatar_types = array_merge(
+ $avatar_types,
+ array(
+ 'timestamp' => $recycle_timestamp,
+ )
+ );
+ }
+
+ // No errors, fire the hook used when an avatar is set.
+ if ( ! $recycle_errors && $historical_types['full'] !== $avatar_types['full'] ) {
+ $r = array(
+ 'item_id' => $item_id,
+ 'object' => $object,
+ 'avatar_dir' => $avatar_dir,
+ );
+
+ $action_hook = 'bp_members_avatar_uploaded';
+ if ( 'group' === $object ) {
+ $action_hook = 'groups_avatar_uploaded';
+ }
+
+ /** This action is documented in bp-core/bp-core-avatars.php */
+ do_action( $action_hook, $item_id, 'recycle', $r, $avatar_types );
+ } else {
+ $recycle_error = reset( $recycle_errors );
+
+ wp_send_json_error(
+ array(
+ 'message' => join( "\n", $recycle_error ),
+ )
+ );
+ }
+ } else {
+ wp_send_json_error(
+ array(
+ 'message' => join( "\n", $revision_errors ),
+ )
+ );
+ }
+
+ $return = array(
+ 'avatar' => esc_url(
+ bp_core_fetch_avatar(
+ array(
+ 'object' => $object,
+ 'item_id' => $item_id,
+ 'html' => false,
+ 'type' => 'full',
+ )
+ )
+ ),
+ 'feedback_code' => 5,
+ 'item_id' => $item_id,
+ );
+
+ // Get the created revision object if it exists.
+ if ( $avatar_full_revision_path ) {
+ $history_dir = dirname( $avatar_full_revision_path );
+ $avatars_history = bp_attachments_list_directory_files( $history_dir );
+ $latest_id = pathinfo( wp_basename( $avatar_full_revision_path ), PATHINFO_FILENAME );
+
+ if ( isset( $avatars_history[ $latest_id ] ) ) {
+ $gmdate = gmdate( 'Y-m-d H:i:s', $avatars_history[ $latest_id ]->last_modified );
+ $date = strtotime( get_date_from_gmt( $gmdate ) );
+ $history_url = trailingslashit( bp_core_avatar_url() ) . $avatar_dir . '/' . $item_id . '/history';
+
+ // Prepare the avatar object for JavaScript.
+ $avatars_history[ $latest_id ]->date = sprintf(
+ '%1$s (%2$s)',
+ date_i18n( get_option( 'date_format' ), $date ),
+ date_i18n( get_option( 'time_format' ), $date )
+ );
+ $avatars_history[ $latest_id ]->type = 'full';
+ $avatars_history[ $latest_id ]->url = $history_url . '/' . $avatars_history[ $latest_id ]->name;
+
+ // Remove the path.
+ unset( $avatars_history[ $latest_id ]->path );
+
+ // Set the new object to add to the revision list.
+ $return['historicalAvatar'] = $avatars_history[ $latest_id ];
+ }
+ }
+
+ wp_send_json_success( $return );
+}
+add_action( 'wp_ajax_bp_avatar_recycle_previous', 'bp_avatar_ajax_recycle_previous_avatar' );
+
+/**
+ * Delete a previously uploaded avatar from avatars history.
+ *
+ * @since 10.0.0
+ */
+function bp_avatar_ajax_delete_previous_avatar() {
+ if ( ! bp_is_post_request() ) {
+ wp_send_json_error();
+ }
+
+ $avatar_data = bp_parse_args(
+ $_POST,
+ array(
+ 'object' => '',
+ 'item_id' => 0,
+ 'avatar_id' => '',
+ )
+ );
+
+ if ( ! $avatar_data['object'] || ! $avatar_data['item_id'] || ! $avatar_data['avatar_id'] ) {
+ wp_send_json_error();
+ }
+
+ // Check the nonce.
+ check_admin_referer( 'bp_avatar_delete_previous', 'nonce' );
+
+ // Capability check.
+ if ( ! bp_attachments_current_user_can( 'edit_avatar', $avatar_data ) ) {
+ wp_send_json_error();
+ }
+
+ $object = sanitize_key( $avatar_data['object'] );
+ if ( 'user' === $object ) {
+ $avatar_dir = 'avatars';
+ } else {
+ $avatar_dir = $object . '-avatars';
+ }
+
+ $item_id = (int) $avatar_data['item_id'];
+ $avatar_id = sanitize_file_name( $avatar_data['avatar_id'] );
+ $suffix = '-bpfull';
+ $history_dir = trailingslashit( bp_core_avatar_upload_path() ) . $avatar_dir . '/' . $item_id . '/history';
+ $avatars = bp_attachments_list_directory_files( $history_dir );
+
+ if ( ! isset( $avatars[ $avatar_id ] ) ) {
+ wp_send_json_error(
+ array(
+ 'message' => __( 'The profile photo you want to delete cannot be found.', 'buddypress' ),
+ )
+ );
+ }
+
+ $avatar_types = array(
+ 'full' => '',
+ 'thumb' => '',
+ );
+
+ // Use the found previous avatar.
+ $avatar = $avatars[ $avatar_id ];
+ $avatar_types['full'] = $avatar->path;
+ $avatar_types['thumb'] = str_replace( $suffix, '-bpthumb', $avatar->path );
+
+ // It's a legacy avatar, we need to find the thumb version using file last modified date.
+ if ( ! file_exists( $avatar_types['thumb'] ) ) {
+ $avatar_types['thumb'] = '';
+ $possible_thumb_avatars = wp_list_pluck( $avatars, 'last_modified', 'id' );
+
+ foreach ( $possible_thumb_avatars as $type_id => $modified_date ) {
+ $timediff = $avatar->last_modified - $modified_date;
+ if ( 1000 >= absint( $timediff ) && $avatar_id !== $type_id ) {
+ $avatar_types['thumb'] = $avatars[ $type_id ]->path;
+ break;
+ }
+ }
+ }
+
+ // Remove the files.
+ foreach ( $avatar_types as $avatar_path ) {
+ if ( ! $avatar_path ) {
+ continue;
+ }
+
+ @unlink( $avatar_path );
+ }
+
+ $timestamp = str_replace( '-bpfull', '', $avatar_id );
+ if ( ! is_numeric( $timestamp ) ) {
+ $timestamp = $avatar->last_modified;
+ }
+
+ /**
+ * Hook here to run custom code once the previous avatar has been deleted.
+ *
+ * @since 10.0.0
+ *
+ * @param int $item_id The object ID.
+ * @param int $timestamp The avatar timestamp.
+ */
+ do_action( "bp_previous_{$object}_avatar_deleted", $item_id, $timestamp );
+
+ // Finally inform about the deletion success.
+ wp_send_json_success(
+ array(
+ 'feedback_code' => 6,
+ )
+ );
+}
+add_action( 'wp_ajax_bp_avatar_delete_previous', 'bp_avatar_ajax_delete_previous_avatar' );
</ins></span></pre>
</div>
</div>
</body>
</html>